-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 annotation within parentheses not recognized #2629
Comments
It works within parenthesized sequences: $ echo 'a(); ( /*@__PURE__*/x(), y() ); c();' | bin/uglifyjs -c
a(),y(),c();
$ echo 'a(); ( w(), /*@__PURE__*/x(), y() ); c();' | bin/uglifyjs -c
a(),w(),y(),c(); Arrays are fine too: $ echo 'a(); [ /*@__PURE__*/b() ]; c();' | bin/uglifyjs -c
a(),c();
$ echo 'a(); [ /*@__PURE__*/x(), y() ]; c();' | bin/uglifyjs -c
a(),y(),c();
$ echo 'a(); [ w(), /*@__PURE__*/x(), y() ]; c();' | bin/uglifyjs -c
a(),w(),y(),c(); |
This can be fixed in either the parser or the compressor. I think the compressor is the safer option given the complexity of parsing sequences, arrow expressions and parameters: --- a/lib/compress.js
+++ b/lib/compress.js
@@ -2290,17 +2290,18 @@ merge(Compressor.prototype, {
AST_Call.DEFMETHOD("has_pure_annotation", function(compressor) {
if (!compressor.option("side_effects")) return false;
if (this.pure !== undefined) return this.pure;
- var pure = false;
- var comments, pure_comment;
- if (this.start
- && (comments = this.start.comments_before)
- && comments.length
- && (pure_comment = find_if(function (comment) {
- return /[@#]__PURE__/.test(comment.value);
- }, comments))) {
- pure = pure_comment;
- }
- return this.pure = pure;
+ return this.pure = pure_comment(this) || pure_comment(this.expression) || false;
+
+ function pure_comment(node) {
+ var comments;
+ if (node.start
+ && (comments = node.start.comments_before)
+ && comments.length) {
+ return find_if(function (comment) {
+ return /[@#]__PURE__/.test(comment.value);
+ }, comments);
+ }
+ }
});
var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError"); |
That patch would also make |
|
It wouldn't for the same reason the pure annotation did not work in the top post. With patch: $ echo '(/*@__PURE__*/b)();' | bin/uglifyjs -c toplevel
b(); |
Without the parens they will generally both share the same comment string instances. The patch above is inefficient because in most non-pure cases |
@alexlamsl I'm not crazy about the patch above even if the noted inefficiency was addressed. If you like you can see if you can come up with an alternative parser-side fix, although it will be different for |
Although the following is not considered a pure annotation with the patch above: $ echo 'a(); ( /*@__PURE__*/b )(); c();' | bin/uglifyjs -c
a(),b(),c(); this unfortunately is: $ echo 'a(); ( 0, /*@__PURE__*/b )(); c();' | bin/uglifyjs -c
a(),c(); We need a better solution. |
The |
I guess it's |
I'm no longer in favor of the compressor fix. It should be fixed in the parser. |
The annotation works in sequences. In the parser a parenthesized expression is similar to a degenerate sequence of 1 element. The point at which the decision is made to return a single element the In In |
I'm not sure why the parser is considered wrong in the case of
which seems entirely reasonable to me. |
Bug report or feature request?
bug
ES5 or ES6+ input?
any
Uglify version (
uglifyjs -V
)3.2.2
JavaScript input
original report: babel/babel#7044 (comment)
The text was updated successfully, but these errors were encountered: