Skip to content

Keep name of variable with anonymous function #191

Closed

Description

@GevatterGaul

Bug report or Feature request?

Feature Request?

Version (complete output of terser -V)

terser 3.11.0

Complete CLI command or minify() options used

npm test

terser input

keep_var_fnames: {
    mangle = {
        keep_fnames: true,
    }
    input: {
        function foo() {
            const bar = function() {
                return "foobar";
            }
        }
    }
    expect: {
        function foo() {
            const bar = function() {
                return "foobar";
            }
        }
    }
}

terser output or error

Running test [keep_var_fnames]
!!! failed
---INPUT---
{
    function foo() {
        const bar = function() {
            return "foobar";
        };
    }
}
---OUTPUT---
function foo(){const o=function(){return"foobar"}}
---EXPECTED---
function foo(){const bar=function(){return"foobar"}}


Expected result

It would be mighty convenient if terser would recognize bar as function and not mangle the name when keep_fnames is set to true.

Activity

  1. fabiosantoscode commented on Dec 19, 2018

    @fabiosantoscode
    Collaborator

    Nice issue.

    The workaround is not to mangle. Not a good workaround.

    I'll start working on it.

  2. fabiosantoscode commented on Dec 19, 2018

    @fabiosantoscode
    Collaborator

    ...But I have no idea how to do it. I'll work on it later.

  3. fabiosantoscode commented on Dec 19, 2018

    @fabiosantoscode
    Collaborator

    PRs welcome!

  4. jahed commented on Mar 14, 2019

    @jahed

    This also applies to arrow functions. Using the same example with keep_fnames,

    const bar = () => 'foobar'

    becomes

    const o = () => 'foobar'
  5. fabiosantoscode commented on Sep 29, 2019

    @fabiosantoscode
    Collaborator

    It should be shipped soon. I don't know what took me so long, I love using anonymous arrow functions and giving them meaningful names.

  6. thaoms commented on Oct 2, 2019

    @thaoms

    Thank you for fixing this! This made all our React Components that are written as functional hooks Anonymous.

  7. fabiosantoscode commented on Oct 2, 2019

    @fabiosantoscode
    Collaborator

    My use case was mostly for React too!

  8. gdh1995 commented on Oct 10, 2019

    @gdh1995
    Contributor

    @fabiosantoscode The commit ff16f02 is not correct enough - keep_fnames may be a regexp, but this commit treats a regexp as true when handing such functions.

  9. added a commit that references this issue on Nov 10, 2019
    a0a50ea
  10. added a commit that references this issue on Apr 23, 2026
    ff16f02
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions