Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: push new token context when braceHashL is seen #11941

Merged
merged 2 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/babel-parser/src/tokenizer/context.js
Expand Up @@ -31,6 +31,7 @@ export const types: {
} = {
braceStatement: new TokContext("{", false),
braceExpression: new TokContext("{", true),
recordExpression: new TokContext("#{", true),
templateQuasi: new TokContext("${", false),
parenStatement: new TokContext("(", false),
parenExpression: new TokContext("(", true),
Expand Down Expand Up @@ -140,3 +141,8 @@ tt.backQuote.updateContext = function () {
tt.star.updateContext = function () {
this.state.exprAllowed = false;
};

tt.braceHashL.updateContext = function () {
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
this.state.context.push(types.recordExpression);
this.state.exprAllowed = true; /* tt.braceHashL.beforeExpr */
};
@@ -0,0 +1 @@
<Card card={{|title: "foo"|}}></Card>
@@ -0,0 +1,3 @@
{
"plugins": ["jsx", "flow", ["recordAndTuple", { "syntaxType": "bar" }]]
}
@@ -0,0 +1,83 @@
{
"type": "File",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"program": {
"type": "Program",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"expression": {
"type": "JSXElement",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":5,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":5}},
"name": "Card"
},
"attributes": [
{
"type": "JSXAttribute",
"start":6,"end":29,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":29}},
"name": {
"type": "JSXIdentifier",
"start":6,"end":10,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
"name": "card"
},
"value": {
"type": "JSXExpressionContainer",
"start":11,"end":29,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":29}},
"expression": {
"type": "RecordExpression",
"start":12,"end":28,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":28}},
"properties": [
{
"type": "ObjectProperty",
"start":14,"end":26,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":26}},
"method": false,
"key": {
"type": "Identifier",
"start":14,"end":19,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":19},"identifierName":"title"},
"name": "title"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
]
}
}
}
],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":30,"end":37,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":37}},
"name": {
"type": "JSXIdentifier",
"start":32,"end":36,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":36}},
"name": "Card"
}
},
"children": []
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
<Card card={#{title: "foo"}}></Card>
@@ -0,0 +1,3 @@
{
"plugins": ["jsx", "flow", ["recordAndTuple", { "syntaxType": "hash" }]]
}
@@ -0,0 +1,83 @@
{
"type": "File",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"program": {
"type": "Program",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"expression": {
"type": "JSXElement",
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
"openingElement": {
"type": "JSXOpeningElement",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}},
"name": {
"type": "JSXIdentifier",
"start":1,"end":5,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":5}},
"name": "Card"
},
"attributes": [
{
"type": "JSXAttribute",
"start":6,"end":28,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":28}},
"name": {
"type": "JSXIdentifier",
"start":6,"end":10,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":10}},
"name": "card"
},
"value": {
"type": "JSXExpressionContainer",
"start":11,"end":28,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":28}},
"expression": {
"type": "RecordExpression",
"start":12,"end":27,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":27}},
"properties": [
{
"type": "ObjectProperty",
"start":14,"end":26,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":26}},
"method": false,
"key": {
"type": "Identifier",
"start":14,"end":19,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":19},"identifierName":"title"},
"name": "title"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
]
}
}
}
],
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start":29,"end":36,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":36}},
"name": {
"type": "JSXIdentifier",
"start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35}},
"name": "Card"
}
},
"children": []
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
(#{})/foo
Copy link
Contributor Author

@JLHwung JLHwung Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case fails on current main, because a token context is not pushed and both tt.braceR.updateContext and tt.parenR.updateContext incorrectly set this.state.exprAllowed = true (when context.length is 1) so /foo is parsed as unterminated regex.

@@ -0,0 +1,3 @@
{
"plugins": ["jsx", "flow", ["recordAndTuple", { "syntaxType": "hash" }]]
}
@@ -0,0 +1,36 @@
{
"type": "File",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"program": {
"type": "Program",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"left": {
"type": "RecordExpression",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"properties": [],
"extra": {
"parenthesized": true,
"parenStart": 0
}
},
"operator": "/",
"right": {
"type": "Identifier",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"foo"},
"name": "foo"
}
}
}
],
"directives": []
}
}