Skip to content

Compress emits let outside of block in v1.11.12 - v1.11.13 #10328

@traviscross

Description

@traviscross

Describe the bug

Starting in v1.11.12, SWC compress produces invalid syntax by emitting a let outside of a block in certain situations. See the example below.

This may have been introduced in:

This bug is fixed in v1.11.15, but I don't see an issue for this bug or an indication it was fixed intentionally, so I'm filing this to document the earlier problem and inform the maintainers in case any other follow-up actions are appropriate.

Input code

function f() {
  const h = i({
    onCancel: () => h(),
  });
}

function g(x, v) {
    if (x === "a") {
        f(v);
    }
}

g("a");
g("b");

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.11.12&code=H4sIAAAAAAAAAzVMSQqAMAy89xWDpxR8gSVefIkWowVJDy4I0r%2FbIOYymVVOjUfKCiGPxwEx635gBSORcSDrMGqctw41wj1W8m01ig%2BuOCf%2FwEJ3i%2BsbAZKAbjAzmrH5RTuhqxbtK1ZfyPxgOBm%2BBS1rDJAAAAA%3D&config=H4sIAAAAAAAAA32US47bMAyG93OKwOsu2gLtogformcQFIlylMqiIVKZGIPcvbTsPDqhs7P58SclkuLH227XHcl1v3Yf8ik%2Foy0E5fYvFpoy27NYOnCDJVfiyN2XKz3SjIJNBM10WUjHtvTATUXfv377sSq6hEhwVay2IeYYpsecDoexANGDTawSsg6Qmf7Xr6zg%2Bwy41Ef7HjGBzS%2BIsWRiZuihaIEdpmRHAnOyRYkyn9SWSKilmGFl8GYsOKo8%2B8gRs%2BR8ph6sNw49KCgWcBxPoMkkl8gyyfWU%2BzTsYV%2F7vvX5kxpONlXLSk44t5bIaZWoB4zEJtSslXCBGzVY4Frcz8oYTAGuJT%2FrjhjzRk%2F%2BAkgFkiXKdgAtbvMIMk9b6vBSGXOQkeVJ4TLf2i0z9FJUE2NQKjtXBgpHrZsFfHUwV9Zpx1nxRvkoejAQgsyKEpreI7uDlpSnETAoQPprgzZVCzC3V7jB5wfxAv%2BWW7I%2BYKvHYPmwTWka9pheJBiAD%2BhfOEgrGLdxkS1xHrd5zR5kNMCrLpUaeF4C8gAYTWr78mk25HlIRNMn3N%2FXxOpwue3hweb%2B%2Ft6XVfy2OnQD%2BtrguuTn%2Fi6r%2BWd3d7pu4dvBu0h%2FrsqW9PIPvadASjAGAAA%3D

SWC Info output

No response

Expected behavior

The behavior in v1.11.11 or v1.11.15 is correct for this example.

Actual behavior

No response

Version

1.11.12

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions