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: implement early errors for record and tuple #11652
Merged
nicolo-ribaudo
merged 5 commits into
babel:main
from
JLHwung:early-error-record-and-tuple
Jun 20, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef33035
fix: add early errors for record and tuple
JLHwung f534262
fix: update type assertion
JLHwung 3a85c92
refactor: simplify estree overwritten checkDuplicateProto
JLHwung 5d91a42
fix: disallow __proto__ in record
JLHwung 9705b1e
make multiple comma error thrown in exprList recoverable
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,19 +74,22 @@ export default class ExpressionParser extends LValParser { | |
+takeDecorators: (node: N.HasDecorators) => void; | ||
*/ | ||
|
||
// Check if property __proto__ has been used more than once. | ||
// For object literal, check if property __proto__ has been used more than once. | ||
// If the expression is a destructuring assignment, then __proto__ may appear | ||
// multiple times. Otherwise, __proto__ is a duplicated key. | ||
|
||
checkDuplicatedProto( | ||
// For record expression, check if property __proto__ exists | ||
|
||
checkProto( | ||
prop: N.ObjectMember | N.SpreadElement, | ||
isRecord: boolean, | ||
protoRef: { used: boolean }, | ||
refExpressionErrors: ?ExpressionErrors, | ||
): void { | ||
if ( | ||
prop.type === "SpreadElement" || | ||
prop.type === "ObjectMethod" || | ||
prop.computed || | ||
prop.kind || | ||
// $FlowIgnore | ||
prop.shorthand | ||
) { | ||
|
@@ -95,9 +98,13 @@ export default class ExpressionParser extends LValParser { | |
|
||
const key = prop.key; | ||
// It is either an Identifier or a String/NumericLiteral | ||
const name = key.type === "Identifier" ? key.name : String(key.value); | ||
const name = key.type === "Identifier" ? key.name : key.value; | ||
|
||
if (name === "__proto__") { | ||
if (isRecord) { | ||
this.raise(key.start, Errors.RecordNoProto); | ||
return; | ||
} | ||
if (protoRef.used) { | ||
if (refExpressionErrors) { | ||
// Store the first redefinition's position, otherwise ignore because | ||
|
@@ -1050,7 +1057,7 @@ export default class ExpressionParser extends LValParser { | |
this.next(); | ||
node.elements = this.parseExprList( | ||
close, | ||
true, | ||
false, | ||
refExpressionErrors, | ||
node, | ||
); | ||
|
@@ -1554,7 +1561,15 @@ export default class ExpressionParser extends LValParser { | |
const prop = this.parseObjectMember(isPattern, refExpressionErrors); | ||
if (!isPattern) { | ||
// $FlowIgnore RestElement will never be returned if !isPattern | ||
this.checkDuplicatedProto(prop, propHash, refExpressionErrors); | ||
this.checkProto(prop, isRecord, propHash, refExpressionErrors); | ||
} | ||
|
||
if ( | ||
isRecord && | ||
prop.type !== "ObjectProperty" && | ||
prop.type !== "SpreadElement" | ||
) { | ||
this.raise(prop.start, Errors.InvalidRecordProperty); | ||
} | ||
|
||
// $FlowIgnore | ||
|
@@ -2088,7 +2103,10 @@ export default class ExpressionParser extends LValParser { | |
allowPlaceholder: ?boolean, | ||
): ?N.Expression { | ||
let elt; | ||
if (allowEmpty && this.match(tt.comma)) { | ||
if (this.match(tt.comma)) { | ||
if (!allowEmpty) { | ||
this.raise(this.state.pos, Errors.UnexpectedToken, ","); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I resort to the most generic error message. I still feel awkward mentioning holes when throwing At least the error is now recoverable. |
||
} | ||
elt = null; | ||
} else if (this.match(tt.ellipsis)) { | ||
const spreadNodeStartPos = this.state.start; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
packages/babel-parser/test/fixtures/es2017/trailing-function-commas/5/options.json
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
packages/babel-parser/test/fixtures/es2017/trailing-function-commas/5/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, | ||
"errors": [ | ||
"SyntaxError: Unexpected token ',' (1:5)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, | ||
"sourceType": "script", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, | ||
"expression": { | ||
"type": "CallExpression", | ||
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}, | ||
"callee": { | ||
"type": "Identifier", | ||
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3},"identifierName":"log"}, | ||
"name": "log" | ||
}, | ||
"extra": { | ||
"trailingComma": 4 | ||
}, | ||
"arguments": [ | ||
null | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...s/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-record-method/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#{ | ||
a() {}, | ||
async b() {}, | ||
get c() {}, | ||
set d(_) {}, | ||
*e() {} | ||
} |
3 changes: 3 additions & 0 deletions
3
...bel-parser/test/fixtures/experimental/record-and-tuple/invalid-record-method/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]] | ||
} |
146 changes: 146 additions & 0 deletions
146
...abel-parser/test/fixtures/experimental/record-and-tuple/invalid-record-method/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ges/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-tuple-holes/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#[,] |
10 changes: 10 additions & 0 deletions
10
...babel-parser/test/fixtures/experimental/record-and-tuple/invalid-tuple-holes/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"recordAndTuple", | ||
{ | ||
"syntaxType": "hash" | ||
} | ||
] | ||
] | ||
} |
30 changes: 30 additions & 0 deletions
30
.../babel-parser/test/fixtures/experimental/record-and-tuple/invalid-tuple-holes/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, | ||
"errors": [ | ||
"SyntaxError: Unexpected token ',' (1:3)" | ||
], | ||
"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}}, | ||
"extra": { | ||
"trailingComma": 2 | ||
}, | ||
"elements": [ | ||
null | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...es/babel-parser/test/fixtures/experimental/record-and-tuple/tuple-trailing-comma/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#[1,] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️