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
Changes from 3 commits
e4d4746
d6c01d3
961233c
e039d8e
a7728f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
((a, { | ||
b: _b = 0, | ||
c: _c = 3 | ||
c = 3 | ||
}) => { | ||
return a === 1 && _b === 2 && _c === 3; | ||
return a === 1 && _b === 2 && c === 3; | ||
})(1, { | ||
b: 2 | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -26,6 +27,22 @@ const renameVisitor: Visitor<Renamer> = { | |
} | ||
} | ||
}, | ||
ObjectProperty(path: NodePath<ObjectProperty>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this function we should also check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it doesn't matter? Because this should not happen normally in AST. Also technically, even |
||
const { extra, key, value, computed, shorthand } = path.node; | ||
|
||
if (computed) { | ||
return; | ||
} | ||
|
||
if (!shorthand) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh yes, shorthand properties cannot be computed. |
||
|
||
if ((key as Identifier).name !== (value as Identifier).name) { | ||
path.node.shorthand = false; | ||
if (extra?.shorthand) extra.shorthand = false; | ||
} | ||
}, | ||
|
||
"AssignmentExpression|Declaration|VariableDeclarator"( | ||
path: NodePath<t.AssignmentPattern | t.Declaration | t.VariableDeclarator>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1038,5 +1038,58 @@ describe("scope", () => { | |
}, | ||
}); | ||
}); | ||
|
||
it(".shorthand after renaming `ObjectProperty ` in `ObjectPattern`", () => { | ||
const program = getPath(` | ||
const { a } = b; | ||
({ a } = b); | ||
`); | ||
|
||
program.traverse({ | ||
Identifier(path) { | ||
if (path.node.name !== "a") return; | ||
|
||
path.scope.rename("a"); | ||
}, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could:
Also, it would be good to have the following tests:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
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);" | ||
`); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should be transformed, because Edge 15 doesn't support default values in destructuring in parameters of arrow functions.
I don't understand why the first property is still transformed, but the second one isn't 🤔
The source code of this transform is https://github.com/babel/preset-modules/blob/master/src/plugins/transform-edge-default-parameters/index.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the reason is setting:
And if
shorthand
set tofalse
no fix forObjectProperty
occurred.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also first version of my PR didn't affect additional fixtures, should I revert to previous version?
@liuxingbaoyu have you test this case locally? My tests with "additional traversing" worked good.