Fix bug: isSymbolReferencedInFile should return true for shorthand property assignment (#23314)

* Fix bug: isSymbolReferencedInFile should return true for shorthand property assignment

* Also test for export specifier
This commit is contained in:
Andy 2018-04-11 19:01:30 -07:00 committed by GitHub
parent d36f83a98d
commit edcf087145
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 1 deletions

View file

@ -247,6 +247,24 @@ import D from "lib";
},
libFile);
testOrganizeImports("Unused_false_positive_shorthand_assignment",
{
path: "/test.ts",
content: `
import { x } from "a";
const o = { x };
`
});
testOrganizeImports("Unused_false_positive_export_shorthand",
{
path: "/test.ts",
content: `
import { x } from "a";
export { x };
`
});
testOrganizeImports("MoveToTop",
{
path: "/test.ts",

View file

@ -708,7 +708,11 @@ namespace ts.FindAllReferences.Core {
if (!symbol) return true; // Be lenient with invalid code.
return getPossibleSymbolReferencePositions(sourceFile, symbol.name).some(position => {
const token = tryCast(getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true), isIdentifier);
return token && token !== definition && token.escapedText === definition.escapedText && checker.getSymbolAtLocation(token) === symbol;
if (!token || token === definition || token.escapedText !== definition.escapedText) return false;
const referenceSymbol = checker.getSymbolAtLocation(token);
return referenceSymbol === symbol
|| checker.getShorthandAssignmentValueSymbol(token.parent) === symbol
|| isExportSpecifier(token.parent) && getLocalSymbolForExportSpecifier(token, referenceSymbol, token.parent, checker) === symbol;
});
}

View file

@ -0,0 +1,9 @@
// ==ORIGINAL==
import { x } from "a";
export { x };
// ==ORGANIZED==
import { x } from "a";
export { x };

View file

@ -0,0 +1,9 @@
// ==ORIGINAL==
import { x } from "a";
const o = { x };
// ==ORGANIZED==
import { x } from "a";
const o = { x };