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
pure annotations incorrectly output, leading to bad bundles #10306
Comments
Hey @kzc! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite. |
This is because in JavaScript parentheses aren't meaningful: the spec defines If you need Babel to preserve parentheses, you can enable the |
I thought comments in Babel were already associated with specific AST nodes by default - i.e.: |
Adding
Unfortunately, running babel on the second test case with this new
|
I managed to get the diff -u node_modules/regenerator-transform/lib/emit.js-orig node_modules/regenerator-transform/lib/emit.js
--- node_modules/regenerator-transform/lib/emit.js-orig 2019-08-08 19:24:16.000000000 -0400
+++ node_modules/regenerator-transform/lib/emit.js 2019-08-08 19:23:28.000000000 -0400
@@ -942,6 +942,9 @@
self.mark(after);
return self.contextProperty("sent");
+ case "ParenthesizedExpression":
+ return finish(t.parenthesizedExpression(self.explodeExpression(path.get("expression"))));
+
default:
throw new Error("unknown Expression of type " + JSON.stringify(expr.type));
}
The patch is probably wrong, but it's a good start for someone who knows what they're doing to put together a proper PR for regenerator. I won't work on this further. This problem wasn't trivial to debug. Is there a chance that Babel could enable the parser option Thanks for your help @nicolo-ribaudo. |
This old PR could be applicable to the AFAICT PR #7044 would not remedy the first test case in the top post when |
Closing out issue since parentheses output behavior with respect to comments appears to be by design and the default is not likely going to change. |
Bug Report
Current Behavior
Pure annotations of calls within compound call expressions are not emitted correctly. They lack the necessary parentheses required to maintain the intent of the annotations.
Input Code
Expected output
Actual incorrect generated code
Pure annotations are greedy - without parentheses they apply to the longest subsequent call (or
new
) expression. When this babel output is passed through uglify-js, terser or rollup it produces an incorrect result:You can see above that
statement.should().be().kept();
was dropped due to the unintended altered pure annotation.This is the desired result, as demonstrated by Rollup on the original input:
Babel Configuration (.babelrc, package.json, cli command)
Modules installed:
Environment
Possible Solution
Parentheses could be output around pure annotated calls if nested within a call expression or
new
expression. Alternatively, an unnecessary pure annotation could be removed from a call subexpression from within a larger call expression.A more complex example with an immediately invoked bind of an async function
Expected result:
Babel generated output:
If the babel generated output is run directly with node, it's correct:
However, the result is incorrect if the babel generated code is run through terser:
The result is also incorrect when the babel generated output is run through rollup without minification:
Here's how the babel generated output ought to look with the correct parentheses inserted:
The babel output with modifications now produces the desired result when run through terser:
...and with rollup:
The diff between the babel output and the corrected babel output:
Additional information on pure annotations:
rollup/rollup#2429 (comment)
The text was updated successfully, but these errors were encountered: