diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-08-20 03:10:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-20 10:10:10 +0800 |
commit | 9634a9d1fd882bcd17047c285c9e3d656ba08688 (patch) | |
tree | 75e66324fff696302f544e8969cacf4592bbb364 /test | |
parent | befb99bd7197d281417d7eb15e0f7b8ed992174d (diff) | |
download | tracifyjs-9634a9d1fd882bcd17047c285c9e3d656ba08688.tar.gz tracifyjs-9634a9d1fd882bcd17047c285c9e3d656ba08688.zip |
fix corner cases in `optional_chains` (#5110)
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/optional-chains.js | 115 | ||||
-rw-r--r-- | test/input/invalid/optional-template.js | 1 | ||||
-rw-r--r-- | test/mocha/cli.js | 14 |
3 files changed, 129 insertions, 1 deletions
diff --git a/test/compress/optional-chains.js b/test/compress/optional-chains.js index 6e06f49c..8d16fdac 100644 --- a/test/compress/optional-chains.js +++ b/test/compress/optional-chains.js @@ -58,7 +58,7 @@ assign_parentheses_dot: { input: { (console?.log).name.p = console.log("PASS"); } - expect_exact: '(console?.log.name).p=console.log("PASS");' + expect_exact: '(console?.log).name.p=console.log("PASS");' expect_stdout: "PASS" node_version: ">=14" } @@ -72,6 +72,26 @@ assign_no_parentheses: { node_version: ">=14" } +call_parentheses: { + input: { + (function(o) { + console.log(o.f("FAIL"), (o.f)("FAIL"), (0, o.f)(42)); + console.log(o?.f("FAIL"), (o?.f)("FAIL"), (0, o?.f)(42)); + })({ + a: "PASS", + f(b) { + return this.a || b; + }, + }); + } + expect_exact: '(function(o){console.log(o.f("FAIL"),o.f("FAIL"),(0,o.f)(42));console.log(o?.f("FAIL"),(o?.f)("FAIL"),(0,o?.f)(42))})({a:"PASS",f(b){return this.a||b}});' + expect_stdout: [ + "PASS PASS 42", + "PASS PASS 42", + ] + node_version: ">=14" +} + unary_parentheses: { input: { var o = { p: 41 }; @@ -237,6 +257,99 @@ trim_2: { node_version: ">=14" } +trim_dot_call_1: { + options = { + evaluate: true, + optional_chains: true, + } + input: { + console.log(null?.f()); + } + expect: { + console.log(void 0); + } + expect_stdout: "undefined" + node_version: ">=14" +} + +trim_dot_call_2: { + options = { + evaluate: true, + optional_chains: true, + unsafe: true, + } + input: { + try { + (null?.p)(); + } catch (e) { + console.log("PASS"); + } + } + expect: { + try { + (void 0)(); + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" + node_version: ">=14" +} + +trim_dot_call_3: { + options = { + evaluate: true, + optional_chains: true, + unsafe: true, + } + input: { + try { + ({ p: null })?.p(); + } catch (e) { + console.log("PASS"); + } + } + expect: { + try { + null(); + } catch (e) { + console.log("PASS"); + } + } + expect_stdout: "PASS" + node_version: ">=14" +} + +trim_dot_sub: { + options = { + evaluate: true, + optional_chains: true, + } + input: { + console.log(null?.p[42]); + } + expect: { + console.log(void 0); + } + expect_stdout: "undefined" + node_version: ">=14" +} + +trim_sub_call_call: { + options = { + evaluate: true, + optional_chains: true, + } + input: { + console.log(null?.[42]()()); + } + expect: { + console.log(void 0); + } + expect_stdout: "undefined" + node_version: ">=14" +} + issue_4906: { options = { toplevel: true, diff --git a/test/input/invalid/optional-template.js b/test/input/invalid/optional-template.js new file mode 100644 index 00000000..608f4831 --- /dev/null +++ b/test/input/invalid/optional-template.js @@ -0,0 +1 @@ +console?.log``; diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 150af5a3..9fb10a16 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -721,6 +721,20 @@ describe("bin/uglifyjs", function() { done(); }); }); + it("Should throw syntax error (console?.log``)", function(done) { + var command = uglifyjscmd + " test/input/invalid/optional-template.js"; + exec(command, function(err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/optional-template.js:1,12", + "console?.log``;", + " ^", + "ERROR: Invalid template on optional chain", + ].join("\n")); + done(); + }); + }); it("Should handle literal string as source map input", function(done) { var command = [ uglifyjscmd, |