-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
object rest - fix when destructuring in variables/parameters #4755
Conversation
var { [a]: b, ...c } = z; | ||
var {x1, ...y1} = z; | ||
let {x2, y2, ...z2} = z; | ||
const {w3, x3, y3, ...z4} = z; |
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.
Should also check, just to see if things are preserved as expected:
let [ x4, { y4, ...z4 } ] = z
let { x: { y: [ y5 ] = [], ...z5 } = {} } = z
var { x1 } = z; | ||
var y1 = babelHelpers.objectWithoutProperties(z, ["x1"]); | ||
var { [a]: b } = z; | ||
var c = babelHelpers.objectWithoutProperties(z, [a]); |
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.
(unrelated but) this breaks in real code when typeof a
is not string or symbol :(
c5dd9b6
to
7744976
Compare
Current coverage is 89.32% (diff: 100%)@@ master #4755 diff @@
==========================================
Files 196 196
Lines 13816 13936 +120
Methods 1436 1449 +13
Messages 0 0
Branches 3211 3239 +28
==========================================
+ Hits 12335 12449 +114
- Misses 1481 1487 +6
Partials 0 0
|
7744976
to
d4f16c7
Compare
cc @sebmarkbage http://astexplorer.net/#/Y32VLF7LZ0/13 based on the destructuring transform there are a few other (weird) ways to use RestProperty? Common // VariableDeclaration
const { a, ...b } = c;
// in Parameters
function a({ b, ...c }) {} // ExportNamedDeclaration
export var { b, ...c } = asdf2; Odd/not fixed yet// AssignmentExpression
({a, ...b} = c);
// ForXStatement
for (let {a, ...b} of []) {}
// CatchClause
try {}
catch ({a, ...b}) {} |
98a6238
to
7e173dd
Compare
|
Oops that is a wrong example! We already fixed those static errors in babylon (https://github.com/babel/babylon/releases/tag/v6.11.3) |
Was going to ask if it'd be worth breaking out any of the copied code from visitor/transform code from one of the plugins so both can use it, but in reality that level of abstraction is probably just going to create more of a maintenance pain. |
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.
Stared at the test cases for quite a bit, and I couldn't find anything you weren't covering.
Yeah exactly - I think in reality not everything is updated/shared that much anyway? |
var y1 = babelHelpers.objectWithoutProperties(z, ["x1"]); | ||
x1++; | ||
var { [a]: b } = z; | ||
var c = babelHelpers.objectWithoutProperties(z, [a]); |
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.
a
is evaluated twice (fine here, but what if it's a CallExpression
or a MemberExpression
?)
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.
objectWithoutProperties
also won't behave correctly if typeof a !== 'string'
: https://github.com/babel/babel/blob/master/packages/babel-helpers/src/helpers.js#L434
function hasRestProperty(node) { | ||
for (let property of (node.properties)) { | ||
if (t.isRestProperty(property)) { | ||
return true; |
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.
to be more type consistent I would also add a return false
at the end of the function.
RestProperty(path) { | ||
if (!this.originalPath.node) { | ||
return; | ||
} |
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.
Too bad we don't have working coverage reports, would be interested if this is covered. Don't see why it should be falsey.
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.
I think it was because at the time I was getting an infinite loop or the node was removed and I was trying to use it - will remove.
3af851e
to
ddfcd88
Compare
Ok let's do this. 🎉 finally |
👍 |
Docs are still mentioning this PR: https://babeljs.io/docs/plugins/transform-object-rest-spread/ |
|
Sync this README with https://github.com/babel/babel.github.io/blob/master/docs/plugins/transform-object-rest-spread.md I removed the quote which mention babel#4755 because it's already fixed.
https://babeljs.io/docs/plugins/transform-object-rest-spread/ Object Rest currently depends on the destructuring transform and parameters transform Even if you are using Node 6 or a platform that supports destructuring, transform-es2015-destructuring and transform-es2015-parameters will currently need to be enabled if using object rest properties. There is a PR open to fix this and make this transform standalone: babel/babel#4755
It seems like the docs here are still wrong about this: https://babeljs.io/docs/plugins/transform-object-rest-spread/ |
I've been having issues with Babel and When looking at the transpiled code it looks like the line that is causing the error was transpiled away but looking further it seems that it was moved elsewhere. Here's what my {
"presets": [
["env", {
"targets": {
"node": 7.2
}
}]
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread",
"transform-flow-strip-types"
]
} I tried reproducing this in the Babel REPL but was unable to. Is it possible I'm seeing some type of edge case? |
In order to help you more you'll need to provide a repro case with code, otherwise it's not easy to find the issue. There are a few issues with it already that have PRs. #5088, #5151. if you need more want help, I would recommend checking out our slack community |
On a related note, would be nice to have a tool that takes a git commit as input and tells you whether it was published on npm (bonus: alerts you by email when it is :)). |
😄 yeah I want the bot (babel-bot) to make a message when the merged pr is released and in what version etc. |
Description
Fixes #4074
The issue is that this plugin (http://babeljs.io/docs/plugins/transform-object-rest-spread/) depends on the destructuring/parameter plugins to work correctly. Thus it doesn't work standalone and this PR is trying to fix that!
REPL
Test with http://astexplorer.net/#/Y32VLF7LZ0/16
Test Cases
RestProperty
SpreadProperty
Testing
Can test locally by replacing in
node_modules
: https://gist.github.com/hzoo/d269ccfd5758c556c900f47dd752cd61replace
node_modules/babel-plugin-transform-object-rest-spread/lib/index.js
Future
#4755 (comment)