aboutsummaryrefslogtreecommitdiff
path: root/test/compress/side_effects.js
blob: fd6cabc6d9dd63e223c1b0119b9e44e5f6b25732 (about) (plain) '>192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
1
2
3
4
5
6
7
8
9
10
11
2020-05-11enhance `if_return` (#3875)Alex Lam S.L
accessor: {
    options = {
        side_effects: true,
    }
    input: {
        ({
            get a() {},
            set a(v){
                this.b = 2;
            },
            b: 1
        });
    }
    expect: {}
}

issue_2233_1: {
    options = {
        pure_getters: "strict",
        side_effects: true,
        unsafe: true,
    }
    input: {
        Array.isArray;
        Boolean;
        console.log;
        Date;
        decodeURI;
        decodeURIComponent;
        encodeURI;
        encodeURIComponent;
        Error.name;
        escape;
        eval;
        EvalError;
        Function.length;
        isFinite;
        isNaN;
        JSON;
        Math.random;
        Number.isNaN;
        parseFloat;
        parseInt;
        RegExp;
        Object.defineProperty;
        String.fromCharCode;
        RangeError;
        ReferenceError;
        SyntaxError;
        TypeError;
        unescape;
        URIError;
    }
    expect: {}
    expect_stdout: true
}

global_timeout_and_interval_symbols: {
    options = {
        pure_getters: "strict",
        side_effects: true,
        unsafe: true,
    }
    input: {
        // These global symbols do not exist in the test sandbox
        // and must be tested separately.
        clearInterval;
        clearTimeout;
        setInterval;
        setTimeout;
    }
    expect: {}
}

issue_2233_2: {
    options = {
        pure_getters: "strict",
        reduce_funcs: true,
        reduce_vars: true,
        side_effects: true,
        unsafe: true,
        unused: true,
    }
    input: {
        var RegExp;
        Array.isArray;
        RegExp;
        UndeclaredGlobal;
        function foo() {
            var Number;
            AnotherUndeclaredGlobal;
            Math.sin;
            Number.isNaN;
        }
    }
    expect: {
        var RegExp;
        UndeclaredGlobal;
        function foo() {
            AnotherUndeclaredGlobal;
            (void 0).isNaN;
        }
    }
}

issue_2233_3: {
    options = {
        pure_getters: "strict",
        reduce_funcs: true,
        reduce_vars: true,
        side_effects: true,
        toplevel: true,
        unsafe: true,
        unused: true,
    }
    input: {
        var RegExp;
        Array.isArray;
        RegExp;
        UndeclaredGlobal;
        function foo() {
            var Number;
            AnotherUndeclaredGlobal;
            Math.sin;
            Number.isNaN;
        }
    }
    expect: {
        UndeclaredGlobal;
    }
}

global_fns: {
    options = {
        side_effects: true,
        unsafe: true,
    }
    input: {
        Boolean(1, 2);
        decodeURI(1, 2);
        decodeURIComponent(1, 2);
        Date(1, 2);
        encodeURI(1, 2);
        encodeURIComponent(1, 2);
        Error(1, 2);
        escape(1, 2);
        EvalError(1, 2);
        isFinite(1, 2);
        isNaN(1, 2);
        Number(1, 2);
        Object(1, 2);
        parseFloat(1, 2);
        parseInt(1, 2);
        RangeError(1, 2);
        ReferenceError(1, 2);
        String(1, 2);
        SyntaxError(1, 2);
        TypeError(1, 2);
        unescape(1, 2);
        URIError(1, 2);
        try {
            Function(1, 2);
        } catch (e) {
            console.log(e.name);
        }
        try {
            RegExp(1, 2);
        } catch (e) {
            console.log(e.name);
        }
        try {
            Array(NaN);
        } catch (e) {
            console.log(e.name);
        }
    }
    expect: {
        try {
            Function(1, 2);
        } catch (e) {
            console.log(e.name);
        }
        try {
            RegExp(1, 2);
        } catch (e) {
            console.log(e.name);
        }
        try {
            Array(NaN);
        } catch (e) {
            console.log(e.name);
        }
    }
    expect_stdout: [
        "SyntaxError",
        "SyntaxError",
        "RangeError",
    ]
}

unsafe_builtin_1: {
    options = {
        side_effects: true,
        unsafe: true,
    }
    input: {
        (!w).constructor(x);
        Math.abs(y);
        [ 1, 2, z ].valueOf();
    }
    expect: {
        w, x;
        y;
        z;
    }
}

unsafe_builtin_2: {
    options = {
        side_effects: true,
        unsafe: true,
    }
    input: {
        var o = {};
        constructor.call(o, 42);
        __defineGetter__.call(o, "foo", function() {
            return o.p;
        });
        __defineSetter__.call(o, void 0, function(a) {
            o.p = a;
        });
        console.log(typeof o, o.undefined = "PASS", o.foo);
    }
    expect: {
        var o = {};
        constructor.call(o, 42);
        __defineGetter__.call(o, "foo", function() {
            return o.p;
        });
        __defineSetter__.call(o, void 0, function(a) {
            o.p = a;
        });
        console.log(typeof o, o.undefined = "PASS", o.foo);
    }
    expect_stdout: "object PASS PASS"
}

unsafe_builtin_3: {
    options = {
        conditionals: true,
        side_effects: true,
        toplevel: true,
        unsafe: true,
    }
    input: {
        var o = {};
        if (42 < Math.random())
            o.p = "FAIL";
        else
            o.p = "PASS";
        for (var k in o)
            console.log(k, o[k]);
    }
    expect: {
        var o = {};
        o.p = 42 < Math.random() ? "FAIL" : "PASS";
        for (var k in o)
            console.log(k, o[k]);
    }
    expect_stdout: "p PASS"
}

unsafe_string_replace: {
    options = {
        side_effects: true,
        unsafe: true,
    }
    input: {
        "foo".replace("f", function() {
            console.log("PASS");
        });
    }
    expect: {
        "foo".replace("f", function() {
            console.log("PASS");
        });
    }
    expect_stdout: "PASS"
}

drop_value: {
    options = {
        side_effects: true,
    }
    input: {
        (1, [2, foo()], 3, {a:1, b:bar()});
    }
    expect: {
        foo(), bar();
    }
}

operator_in: {
    options = {
        side_effects: true,
    }
    input: {
        try {
            "foo" in true;
            console.log("FAIL");
        } catch (e) {
            console.log("PASS");
        }
    }
    expect: {
        try {
            0 in true;
            console.log("FAIL");
        } catch (e) {
            console.log("PASS");
        }
    }
    expect_stdout: "PASS"
}

issue_3983_1: {
    options = {
        collapse_vars: true,
        conditionals: true,
        evaluate: true,
        inline: true,
        reduce_vars: true,
        side_effects: true,
        toplevel: true,
        unused: true,
    }
    input: {
        var a = "PASS";
        function f() {
            g && g();
        }
        f();
        function g() {
            0 ? a : 0;
        }
        var b = a;
        console.log(a);
    }
    expect: {
        var a = "PASS";
        console.log(a);
    }
    expect_stdout: "PASS"
}

issue_3983_2: {
    options = {
        collapse_vars: true,
        conditionals: true,
        evaluate: true,
        inline: true,
        passes: 2,
        reduce_vars: true,
        side_effects: true,
        toplevel: true,
        unused: true,
    }
    input: {
        var a = "PASS";
        function f() {
            g && g();
        }
        f();
        function g() {
            0 ? a : 0;
        }
        var b = a;
        console.log(a);
    }
    expect: {
        console.log("PASS");
    }
    expect_stdout: "PASS"
}

issue_4008: {
    options = {
        collapse_vars: true,
        evaluate: true,
        inline: true,
        pure_getters: "strict",
        reduce_vars: true,
        side_effects: true,
        toplevel: true,
    }
    input: {
        var a = "PASS";
        function f(b, b) {
            console.log(b);
        }
        f && f(a && a[a]);
        console.log(a);
    }
    expect: {
        var a = "PASS";
        function f(b, b) {
            console.log(b);
        }
        f(a[a]);
        console.log(a);
    }
    expect_stdout: [
        "undefined",
        "PASS",
    ]
}

trim_new: {
    options = {
        side_effects: true,
    }
    input: {
        new function(a) {
            console.log(a);
        }("PASS");
    }
    expect: {
        (function(a) {
            console.log(a);
        })("PASS");
    }
    expect_stdout: "PASS"
}

issue_4325: {
    options = {
        keep_fargs: false,
        passes: 2,
        pure_getters: "strict",
        reduce_vars: true,
        side_effects: true,
        unused: true,
    }
    input: {
        (function f() {
            (function(b, c) {
                try {
                    c.p = 0;
                } catch (e) {
                    console.log("PASS");
                    return b;
                }
                c;
            })(f++);
        })();
    }
    expect: {
        (function() {
            (function(c) {
                try {
                    c.p = 0;
                } catch (e) {
                    console.log("PASS");
                    return;
                }
            })(void 0);
        })();
    }
    expect_stdout: "PASS"
}

issue_4366_1: {
    options = {
        side_effects: true,
    }
    input: {
        ({
            p: 42,
            get p() {},
            q: console.log("PASS"),
        });
    }
    expect: {
        console.log("PASS");
    }
    expect_stdout: "PASS"
    node_version: ">=4"
}

issue_4366_2: {
    options = {
        side_effects: true,
    }
    input: {
        ({
            set p(v) {},
            q: console.log("PASS"),
            p: 42,
        });
    }
    expect: {
        console.log("PASS");
    }
    expect_stdout: "PASS"
    node_version: ">=4"
}

issue_4668: {
    options = {
        conditionals: true,
        keep_fargs: false,
        keep_fnames: true,
        reduce_vars: true,
        side_effects: true,
        toplevel: true,
        unused: true,
    }
    input: {
        function f(a) {
            var b, c;
            function g() {
                return a = 0 + a, !d || (a = 0);
            }
            c = g();
        }
        console.log(f());
        var d = 0;
    }
    expect: {
        console.log(function f() {
            (function g() {
                0;
            })();
        }());
    }
    expect_stdout: "undefined"
}

drop_side_effect_free_call: {
    options = {
        inline: true,
        reduce_vars: true,
        side_effects: true,
        toplevel: true,
    }
    input: {
        function f(a) {
            return "PA" + a;
        }
        f(42);
        console.log(f("SS"));
    }
    expect: {
        function f(a) {
            return "PA" + a;
        }
        console.log(f("SS"));
    }
    expect_stdout: "PASS"
}