Skip to content

Commit 58b147e

Browse files
committed
add spelling suggestion support for module
1 parent d1d6960 commit 58b147e

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ namespace ts {
294294
getAllPossiblePropertiesOfTypes,
295295
getSuggestionForNonexistentProperty: (node, type) => getSuggestionForNonexistentProperty(node, type),
296296
getSuggestionForNonexistentSymbol: (location, name, meaning) => getSuggestionForNonexistentSymbol(location, escapeLeadingUnderscores(name), meaning),
297+
getSuggestionForNonexistentModule: (node, target) => getSuggestionForNonexistentModule(node, target),
297298
getBaseConstraintOfType,
298299
getDefaultFromTypeParameter: type => type && type.flags & TypeFlags.TypeParameter ? getDefaultFromTypeParameter(type as TypeParameter) : undefined,
299300
resolveName(name, location, meaning, excludeGlobals) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,7 @@ namespace ts {
28902890
getApparentType(type: Type): Type;
28912891
getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined;
28922892
getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined;
2893+
getSuggestionForNonexistentModule(node: Identifier, target: Symbol): string | undefined;
28932894
getBaseConstraintOfType(type: Type): Type | undefined;
28942895
getDefaultFromTypeParameter(type: Type): Type | undefined;
28952896

src/services/codefixes/fixSpelling.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ namespace ts.codefix {
44
const errorCodes = [
55
Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code,
66
Diagnostics.Cannot_find_name_0_Did_you_mean_1.code,
7+
Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_2.code,
78
];
89
registerCodeFix({
910
errorCodes,
1011
getCodeActions(context) {
1112
const { sourceFile } = context;
12-
const info = getInfo(sourceFile, context.span.start, context.program.getTypeChecker());
13+
const info = getInfo(sourceFile, context.span.start, context);
1314
if (!info) return undefined;
1415
const { node, suggestion } = info;
1516
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion));
@@ -18,23 +19,32 @@ namespace ts.codefix {
1819
},
1920
fixIds: [fixId],
2021
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
21-
const info = getInfo(diag.file!, diag.start!, context.program.getTypeChecker());
22+
const info = getInfo(diag.file!, diag.start!, context);
2223
if (info) doChange(changes, context.sourceFile, info.node, info.suggestion);
2324
}),
2425
});
2526

26-
function getInfo(sourceFile: SourceFile, pos: number, checker: TypeChecker): { node: Node, suggestion: string } | undefined {
27+
function getInfo(sourceFile: SourceFile, pos: number, context: CodeFixContextBase): { node: Node, suggestion: string } | undefined {
2728
// This is the identifier of the misspelled word. eg:
2829
// this.speling = 1;
2930
// ^^^^^^^
3031
const node = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false); // TODO: GH#15852
32+
const checker = context.program.getTypeChecker();
3133

3234
let suggestion: string;
3335
if (isPropertyAccessExpression(node.parent) && node.parent.name === node) {
3436
Debug.assert(node.kind === SyntaxKind.Identifier);
3537
const containingType = checker.getTypeAtLocation(node.parent.expression);
3638
suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType);
3739
}
40+
else if (isImportSpecifier(node.parent) && node.parent.name === node) {
41+
Debug.assert(node.kind === SyntaxKind.Identifier);
42+
const importDeclaration = findAncestor(node, isImportDeclaration);
43+
const resolvedSourceFile = getResolvedSourceFileFromImportDeclaration(sourceFile, context, importDeclaration);
44+
if (resolvedSourceFile && resolvedSourceFile.symbol) {
45+
suggestion = checker.getSuggestionForNonexistentModule(node as Identifier, resolvedSourceFile.symbol);
46+
}
47+
}
3848
else {
3949
const meaning = getMeaningFromLocation(node);
4050
const name = getTextOfNode(node);
@@ -62,4 +72,13 @@ namespace ts.codefix {
6272
}
6373
return flags;
6474
}
75+
76+
function getResolvedSourceFileFromImportDeclaration (sourceFile: SourceFile, context: CodeFixContextBase, importDeclaration: ImportDeclaration): SourceFile | undefined {
77+
if (!importDeclaration || !isStringLiteralLike(importDeclaration.moduleSpecifier)) return undefined;
78+
79+
const resolvedModule = getResolvedModule(sourceFile, importDeclaration.moduleSpecifier.text);
80+
if (!resolvedModule) return undefined;
81+
82+
return context.program.getSourceFile(resolvedModule.resolvedFileName);
83+
}
6584
}

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,7 @@ declare namespace ts {
18201820
getApparentType(type: Type): Type;
18211821
getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined;
18221822
getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined;
1823+
getSuggestionForNonexistentModule(node: Identifier, target: Symbol): string | undefined;
18231824
getBaseConstraintOfType(type: Type): Type | undefined;
18241825
getDefaultFromTypeParameter(type: Type): Type | undefined;
18251826
}

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,7 @@ declare namespace ts {
18201820
getApparentType(type: Type): Type;
18211821
getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined;
18221822
getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined;
1823+
getSuggestionForNonexistentModule(node: Identifier, target: Symbol): string | undefined;
18231824
getBaseConstraintOfType(type: Type): Type | undefined;
18241825
getDefaultFromTypeParameter(type: Type): Type | undefined;
18251826
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: f1.ts
4+
////export const fooooooooo = 1;
5+
6+
// @Filename: f2.ts
7+
////import {[|fooooooooa|]} from "./f1";
8+
9+
goTo.file("f2.ts")
10+
verify.rangeAfterCodeFix(`fooooooooo`);

0 commit comments

Comments
 (0)