Skip to content

Commit 0f218a7

Browse files
committed
convert edit commands
1 parent a46c920 commit 0f218a7

File tree

43 files changed

+2036
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2036
-1
lines changed

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,64 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
146146
if (namespace.text === "goTo" && func.text === "marker") {
147147
return parseGoToMarkerArgs(callExpression.arguments);
148148
}
149+
// `edit....`
150+
if (namespace.text === "edit") {
151+
const result = parseEditStatement(func.text, callExpression.arguments);
152+
if (!result) {
153+
return undefined;
154+
}
155+
return [result];
156+
}
149157
// !!! other fourslash commands
150158
}
151159
console.error(`Unrecognized fourslash statement: ${statement.getText()}`);
152160
return undefined;
153161
}
154162

163+
function parseEditStatement(funcName: string, args: readonly ts.Expression[]): EditCmd | undefined {
164+
switch (funcName) {
165+
case "insert":
166+
case "paste":
167+
case "insertLine":
168+
if (args.length !== 1 || !ts.isStringLiteralLike(args[0])) {
169+
console.error(`Expected a single string literal argument in edit.${funcName}, got ${args.map(arg => arg.getText()).join(", ")}`);
170+
return undefined;
171+
}
172+
return {
173+
kind: "edit",
174+
goStatement: `f.${funcName.charAt(0).toUpperCase() + funcName.slice(1)}(t, ${getGoStringLiteral(args[0].text)})`,
175+
};
176+
case "replaceLine":
177+
if (args.length !== 2 || !ts.isNumericLiteral(args[0]) || !ts.isStringLiteral(args[1])) {
178+
console.error(`Expected a single string literal argument in edit.insert, got ${args.map(arg => arg.getText()).join(", ")}`);
179+
return undefined;
180+
}
181+
return {
182+
kind: "edit",
183+
goStatement: `f.ReplaceLine(t, ${args[0].text}, ${getGoStringLiteral(args[1].text)})`,
184+
};
185+
case "backspace":
186+
const arg = args[0];
187+
if (arg) {
188+
if (!ts.isNumericLiteral(arg)) {
189+
console.error(`Expected numeric literal argument in edit.backspace, got ${arg.getText()}`);
190+
return undefined;
191+
}
192+
return {
193+
kind: "edit",
194+
goStatement: `f.Backspace(t, ${arg.text})`,
195+
};
196+
}
197+
return {
198+
kind: "edit",
199+
goStatement: `f.Backspace(t, 1)`,
200+
};
201+
default:
202+
console.error(`Unrecognized edit function: ${funcName}`);
203+
return undefined;
204+
}
205+
}
206+
155207
function getGoStringLiteral(text: string): string {
156208
return `${JSON.stringify(text)}`;
157209
}
@@ -614,7 +666,12 @@ interface GoToMarkerCmd {
614666
marker: string;
615667
}
616668

617-
type Cmd = VerifyCompletionsCmd | GoToMarkerCmd;
669+
interface EditCmd {
670+
kind: "edit";
671+
goStatement: string;
672+
}
673+
674+
type Cmd = VerifyCompletionsCmd | GoToMarkerCmd | EditCmd;
618675

619676
function generateVerifyCompletions({ marker, args, isNewIdentifierLocation }: VerifyCompletionsCmd): string {
620677
let expectedList = "nil";
@@ -649,6 +706,8 @@ function generateCmd(cmd: Cmd): string {
649706
return generateVerifyCompletions(cmd as VerifyCompletionsCmd);
650707
case "goToMarker":
651708
return generateGoToMarker(cmd as GoToMarkerCmd);
709+
case "edit":
710+
return cmd.goStatement;
652711
default:
653712
throw new Error(`Unknown command kind: ${cmd}`);
654713
}

internal/fourslash/_scripts/failingTests.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ TestCompletionListAndMemberListOnCommentedLine
6262
TestCompletionListAndMemberListOnCommentedWhiteSpace
6363
TestCompletionListAtInvalidLocations
6464
TestCompletionListBuilderLocations_VariableDeclarations
65+
TestCompletionListCladule
6566
TestCompletionListForExportEquals
6667
TestCompletionListForTransitivelyExportedMembers01
6768
TestCompletionListForTransitivelyExportedMembers04
6869
TestCompletionListForUnicodeEscapeName
70+
TestCompletionListFunctionExpression
6971
TestCompletionListFunctionMembers
7072
TestCompletionListInArrowFunctionInUnclosedCallSite01
7173
TestCompletionListInClassStaticBlocks
@@ -74,6 +76,7 @@ TestCompletionListInComments
7476
TestCompletionListInComments2
7577
TestCompletionListInComments3
7678
TestCompletionListInExtendsClause
79+
TestCompletionListInFunctionDeclaration
7780
TestCompletionListInImportClause01
7881
TestCompletionListInImportClause05
7982
TestCompletionListInImportClause06
@@ -108,6 +111,7 @@ TestCompletionsAfterJSDoc
108111
TestCompletionsBeforeRestArg1
109112
TestCompletionsECMAPrivateMemberTriggerCharacter
110113
TestCompletionsImportDefaultExportCrash1
114+
TestCompletionsImport_computedSymbolName
111115
TestCompletionsImport_umdDefaultNoCrash2
112116
TestCompletionsInRequire
113117
TestCompletionsInterfaceElement
@@ -161,15 +165,19 @@ TestGetJavaScriptCompletions12
161165
TestGetJavaScriptCompletions13
162166
TestGetJavaScriptCompletions14
163167
TestGetJavaScriptCompletions15
168+
TestGetJavaScriptCompletions18
169+
TestGetJavaScriptCompletions19
164170
TestGetJavaScriptCompletions2
165171
TestGetJavaScriptCompletions20
166172
TestGetJavaScriptCompletions21
173+
TestGetJavaScriptCompletions22
167174
TestGetJavaScriptCompletions3
168175
TestGetJavaScriptCompletions4
169176
TestGetJavaScriptCompletions5
170177
TestGetJavaScriptCompletions8
171178
TestGetJavaScriptCompletions9
172179
TestGetJavaScriptGlobalCompletions1
180+
TestGetJavaScriptQuickInfo8
173181
TestImportCompletionsPackageJsonExportsSpecifierEndsInTs
174182
TestImportCompletionsPackageJsonExportsTrailingSlash1
175183
TestImportCompletionsPackageJsonImportsConditions1
@@ -197,6 +205,7 @@ TestJavaScriptModules14
197205
TestJavascriptModules20
198206
TestJavascriptModules21
199207
TestJavascriptModulesTypeImport
208+
TestJsDocFunctionSignatures3
200209
TestJsDocGenerics1
201210
TestJsdocExtendsTagCompletion
202211
TestJsdocImplementsTagCompletion
@@ -220,12 +229,16 @@ TestMemberListOnConstructorType
220229
TestNoCompletionListOnCommentsInsideObjectLiterals
221230
TestNodeModulesImportCompletions1
222231
TestPathCompletionsAllowModuleAugmentationExtensions
232+
TestPathCompletionsAllowTsExtensions
223233
TestPathCompletionsPackageJsonExportsBundlerNoNodeCondition
224234
TestPathCompletionsPackageJsonExportsCustomConditions
225235
TestPathCompletionsPackageJsonExportsWildcard1
226236
TestPathCompletionsPackageJsonExportsWildcard10
227237
TestPathCompletionsPackageJsonExportsWildcard11
228238
TestPathCompletionsPackageJsonExportsWildcard12
239+
TestPathCompletionsPackageJsonExportsWildcard2
240+
TestPathCompletionsPackageJsonExportsWildcard3
241+
TestPathCompletionsPackageJsonExportsWildcard4
229242
TestPathCompletionsPackageJsonExportsWildcard5
230243
TestPathCompletionsPackageJsonExportsWildcard6
231244
TestPathCompletionsPackageJsonExportsWildcard7
@@ -236,6 +249,9 @@ TestPathCompletionsPackageJsonImportsCustomConditions
236249
TestPathCompletionsPackageJsonImportsIgnoreMatchingNodeModule2
237250
TestPathCompletionsPackageJsonImportsOnlyFromClosestScope1
238251
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard1
252+
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard2
253+
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard3
254+
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard4
239255
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard5
240256
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard6
241257
TestPathCompletionsPackageJsonImportsSrcNoDistWildcard7
@@ -245,13 +261,21 @@ TestPathCompletionsPackageJsonImportsWildcard1
245261
TestPathCompletionsPackageJsonImportsWildcard10
246262
TestPathCompletionsPackageJsonImportsWildcard11
247263
TestPathCompletionsPackageJsonImportsWildcard12
264+
TestPathCompletionsPackageJsonImportsWildcard2
265+
TestPathCompletionsPackageJsonImportsWildcard3
266+
TestPathCompletionsPackageJsonImportsWildcard4
248267
TestPathCompletionsPackageJsonImportsWildcard5
249268
TestPathCompletionsPackageJsonImportsWildcard6
250269
TestPathCompletionsPackageJsonImportsWildcard7
251270
TestPathCompletionsPackageJsonImportsWildcard8
252271
TestPathCompletionsPackageJsonImportsWildcard9
253272
TestPathCompletionsTypesVersionsLocal
273+
TestPathCompletionsTypesVersionsWildcard1
254274
TestPathCompletionsTypesVersionsWildcard2
275+
TestPathCompletionsTypesVersionsWildcard3
276+
TestPathCompletionsTypesVersionsWildcard4
277+
TestPathCompletionsTypesVersionsWildcard5
278+
TestPathCompletionsTypesVersionsWildcard6
255279
TestSatisfiesOperatorCompletion
256280
TestStringCompletionsImportOrExportSpecifier
257281
TestStringCompletionsVsEscaping
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestAddDeclareToModule(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `/**/module mAmbient {
15+
module m3 { }
16+
}`
17+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
18+
f.GoToMarker(t, "")
19+
f.Insert(t, "declare ")
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestAddSignaturePartial(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = ``
15+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
16+
f.Insert(t, "interface Number { toFixed")
17+
f.Insert(t, "(")
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestCompletionListAfterFunction2(t *testing.T) {
11+
t.Parallel()
12+
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `// Outside the function expression
15+
declare var f1: (a: number) => void; /*1*/
16+
17+
declare var f1: (b: number, b2: /*2*/) => void;`
18+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
19+
f.VerifyCompletions(t, "1", &fourslash.CompletionsExpectedList{
20+
IsIncomplete: false,
21+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
22+
CommitCharacters: &defaultCommitCharacters,
23+
EditRange: ignored,
24+
},
25+
Items: &fourslash.CompletionsExpectedItems{
26+
Excludes: []string{"a"},
27+
},
28+
})
29+
f.VerifyCompletions(t, "2", &fourslash.CompletionsExpectedList{
30+
IsIncomplete: false,
31+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
32+
CommitCharacters: &defaultCommitCharacters,
33+
EditRange: ignored,
34+
},
35+
Items: &fourslash.CompletionsExpectedItems{
36+
Excludes: []string{"b"},
37+
},
38+
})
39+
f.Insert(t, "typeof ")
40+
f.VerifyCompletions(t, nil, &fourslash.CompletionsExpectedList{
41+
IsIncomplete: false,
42+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
43+
CommitCharacters: &defaultCommitCharacters,
44+
EditRange: ignored,
45+
},
46+
Items: &fourslash.CompletionsExpectedItems{
47+
Includes: []fourslash.CompletionsExpectedItem{"b"},
48+
},
49+
})
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/ls"
8+
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
9+
"github.com/microsoft/typescript-go/internal/testutil"
10+
)
11+
12+
func TestCompletionListCladule(t *testing.T) {
13+
t.Parallel()
14+
t.Skip()
15+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
16+
const content = `class Foo {
17+
doStuff(): number { return 0; }
18+
static staticMethod() {}
19+
}
20+
module Foo {
21+
export var x: number;
22+
}
23+
Foo/*c1*/; // should get "x", "prototype"
24+
var s: Foo/*c2*/; // no types, in Foo, so shouldnt have anything
25+
var f = new Foo();
26+
f/*c3*/;`
27+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
28+
f.GoToMarker(t, "c1")
29+
f.Insert(t, ".")
30+
f.VerifyCompletions(t, nil, &fourslash.CompletionsExpectedList{
31+
IsIncomplete: false,
32+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
33+
CommitCharacters: &defaultCommitCharacters,
34+
EditRange: ignored,
35+
},
36+
Items: &fourslash.CompletionsExpectedItems{
37+
Includes: []fourslash.CompletionsExpectedItem{&lsproto.CompletionItem{SortText: ptrTo(string(ls.SortTextLocationPriority)), Label: "x"}, &lsproto.CompletionItem{SortText: ptrTo(string(ls.SortTextLocationPriority)), Label: "prototype"}, &lsproto.CompletionItem{SortText: ptrTo(string(ls.SortTextLocalDeclarationPriority)), Label: "staticMethod"}},
38+
},
39+
})
40+
f.GoToMarker(t, "c2")
41+
f.Insert(t, ".")
42+
f.VerifyCompletions(t, nil, nil)
43+
f.GoToMarker(t, "c3")
44+
f.Insert(t, ".")
45+
f.VerifyCompletions(t, nil, &fourslash.CompletionsExpectedList{
46+
IsIncomplete: false,
47+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
48+
CommitCharacters: &defaultCommitCharacters,
49+
EditRange: ignored,
50+
},
51+
Items: &fourslash.CompletionsExpectedItems{
52+
Exact: []fourslash.CompletionsExpectedItem{"doStuff"},
53+
},
54+
})
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package fourslash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/fourslash"
7+
"github.com/microsoft/typescript-go/internal/testutil"
8+
)
9+
10+
func TestCompletionListFunctionExpression(t *testing.T) {
11+
t.Parallel()
12+
t.Skip()
13+
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
14+
const content = `class DataHandler {
15+
dataArray: Uint8Array;
16+
loadData(filename) {
17+
var xmlReq = new XMLHttpRequest();
18+
xmlReq.open("GET", "/" + filename, true);
19+
xmlReq.responseType = "arraybuffer";
20+
xmlReq.onload = function(xmlEvent) {
21+
/*local*/
22+
this./*this*/;
23+
}
24+
}
25+
}`
26+
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
27+
f.GoToMarker(t, "local")
28+
f.InsertLine(t, "")
29+
f.VerifyCompletions(t, nil, &fourslash.CompletionsExpectedList{
30+
IsIncomplete: false,
31+
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
32+
CommitCharacters: &defaultCommitCharacters,
33+
EditRange: ignored,
34+
},
35+
Items: &fourslash.CompletionsExpectedItems{
36+
Includes: []fourslash.CompletionsExpectedItem{"xmlEvent"},
37+
},
38+
})
39+
f.VerifyCompletions(t, "this", nil)
40+
}

0 commit comments

Comments
 (0)