fix(es/parser): Fix top-level await with binary expr (#4426)

This commit is contained in:
Pig Fang 2022-04-26 07:10:59 +08:00 committed by GitHub
parent 7cbf30d5ac
commit 465cc2f929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 346 additions and 3 deletions

View File

@ -122,13 +122,22 @@ fn create_matrix(entry: &Path) -> Vec<Options> {
} }
#[testing::fixture("tests/exec/**/exec.js")] #[testing::fixture("tests/exec/**/exec.js")]
#[testing::fixture("tests/exec/**/exec.mjs")]
#[testing::fixture("tests/exec/**/exec.ts")] #[testing::fixture("tests/exec/**/exec.ts")]
fn run_fixture_test(entry: PathBuf) { fn run_fixture_test(entry: PathBuf) {
let _guard = testing::init(); let _guard = testing::init();
let matrix = create_matrix(&entry); let matrix = create_matrix(&entry);
let input_code = fs::read_to_string(&entry).expect("failed to read entry file"); let input_code = fs::read_to_string(&entry).expect("failed to read entry file");
let expected_stdout = stdout_of(&input_code).expect("failed to get stdout"); let expected_stdout = stdout_of(
&input_code,
if entry.extension().unwrap() == "mjs" {
NodeModuleType::Module
} else {
NodeModuleType::CommonJs
},
)
.expect("failed to get stdout");
eprintln!( eprintln!(
"----- {} -----\n{}\n-----", "----- {} -----\n{}\n-----",
@ -205,7 +214,14 @@ fn test_file_with_opts(
res.code res.code
); );
let actual_stdout = stdout_of(&res.code)?; let actual_stdout = stdout_of(
&res.code,
if entry.extension().unwrap() == "mjs" {
NodeModuleType::Module
} else {
NodeModuleType::CommonJs
},
)?;
assert_eq!( assert_eq!(
expected_stdout, expected_stdout,
@ -223,8 +239,18 @@ fn test_file_with_opts(
.with_context(|| format!("failed to compile with opts: {:#?}", opts)) .with_context(|| format!("failed to compile with opts: {:#?}", opts))
} }
fn stdout_of(code: &str) -> Result<String, Error> { enum NodeModuleType {
CommonJs,
Module,
}
fn stdout_of(code: &str, module_type: NodeModuleType) -> Result<String, Error> {
let module_type = match module_type {
NodeModuleType::CommonJs => "--input-type=commonjs",
NodeModuleType::Module => "--input-type=module",
};
let actual_output = Command::new("node") let actual_output = Command::new("node")
.arg(module_type)
.arg("-e") .arg("-e")
.arg(&code) .arg(&code)
.output() .output()

View File

@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es2017"
},
"module": {
"type": "es6"
},
"isModule": true
}

View File

@ -0,0 +1 @@
console.log(await Promise.resolve(false) || 2)

View File

@ -128,6 +128,9 @@ impl<'a, I: Tokens> Parser<I> {
} }
let expr = self.parse_await_expr()?; let expr = self.parse_await_expr()?;
let expr = self
.include_in_expr(true)
.parse_bin_op_recursively(expr, 0)?;
eat!(self, ';'); eat!(self, ';');
let span = span!(self, start); let span = span!(self, start);

View File

@ -0,0 +1,6 @@
await 1 + 2
await 1 * 2
await 1 || 2
await null ?? 3
await a instanceof await b
await a in await b

View File

@ -0,0 +1,295 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 98,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 0,
"end": 11,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 0,
"end": 11,
"ctxt": 0
},
"operator": "+",
"left": {
"type": "AwaitExpression",
"span": {
"start": 0,
"end": 7,
"ctxt": 0
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 6,
"end": 7,
"ctxt": 0
},
"value": 1.0,
"raw": "1"
}
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 10,
"end": 11,
"ctxt": 0
},
"value": 2.0,
"raw": "2"
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 12,
"end": 23,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 12,
"end": 23,
"ctxt": 0
},
"operator": "*",
"left": {
"type": "AwaitExpression",
"span": {
"start": 12,
"end": 19,
"ctxt": 0
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 18,
"end": 19,
"ctxt": 0
},
"value": 1.0,
"raw": "1"
}
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 22,
"end": 23,
"ctxt": 0
},
"value": 2.0,
"raw": "2"
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 24,
"end": 36,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 24,
"end": 36,
"ctxt": 0
},
"operator": "||",
"left": {
"type": "AwaitExpression",
"span": {
"start": 24,
"end": 31,
"ctxt": 0
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 30,
"end": 31,
"ctxt": 0
},
"value": 1.0,
"raw": "1"
}
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 35,
"end": 36,
"ctxt": 0
},
"value": 2.0,
"raw": "2"
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 37,
"end": 52,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 37,
"end": 52,
"ctxt": 0
},
"operator": "??",
"left": {
"type": "AwaitExpression",
"span": {
"start": 37,
"end": 47,
"ctxt": 0
},
"argument": {
"type": "NullLiteral",
"span": {
"start": 43,
"end": 47,
"ctxt": 0
}
}
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 51,
"end": 52,
"ctxt": 0
},
"value": 3.0,
"raw": "3"
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 53,
"end": 79,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 53,
"end": 79,
"ctxt": 0
},
"operator": "instanceof",
"left": {
"type": "AwaitExpression",
"span": {
"start": 53,
"end": 60,
"ctxt": 0
},
"argument": {
"type": "Identifier",
"span": {
"start": 59,
"end": 60,
"ctxt": 0
},
"value": "a",
"optional": false
}
},
"right": {
"type": "AwaitExpression",
"span": {
"start": 72,
"end": 79,
"ctxt": 0
},
"argument": {
"type": "Identifier",
"span": {
"start": 78,
"end": 79,
"ctxt": 0
},
"value": "b",
"optional": false
}
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 80,
"end": 98,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 80,
"end": 98,
"ctxt": 0
},
"operator": "in",
"left": {
"type": "AwaitExpression",
"span": {
"start": 80,
"end": 87,
"ctxt": 0
},
"argument": {
"type": "Identifier",
"span": {
"start": 86,
"end": 87,
"ctxt": 0
},
"value": "a",
"optional": false
}
},
"right": {
"type": "AwaitExpression",
"span": {
"start": 91,
"end": 98,
"ctxt": 0
},
"argument": {
"type": "Identifier",
"span": {
"start": 97,
"end": 98,
"ctxt": 0
},
"value": "b",
"optional": false
}
}
}
}
],
"interpreter": null
}