Skip to content

Commit edcf087

Browse files
author
Andy
authored
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
1 parent d36f83a commit edcf087

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/harness/unittests/organizeImports.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,24 @@ import D from "lib";
247247
},
248248
libFile);
249249

250+
testOrganizeImports("Unused_false_positive_shorthand_assignment",
251+
{
252+
path: "/test.ts",
253+
content: `
254+
import { x } from "a";
255+
const o = { x };
256+
`
257+
});
258+
259+
testOrganizeImports("Unused_false_positive_export_shorthand",
260+
{
261+
path: "/test.ts",
262+
content: `
263+
import { x } from "a";
264+
export { x };
265+
`
266+
});
267+
250268
testOrganizeImports("MoveToTop",
251269
{
252270
path: "/test.ts",

src/services/findAllReferences.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,11 @@ namespace ts.FindAllReferences.Core {
708708
if (!symbol) return true; // Be lenient with invalid code.
709709
return getPossibleSymbolReferencePositions(sourceFile, symbol.name).some(position => {
710710
const token = tryCast(getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true), isIdentifier);
711-
return token && token !== definition && token.escapedText === definition.escapedText && checker.getSymbolAtLocation(token) === symbol;
711+
if (!token || token === definition || token.escapedText !== definition.escapedText) return false;
712+
const referenceSymbol = checker.getSymbolAtLocation(token);
713+
return referenceSymbol === symbol
714+
|| checker.getShorthandAssignmentValueSymbol(token.parent) === symbol
715+
|| isExportSpecifier(token.parent) && getLocalSymbolForExportSpecifier(token, referenceSymbol, token.parent, checker) === symbol;
712716
});
713717
}
714718

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ==ORIGINAL==
2+
3+
import { x } from "a";
4+
export { x };
5+
6+
// ==ORGANIZED==
7+
8+
import { x } from "a";
9+
export { x };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ==ORIGINAL==
2+
3+
import { x } from "a";
4+
const o = { x };
5+
6+
// ==ORGANIZED==
7+
8+
import { x } from "a";
9+
const o = { x };

0 commit comments

Comments
 (0)