Skip to content

Commit

Permalink
fix(parser): correctly parse record and tuple tokens (#13418)
Browse files Browse the repository at this point in the history
* fix(parser): correctly parse token location for `#{` and `#[`

* fix: `bar` tokens

* fix: don't parse record and tuple pipe closing tokens as operators
  • Loading branch information
fedeci committed Jun 9, 2021
1 parent 56db172 commit a64d08c
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -486,14 +486,14 @@ export default class Tokenizer extends ParserErrors {
);
}

this.state.pos += 2;
if (next === charCodes.leftCurlyBrace) {
// #{
this.finishToken(tt.braceHashL);
} else {
// #[
this.finishToken(tt.bracketHashL);
}
this.state.pos += 2;
} else if (isIdentifierStart(next)) {
++this.state.pos;
this.finishToken(tt.privateName, this.readWord1(next));
Expand Down Expand Up @@ -615,8 +615,8 @@ export default class Tokenizer extends ParserErrors {
Errors.RecordExpressionBarIncorrectEndSyntaxType,
);
}

this.finishOp(tt.braceBarR, 2);
this.state.pos += 2;
this.finishToken(tt.braceBarR);
return;
}

Expand All @@ -631,8 +631,8 @@ export default class Tokenizer extends ParserErrors {
Errors.TupleExpressionBarIncorrectEndSyntaxType,
);
}

this.finishOp(tt.bracketBarR, 2);
this.state.pos += 2;
this.finishToken(tt.bracketBarR);
return;
}
}
Expand Down Expand Up @@ -812,8 +812,8 @@ export default class Tokenizer extends ParserErrors {
}

// [|
this.finishToken(tt.bracketBarL);
this.state.pos += 2;
this.finishToken(tt.bracketBarL);
} else {
++this.state.pos;
this.finishToken(tt.bracketL);
Expand All @@ -836,8 +836,8 @@ export default class Tokenizer extends ParserErrors {
}

// {|
this.finishToken(tt.braceBarL);
this.state.pos += 2;
this.finishToken(tt.braceBarL);
} else {
++this.state.pos;
this.finishToken(tt.braceL);
Expand Down
@@ -0,0 +1 @@
{||}
@@ -0,0 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "bar" }]],
"tokens": true
}
@@ -0,0 +1,69 @@
{
"type": "File",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"program": {
"type": "Program",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"expression": {
"type": "RecordExpression",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"properties": []
}
}
],
"directives": []
},
"tokens": [
{
"type": {
"label": "{|",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":0,"end":2,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":2}}
},
{
"type": {
"label": "|}",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":2,"end":4,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":4}}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":4,"end":4,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":4}}
}
]
}
@@ -0,0 +1 @@
#{}
@@ -0,0 +1,4 @@
{
"plugins": [[ "recordAndTuple", { "syntaxType": "hash" }]],
"tokens": true
}
@@ -0,0 +1,67 @@
{
"type": "File",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"program": {
"type": "Program",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"expression": {
"type": "RecordExpression",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"properties": []
}
}
],
"directives": []
},
"tokens": [
{
"type": {
"label": "#{",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start":0,"end":2,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":2}}
},
{
"type": {
"label": "}",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3}}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":3,"end":3,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":3}}
}
]
}
@@ -0,0 +1 @@
[||]
@@ -0,0 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "bar" }]],
"tokens": true
}
@@ -0,0 +1,69 @@
{
"type": "File",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"program": {
"type": "Program",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"expression": {
"type": "TupleExpression",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}},
"elements": []
}
}
],
"directives": []
},
"tokens": [
{
"type": {
"label": "[|",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":0,"end":2,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":2}}
},
{
"type": {
"label": "|]",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":2,"end":4,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":4}}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":4,"end":4,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":4}}
}
]
}
@@ -0,0 +1 @@
#[]
@@ -0,0 +1,4 @@
{
"plugins": [[ "recordAndTuple", { "syntaxType": "hash" }]],
"tokens": true
}
@@ -0,0 +1,69 @@
{
"type": "File",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"program": {
"type": "Program",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"expression": {
"type": "TupleExpression",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"elements": []
}
}
],
"directives": []
},
"tokens": [
{
"type": {
"label": "#[",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":0,"end":2,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":2}}
},
{
"type": {
"label": "]",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3}}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start":3,"end":3,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":3}}
}
]
}

0 comments on commit a64d08c

Please sign in to comment.