Skip to content

Commit

Permalink
fix: add early errors for record and tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 30, 2020
1 parent e6d873e commit f521222
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -1050,7 +1050,7 @@ export default class ExpressionParser extends LValParser {
this.next();
node.elements = this.parseExprList(
close,
true,
false,
refExpressionErrors,
node,
);
Expand Down Expand Up @@ -1557,6 +1557,14 @@ export default class ExpressionParser extends LValParser {
this.checkDuplicatedProto(prop, propHash, refExpressionErrors);
}

if (
isRecord &&
prop.type !== "ObjectProperty" &&
prop.type !== "SpreadElement"
) {
this.raise(prop.start, Errors.InvalidRecordProperty);
}

// $FlowIgnore
if (prop.shorthand) {
this.addExtra(prop, "shorthand", true);
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/location.js
Expand Up @@ -85,6 +85,8 @@ export const Errors = Object.freeze({
InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern",
InvalidPrivateFieldResolution: "Private name #%0 is not defined",
InvalidPropertyBindingPattern: "Binding member expression",
InvalidRecordProperty:
"Only properties and spread elements are allowed in record definitions",
InvalidRestAssignmentPattern: "Invalid rest operator's argument",
LabelRedeclaration: "Label '%0' is already declared",
LetInLexicalBinding:
Expand Down
@@ -0,0 +1,7 @@
#{
a() {},
async b() {},
get c() {},
set d(_) {},
*e() {}
}
@@ -0,0 +1,3 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]]
}
@@ -0,0 +1,146 @@
{
"type": "File",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"errors": [
"SyntaxError: Only properties and spread elements are allowed in record definitions (2:2)",
"SyntaxError: Only properties and spread elements are allowed in record definitions (3:2)",
"SyntaxError: Only properties and spread elements are allowed in record definitions (4:2)",
"SyntaxError: Only properties and spread elements are allowed in record definitions (5:2)",
"SyntaxError: Only properties and spread elements are allowed in record definitions (6:2)"
],
"program": {
"type": "Program",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"expression": {
"type": "RecordExpression",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"properties": [
{
"type": "ObjectMethod",
"start":5,"end":11,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":8}},
"method": true,
"key": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":3},"identifierName":"a"},
"name": "a"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":9,"end":11,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":8}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":15,"end":27,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":14}},
"method": true,
"key": {
"type": "Identifier",
"start":21,"end":22,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":9},"identifierName":"b"},
"name": "b"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start":25,"end":27,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":14}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":31,"end":41,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":12}},
"method": false,
"key": {
"type": "Identifier",
"start":35,"end":36,"loc":{"start":{"line":4,"column":6},"end":{"line":4,"column":7},"identifierName":"c"},
"name": "c"
},
"computed": false,
"kind": "get",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":39,"end":41,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":12}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":45,"end":56,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":13}},
"method": false,
"key": {
"type": "Identifier",
"start":49,"end":50,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":7},"identifierName":"d"},
"name": "d"
},
"computed": false,
"kind": "set",
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":51,"end":52,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":9},"identifierName":"_"},
"name": "_"
}
],
"body": {
"type": "BlockStatement",
"start":54,"end":56,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":13}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":60,"end":67,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":9}},
"method": true,
"key": {
"type": "Identifier",
"start":61,"end":62,"loc":{"start":{"line":6,"column":3},"end":{"line":6,"column":4},"identifierName":"e"},
"name": "e"
},
"computed": false,
"kind": "method",
"id": null,
"generator": true,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":65,"end":67,"loc":{"start":{"line":6,"column":7},"end":{"line":6,"column":9}},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
#[,]
@@ -0,0 +1,11 @@
{
"plugins": [
[
"recordAndTuple",
{
"syntaxType": "hash"
}
]
],
"throws": "Unexpected token (1:2)"
}
@@ -0,0 +1 @@
#[1,]
@@ -0,0 +1,3 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]]
}
@@ -0,0 +1,35 @@
{
"type": "File",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"program": {
"type": "Program",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"expression": {
"type": "TupleExpression",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"extra": {
"trailingComma": 3
},
"elements": [
{
"type": "NumericLiteral",
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
]
}
}
],
"directives": []
}
}

0 comments on commit f521222

Please sign in to comment.