support nullish coalescing operator (#4678)

This commit is contained in:
Alex Lam S.L
2021-02-23 21:57:11 +00:00
committed by GitHub
parent d68d155f93
commit c885660347
16 changed files with 177 additions and 36 deletions

View File

@@ -34,7 +34,7 @@ destructured_funarg: {
node_version: ">=6"
}
await_parenthesis: {
await_parentheses: {
input: {
async function f() {
await (a => a);
@@ -43,7 +43,7 @@ await_parenthesis: {
expect_exact: "async function f(){await(a=>a)}"
}
for_parenthesis_init: {
for_parentheses_init: {
input: {
for (a => (a in a); console.log(42););
}
@@ -52,7 +52,7 @@ for_parenthesis_init: {
node_version: ">=4"
}
for_parenthesis_condition: {
for_parentheses_condition: {
input: {
for (console.log(42); a => (a in a);)
break;
@@ -62,7 +62,7 @@ for_parenthesis_condition: {
node_version: ">=4"
}
for_parenthesis_step: {
for_parentheses_step: {
input: {
for (; console.log(42); a => (a in a));
}
@@ -71,7 +71,7 @@ for_parenthesis_step: {
node_version: ">=4"
}
for_assign_parenthesis_init: {
for_assign_parentheses_init: {
input: {
for (f = a => (a in a); console.log(42););
}
@@ -80,7 +80,7 @@ for_assign_parenthesis_init: {
node_version: ">=4"
}
for_assign_parenthesis_condition: {
for_assign_parentheses_condition: {
input: {
for (console.log(42); f = a => (a in a);)
break;
@@ -90,7 +90,7 @@ for_assign_parenthesis_condition: {
node_version: ">=4"
}
for_assign_parenthesis_step: {
for_assign_parentheses_step: {
input: {
for (; console.log(42); f = a => (a in a));
}
@@ -99,7 +99,7 @@ for_assign_parenthesis_step: {
node_version: ">=4"
}
for_declaration_parenthesis_init: {
for_declaration_parentheses_init: {
input: {
for (var f = a => (a in a); console.log(42););
}
@@ -108,7 +108,7 @@ for_declaration_parenthesis_init: {
node_version: ">=4"
}
for_statement_parenthesis_init: {
for_statement_parentheses_init: {
input: {
for (a => {
a in a;

View File

@@ -158,7 +158,7 @@ yield: {
node_version: ">=12"
}
conditional_parenthesis: {
conditional_parentheses: {
options = {
conditionals: true,
}

View File

@@ -38,7 +38,7 @@ defaults: {
expect_exact: "export default 42;export default async;export default(x,y)=>x*x;export default class{}export default function*(a,b){}export default async function f({c:c},...[d]){}"
}
defaults_parenthesis_1: {
defaults_parentheses_1: {
input: {
export default function() {
console.log("FAIL");
@@ -47,7 +47,7 @@ defaults_parenthesis_1: {
expect_exact: 'export default function(){console.log("FAIL")}console.log("PASS");'
}
defaults_parenthesis_2: {
defaults_parentheses_2: {
input: {
export default (async function() {
console.log("PASS");

View File

@@ -2053,7 +2053,7 @@ issue_2898: {
expect_stdout: "2"
}
deduplicate_parenthesis: {
deduplicate_parentheses: {
input: {
({}).a = b;
(({}).a = b)();

View File

@@ -501,14 +501,14 @@ do_switch: {
}
}
in_parenthesis_1: {
in_parentheses_1: {
input: {
for (("foo" in {});0;);
}
expect_exact: 'for(("foo"in{});0;);'
}
in_parenthesis_2: {
in_parentheses_2: {
input: {
for ((function(){ "foo" in {}; });0;);
}

View File

@@ -85,7 +85,7 @@ new_with_unary_prefix: {
expect_exact: 'var bar=(+new Date).toString(32);';
}
dot_parenthesis_1: {
dot_parentheses_1: {
input: {
console.log(new (Math.random().constructor) instanceof Number);
}
@@ -93,7 +93,7 @@ dot_parenthesis_1: {
expect_stdout: "true"
}
dot_parenthesis_2: {
dot_parentheses_2: {
input: {
console.log(typeof new function(){Math.random()}.constructor);
}

111
test/compress/nullish.js Normal file
View File

@@ -0,0 +1,111 @@
parentheses: {
input: {
(console.log("foo") || console.log("bar") ?? console.log("baz")) && console.log("moo");
}
expect_exact:'((console.log("foo")||console.log("bar"))??console.log("baz"))&&console.log("moo");'
expect_stdout: [
"foo",
"bar",
"baz",
]
node_version: ">=14"
}
evaluate: {
options = {
evaluate: true,
side_effects: true,
}
input: {
void console.log("foo" ?? "bar") ?? console.log("baz");
}
expect: {
console.log("foo"),
console.log("baz");
}
expect_stdout: [
"foo",
"baz",
]
node_version: ">=14"
}
conditional_assignment_1: {
options = {
collapse_vars: true,
}
input: {
console.log(function(a, b) {
b ?? (a = "FAIL");
return a;
}("PASS", !console));
}
expect: {
console.log(function(a, b) {
b ?? (a = "FAIL");
return a;
}("PASS", !console));
}
expect_stdout: "PASS"
node_version: ">=14"
}
conditional_assignment_2: {
options = {
conditionals: true,
}
input: {
var a, b = false;
a = "PASS",
b ?? (a = "FAIL"),
console.log(a);
}
expect: {
var a, b = false;
a = "PASS",
b ?? (a = "FAIL"),
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=14"
}
conditional_assignment_3: {
options = {
conditionals: true,
join_vars: true,
}
input: {
var a, b = false;
a = "PASS",
b ?? (a = "FAIL"),
console.log(a);
}
expect: {
var a, b = false, a = "PASS";
b ?? (a = "FAIL"),
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=14"
}
conditional_assignment_4: {
options = {
side_effects: true,
}
input: {
console.log(function(a) {
!console ?? (a = "FAIL");
return a;
}("PASS"));
}
expect: {
console.log(function(a) {
!console ?? (a = "FAIL");
return a;
}("PASS"));
}
expect_stdout: "PASS"
node_version: ">=14"
}

View File

@@ -777,7 +777,7 @@ issue_1710: {
expect_stdout: true
}
unary_binary_parenthesis: {
unary_binary_parentheses: {
options = {
evaluate: true,
}

View File

@@ -53,7 +53,7 @@ tagged_chain: {
node_version: ">=4"
}
tag_parenthesis_arrow: {
tag_parentheses_arrow: {
input: {
console.log((s => s.raw[0])`\tPASS`.slice(2));
}
@@ -62,7 +62,7 @@ tag_parenthesis_arrow: {
node_version: ">=4"
}
tag_parenthesis_new: {
tag_parentheses_new: {
input: {
(new function() {
return console.log;

View File

@@ -259,7 +259,7 @@ describe("comments", function() {
assert.strictEqual(result.code, code);
});
it("Should handle comments around parenthesis correctly", function() {
it("Should handle comments around parentheses correctly", function() {
var code = [
"a();",
"/* foo */",

View File

@@ -84,7 +84,7 @@ describe("parentheses", function() {
}
});
it("Should compress leading parenthesis with reasonable performance", function() {
it("Should compress leading parentheses with reasonable performance", function() {
this.timeout(30000);
var code = [
"({}?0:1)&&x();",

View File

@@ -149,6 +149,7 @@ var SUPPORT = function(matrix) {
for_of: "for (var a of []);",
generator: "function* f(){}",
let: "let a;",
nullish: "0 ?? 0",
rest: "var [...a] = [];",
rest_object: "var {...a} = {};",
spread: "[...[]];",
@@ -231,6 +232,7 @@ var BINARY_OPS = [
",",
];
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
if (SUPPORT.nullish) BINARY_OPS.push("??");
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
if (SUPPORT.exponentiation) BINARY_OPS.push("**");