Skip to content

Function declaration in a block conflicts with global const declaration #1043

Closed

Description

@lal12

Version (complete output of terser -V or specific git commit)
terser 4.8.0

Complete CLI command or minify() options used
./node_modules/.bin/terser asd.js --mangle --compress

terser input

if(true){
	function asd(){}
}

const asd = 123;

terser output or error

Parse error at asd.js:8,6
const asd = 123;
      ^
ERROR: "asd" is redeclared
    at js_error (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/parse.js:369:11)
    at On.visit (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/scope.js:328:17)
    at On._visit (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:1509:24)
    at AST_SymbolConst._walk (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:121:24)
    at AST_VarDef.call (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:809:23)
    at On._visit (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:1513:21)
    at AST_VarDef._walk (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:808:24)
    at AST_Const.call (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:779:32)
    at On._visit (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:1513:21)
    at AST_Const._walk (/home/wutll/home/git/rui/main/server/node_modules/terser/lib/ast.js:776:24)

Expected result
The code works, because JS hoisting is not the same thing as a declaration, there is no redeclaration. Terser works fine when the const asd is above the if-block.

Maybe there is an option for this, but I think this should work out of the box by default...

Btw I am aware that this isn't nice code, but this is something dropping out of rollup.

Activity

  1. changed the title [-]Function Declaration in a block conflicts with global const. [/-] [+]Function declaration in a block conflicts with global const declaration[/+] on Aug 9, 2021
  2. jridgewell commented on Sep 19, 2021

    @jridgewell
    Collaborator

    Seems to be a parser issue? If I add a "use strict" directive it works correctly, but without it errors.

  3. fabiosantoscode commented on Sep 25, 2021

    @fabiosantoscode
    Collaborator

    The intersection of declaring a function decl in a block, and strict/nonstrict mode, is really weird.

    https://tc39.es/ecma262/multipage/additional-ecmascript-features-for-web-browsers.html#sec-block-level-function-declarations-web-legacy-compatibility-semantics

    This shouldn't be a parse error though. js_error is called in lib/scope. That error message is a bit misleading though so it should probably be tackled as well.

  4. lal12 commented on Sep 26, 2021

    @lal12
    Author

    I think in my original use case I also had the error with "use strict";, but I will try to find out if that really was the case.

  5. added a commit that references this issue on Feb 27, 2024
    1a99798
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions