diff --git a/src/harness/unittests/organizeImports.ts b/src/harness/unittests/organizeImports.ts index 3de5588af8e..7d496b97d99 100644 --- a/src/harness/unittests/organizeImports.ts +++ b/src/harness/unittests/organizeImports.ts @@ -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", diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index e2d9219b96b..80ed225575b 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -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; }); } diff --git a/tests/baselines/reference/organizeImports/Unused_false_positive_export_shorthand.ts b/tests/baselines/reference/organizeImports/Unused_false_positive_export_shorthand.ts new file mode 100644 index 00000000000..5cf0cbcae24 --- /dev/null +++ b/tests/baselines/reference/organizeImports/Unused_false_positive_export_shorthand.ts @@ -0,0 +1,9 @@ +// ==ORIGINAL== + +import { x } from "a"; +export { x }; + +// ==ORGANIZED== + +import { x } from "a"; +export { x }; diff --git a/tests/baselines/reference/organizeImports/Unused_false_positive_shorthand_assignment.ts b/tests/baselines/reference/organizeImports/Unused_false_positive_shorthand_assignment.ts new file mode 100644 index 00000000000..578b3dfc5e7 --- /dev/null +++ b/tests/baselines/reference/organizeImports/Unused_false_positive_shorthand_assignment.ts @@ -0,0 +1,9 @@ +// ==ORIGINAL== + +import { x } from "a"; +const o = { x }; + +// ==ORGANIZED== + +import { x } from "a"; +const o = { x };