Skip to content

Commit

Permalink
fix: disallow JSX tag after non-null assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jun 10, 2021
1 parent f04406f commit c376be7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2043,6 +2043,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
state: N.ParseSubscriptState,
): N.Expression {
if (!this.hasPrecedingLineBreak() && this.match(tt.bang)) {
// When ! is consumed as a postfix operator (non-null assertion),
// disallow JSX tag forming after. e.g. When parsing `p! < n.p!`
// `<n.p` can not be a start of JSX tag
this.state.exprAllowed = false;
this.next();

const nonNullExpression: N.TsNonNullExpression = this.startNodeAt(
Expand Down
@@ -0,0 +1 @@
x.v! < y.v!;
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "jsx"]
}
@@ -0,0 +1,60 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}},
"left": {
"type": "TSNonNullExpression",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"expression": {
"type": "MemberExpression",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"object": {
"type": "Identifier",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1},"identifierName":"x"},
"name": "x"
},
"computed": false,
"property": {
"type": "Identifier",
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3},"identifierName":"v"},
"name": "v"
}
}
},
"operator": "<",
"right": {
"type": "TSNonNullExpression",
"start":7,"end":11,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":11}},
"expression": {
"type": "MemberExpression",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"object": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"y"},
"name": "y"
},
"computed": false,
"property": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"v"},
"name": "v"
}
}
}
}
}
],
"directives": []
}
}

0 comments on commit c376be7

Please sign in to comment.