Skip to content

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

Closed

Description

@brycecr

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

Activity

  1. mishoo commented on Jan 27, 2015

    @mishoo
    Owner

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

  2. michaelficarra commented on Jan 28, 2015

    @michaelficarra
    Contributor

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

  3. mpcref commented on Jan 30, 2015

    @mpcref

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

  4. rvanvelzen commented on Feb 11, 2015

    @rvanvelzen
    Collaborator

    Track #640 for follow-up.

  5. added 4 commits that reference this issue on Mar 1, 2017
    dfb2d43
    91f29e8
    f01c0c4
    07accd2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions