Skip to content

Commit e5279fd

Browse files
committed
Rename and simplify fourslash interface
1 parent 1b60a97 commit e5279fd

37 files changed

Lines changed: 67 additions & 80 deletions

File tree

src/harness/fourslash.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ namespace FourSlash {
9292
end: number;
9393
}
9494

95-
export interface ErrorIdentifier {
95+
export interface CodeFixIdentifier {
96+
/**
97+
* Error code to search over for codefix.
98+
*/
9699
code: number;
97100
/**
98101
* In a file where there is more than one error with code `code`, `count` refers
99-
* to which 0-indexed error, sorted by order of occurence, to consider.
102+
* to which 0-indexed codefix, sorted by order of occurence, to consider.
100103
*/
101104
count: number;
102105
}
@@ -2022,14 +2025,14 @@ namespace FourSlash {
20222025
* Because codefixes are only applied on the working file, it is unsafe
20232026
* to apply this more than once (consider a refactoring across files).
20242027
*/
2025-
public verifyCodeFixAtPosition(expectedText: string, errorCode?: number) {
2028+
public verifyRangeAfterCodeFix(expectedText: string, codeFixIdentifier?: CodeFixIdentifier) {
20262029
const ranges = this.getRanges();
20272030
if (ranges.length !== 1) {
20282031
this.raiseError("Exactly one range should be specified in the testfile.");
20292032
}
20302033

20312034
const fileName = this.activeFile.fileName;
2032-
const codeFix: ts.CodeAction = this.getCodeFix(fileName, errorCode ? { code: errorCode, count: 0 } : undefined);
2035+
const codeFix: ts.CodeAction = this.getCodeFix(fileName, codeFixIdentifier);
20332036

20342037
if (!codeFix) {
20352038
this.raiseError("Should find exactly one codefix.");
@@ -2052,37 +2055,36 @@ namespace FourSlash {
20522055
* Applies fixes for the errors in fileName and compares the results to
20532056
* expectedContents after all fixes have been applied.
20542057
*
2058+
* It is safe to apply this multiple times in a single test.
2059+
*
20552060
* Note: applying one codefix may generate another (eg: remove duplicate implements
20562061
* may generate an extends -> interface conversion fix).
20572062
* @param expectedContents The contents of the file after the fixes are applied.
20582063
* @param fileName The file to check. If not supplied, the current open file is used.
20592064
* @param errorsToFix An array of errors for which quickfixes will be applied. If not
20602065
* supplied, all codefixes in the file are applied until none are left, starting from
20612066
* the first available codefix.
2062-
*
2067+
*
20632068
*/
2064-
public verifyFileAfterCodeFix(expectedContents: string, fileName?: string, errorsToFix?: ErrorIdentifier[]) {
2069+
public verifyFileAfterCodeFix(expectedContents: string, fileName?: string, codeFixIdentifier?: CodeFixIdentifier) {
20652070
fileName = fileName ? fileName : this.activeFile.fileName;
20662071

2067-
if (errorsToFix) {
2068-
for (const error of errorsToFix) {
2069-
const fix = this.getCodeFix(fileName, error);
2070-
if (fix === undefined) {
2071-
this.raiseError(`Couldn't find the ${error.count}'th error with code ${error.code}.`);
2072-
}
2073-
this.applyCodeAction(fix);
2072+
const codeFix = this.getCodeFix(fileName, codeFixIdentifier);
2073+
2074+
if (codeFix === undefined) {
2075+
if (codeFixIdentifier) {
2076+
this.raiseError(`Couldn't find the ${codeFixIdentifier.count}'th error with code ${codeFixIdentifier.code}.`);
20742077
}
2075-
}
2076-
else {
2077-
let fix: ts.CodeAction;
2078-
while (fix = this.getCodeFix(fileName)) {
2079-
this.applyCodeAction(fix);
2078+
else {
2079+
this.raiseError("No code fix could be found.");
20802080
}
20812081
}
20822082

2083+
this.applyCodeAction(codeFix);
2084+
20832085
const actualContents: string = this.getFileContent(fileName);
20842086
if (this.removeWhitespace(actualContents) !== this.removeWhitespace(expectedContents)) {
2085-
this.raiseError(`Actual text doesn't match expected text. Actual:\n${actualContents}\n\nExpected:\n${expectedContents}`);
2087+
this.raiseError(`Actual text doesn't match expected text. Actual:\n${actualContents}\n\nExpected:\n\n${expectedContents}`);
20862088
}
20872089
}
20882090

@@ -2093,7 +2095,7 @@ namespace FourSlash {
20932095
*
20942096
* If undefined, we get the first codefix available.
20952097
*/
2096-
private getCodeFix(fileName: string, error?: ErrorIdentifier): ts.CodeAction | undefined {
2098+
private getCodeFix(fileName: string, error?: CodeFixIdentifier): ts.CodeAction | undefined {
20972099
const diagnostics: ts.Diagnostic[] = this.getDiagnostics(fileName);
20982100
const errorCount = error ? error.count : 0;
20992101

@@ -3364,12 +3366,12 @@ namespace FourSlashInterface {
33643366
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
33653367
}
33663368

3367-
public codeFixAtPosition(expectedText: string, errorCode?: number): void {
3368-
this.state.verifyCodeFixAtPosition(expectedText, errorCode);
3369+
public rangeAfterCodeFix(expectedText: string, codeFixidentifier?: FourSlash.CodeFixIdentifier): void {
3370+
this.state.verifyRangeAfterCodeFix(expectedText, codeFixidentifier);
33693371
}
33703372

3371-
public fileAfterCodeFixes(expectedContents: string, fileName?: string, errorsToFix?: FourSlash.ErrorIdentifier[]): void {
3372-
this.state.verifyFileAfterCodeFix(expectedContents, fileName, errorsToFix);
3373+
public fileAfterCodeFix(expectedContents: string, fileName?: string, codeFixidentifier?: FourSlash.CodeFixIdentifier): void {
3374+
this.state.verifyFileAfterCodeFix(expectedContents, fileName, codeFixidentifier);
33733375
}
33743376

33753377
public navigationBar(json: any) {

tests/cases/fourslash/codeFixAddSuperCall.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//// }
88
////}
99

10-
verify.codeFixAtPosition('super();');
10+
verify.rangeAfterCodeFix('super();');

tests/cases/fourslash/codeFixChangeExtendsToImplementsFS1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
//// interface I1 {}
44
//// [|class c1 extends I1|]{}
55

6-
verify.codeFixAtPosition("class c1 implements I1");
6+
verify.rangeAfterCodeFix("class c1 implements I1");

tests/cases/fourslash/codeFixChangeExtendsToImplementsFS2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
////interface I1 {}
44
////[|class c1<T extends string , U> extends I1|]{}
55

6-
verify.codeFixAtPosition("class c1<T extends string , U> implements I1");
6+
verify.rangeAfterCodeFix("class c1<T extends string , U> implements I1");

tests/cases/fourslash/codeFixClassExtendsAbstractFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//// class C extends A {[|
88
//// |]}
99

10-
verify.codeFixAtPosition(`f(){
10+
verify.rangeAfterCodeFix(`f(){
1111
throw new Error('Method not Implemented');
1212
}
1313
`);

tests/cases/fourslash/codeFixClassExtendsAbstractNumber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
//// class C extends A {[|
88
//// |]}
99

10-
verify.codeFixAtPosition(`
10+
verify.rangeAfterCodeFix(`
1111
abstract x: number;
1212
`);

tests/cases/fourslash/codeFixClassExtendsAbstractProtectedNumber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
//// class C extends A {[|
88
//// |]}
99

10-
verify.codeFixAtPosition(`
10+
verify.rangeAfterCodeFix(`
1111
protected abstract x: number;
1212
`);

tests/cases/fourslash/codeFixClassExtendsAbstractPublicNumber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
//// class C extends A {[|
88
//// |]}
99

10-
verify.codeFixAtPosition(`
10+
verify.rangeAfterCodeFix(`
1111
public abstract x: number;
1212
`);
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
/// <reference path='fourslash.ts' />
22

3-
//// interface I1 { }
4-
//// class C1 extends I1 { }
5-
//// interface I2 { }
6-
//// class C2 extends I2 { }
3+
//// interface I { }
4+
//// class C extends I { }
75

8-
// verify.codeFixAvailable();
9-
verify.fileAfterCodeFixes(`
10-
interface I1 { }
11-
class C1 implements I1 { }
12-
interface I2 { }
13-
class C2 implements I2 { }
6+
verify.fileAfterCodeFix(`
7+
interface I { }
8+
class C implements I { }
149
`);

tests/cases/fourslash/codeFixReOrderSuper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
//// }
1111
////}
1212

13-
verify.codeFixAtPosition("super(); this.a = 12;");
13+
verify.rangeAfterCodeFix("super(); this.a = 12;");

0 commit comments

Comments
 (0)