-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
The keyword 'await' is reserved #564
Comments
Appears to only happen when the value is an anonymous function and using the babel plugin.
|
Apparently this has been seen before.... d3/d3-queue#55 |
I can't seem to reproduce the error. Here's what I did:
Here's my relevant version info:
What happens when you try the same thing? |
Following your setup I see no error. However after adding the babel plugin and the es2015-rollup preset I see the error I described. ./node_modules/.bin/rollup -c rollup.config.js -o out.js
Error parsing /Users/jayson/workspace/temp/rollup-564/index.js: The keyword 'await' is reserved (4:18) in /Users/jayson/workspace/temp/rollup-564/index.js
Type rollup --help for help, or visit https://github.com/rollup/rollup/wiki Gist: |
I'll take a look. |
Babel-cli does not complain: babel index.js
export var test = {
// await: 123, // works!
// [`await`]: a => 2 * a, // works!
await: function await(a) {
return 2 * a;
}, // Error parsing [path]/utils.js: The keyword 'await' is reserved (8:18) in [path]/utils.js
add: function add(n, m) {
return n + m;
}
}; |
It's the |
Okay, I tracked it down a bit more and found acornjs/acorn#326, which implies that the code generated by babel is invalid. Therefore, I believe this is actually a babel bug. |
So |
"invalid as an identifier" means |
As properties, yes. But the part acorn is complaining about is the function name. |
Ok, so properties are "IdentifierNames" while function names are "Identifiers". When Babel encounters an anonymous function it transpiles to using the IdentifierName as the function name. (https://babeljs.io/repl/#?evaluate=false&presets=es2015&experimental=true&loose=false&spec=false&code=%0A%0Aexport%20const%20test%20%3D%20%7B%0A%20%20await%3A%20a%20%3D%3E%202%20*%20a%0A%7D). I think I have see this listed somewhere before as unexpected behavior. Not really a rollup bug, it's just complaining about bad Babel output. |
Specifically "babel-plugin-transform-es2015-function-name" is doing the naming. |
Adding:
to babel-preset-es2015-rollup resolves this issue... unsure if it creates more. |
I submitted a bug to babel. https://phabricator.babeljs.io/T7235 I'm guessing the correct behavior is for babel-plugin-transform-es2015-function-name to not name a function a reserved word. Alternatively babel-preset-es2015-rollup can remove the 'babel-plugin-transform-es2015-function-name' plugin. The output looks good to me with 'babel-plugin-transform-es2015-function-name' plugin removed:
|
I'd be happy to submit a PR to babel-preset-es2015-rollup if we think that is where it belongs... @eventualbuddha @Rich-Harris |
Ideally this should be fixed in babel, though I don't know what their stance will be on whether this is a bug. Another possibility is to replace acorn with babylon inside rollup. And yet another possibility is to set |
Also pretty easy to disable function renaming in the babel rollup preset: https://github.com/rollup/babel-preset-es2015-rollup/blob/master/index.js I don't really see the benefit of the renaming plugin... Maybe it is necessary for es2016 compliance? I'm pretty sure very few people are encountering this issue. |
@Hypercubed sure, but that's behavior that people may have been relying on. I'm not sure how exactly, and maybe there's no behavioral difference and it's just a nice thing for stack traces. Enabling the |
True, and it seems that the naming is part of the spec... But I'd imagine if anyone is relying on that they would have trouble after minification. |
@Hypercubed thanks for digging in further on this than i got in d3/d3-queue#55 |
👍 to |
You mean a doc warning?
|
I was thinking something like this, logged to the console during bundling:
Of course ideally this category of warning would be handled in a consistent fashion, i.e we have some helpers for logging syntax errors with code snippets (can't remember where we previously discussed this) – this case is a bit tricky because whereas you generally want to refer back to the original source (i.e. trace sourcemaps), here the problems is introduced during transformation... |
BTW @Rich-Harris : My solution for babel that avoids this issue with rollup/acorn (using babel) is:
But, this causes an error in buble: "Computed properties are not supported" 😞 |
I don't know if Babel still has this bug, but I've raised #709 as a possible solution |
Actually, looking into this further, it may be a bug with Acorn. Doesn't look like there's a problem with |
Just released 0.31.0 which lets you specify options to pass to Acorn: rollup.rollup({
entry: 'src/main.js',
acorn: {
allowReserved: true
}
}).then( bundle => ... ); |
Not sure if this is a bug in rollup or if there is some restriction in using reserved words in objects. The strange thing is the error only occurs if the value is a function:
See here:
https://github.com/Hypercubed/rollup-starter-project/blob/keyword-bug/lib/utils.js#L5
The text was updated successfully, but these errors were encountered: