Skip to content

Commit 4f0b81d

Browse files
authored
fix(32341): add prefix name for module exports properties (microsoft#38541)
1 parent fa49ac0 commit 4f0b81d

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

src/services/findAllReferences.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,23 +399,40 @@ namespace ts.FindAllReferences {
399399
function getPrefixAndSuffixText(entry: Entry, originalNode: Node, checker: TypeChecker): PrefixAndSuffix {
400400
if (entry.kind !== EntryKind.Span && isIdentifier(originalNode)) {
401401
const { node, kind } = entry;
402+
const parent = node.parent;
402403
const name = originalNode.text;
403-
const isShorthandAssignment = isShorthandPropertyAssignment(node.parent);
404-
if (isShorthandAssignment || isObjectBindingElementWithoutPropertyName(node.parent) && node.parent.name === node) {
404+
const isShorthandAssignment = isShorthandPropertyAssignment(parent);
405+
if (isShorthandAssignment || isObjectBindingElementWithoutPropertyName(parent) && parent.name === node) {
405406
const prefixColon: PrefixAndSuffix = { prefixText: name + ": " };
406407
const suffixColon: PrefixAndSuffix = { suffixText: ": " + name };
407-
return kind === EntryKind.SearchedLocalFoundProperty ? prefixColon
408-
: kind === EntryKind.SearchedPropertyFoundLocal ? suffixColon
409-
// In `const o = { x }; o.x`, symbolAtLocation at `x` in `{ x }` is the property symbol.
410-
// For a binding element `const { x } = o;`, symbolAtLocation at `x` is the property symbol.
411-
: isShorthandAssignment ? suffixColon : prefixColon;
408+
if (kind === EntryKind.SearchedLocalFoundProperty) {
409+
return prefixColon;
410+
}
411+
if (kind === EntryKind.SearchedPropertyFoundLocal) {
412+
return suffixColon;
413+
}
414+
415+
// In `const o = { x }; o.x`, symbolAtLocation at `x` in `{ x }` is the property symbol.
416+
// For a binding element `const { x } = o;`, symbolAtLocation at `x` is the property symbol.
417+
if (isShorthandAssignment) {
418+
const grandParent = parent.parent;
419+
if (isObjectLiteralExpression(grandParent) &&
420+
isBinaryExpression(grandParent.parent) &&
421+
isModuleExportsAccessExpression(grandParent.parent.left)) {
422+
return prefixColon;
423+
}
424+
return suffixColon;
425+
}
426+
else {
427+
return prefixColon;
428+
}
412429
}
413-
else if (isImportSpecifier(entry.node.parent) && !entry.node.parent.propertyName) {
430+
else if (isImportSpecifier(parent) && !parent.propertyName) {
414431
// If the original symbol was using this alias, just rename the alias.
415432
const originalSymbol = isExportSpecifier(originalNode.parent) ? checker.getExportSpecifierLocalTargetSymbol(originalNode.parent) : checker.getSymbolAtLocation(originalNode);
416-
return contains(originalSymbol!.declarations, entry.node.parent) ? { prefixText: name + " as " } : emptyOptions;
433+
return contains(originalSymbol!.declarations, parent) ? { prefixText: name + " as " } : emptyOptions;
417434
}
418-
else if (isExportSpecifier(entry.node.parent) && !entry.node.parent.propertyName) {
435+
else if (isExportSpecifier(parent) && !parent.propertyName) {
419436
// If the symbol for the node is same as declared node symbol use prefix text
420437
return originalNode === entry.node || checker.getSymbolAtLocation(originalNode) === checker.getSymbolAtLocation(entry.node) ?
421438
{ prefixText: name + " as " } :
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
4+
////module.exports = { [|A|] }
5+
6+
const [r0Def, r0, r1] = test.ranges();
7+
verify.renameLocations(r0, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });
8+
verify.renameLocations(r1, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
4+
////module.exports = { B: [|A|] }
5+
6+
const [r0Def, r0, r1] = test.ranges();
7+
verify.renameLocations(r0, [r0, r1]);
8+
verify.renameLocations(r1, [r0, r1]);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
////[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
6+
////module.exports = { [|A|] }
7+
8+
const [r0Def, r0, r1] = test.ranges();
9+
verify.renameLocations(r0, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });
10+
verify.renameLocations(r1, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });

0 commit comments

Comments
 (0)