Skip to content
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

Set shorthand: false when renaming an identifier inside an object property #15649

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/babel-traverse/src/scope/lib/renamer.ts
Expand Up @@ -5,6 +5,7 @@ import type { NodePath, Visitor } from "../..";
import { requeueComputedKeyAndDecorators } from "@babel/helper-environment-visitor";
import { traverseNode } from "../../traverse-node";
import { explode } from "../../visitors";
import type { Identifier, ObjectProperty } from "@babel/types";

const renameVisitor: Visitor<Renamer> = {
ReferencedIdentifier({ node }, state) {
Expand Down Expand Up @@ -112,6 +113,26 @@ export default class Renamer {
// assignmentExpression("=", identifier(this.newName), path.node),
// );
}
maybeFixObjectProperty(name: string, path: NodePath) {
path.traverse({
ObjectProperty(path: NodePath<ObjectProperty>) {
const { key, computed, shorthand } = path.node;

if (computed) {
return;
}

if (!shorthand) {
return;
}

if (name === (key as Identifier).name) {
path.node.shorthand = false;
path.stop();
}
},
});
}

rename(/* Babel 7 - block?: t.Pattern | t.Scopable */) {
const { binding, oldName, newName } = this;
Expand Down Expand Up @@ -157,6 +178,7 @@ export default class Renamer {
}

if (parentDeclar) {
this.maybeFixObjectProperty(oldName, path);
this.maybeConvertFromClassFunctionDeclaration(path);
this.maybeConvertFromClassFunctionExpression(path);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-traverse/test/scope.js
Expand Up @@ -1038,5 +1038,18 @@ describe("scope", () => {
},
});
});
it("shorthand of ObjectProperty inside ObjectPattern", () => {
const program = getPath(`
const {a} = b;
`);
program.traverse({
VariableDeclaration(path) {
path.scope.rename("a", "zzz");
expect(
path.node.declarations[0].id.properties[0].shorthand,
).toBeFalsy();
},
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could:

  • Move the assertion from inside the visitor to after the program.traverse call, to be 100% sure that it runs (I know you copied this pattern from the test above, but we can do better 🙂)
  • Replace .toBeFalsy() with .toBe(false) (since we expect false and not any falsy value)
  • Add an expect(program.toString()).toMatchInlineSnapshot() at the end, which helps when reading the tests to more easily see what's happening

Also, it would be good to have the following tests:

  • const a = 1, obj = { a };, renaming a
  • const { a } = 1; { const { b } = 2 } renaming a to b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did another fix locally.
I don't think it's necessary to traverse twice to fix this, just add the ObjectProperty visitor.
At the same time I also found a strange thing, we also have an extra.shorthand in the AST, I don't understand why.🤔
image

});
});
});