fix(es/parser): Allow instantiation expression followed by a line break or a binary operator (#5000)

This commit is contained in:
Pig Fang 2022-06-20 01:52:05 +08:00 committed by GitHub
parent fb7b314721
commit a62b2b3244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 2897 additions and 87 deletions

View File

@ -556,10 +556,21 @@ impl<I: Tokens> Parser<I> {
self.try_parse_ts(|p| {
let type_args = p.parse_ts_type_args()?;
if matches!(cur!(p, false), Ok(Token::BinOp(..))) || p.is_start_of_expr()? {
if is_one_of!(
p, '<', // invalid syntax
'>', '+', '-', // becomes relational expression
/* these should be type arguments in function call or template,
* not instantiation expression */
'(', '`'
) {
Ok(None)
} else {
} else if p.input.had_line_break_before_cur()
|| matches!(cur!(p, false), Ok(Token::BinOp(..)))
|| !p.is_start_of_expr()?
{
Ok(Some(type_args))
} else {
Ok(None)
}
})
}

View File

@ -1,7 +1,6 @@
x Unexpected token `&`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp, `
| for template literal, (, or an identifier
x Expected '>', got ';'
,-[$DIR/tests/typescript-errors/instantiation-expr/case3/input.ts:1:1]
1 | f<x> & g<y>;
: ^
1 | f<x> < g<y>;
: ^
`----

View File

@ -2,14 +2,58 @@
const x1 = f<true>
(true);
// Parsed as relational expression
// Parsed as relational expressions
const r1 = f < true > true;
const r2 = f < true > +1;
const r3 = f < true > -1;
// All of the following are parsed as instantiation expressions
const x2 = f<true>
true;
// Parsed as instantiation expression
const x3 = f<true>;
true;
// Parsed as instantiation expression
const x4 = f<true>
if (true) {}
const x5 = f<true>
let yy = 0;
const x6 = f<true>
interface I {}
let x10 = f<true>
this.bar()
let x11 = f<true>
function bar() {}
let x12 = f<true>
class C {}
let x13 = f<true>
bar()
let x14 = f<true>
void bar()
class C1 {
static specialFoo = f<string>
static bar = 123
}
class C2 {
public specialFoo = f<string>
public bar = 123
}
class C3 {
private specialFoo = f<string>
private bar = 123
}
class C4 {
protected specialFoo = f<string>
protected bar = 123
}

View File

@ -1,3 +1,4 @@
0 < 2 >> 1;
a<b>>c<d>;
a<b> + c;
a<b> - c;

View File

@ -2,7 +2,7 @@
"type": "Script",
"span": {
"start": 1,
"end": 33,
"end": 43,
"ctxt": 0
},
"body": [
@ -219,6 +219,71 @@
}
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 34,
"end": 43,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 34,
"end": 42,
"ctxt": 0
},
"operator": ">",
"left": {
"type": "BinaryExpression",
"span": {
"start": 34,
"end": 37,
"ctxt": 0
},
"operator": "<",
"left": {
"type": "Identifier",
"span": {
"start": 34,
"end": 35,
"ctxt": 0
},
"value": "a",
"optional": false
},
"right": {
"type": "Identifier",
"span": {
"start": 36,
"end": 37,
"ctxt": 0
},
"value": "b",
"optional": false
}
},
"right": {
"type": "UnaryExpression",
"span": {
"start": 39,
"end": 42,
"ctxt": 0
},
"operator": "-",
"argument": {
"type": "Identifier",
"span": {
"start": 41,
"end": 42,
"ctxt": 0
},
"value": "c",
"optional": false
}
}
}
}
],
"interpreter": null

View File

@ -1,4 +1,14 @@
f<x>, g<y>;
[f<x>];
f<x> ? g<y> : h<z>;
f<x> ^ g<y>;
f<x> & g<y>;
f<x> | g<y>;
f<x> && g<y>;
f<x> || g<y>;
{ f<x> }
f<x> ?? g<y>;
f<x> == g<y>;
f<x> === g<y>;
f<x> != g<y>;
f<x> !== g<y>;