Skip to content

Commit

Permalink
fix: push new token context when braceHashL is seen (#11941)
Browse files Browse the repository at this point in the history
* fix: push new token context when braceHashL is seen

* Update packages/babel-parser/src/tokenizer/context.js
  • Loading branch information
JLHwung committed Aug 11, 2020
1 parent a5bc486 commit 3995160
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 0 deletions.
7 changes: 7 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,9 @@ tt.backQuote.updateContext = function () {
tt.star.updateContext = function () {
this.state.exprAllowed = false;
};

// we don't need to update context for tt.braceBarL because we do not pop context for tt.braceBarR
tt.braceHashL.updateContext = function () {
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
@@ -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": []
}
}

0 comments on commit 3995160

Please sign in to comment.