Skip to content
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

Writing self-executing functions with !function(){}() is sometimes unsafe #625

Closed
brycecr opened this issue Jan 27, 2015 · 4 comments · Fixed by #1522
Closed

Writing self-executing functions with !function(){}() is sometimes unsafe #625

brycecr opened this issue Jan 27, 2015 · 4 comments · Fixed by #1522

Comments

@brycecr
Copy link

brycecr commented Jan 27, 2015

Consider the example:

(function(){ doSideEffects(); return 5; })() // A

versus

!function(){ doSideEffects(); return 5; }() // B

A and B cause the same side effects, but the result of each expression is different, 5 in A and false in B

I am uglifying code files that look something like A, which are rewritten to B, and I am running into this issue because the minified files are passed to a processor that uses the result of eval() on the minified code.

I did not find a flag to uglify that controls this behavior, but I was curious whether:

  • Such a flag exists
  • There is a way around this problem without hacking around uglify
  • There should be a flag (or patch) for this
  • Other thoughts of people who understand Uglify better than I do
@mishoo
Copy link
Owner

mishoo commented Jan 27, 2015

There is a flag, try passing -c negate_iife=false.

@mishoo mishoo closed this as completed Jan 27, 2015
@michaelficarra
Copy link
Contributor

That is not the right answer. You are looking to preserve the program completion value. See #543 for the previous discussion about this.

@mpcref
Copy link

mpcref commented Jan 30, 2015

I propose the following fix:
never negate an IIFR that is also the program completion value.
New issue #628 created.

@rvanvelzen
Copy link
Collaborator

Track #640 for follow-up.

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.

5 participants