Skip to content

Don't replace (function(){})(); with !function(){}(); unless the return value isn't used #354

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

Closed
Daniel-Hug opened this issue Nov 20, 2013 · 3 comments · Fixed by #1522
Closed

Comments

@Daniel-Hug
Copy link

I use UglifyJS to compress and mangle my JS and stick it in a bookmarklet.

This is the bookmarklet's JS (the anon function is to make sure it evaluates to undefined):

(function() {
  /*bookmarklet stuff*/
})();

This is the JS after compression (notice the exclamation point):

!function(){/*bookmarklet stuff*/}();

The exclamation point negates the return value of the anon function so instead of evaluating to undefined, it will evaluate to true. Then when I prepend the javascript: "protocol" the bookmarklet instead navigates to a blank page displaying true.

IMO UglifyJS shouldn't negate anonymous functions if the return value is used or in this case if the function is the root function.

@michaelficarra
Copy link
Contributor

Related: estools/esmangle#78

@rvanvelzen
Copy link
Collaborator

I would say adding -c negate_iife=false would fix this.

@mishoo
Copy link
Owner

mishoo commented Nov 21, 2013

In general I think we prefer the current behaviour, and when you're building for a bookmarklet you can pass -c negate-iife=false as @rvanvelzen suggested.

@mishoo mishoo closed this as completed Nov 21, 2013
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 1, 2017
Things like Angular Expression and Bookmarklet do not specify `return`, but implicitedly assumes the evaluated value from the final statement to be the return value.

fixes mishoo#354
fixes mishoo#543
fixes mishoo#625
fixes mishoo#628
fixes mishoo#640
closes mishoo#1293
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 2, 2017
Bookmarklet for instance implicitedly assumes a "completion value" without using `return`.
The `expression` option now supports such use cases.
Optimisations on IIFEs also enhanced.

fixes mishoo#354
fixes mishoo#543
fixes mishoo#625
fixes mishoo#628
fixes mishoo#640
closes mishoo#1293
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Mar 3, 2017
Bookmarklet for instance implicitedly assumes a "completion value" without using `return`.
The `expression` option now supports such use cases.
Optimisations on IIFEs also enhanced.

fixes mishoo#354
fixes mishoo#543
fixes mishoo#625
fixes mishoo#628
fixes mishoo#640
closes mishoo#1293
alexlamsl added a commit that referenced this issue Mar 3, 2017
Bookmarklet for instance implicitedly assumes a "completion value" without using `return`.
The `expression` option now supports such use cases.
Optimisations on IIFEs also enhanced.

fixes #354
fixes #543
fixes #625
fixes #628
fixes #640
closes #1293
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants