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

Add option to prevent reversing AST_Binary nodes #1359

Closed
iccir opened this issue Mar 19, 2023 · 4 comments
Closed

Add option to prevent reversing AST_Binary nodes #1359

iccir opened this issue Mar 19, 2023 · 4 comments

Comments

@iccir
Copy link
Contributor

iccir commented Mar 19, 2023

Bug report or Feature request?
Feature request

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

Complete CLI command or minify() options used

    let results = (await minify(inContents, {
        mangle: false,
        compress: {
            arguments     : false,
            arrows        : false,
            booleans      : false,
            booleans_as_integers : false,
            collapse_vars : false,
            comparisons   : false,
            computed_props: false,
            conditionals  : false,
            dead_code     : false,
            defaults      : false,
            directives    : false,
            drop_console  : false,
            drop_debugger : false,
            evaluate      : false,
            expression    : false,
            global_defs   : false,
            hoist_funs    : false,
            hoist_props   : false,
            hoist_vars    : false,
            ie8           : false,
            if_return     : false,
            inline        : false,
            join_vars     : false,
            keep_classnames: false,
            keep_fargs    : false,
            keep_fnames   : false,
            keep_infinity : false,
            loops         : false,
            module        : false,
            negate_iife   : false,
            passes        : 1,
            properties    : false,
            pure_getters  : false,
            pure_funcs    : null,
            reduce_funcs  : false,
            reduce_vars   : false,
            sequences     : false,
            side_effects  : false,
            switches      : false,
            top_retain    : null,
            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        : false,
            warnings      : false  // legacy
        },
        output: {
            beautify: true
        }
    })).code;

terser input
if (foo == 42) { bar(); }

terser output or error
if (42 == foo) bar()

Expected result
if (foo == 42) bar()

Notes
Right now, if you turn on compress, there is no way of opting-out of AST_Binary's reversible()/reverse() logic, even when comparisons is set to false. Could there be a new option to prevent this, or could the comparisons option prevent reversing.

Specifically, in our use case, we need to run with compress for dead_code and unused, but we want to prevent as many additional transformations as possible.

@iccir
Copy link
Contributor Author

iccir commented Mar 19, 2023

Upon further investigation, the fix involves adding an option check to Line 2134 of lib/compress/index.js.

I believe that the default value of this option needs to be true rather than !false_by_default, as too many test cases expect constants to be moved to the left-hand side.

@fabiosantoscode
Copy link
Collaborator

Hello!

As an unrelated hint, you can set defaults: false in the compressor and only enable what you need, avoiding that big options object.

Would you be open to issuing a PR for this?

@iccir
Copy link
Contributor Author

iccir commented Mar 25, 2023

Thanks for the hint about defaults: false, I didn't realize that was API.

I think the "move constants to left-hand side" option should be disabled with defaults: false, even though it means auditing all of the failing test cases. Let me investigate and I'll issue a PR!

iccir added a commit to iccir/terser that referenced this issue Mar 25, 2023
fabiosantoscode pushed a commit that referenced this issue Mar 26, 2023
* First pass at #1359

* Adding test case for lhs_constants
@iccir iccir mentioned this issue Apr 9, 2023
@iccir
Copy link
Contributor Author

iccir commented Apr 10, 2023

In version 5.16.9.

@iccir iccir closed this as completed Apr 10, 2023
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

2 participants