Skip to content

Commit d1e5ad7

Browse files
author
Andy Hanson
committed
In import code fix, don't treat a re-export as an import
1 parent deefb01 commit d1e5ad7

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ namespace ts.codefix {
133133
const symbolIdActionMap = new ImportCodeActionMap();
134134

135135
// this is a module id -> module import declaration map
136-
const cachedImportDeclarations: (ImportDeclaration | ImportEqualsDeclaration)[][] = [];
136+
const cachedImportDeclarations: AnyImportSyntax[][] = [];
137137
let lastImportDeclaration: Node;
138138

139139
const currentTokenMeaning = getMeaningFromLocation(token);
@@ -199,28 +199,20 @@ namespace ts.codefix {
199199
return cached;
200200
}
201201

202-
const existingDeclarations: (ImportDeclaration | ImportEqualsDeclaration)[] = [];
203-
for (const importModuleSpecifier of sourceFile.imports) {
204-
const importSymbol = checker.getSymbolAtLocation(importModuleSpecifier);
205-
if (importSymbol === moduleSymbol) {
206-
existingDeclarations.push(getImportDeclaration(importModuleSpecifier));
207-
}
208-
}
202+
const existingDeclarations = mapDefined(sourceFile.imports, importModuleSpecifier =>
203+
checker.getSymbolAtLocation(importModuleSpecifier) === moduleSymbol ? getImportDeclaration(importModuleSpecifier) : undefined);
209204
cachedImportDeclarations[moduleSymbolId] = existingDeclarations;
210205
return existingDeclarations;
211206

212-
function getImportDeclaration(moduleSpecifier: LiteralExpression) {
213-
let node: Node = moduleSpecifier;
214-
while (node) {
215-
if (node.kind === SyntaxKind.ImportDeclaration) {
216-
return <ImportDeclaration>node;
217-
}
218-
if (node.kind === SyntaxKind.ImportEqualsDeclaration) {
219-
return <ImportEqualsDeclaration>node;
220-
}
221-
node = node.parent;
207+
function getImportDeclaration({ parent }: LiteralExpression): AnyImportSyntax {
208+
switch (parent.kind) {
209+
case SyntaxKind.ImportDeclaration:
210+
return parent as ImportDeclaration;;
211+
case SyntaxKind.ExternalModuleReference:
212+
return (parent as ExternalModuleReference).parent;
213+
default:
214+
return undefined;
222215
}
223-
return undefined;
224216
}
225217
}
226218

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Test that we are not fooled by a re-export existing in the file already
4+
5+
// @Filename: /a.ts
6+
////export const x = 0";
7+
8+
// @Filename: /b.ts
9+
////[|export { x } from "./a";
10+
////x;|]
11+
12+
goTo.file("/b.ts");
13+
verify.rangeAfterCodeFix(`import { x } from "./a";
14+
15+
export { x } from "./a";
16+
x;`, /*includeWhiteSpace*/ true);

0 commit comments

Comments
 (0)