Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terser 5.13.0 does not elide Array.includes ([].includes) calls which it is reasonably able to #1438

Closed
michaele-blend opened this issue Sep 6, 2023 · 1 comment

Comments

@michaele-blend
Copy link

Feature request

Version 5.13.0

Complete CLI command or minify() options used

/** @returns {terser.MinifyOptions} */
const getTerserOpts = ({ debug }) => ({
  ecma: 2020,
  toplevel: false,

  parse: {},
  mangle: true,
  compress: {
    keep_fargs: false,

    pure_getters: true,
    drop_console: false,
    pure_funcs: [],

    unsafe: true,
    unsafe_arrows: true,
    unsafe_comps: true,
    unsafe_Function: true,
    unsafe_math: true,
    unsafe_symbols: true,
    unsafe_methods: true,
    unsafe_proto: true,

    passes: 10,

    global_defs: {
      DEBUG: debug,
    },
  },
  format: {
    comments: false,
    wrap_func_args: false,
    wrap_iife: false,
  },
});

const code = `
const DEBUG_assert = (expr, msg) => {
  if (!DEBUG) return;
  if (expr) return;
  throw new Error(msg);
};

const foo_test = (term) => {
  DEBUG_assert(Array.isArray(term), 'should be an array')
  DEBUG_assert([].includes(term), 'should be one of []')
}
`

const terser = require('terser');
terser.minify({ 'some-file.js': code }, getTerserOpts({ debug: true })).then(res => console.dir({ debug: true, ...res }))
terser.minify({ 'some-file.js': code }, getTerserOpts({ debug: false })).then(res => console.dir({ debug: false, ...res }))

terser input

see above

terser output or error

Expected result

when debug === true, I expect something to the effect of

{
  debug: true,
  code: 'const DEBUG_assert=(r,s)=>{if(!r)throw Error(s)},foo_test=r=>{DEBUG_assert(Array.isArray(r),"should be an array"),DEBUG_assert([].includes(r),"should be one of []")};'
}

when debug === false, I expect something to the effect of

{
  debug: false,
  code: 'const DEBUG_assert=()=>{},foo_test=s=>{};'
}

Actual

the debug === false case does not match my expectation. the actual output is

{
  debug: false,
  code: 'const DEBUG_assert=()=>{},foo_test=s=>{[].includes(s)};'
}

there is an additional [].includes() call which was kept.

@michaele-blend
Copy link
Author

I just upgraded to latest (5.19.4, at time of writing), and my issue has disappeared. I now am able to enjoy the expected behavior.
did quick bisect, looks like #1330 fixed my issue; the resolution on this is to upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant