Skip to content

fix(32341): Renaming class name switches up order in object literal shorthand #38541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,23 +399,40 @@ namespace ts.FindAllReferences {
function getPrefixAndSuffixText(entry: Entry, originalNode: Node, checker: TypeChecker): PrefixAndSuffix {
if (entry.kind !== EntryKind.Span && isIdentifier(originalNode)) {
const { node, kind } = entry;
const parent = node.parent;
const name = originalNode.text;
const isShorthandAssignment = isShorthandPropertyAssignment(node.parent);
if (isShorthandAssignment || isObjectBindingElementWithoutPropertyName(node.parent) && node.parent.name === node) {
const isShorthandAssignment = isShorthandPropertyAssignment(parent);
if (isShorthandAssignment || isObjectBindingElementWithoutPropertyName(parent) && parent.name === node) {
const prefixColon: PrefixAndSuffix = { prefixText: name + ": " };
const suffixColon: PrefixAndSuffix = { suffixText: ": " + name };
return kind === EntryKind.SearchedLocalFoundProperty ? prefixColon
: kind === EntryKind.SearchedPropertyFoundLocal ? suffixColon
// In `const o = { x }; o.x`, symbolAtLocation at `x` in `{ x }` is the property symbol.
// For a binding element `const { x } = o;`, symbolAtLocation at `x` is the property symbol.
: isShorthandAssignment ? suffixColon : prefixColon;
if (kind === EntryKind.SearchedLocalFoundProperty) {
return prefixColon;
}
if (kind === EntryKind.SearchedPropertyFoundLocal) {
return suffixColon;
}

// In `const o = { x }; o.x`, symbolAtLocation at `x` in `{ x }` is the property symbol.
// For a binding element `const { x } = o;`, symbolAtLocation at `x` is the property symbol.
if (isShorthandAssignment) {
const grandParent = parent.parent;
if (isObjectLiteralExpression(grandParent) &&
isBinaryExpression(grandParent.parent) &&
isModuleExportsAccessExpression(grandParent.parent.left)) {
return prefixColon;
}
return suffixColon;
}
else {
return prefixColon;
}
}
else if (isImportSpecifier(entry.node.parent) && !entry.node.parent.propertyName) {
else if (isImportSpecifier(parent) && !parent.propertyName) {
// If the original symbol was using this alias, just rename the alias.
const originalSymbol = isExportSpecifier(originalNode.parent) ? checker.getExportSpecifierLocalTargetSymbol(originalNode.parent) : checker.getSymbolAtLocation(originalNode);
return contains(originalSymbol!.declarations, entry.node.parent) ? { prefixText: name + " as " } : emptyOptions;
return contains(originalSymbol!.declarations, parent) ? { prefixText: name + " as " } : emptyOptions;
}
else if (isExportSpecifier(entry.node.parent) && !entry.node.parent.propertyName) {
else if (isExportSpecifier(parent) && !parent.propertyName) {
// If the symbol for the node is same as declared node symbol use prefix text
return originalNode === entry.node || checker.getSymbolAtLocation(originalNode) === checker.getSymbolAtLocation(entry.node) ?
{ prefixText: name + " as " } :
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/fourslash/renameModuleExportsProperties1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

////[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
////module.exports = { [|A|] }

const [r0Def, r0, r1] = test.ranges();
verify.renameLocations(r0, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });
verify.renameLocations(r1, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });
8 changes: 8 additions & 0 deletions tests/cases/fourslash/renameModuleExportsProperties2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

////[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
////module.exports = { B: [|A|] }

const [r0Def, r0, r1] = test.ranges();
verify.renameLocations(r0, [r0, r1]);
verify.renameLocations(r1, [r0, r1]);
10 changes: 10 additions & 0 deletions tests/cases/fourslash/renameModuleExportsProperties3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />

// @allowJs: true
// @Filename: a.js
////[|class [|{| "contextRangeIndex": 0 |}A|] {}|]
////module.exports = { [|A|] }

const [r0Def, r0, r1] = test.ranges();
verify.renameLocations(r0, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });
verify.renameLocations(r1, { ranges: [r0, { range: r1, prefixText: "A: " }], providePrefixAndSuffixTextForRename: true });