-
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
Support marking a call as pure #1448
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
58036cd
Support marking a call as pure
kzc d35b92c
remove [#@]__PURE__ hint from comment when pure call dropped
kzc 883ce81
have [@#]__PURE__ regex work with node 0.10.x
kzc b943c0d
have `#__PURE__` hint only work when compress `side_effects` option e…
kzc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
pure_function_calls: { | ||
options = { | ||
evaluate : true, | ||
conditionals : true, | ||
comparisons : true, | ||
side_effects : true, | ||
booleans : true, | ||
unused : true, | ||
if_return : true, | ||
join_vars : true, | ||
cascade : true, | ||
negate_iife : true, | ||
} | ||
input: { | ||
// pure top-level IIFE will be dropped | ||
// @__PURE__ - comment | ||
(function() { | ||
console.log("iife0"); | ||
})(); | ||
|
||
// pure top-level IIFE assigned to unreferenced var will not be dropped | ||
var iife1 = /*@__PURE__*/(function() { | ||
console.log("iife1"); | ||
function iife1() {} | ||
return iife1; | ||
})(); | ||
|
||
(function(){ | ||
// pure IIFE in function scope assigned to unreferenced var will be dropped | ||
var iife2 = /*#__PURE__*/(function() { | ||
console.log("iife2"); | ||
function iife2() {} | ||
return iife2; | ||
})(); | ||
})(); | ||
|
||
// comment #__PURE__ comment | ||
bar(), baz(), quux(); | ||
a.b(), /* @__PURE__ */ c.d.e(), f.g(); | ||
} | ||
expect: { | ||
var iife1 = function() { | ||
console.log("iife1"); | ||
function iife1() {} | ||
return iife1; | ||
}(); | ||
|
||
baz(), quux(); | ||
a.b(), f.g(); | ||
} | ||
expect_warnings: [ | ||
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:17,8]", | ||
"WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:17,8]", | ||
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:30,37]", | ||
"WARN: Dropping unused variable iife2 [test/compress/issue-1261.js:30,16]", | ||
"WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:28,8]", | ||
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:38,8]", | ||
"WARN: Dropping __PURE__ call [test/compress/issue-1261.js:39,31]" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@kzc any reason why the
#__PURE__
comment has to be the last one? why this isnt checking allnode.start.comments_before
?If im gonna write i.e. a babel plugin which annotates calls for me it might interfere with other annotations causing i.e. such situation
and thus preventing it from being dropped as unused.
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.
No particular reason other than efficiency. It probably should check all comments. PR is welcome.
Be aware that it's unlikely to be back-ported to
uglify-js@2.x
which is widely used but whose code base is frozen. So you'd probably want to list#__PURE__
in the last comment to aid legacy uglify users.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.
PR is comming then. :)
While at the moment of adding the comment I can ensure its added as the last comment, I cannot be sure that no other transformation change it. I wouldnt be overly concern about old version of the uglify. Life goes on and library's consumers should upgrade sooner or later :)
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 should mention that the following would still work with current uglify versions:
That's all the proposed babel plugin would have to do to retain backwards compatibility with uglify.