Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 29, 2023
1 parent 961233c commit e039d8e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 58 deletions.
@@ -1,8 +1,8 @@
((a, {
b: _b = 0,
c = 3
c: _c = 3
}) => {
return a === 1 && _b === 2 && c === 3;
return a === 1 && _b === 2 && _c === 3;
})(1, {
b: 2
});
28 changes: 15 additions & 13 deletions packages/babel-traverse/src/scope/lib/renamer.ts
Expand Up @@ -27,20 +27,22 @@ const renameVisitor: Visitor<Renamer> = {
}
}
},
ObjectProperty(path: NodePath<ObjectProperty>) {
const { extra, key, value, computed, shorthand } = path.node;

if (computed) {
return;
}

if (!shorthand) {
return;
}

if ((key as Identifier).name !== (value as Identifier).name) {
path.node.shorthand = false;
if (extra?.shorthand) extra.shorthand = false;
ObjectProperty({ node, scope }: NodePath<ObjectProperty>, state) {
const { name } = node.key as Identifier;
if (!node.shorthand) return;
if (
node.shorthand &&
// In destrucutring the identifier is already renamed by the
// AssignmentExpression|Declaration|VariableDeclarator visitor,
// while in object literals it's renamed later by the
// ReferencedIdentifier visitor.
(name === state.oldName || name === state.newName) &&
// Ignore shadowed bindings
scope.getBindingIdentifier(name) === state.binding.identifier
) {
node.shorthand = false;
if (node.extra?.shorthand) node.extra.shorthand = false;
}
},

Expand Down
121 changes: 78 additions & 43 deletions packages/babel-traverse/test/scope.js
Expand Up @@ -1039,57 +1039,92 @@ describe("scope", () => {
});
});

it(".shorthand after renaming `ObjectProperty ` in `ObjectPattern`", () => {
it(".shorthand after renaming `ObjectProperty`", () => {
const program = getPath(`
const { a } = b;
({ a } = b);
c = { a };
`);
program.scope.rename("a");

const renamedPropertyMatcher = expect.objectContaining({
type: "ObjectProperty",
shorthand: false,
extra: expect.objectContaining({ shorthand: false }),
key: expect.objectContaining({ name: "a" }),
value: expect.objectContaining({
name: expect.not.stringMatching(/^a$/),
}),
});

program.traverse({
Identifier(path) {
if (path.node.name !== "a") return;
const { body } = program.node;
expect(body[0].declarations[0].id.properties[0]).toStrictEqual(
renamedPropertyMatcher,
);
expect(body[1].expression.left.properties[0]).toStrictEqual(
renamedPropertyMatcher,
);
expect(body[2].expression.right.properties[0]).toStrictEqual(
renamedPropertyMatcher,
);

path.scope.rename("a");
},
});
expect(String(program)).toMatchInlineSnapshot(`
"const {
a: _a
} = b;
({
a: _a
} = b);
c = {
a: _a
};"
`);
});

expect(
t.cloneDeepWithoutLoc(
program.node.body[0].declarations[0].id.properties[0],
),
).toMatchInlineSnapshot(`
Object {
"computed": false,
"extra": Object {
"shorthand": false,
},
"key": Object {
"loc": null,
"name": "a",
"type": "Identifier",
},
"loc": null,
"shorthand": false,
"type": "ObjectProperty",
"value": Object {
"extra": Object {},
"loc": null,
"name": "_a",
"type": "Identifier",
},
}
`);
expect(
t.cloneDeepWithoutLoc(program.node.body[0].declarations[1]),
).toMatchInlineSnapshot(`undefined`);
expect(program + "").toMatchInlineSnapshot(`
"const {
a: _a
} = b;
({
a: _a
} = b);"
it(".shorthand after renaming `ObjectProperty` - shadowed", () => {
const program = getPath(`
const a = 1;
{
const { b } = 2;
({ b } = 3);
(_ = { b });
}
`);
program.scope.rename("a", "b");

const originalPropertyMatcher = expect.objectContaining({
type: "ObjectProperty",
shorthand: true,
extra: expect.objectContaining({ shorthand: true }),
key: expect.objectContaining({ name: "b" }),
value: expect.objectContaining({ name: "b" }),
});

const { body } = program.node;
expect(body[1].body[0].declarations[0].id.properties[0]).toStrictEqual(
originalPropertyMatcher,
);
expect(body[1].body[1].expression.left.properties[0]).toStrictEqual(
originalPropertyMatcher,
);
expect(body[1].body[2].expression.right.properties[0]).toStrictEqual(
originalPropertyMatcher,
);

expect(String(program)).toMatchInlineSnapshot(`
"const b = 1;
{
const {
b
} = 2;
({
b
} = 3);
_ = {
b
};
}"
`);
});
});
});

0 comments on commit e039d8e

Please sign in to comment.