Skip to content

Commit 1bff5b7

Browse files
committed
Codefix:suggest spellings+update others w/new codes
1 parent 2345ecd commit 1bff5b7

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

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
@@ -116,6 +116,7 @@ namespace ts.codefix {
116116
registerCodeFix({
117117
errorCodes: [
118118
Diagnostics.Cannot_find_name_0.code,
119+
Diagnostics.Cannot_find_name_0_Did_you_mean_1.code,
119120
Diagnostics.Cannot_find_namespace_0.code,
120121
Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead.code
121122
],

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+
}

0 commit comments

Comments
 (0)