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

Lines changed: 170 additions & 48 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 12 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 53 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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'?

0 commit comments

Comments
 (0)