fix(es/parser): Make parsing of TS InstantiationExpr more permissive (#4332)

This commit is contained in:
Pig Fang 2022-04-15 22:16:31 +08:00 committed by GitHub
parent 0b5c782a8e
commit ea466afdc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 137 additions and 10 deletions

View File

@ -101,7 +101,7 @@ macro_rules! tok {
crate::token::Token::BinOp(crate::token::BinOpToken::Div)
};
("/=") => {
crate::token::Token::AssignOp(DivAssign)
crate::token::Token::AssignOp(crate::token::AssignOpToken::DivAssign)
};
('%') => {
crate::token::Token::BinOp(Mod)

View File

@ -1903,4 +1903,21 @@ impl<'a, I: Tokens> Parser<I> {
}
}
}
fn is_start_of_left_hand_side_expr(&mut self) -> PResult<bool> {
Ok(is_one_of!(
self, "this", "super", "null", "true", "false", Num, BigInt, Str, '`', '(', '[', '{',
"function", "class", "new", Regex, IdentRef
) || (is!(self, "import")
&& (peeked_is!(self, '(') || peeked_is!(self, '<') || peeked_is!(self, '.'))))
}
pub(super) fn is_start_of_expr(&mut self) -> PResult<bool> {
Ok(self.is_start_of_left_hand_side_expr()?
|| is_one_of!(
self, '+', '-', '~', '!', "delete", "typeof", "void", "++", "--", '<', "await",
"yield"
)
|| (is!(self, '#') && peeked_is!(self, IdentName)))
}
}

View File

@ -53,6 +53,13 @@ macro_rules! is {
}
}};
($p:expr,Regex) => {{
match cur!($p, false) {
Ok(&Token::Regex { .. }) => true,
_ => false,
}
}};
($p:expr,BigInt) => {{
match cur!($p, false) {
Ok(&Token::BigInt { .. }) => true,

View File

@ -565,15 +565,10 @@ impl<I: Tokens> Parser<I> {
self.try_parse_ts(|p| {
let type_args = p.parse_ts_type_args()?;
if is_one_of!(
p, ',', '.', '?', ')', ']', ':', '&', '|', '^', '}', "??", "==", "===", "!=",
"!==", "&&", "||"
) || is_exact!(p, ';')
|| eof!(p)
{
Ok(Some(type_args))
} else {
if p.is_start_of_expr()? {
Ok(None)
} else {
Ok(Some(type_args))
}
})
}

View File

@ -9,3 +9,7 @@ true;
// Parsed as instantiation expression
const x3 = f<true>;
true;
// Parsed as instantiation expression
const x4 = f<true>
if (true) {}

View File

@ -2,7 +2,7 @@
"type": "Script",
"span": {
"start": 69,
"end": 221,
"end": 292,
"ctxt": 0
},
"body": [
@ -268,6 +268,110 @@
},
"value": true
}
},
{
"type": "VariableDeclaration",
"span": {
"start": 261,
"end": 279,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 267,
"end": 279,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 267,
"end": 269,
"ctxt": 0
},
"value": "x4",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "TsInstantiation",
"span": {
"start": 272,
"end": 279,
"ctxt": 0
},
"expression": {
"type": "Identifier",
"span": {
"start": 272,
"end": 273,
"ctxt": 0
},
"value": "f",
"optional": false
},
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 273,
"end": 279,
"ctxt": 0
},
"params": [
{
"type": "TsLiteralType",
"span": {
"start": 274,
"end": 278,
"ctxt": 0
},
"literal": {
"type": "BooleanLiteral",
"span": {
"start": 274,
"end": 278,
"ctxt": 0
},
"value": true
}
}
]
}
},
"definite": false
}
]
},
{
"type": "IfStatement",
"span": {
"start": 280,
"end": 292,
"ctxt": 0
},
"test": {
"type": "BooleanLiteral",
"span": {
"start": 284,
"end": 288,
"ctxt": 0
},
"value": true
},
"consequent": {
"type": "BlockStatement",
"span": {
"start": 290,
"end": 292,
"ctxt": 0
},
"stmts": []
},
"alternate": null
}
],
"interpreter": null