Set shorthand: false when renaming an identifier inside an object property#15649
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54566/ |
| path.node.declarations[0].id.properties[0].shorthand, | ||
| ).toBeFalsy(); | ||
| }, | ||
| }); |
There was a problem hiding this comment.
Could:
- Move the assertion from inside the visitor to after the
program.traversecall, 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 };, renamingaconst { a } = 1; { const { b } = 2 }renamingatob
|
Thank you for your PR! |
|
@liuxingbaoyu I'm OK, if we use your fix |
|
@nicolo-ribaudo @liuxingbaoyu just applied update |
33a3668 to
961233c
Compare
| ((a, { | ||
| b: _b = 0, | ||
| c: _c = 3 | ||
| c = 3 |
There was a problem hiding this comment.
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.
Looks like the reason is setting:
path.parent.shorthand = false;
(path.parent.extra || {}).shorthand = false;And if shorthand set to false no fix for ObjectProperty occurred.
There was a problem hiding this comment.
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.
| } | ||
| } | ||
| }, | ||
| ObjectProperty(path: NodePath<ObjectProperty>) { |
There was a problem hiding this comment.
In this function we should also check if value.name === state.oldName, because we only want to modify the nodes that we are renaming.
There was a problem hiding this comment.
Maybe it doesn't matter? Because this should not happen normally in AST.
Also technically, even value.name === state.oldName is not necessarily the node we just renamed. So I tend not to check it.
| if (computed) { | ||
| return; | ||
| } | ||
|
|
||
| if (!shorthand) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
When shorthand===true, must computed===false? I'm not entirely sure about this, any other reviewers know? Thanks!
There was a problem hiding this comment.
Uh yes, shorthand properties cannot be computed.
|
@nicolo-ribaudo @liuxingbaoyu could you please clarify should I make any additional changes? |
|
@coderaiser I pushed a fix, sorry for the confusion. The problem was #15649 (comment): we were marking all the shorthand properties as non-shorthand, even those that we weren't actually renaming. |
7afa628 to
cf066e6
Compare
| path.node.shorthand = false; | ||
| if (extra?.shorthand) extra.shorthand = false; | ||
| ObjectProperty({ node }: NodePath<ObjectProperty>, state) { | ||
| if (node.shorthand && (node.value as Identifier).name === state.oldName) { |
There was a problem hiding this comment.

I remember they were renamed here, so actually in the ObjectProperty visitor it has been renamed. (I was troubled for a long time at that time😂)
Also sorry I didn't understand what you meant, can you explain?
According to my understanding, the shorthand for key!==value is true is illegal ast, we should have no problem marking them as false.
There was a problem hiding this comment.
I think what's happening is that some visitors are running interspersed, so the AST is temporarily in invalid states while some transforms are running.
There was a problem hiding this comment.
Personally I'd still prefer to use the original fix, and fix another plugin that relies on shorthand properties, plugins shouldn't depend on the wrong shorthand property across different visitors.🤔
There was a problem hiding this comment.
I think renaming a variable a should only touch that variable, and it's not responsible for generically fixing other parts of the AST. However, if we find where we generate the invalid AST we should fix it in that place.
There was a problem hiding this comment.
Yes, I agree with should only touch that variable! It's just that I thought it would be faster to use the original fix here. Renaming should be a relatively common function.
By the way, do you have a plan to find that place that generates invalid AST? If not I'd love to try it!
There was a problem hiding this comment.
Feel free to go ahead :)
cf066e6 to
e039d8e
Compare
ObjectPropertyshorthand: false when renaming an identifier inside an object property

Add ability to handle
shorthandproperty ofObjectPropertyinsideObjectPatternwhenpath.scope.rename()called, so recast and any other parser can relay on this property only instead of using some workarounds: