In import code fix, don't treat a re-export as an import (#18341)
This commit is contained in:
parent
409d6597eb
commit
018c645913
2 changed files with 27 additions and 19 deletions
|
@ -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);
|
||||
|
@ -199,30 +199,22 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getUniqueSymbolId(symbol: Symbol) {
|
||||
return getSymbolId(skipAlias(symbol, checker));
|
||||
|
|
16
tests/cases/fourslash/importNameCodeFixReExport.ts
Normal file
16
tests/cases/fourslash/importNameCodeFixReExport.ts
Normal file
|
@ -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);
|
Loading…
Reference in a new issue