Skip to content

Commit 042ce6e

Browse files
author
Andy Hanson
committed
Improve verify.codeFixAvailable
1 parent 9d57903 commit 042ce6e

File tree

34 files changed

+89
-77
lines changed

34 files changed

+89
-77
lines changed

src/harness/fourslash.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,30 +2997,18 @@ Actual: ${stringify(fullActual)}`);
29972997
}
29982998
}
29992999

3000-
public verifyCodeFixAvailable(negative: boolean, info: FourSlashInterface.VerifyCodeFixAvailableOptions[] | undefined) {
3000+
public verifyCodeFixAvailable(info: FourSlashInterface.VerifyCodeFixAvailableOptions[]) {
30013001
const codeFixes = this.getCodeFixes(this.activeFile.fileName);
3002-
3003-
if (negative) {
3004-
if (codeFixes.length) {
3005-
this.raiseError(`verifyCodeFixAvailable failed - expected no fixes but found ${codeFixes.map(c => c.description)}.`);
3002+
assert.equal(info.length, codeFixes.length);
3003+
ts.zipWith(codeFixes, info, (fix, info) => {
3004+
assert.equal(fix.description, info.description);
3005+
this.assertObjectsEqual(fix.commands, info.commands);
3006+
for (const change of fix.changes) {
3007+
for (const textChange of change.textChanges) {
3008+
assert.deepEqual(Object.keys(textChange), ["span", "newText"], `Invalid textChange in codeFix: ${JSON.stringify(fix)}`);
3009+
}
30063010
}
3007-
return;
3008-
}
3009-
3010-
if (!codeFixes.length) {
3011-
this.raiseError(`verifyCodeFixAvailable failed - expected code fixes but none found.`);
3012-
}
3013-
codeFixes.forEach(fix => fix.changes.forEach(change => {
3014-
assert.isObject(change, `Invalid change in code fix: ${JSON.stringify(fix)}`);
3015-
change.textChanges.forEach(textChange => assert.isObject(textChange, `Invalid textChange in codeFix: ${JSON.stringify(fix)}`));
3016-
}));
3017-
if (info) {
3018-
assert.equal(info.length, codeFixes.length);
3019-
ts.zipWith(codeFixes, info, (fix, info) => {
3020-
assert.equal(fix.description, info.description);
3021-
this.assertObjectsEqual(fix.commands, info.commands);
3022-
});
3023-
}
3011+
});
30243012
}
30253013

30263014
public verifyApplicableRefactorAvailableAtMarker(negative: boolean, markerName: string) {
@@ -4106,7 +4094,7 @@ namespace FourSlashInterface {
41064094
}
41074095

41084096
public codeFixAvailable(options?: VerifyCodeFixAvailableOptions[]) {
4109-
this.state.verifyCodeFixAvailable(this.negative, options);
4097+
this.state.verifyCodeFixAvailable(options);
41104098
}
41114099

41124100
public applicableRefactorAvailableAtMarker(markerName: string) {

tests/cases/fourslash/codeFixAddMissingMember8.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
////declare var x: [1, 2];
55
////x.b;
66

7-
verify.not.codeFixAvailable();
7+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixAwaitInSyncFunction3.ts

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

12-
verify.not.codeFixAvailable();
12+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixAwaitInSyncFunction4.ts

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

9-
verify.not.codeFixAvailable();
9+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixAwaitShouldNotCrashIfNotInFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
////await a
44

5-
verify.not.codeFixAvailable();
5+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixCannotFindModule_notIfMissing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ test.setTypesRegistry({
88
});
99

1010
// We only give the fix for an implicit-any module, not for a missing module.
11-
verify.not.codeFixAvailable();
11+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassExtendAbstractPrivateProperty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
// 2) Make x private, and then insert.
1313
// 3) Make x not abstract.
1414
// So we offer no fixes.
15-
verify.not.codeFixAvailable();
15+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementClassMemberAnonymousClass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
//// }
1111
//// class C implements A {[| |]}
1212

13-
verify.not.codeFixAvailable();
13+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementInterfaceCallSignature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
//// }
66
//// class C implements I {[| |]}
77

8-
verify.not.codeFixAvailable();
8+
verify.codeFixAvailable([]);
99

1010

tests/cases/fourslash/codeFixClassImplementInterfaceConstructSignature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
//// }
66
//// class C implements I {[| |]}
77

8-
verify.not.codeFixAvailable();
8+
verify.codeFixAvailable([]);
99

1010

tests/cases/fourslash/codeFixClassImplementInterfaceDuplicateMember1.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
////
1010
//// class C implements I1,I2 {[| |]}
1111

12-
verify.codeFixAvailable();
12+
verify.codeFixAvailable([
13+
{ description: "Implement interface 'I1'" },
14+
{ description: "Implement interface 'I2'" },
15+
]);

tests/cases/fourslash/codeFixClassImplementInterfaceDuplicateMember2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
//// x: number;
1212
//// }
1313

14-
verify.not.codeFixAvailable();
14+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementInterfaceIndexSignaturesNoFix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
////
77
//// class C implements I {[| |]}
88

9-
verify.not.codeFixAvailable();
9+
verify.codeFixAvailable([]);
1010

1111

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleImplements1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ verify.rangeAfterCodeFix(`
1515
x: number;
1616
`);
1717

18-
verify.not.codeFixAvailable();
18+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleImplements2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ verify.rangeAfterCodeFix(`
1515
y: "𣋝ઢȴ¬⏊";
1616
`);
1717

18-
verify.not.codeFixAvailable();
18+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleImplementsIntersection1.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
////
1010
//// class C implements I1,I2 {[| |]}
1111

12-
verify.codeFixAvailable();
12+
verify.codeFixAvailable([
13+
{ description: "Implement interface 'I1'" },
14+
{ description: "Implement interface 'I2'" },
15+
]);

tests/cases/fourslash/codeFixClassImplementInterfaceMultipleImplementsIntersection2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
//// x: string;
1212
//// }
1313

14-
verify.not.codeFixAvailable();
14+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementInterfacePropertyFromParentConstructorFunction.ts

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

9-
verify.not.codeFixAvailable();
9+
verify.codeFixAvailable([]);
1010

1111
// TODO: (arozga) Get this working.
1212
/*

tests/cases/fourslash/codeFixClassImplementInterfaceTypeParamInstantiateError.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
//// x: T;
55
//// }
66
////
7-
//// class C implements I<number> { }
8-
9-
verify.codeFixAvailable();
7+
//// class C implements I<number> { }
108

119
// TODO: (arozga) Don't know how to instantiate in codeFix
1210
// if instantiation is invalid.
13-
// verify.not.codeFixAvailable();
11+
// Should be verify.codeFixAvailable([]);
12+
verify.codeFixAvailable([{ description: "Implement interface 'I<number>'" }]);

tests/cases/fourslash/codeFixClassImplementInterfaceTypeParamInstantiation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
//// x: T;
55
//// }
66
////
7-
//// class C implements I { }
7+
//// class C implements I { }
88

9-
verify.not.codeFixAvailable();
9+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixClassImplementInterfaceUndeclaredSymbol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//// x: T;
55
//// }
66
////
7-
//// class C implements I { }
7+
//// class C implements I { }
88

99
// T is not a declared symbol. There are a couple fixes:
1010
// 1) Declare T.
@@ -14,6 +14,6 @@
1414
// In the latter two cases, it is premature to copy `x:T` into C.
1515
// Since we can't guess the programmer's intent here, we do nothing.
1616

17-
verify.codeFixAvailable();
1817
// TODO: (aozgaa) Acknowledge other errors on class/implemented interface/extended abstract class.
19-
// verify.not.codeFixAvailable();
18+
// Should be verify.codeFixAvailable([]);
19+
verify.codeFixAvailable([{ description: "Implement interface 'I'" }]);

tests/cases/fourslash/codeFixClassPropertyInitialization.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,54 @@
77
//// class TT { constructor () {} }
88
////
99
//// class AT extends A { a () {} }
10-
////
10+
////
1111
//// class Foo {}
1212
////
1313
//// class T {
14-
////
14+
////
1515
//// a: string;
16-
////
16+
////
1717
//// static b: string;
18-
////
18+
////
1919
//// private c: string;
20-
////
20+
////
2121
//// d: number | undefined;
22-
////
22+
////
2323
//// e: string | number;
24-
////
24+
////
2525
//// f: 1;
26-
////
26+
////
2727
//// g: "123" | "456";
28-
////
28+
////
2929
//// h: boolean;
30-
////
30+
////
3131
//// i: TT;
32-
////
32+
////
3333
//// j: A;
34-
////
34+
////
3535
//// k: AT;
36-
////
36+
////
3737
//// l: Foo;
3838
//// }
3939

40-
verify.codeFixAvailable()
40+
function fixes(name: string, type: string, options: { isPrivate?: boolean, noInitializer?: boolean } = {}) {
41+
return [
42+
`Add 'undefined' type to property '${name}'`,
43+
`Add definite assignment assertion to property '${options.isPrivate ? "private " : ""}${name}: ${type};'`,
44+
...(options.noInitializer ? [] : [`Add initializer to property '${name}'`]),
45+
].map(description => ({ description }));
46+
}
47+
48+
verify.codeFixAvailable([
49+
...fixes("a", "string"),
50+
...fixes("c", "string", { isPrivate: true }),
51+
...fixes("e", "string | number"),
52+
...fixes("f", "1"),
53+
...fixes("g", '"123" | "456"'),
54+
...fixes("h", "boolean"),
55+
...fixes("i", "TT"),
56+
...fixes("j", "A", { noInitializer: true }),
57+
...fixes("k", "AT"),
58+
...fixes("l", "Foo"),
59+
{ description: "Remove declaration for: 'c'" },
60+
]);

tests/cases/fourslash/codeFixClassSuperMustPrecedeThisAccess_callWithThisInside.ts

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

12-
verify.not.codeFixAvailable();
12+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixForgottenThisPropertyAccess04.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path='fourslash.ts' />
22

3-
// @jsx: react
3+
// @jsx: react
44
// @jsxFactory: factory
55

66
// @Filename: /a.tsx
@@ -11,4 +11,4 @@
1111
////}
1212

1313

14-
verify.not.codeFixAvailable();
14+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixInferFromUsageInaccessibleTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
////}
1818
////
1919

20-
verify.not.codeFixAvailable();
20+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
////}
1414

1515
goTo.file("/b.ts");
16-
verify.not.codeFixAvailable();
16+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixInferFromUsage_noCrashOnMissingParens.ts

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

1111
// Just testing that we don't crash in `insertTypeAnnotation` from inferFromUsage
12-
verify.not.codeFixAvailable();
12+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixNegativeReplaceQualifiedNameWithIndexedAccessType01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
//// }
99
//// const x: [|Container.Foo.bar|] = ""
1010

11-
verify.not.codeFixAvailable();
11+
verify.codeFixAvailable([]);

tests/cases/fourslash/codeFixUndeclaredPropertyAccesses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
//// let t: T<number>;
1515
//// t.x;
1616

17-
verify.not.codeFixAvailable();
17+
verify.codeFixAvailable([]);

tests/cases/fourslash/importNameCodeFixNewImportExportEqualsPrimitive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
////declare var x: number;
77
////export = x;
88

9-
verify.not.codeFixAvailable(); // See GH#20191
9+
verify.codeFixAvailable([]); // See GH#20191

tests/cases/fourslash/importNameCodeFixNewImportNodeModules5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
//// }
2020

2121
// No fix because this accesses a nested node_modules
22-
verify.not.codeFixAvailable();
22+
verify.codeFixAvailable([]);

tests/cases/fourslash/incompleteFunctionCallCodefix.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
////}
77
////f(
88

9-
verify.not.codeFixAvailable([]);
10-
9+
verify.codeFixAvailable([]);

tests/cases/fourslash/incompleteFunctionCallCodefix3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
// @noImplicitAny: true
44
//// function ...q) {}} f(10);
55

6-
verify.not.codeFixAvailable([]);
6+
verify.codeFixAvailable([]);
77

tests/cases/fourslash/typeToStringCrashInCodeFix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
// @noImplicitAny: true
44
//// function f(y, z = { p: y[
55

6-
verify.not.codeFixAvailable();
6+
verify.codeFixAvailable([]);

0 commit comments

Comments
 (0)