Skip to content

Commit 5143a3c

Browse files
authored
Merge pull request #15507 from Microsoft/spelling-correction
Spelling correction
2 parents cb723cf + 370b561 commit 5143a3c

File tree

107 files changed

+672
-383
lines changed

Some content is hidden

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

107 files changed

+672
-383
lines changed

src/compiler/checker.ts

+170-48
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,14 @@
18431843
"category": "Error",
18441844
"code": 2550
18451845
},
1846+
"Property '{0}' does not exist on type '{1}'. Did you mean '{2}'?": {
1847+
"category": "Error",
1848+
"code": 2551
1849+
},
1850+
"Cannot find name '{0}'. Did you mean '{1}'?": {
1851+
"category": "Error",
1852+
"code": 2552
1853+
},
18461854
"JSX element attributes type '{0}' may not be a union type.": {
18471855
"category": "Error",
18481856
"code": 2600
@@ -3539,7 +3547,10 @@
35393547
"category": "Message",
35403548
"code": 90021
35413549
},
3542-
3550+
"Change spelling to '{0}'.": {
3551+
"category": "Message",
3552+
"code": 90022
3553+
},
35433554

35443555
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
35453556
"category": "Error",

src/compiler/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,8 @@ namespace ts {
25522552

25532553
tryGetMemberInModuleExports(memberName: string, moduleSymbol: Symbol): Symbol | undefined;
25542554
getApparentType(type: Type): Type;
2555+
getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined;
2556+
getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string;
25552557
/* @internal */ getBaseConstraintOfType(type: Type): Type;
25562558

25572559
/* @internal */ tryFindAmbientModuleWithoutAugmentations(moduleName: string): Symbol;

src/compiler/utilities.ts

+23
Original file line numberDiff line numberDiff line change
@@ -4641,4 +4641,27 @@ namespace ts {
46414641
export function unescapeIdentifier(identifier: string): string {
46424642
return identifier.length >= 3 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ && identifier.charCodeAt(2) === CharacterCodes._ ? identifier.substr(1) : identifier;
46434643
}
4644+
4645+
export function levenshtein(s1: string, s2: string): number {
4646+
let previous: number[] = new Array(s2.length + 1);
4647+
let current: number[] = new Array(s2.length + 1);
4648+
for (let i = 0; i < s2.length + 1; i++) {
4649+
previous[i] = i;
4650+
current[i] = -1;
4651+
}
4652+
for (let i = 1; i < s1.length + 1; i++) {
4653+
current[0] = i;
4654+
for (let j = 1; j < s2.length + 1; j++) {
4655+
current[j] = Math.min(
4656+
previous[j] + 1,
4657+
current[j - 1] + 1,
4658+
previous[j - 1] + (s1[i - 1] === s2[j - 1] ? 0 : 2));
4659+
}
4660+
// shift current back to previous, and then reuse previous' array
4661+
const tmp = previous;
4662+
previous = current;
4663+
current = tmp;
4664+
}
4665+
return previous[previous.length - 1];
4666+
}
46444667
}

src/services/codefixes/fixAddMissingMember.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* @internal */
22
namespace ts.codefix {
33
registerCodeFix({
4-
errorCodes: [Diagnostics.Property_0_does_not_exist_on_type_1.code],
4+
errorCodes: [Diagnostics.Property_0_does_not_exist_on_type_1.code,
5+
Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code],
56
getCodeActions: getActionsForAddMissingMember
67
});
78

@@ -135,4 +136,4 @@ namespace ts.codefix {
135136
return actions;
136137
}
137138
}
138-
}
139+
}

src/services/codefixes/fixSpelling.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
registerCodeFix({
4+
errorCodes: [Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code,
5+
Diagnostics.Cannot_find_name_0_Did_you_mean_1.code],
6+
getCodeActions: getActionsForCorrectSpelling
7+
});
8+
9+
function getActionsForCorrectSpelling(context: CodeFixContext): CodeAction[] | undefined {
10+
const sourceFile = context.sourceFile;
11+
12+
// This is the identifier of the misspelled word. eg:
13+
// this.speling = 1;
14+
// ^^^^^^^
15+
const node = getTokenAtPosition(sourceFile, context.span.start);
16+
const checker = context.program.getTypeChecker();
17+
let suggestion: string;
18+
if (node.kind === SyntaxKind.Identifier && isPropertyAccessExpression(node.parent)) {
19+
const containingType = checker.getTypeAtLocation(node.parent.expression);
20+
suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType);
21+
}
22+
else {
23+
const meaning = getMeaningFromLocation(node);
24+
suggestion = checker.getSuggestionForNonexistentSymbol(node, getTextOfNode(node), convertSemanticMeaningToSymbolFlags(meaning));
25+
}
26+
if (suggestion) {
27+
return [{
28+
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_spelling_to_0), [suggestion]),
29+
changes: [{
30+
fileName: sourceFile.fileName,
31+
textChanges: [{
32+
span: { start: node.getStart(), length: node.getWidth() },
33+
newText: suggestion
34+
}],
35+
}],
36+
}];
37+
}
38+
}
39+
40+
function convertSemanticMeaningToSymbolFlags(meaning: SemanticMeaning): SymbolFlags {
41+
let flags = 0;
42+
if (meaning & SemanticMeaning.Namespace) {
43+
flags |= SymbolFlags.Namespace;
44+
}
45+
if (meaning & SemanticMeaning.Type) {
46+
flags |= SymbolFlags.Type;
47+
}
48+
if (meaning & SemanticMeaning.Value) {
49+
flags |= SymbolFlags.Value;
50+
}
51+
return flags;
52+
}
53+
}

src/services/codefixes/fixes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="fixClassIncorrectlyImplementsInterface.ts" />
22
/// <reference path="fixAddMissingMember.ts" />
3+
/// <reference path="fixSpelling.ts" />
34
/// <reference path="fixClassDoesntImplementInheritedAbstractMember.ts" />
45
/// <reference path="fixClassSuperMustPrecedeThisAccess.ts" />
56
/// <reference path="fixConstructorForDerivedNeedSuperCall.ts" />

src/services/codefixes/importFixes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace ts.codefix {
33
registerCodeFix({
44
errorCodes: [
55
Diagnostics.Cannot_find_name_0.code,
6+
Diagnostics.Cannot_find_name_0_Did_you_mean_1.code,
67
Diagnostics.Cannot_find_namespace_0.code,
78
Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead.code
89
],

src/services/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"formatting/tokenRange.ts",
8383
"codeFixProvider.ts",
8484
"codefixes/fixAddMissingMember.ts",
85+
"codefixes/fixSpelling.ts",
8586
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
8687
"codefixes/fixClassIncorrectlyImplementsInterface.ts",
8788
"codefixes/fixClassDoesntImplementInheritedAbstractMember.ts",
@@ -94,4 +95,4 @@
9495
"codefixes/unusedIdentifierFixes.ts",
9596
"codefixes/disableJsDiagnostics.ts"
9697
]
97-
}
98+
}

tests/baselines/reference/ModuleWithExportedAndNonExportedFunctions.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/internalModules/exportDeclarations/ModuleWithExportedAndNonExportedFunctions.ts(28,13): error TS2339: Property 'fn2' does not exist on type 'typeof A'.
2-
tests/cases/conformance/internalModules/exportDeclarations/ModuleWithExportedAndNonExportedFunctions.ts(29,14): error TS2339: Property 'fng2' does not exist on type 'typeof A'.
2+
tests/cases/conformance/internalModules/exportDeclarations/ModuleWithExportedAndNonExportedFunctions.ts(29,14): error TS2551: Property 'fng2' does not exist on type 'typeof A'. Did you mean 'fng'?
33

44

55
==== tests/cases/conformance/internalModules/exportDeclarations/ModuleWithExportedAndNonExportedFunctions.ts (2 errors) ====
@@ -35,4 +35,4 @@ tests/cases/conformance/internalModules/exportDeclarations/ModuleWithExportedAnd
3535
!!! error TS2339: Property 'fn2' does not exist on type 'typeof A'.
3636
var fng2 = A.fng2;
3737
~~~~
38-
!!! error TS2339: Property 'fng2' does not exist on type 'typeof A'.
38+
!!! error TS2551: Property 'fng2' does not exist on type 'typeof A'. Did you mean 'fng'?

tests/baselines/reference/anonymousClassExpression2.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/anonymousClassExpression2.ts(13,18): error TS2339: Property 'methodA' does not exist on type 'B'.
1+
tests/cases/compiler/anonymousClassExpression2.ts(13,18): error TS2551: Property 'methodA' does not exist on type 'B'. Did you mean 'methodB'?
22

33

44
==== tests/cases/compiler/anonymousClassExpression2.ts (1 errors) ====
@@ -16,7 +16,7 @@ tests/cases/compiler/anonymousClassExpression2.ts(13,18): error TS2339: Property
1616
methodB() {
1717
this.methodA; // error
1818
~~~~~~~
19-
!!! error TS2339: Property 'methodA' does not exist on type 'B'.
19+
!!! error TS2551: Property 'methodA' does not exist on type 'B'. Did you mean 'methodB'?
2020
this.methodB; // ok
2121
}
2222
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/assignmentRestElementWithErrorSourceType.ts(2,5): error TS2304: Cannot find name 'c'.
2-
tests/cases/compiler/assignmentRestElementWithErrorSourceType.ts(2,10): error TS2304: Cannot find name 'tupel'.
2+
tests/cases/compiler/assignmentRestElementWithErrorSourceType.ts(2,10): error TS2552: Cannot find name 'tupel'. Did you mean 'tuple'?
33

44

55
==== tests/cases/compiler/assignmentRestElementWithErrorSourceType.ts (2 errors) ====
@@ -8,4 +8,4 @@ tests/cases/compiler/assignmentRestElementWithErrorSourceType.ts(2,10): error TS
88
~
99
!!! error TS2304: Cannot find name 'c'.
1010
~~~~~
11-
!!! error TS2304: Cannot find name 'tupel'.
11+
!!! error TS2552: Cannot find name 'tupel'. Did you mean 'tuple'?

tests/baselines/reference/autoLift2.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
tests/cases/compiler/autoLift2.ts(5,14): error TS2339: Property 'foo' does not exist on type 'A'.
22
tests/cases/compiler/autoLift2.ts(5,17): error TS1005: ';' expected.
3-
tests/cases/compiler/autoLift2.ts(5,19): error TS2304: Cannot find name 'any'.
3+
tests/cases/compiler/autoLift2.ts(5,19): error TS2693: 'any' only refers to a type, but is being used as a value here.
44
tests/cases/compiler/autoLift2.ts(6,14): error TS2339: Property 'bar' does not exist on type 'A'.
55
tests/cases/compiler/autoLift2.ts(6,17): error TS1005: ';' expected.
6-
tests/cases/compiler/autoLift2.ts(6,19): error TS2304: Cannot find name 'any'.
6+
tests/cases/compiler/autoLift2.ts(6,19): error TS2693: 'any' only refers to a type, but is being used as a value here.
77
tests/cases/compiler/autoLift2.ts(12,11): error TS2339: Property 'foo' does not exist on type 'A'.
88
tests/cases/compiler/autoLift2.ts(14,11): error TS2339: Property 'bar' does not exist on type 'A'.
99
tests/cases/compiler/autoLift2.ts(16,33): error TS2339: Property 'foo' does not exist on type 'A'.
@@ -21,14 +21,14 @@ tests/cases/compiler/autoLift2.ts(18,33): error TS2339: Property 'bar' does not
2121
~
2222
!!! error TS1005: ';' expected.
2323
~~~
24-
!!! error TS2304: Cannot find name 'any'.
24+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
2525
this.bar: any;
2626
~~~
2727
!!! error TS2339: Property 'bar' does not exist on type 'A'.
2828
~
2929
!!! error TS1005: ';' expected.
3030
~~~
31-
!!! error TS2304: Cannot find name 'any'.
31+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
3232
}
3333

3434

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/badArrayIndex.ts(1,15): error TS2304: Cannot find name 'number'.
1+
tests/cases/compiler/badArrayIndex.ts(1,15): error TS2693: 'number' only refers to a type, but is being used as a value here.
22
tests/cases/compiler/badArrayIndex.ts(1,22): error TS1109: Expression expected.
33

44

55
==== tests/cases/compiler/badArrayIndex.ts (2 errors) ====
66
var results = number[];
77
~~~~~~
8-
!!! error TS2304: Cannot find name 'number'.
8+
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
99
~
1010
!!! error TS1109: Expression expected.

tests/baselines/reference/baseCheck.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/baseCheck.ts(9,18): error TS2304: Cannot find name 'loc'.
1+
tests/cases/compiler/baseCheck.ts(9,18): error TS2552: Cannot find name 'loc'. Did you mean 'ELoc'?
22
tests/cases/compiler/baseCheck.ts(17,53): error TS2346: Supplied parameters do not match any signature of call target.
33
tests/cases/compiler/baseCheck.ts(17,59): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
44
tests/cases/compiler/baseCheck.ts(18,62): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
@@ -20,7 +20,7 @@ tests/cases/compiler/baseCheck.ts(26,9): error TS2304: Cannot find name 'x'.
2020
constructor(x: number) {
2121
super(0, loc);
2222
~~~
23-
!!! error TS2304: Cannot find name 'loc'.
23+
!!! error TS2552: Cannot find name 'loc'. Did you mean 'ELoc'?
2424
}
2525

2626
m() {

tests/baselines/reference/bases.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
tests/cases/compiler/bases.ts(7,14): error TS2339: Property 'y' does not exist on type 'B'.
22
tests/cases/compiler/bases.ts(7,15): error TS1005: ';' expected.
3-
tests/cases/compiler/bases.ts(7,17): error TS2304: Cannot find name 'any'.
3+
tests/cases/compiler/bases.ts(7,17): error TS2693: 'any' only refers to a type, but is being used as a value here.
44
tests/cases/compiler/bases.ts(11,7): error TS2420: Class 'C' incorrectly implements interface 'I'.
55
Property 'x' is missing in type 'C'.
66
tests/cases/compiler/bases.ts(12,5): error TS2377: Constructors for derived classes must contain a 'super' call.
77
tests/cases/compiler/bases.ts(13,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
88
tests/cases/compiler/bases.ts(13,14): error TS2339: Property 'x' does not exist on type 'C'.
99
tests/cases/compiler/bases.ts(13,15): error TS1005: ';' expected.
10-
tests/cases/compiler/bases.ts(13,17): error TS2304: Cannot find name 'any'.
10+
tests/cases/compiler/bases.ts(13,17): error TS2693: 'any' only refers to a type, but is being used as a value here.
1111
tests/cases/compiler/bases.ts(17,9): error TS2339: Property 'x' does not exist on type 'C'.
1212
tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist on type 'C'.
1313

@@ -25,7 +25,7 @@ tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist o
2525
~
2626
!!! error TS1005: ';' expected.
2727
~~~
28-
!!! error TS2304: Cannot find name 'any'.
28+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
2929
}
3030
}
3131

@@ -44,7 +44,7 @@ tests/cases/compiler/bases.ts(18,9): error TS2339: Property 'y' does not exist o
4444
~
4545
!!! error TS1005: ';' expected.
4646
~~~
47-
!!! error TS2304: Cannot find name 'any'.
47+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.
4848
}
4949
~~~~~
5050
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2304: Cannot find name 'any'.
1+
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2693: 'any' only refers to a type, but is being used as a value here.
22

33

44
==== tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts (1 errors) ====
55
var test: any[] = new any[1];
66
~~~
7-
!!! error TS2304: Cannot find name 'any'.
7+
!!! error TS2693: 'any' only refers to a type, but is being used as a value here.

tests/baselines/reference/checkJsxChildrenProperty4.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/jsx/file.tsx(24,28): error TS2339: Property 'NAme' does not exist on type 'IUser'.
1+
tests/cases/conformance/jsx/file.tsx(24,28): error TS2551: Property 'NAme' does not exist on type 'IUser'. Did you mean 'Name'?
22
tests/cases/conformance/jsx/file.tsx(32,9): error TS2322: Type '{ children: ((user: IUser) => Element)[]; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<FetchUser> & IFetchUserProps & { children?: ReactNode; }'.
33
Type '{ children: ((user: IUser) => Element)[]; }' is not assignable to type 'IFetchUserProps'.
44
Types of property 'children' are incompatible.
@@ -32,7 +32,7 @@ tests/cases/conformance/jsx/file.tsx(32,9): error TS2322: Type '{ children: ((us
3232
{ user => (
3333
<h1>{ user.NAme }</h1>
3434
~~~~
35-
!!! error TS2339: Property 'NAme' does not exist on type 'IUser'.
35+
!!! error TS2551: Property 'NAme' does not exist on type 'IUser'. Did you mean 'Name'?
3636
) }
3737
</FetchUser>
3838
);

tests/baselines/reference/classExtendingPrimitive.errors.txt

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(3,17): error TS2304: Cannot find name 'number'.
2-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(4,18): error TS2304: Cannot find name 'string'.
3-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(5,18): error TS2304: Cannot find name 'boolean'.
1+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(3,17): error TS2693: 'number' only refers to a type, but is being used as a value here.
2+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(4,18): error TS2693: 'string' only refers to a type, but is being used as a value here.
3+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(5,18): error TS2693: 'boolean' only refers to a type, but is being used as a value here.
44
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(6,18): error TS2304: Cannot find name 'Void'.
55
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(7,19): error TS1109: Expression expected.
66
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(8,18): error TS2304: Cannot find name 'Null'.
77
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(10,18): error TS2507: Type 'undefined' is not a constructor function type.
8-
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(11,18): error TS2304: Cannot find name 'Undefined'.
8+
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(11,18): error TS2552: Cannot find name 'Undefined'. Did you mean 'undefined'?
99
tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/classExtendingPrimitive.ts(14,18): error TS2507: Type 'typeof E' is not a constructor function type.
1010

1111

@@ -14,13 +14,13 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla
1414

1515
class C extends number { }
1616
~~~~~~
17-
!!! error TS2304: Cannot find name 'number'.
17+
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
1818
class C2 extends string { }
1919
~~~~~~
20-
!!! error TS2304: Cannot find name 'string'.
20+
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
2121
class C3 extends boolean { }
2222
~~~~~~~
23-
!!! error TS2304: Cannot find name 'boolean'.
23+
!!! error TS2693: 'boolean' only refers to a type, but is being used as a value here.
2424
class C4 extends Void { }
2525
~~~~
2626
!!! error TS2304: Cannot find name 'Void'.
@@ -36,7 +36,7 @@ tests/cases/conformance/classes/classDeclarations/classHeritageSpecification/cla
3636
!!! error TS2507: Type 'undefined' is not a constructor function type.
3737
class C7 extends Undefined { }
3838
~~~~~~~~~
39-
!!! error TS2304: Cannot find name 'Undefined'.
39+
!!! error TS2552: Cannot find name 'Undefined'. Did you mean 'undefined'?
4040

4141
enum E { A }
4242
class C8 extends E { }

0 commit comments

Comments
 (0)