swc icon indicating copy to clipboard operation
swc copied to clipboard

Minifier should respect TDZ

Open kdy1 opened this issue 3 years ago • 2 comments

Describe the bug

Minifier ignores TDZ and optimizes errornously.

I reduced minifier options

Input code

expect(() => {
  function f() {
    x;
  }
  f();
  let x;
}).toThrow(ReferenceError);

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript"
    },
    "target": "es2022",
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": false,
        "booleans": false,
        "booleans_as_integers": false,
        "collapse_vars": false,
        "comparisons": false,
        "computed_props": false,
        "conditionals": false,
        "dead_code": true,
        "directives": false,
        "drop_console": false,
        "drop_debugger": false,
        "evaluate": false,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": false,
        "hoist_vars": false,
        "if_return": false,
        "join_vars": false,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": false,
        "negate_iife": false,
        "properties": false,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": false,
        "switches": false,
        "typeofs": false,
        "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": false,
        "pristine_globals": false
      },
      "mangle": false
    },
    "loose": false
  },
  "minify": false
}

Playground link

https://play.swc.rs/?version=1.2.241&code=H4sIAAAAAAAAA0utKEhNLtHQ0FSwtVOo5lJQSCvNSy7JzM9TSAOKgQQUFCqsgVQtSE5DE8TMSS0BidVq6pXkh2QU5ZdrBKWmpRal5iWnuhYV5RcBFQEAvgy0K1cAAAA%3D&config=H4sIAAAAAAAAA22TS3LDIAyG78I6i06WOUCvwRAQjloeHiTSeDK9e0Wd2tjujv9Dln89eKoPsuryVKMpBKWdaEpsHuqiwEZDtuDI6vuk2JQBuGE6v53P6qQiJvRT%2B8TmOBYgamcJqxESi%2FAmEJyElPy1ymvOAUw6Am1IY2IYoKyXNodgRgJ9NxsaxTBSTltWGZweSx57nBwy5iRygQ6M0zY7UBcutQEsYBnv0MVIGolJJO620MG1DkPr1ovC3YRqeA2Dx28%2F5K8LumUk1r52jme0tTuzTbHodQGuZc31kTFtYz4BxGwwRMlE2HEvM6G%2FSmfyTxQmLwPlaaEh98YSDFKhRvRrmc06FMYuVQFXLbQ67QFuHBM60OC9tL2DX8j21qXjaYTsVy3tM52BWerdhr1oW4gDfBdj3M%2FlxaPh257RFK85HFJE4Ft2ByzN4LyHRbb5Me5pTQ6k2eC6i0pNzjNqW8easw7txS3Nls2Qj%2FQQ8nVdZnma0aRh2dHv38FRJ%2F%2Fe6ax%2FAEDL1%2FbyAwAA

Expected behavior

It should respect TDZ

Actual behavior

No response

Version

1.2.241

Additional context

Found by https://github.com/swc-project/swc/pull/5578

kdy1 avatar Aug 22 '22 15:08 kdy1

As I mentioned before, TDZ is hard, and maybe we should ignore it as terser does.

Austaras avatar Aug 25 '22 07:08 Austaras

I think you are right, we need a better testing system which allows disabling minifier tests for some inputs

kdy1 avatar Aug 25 '22 07:08 kdy1

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Oct 30 '22 12:10 swc-bot