Skip to content

In import code fix, don't treat a re-export as an import #18341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace ts.codefix {
const symbolIdActionMap = new ImportCodeActionMap();

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

const currentTokenMeaning = getMeaningFromLocation(token);
Expand Down Expand Up @@ -199,28 +199,20 @@ namespace ts.codefix {
return cached;
}

const existingDeclarations: (ImportDeclaration | ImportEqualsDeclaration)[] = [];
for (const importModuleSpecifier of sourceFile.imports) {
const importSymbol = checker.getSymbolAtLocation(importModuleSpecifier);
if (importSymbol === moduleSymbol) {
existingDeclarations.push(getImportDeclaration(importModuleSpecifier));
}
}
const existingDeclarations = mapDefined(sourceFile.imports, importModuleSpecifier =>
checker.getSymbolAtLocation(importModuleSpecifier) === moduleSymbol ? getImportDeclaration(importModuleSpecifier) : undefined);
cachedImportDeclarations[moduleSymbolId] = existingDeclarations;
return existingDeclarations;

function getImportDeclaration(moduleSpecifier: LiteralExpression) {
let node: Node = moduleSpecifier;
while (node) {
if (node.kind === SyntaxKind.ImportDeclaration) {
return <ImportDeclaration>node;
}
if (node.kind === SyntaxKind.ImportEqualsDeclaration) {
return <ImportEqualsDeclaration>node;
}
node = node.parent;
function getImportDeclaration({ parent }: LiteralExpression): AnyImportSyntax {
switch (parent.kind) {
case SyntaxKind.ImportDeclaration:
return parent as ImportDeclaration;
case SyntaxKind.ExternalModuleReference:
return (parent as ExternalModuleReference).parent;
default:
return undefined;
}
return undefined;
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/importNameCodeFixReExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

// Test that we are not fooled by a re-export existing in the file already

// @Filename: /a.ts
////export const x = 0";

// @Filename: /b.ts
////[|export { x } from "./a";
////x;|]

goTo.file("/b.ts");
verify.rangeAfterCodeFix(`import { x } from "./a";

export { x } from "./a";
x;`, /*includeWhiteSpace*/ true);