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

refactor: add isLiteralPropertyName to parser utils #11547

Merged
merged 5 commits into from May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
12 changes: 3 additions & 9 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1553,11 +1553,8 @@ export default class ExpressionParser extends LValParser {
!prop.computed &&
prop.key.type === "Identifier" &&
prop.key.name === "async" &&
(this.match(tt.name) ||
this.match(tt.num) ||
this.match(tt.string) ||
(this.isLiteralPropertyName() ||
this.match(tt.bracketL) ||
this.state.type.keyword ||
this.match(tt.star)) &&
!this.hasPrecedingLineBreak()
);
Expand Down Expand Up @@ -1646,11 +1643,8 @@ export default class ExpressionParser extends LValParser {
!prop.computed &&
prop.key.type === "Identifier" &&
(prop.key.name === "get" || prop.key.name === "set") &&
(this.match(tt.string) || // get "string"() {}
this.match(tt.num) || // get 1() {}
this.match(tt.bracketL) || // get ["string"]() {}
this.match(tt.name) || // get foo() {}
!!this.state.type.keyword) // get debugger() {}
(this.isLiteralPropertyName() || // get foo() {}
this.match(tt.bracketL)) // get ["string"]() {}
);
}

Expand Down
19 changes: 19 additions & 0 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -259,6 +259,25 @@ export default class UtilParser extends Tokenizer {
this.raise(doubleProto, Errors.DuplicateProto);
}
}

/**
* Test if current token is a literal property name
* https://tc39.es/ecma262/#prod-LiteralPropertyName
* LiteralPropertyName:
existentialism marked this conversation as resolved.
Show resolved Hide resolved
* IdentifierName
* StringLiteral
* NumericLiteral
* BigIntLiteral
*/
isLiteralPropertyName(): boolean {
return (
this.match(tt.name) ||
!!this.state.type.keyword ||
this.match(tt.string) ||
this.match(tt.num) ||
this.match(tt.bigint)
existentialism marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

/**
Expand Down
@@ -1 +1 @@
({ 0n: 0 });
({ 0n: 0, 1n() {}, get 2n(){}, set 3n(_){}, async 4n() {}, *5n() {} });
@@ -1,18 +1,18 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"start":0,"end":71,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":71}},
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"start":0,"end":71,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":71}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"start":0,"end":71,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":71}},
"expression": {
"type": "ObjectExpression",
"start":1,"end":10,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":10}},
"start":1,"end":69,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":69}},
"properties": [
{
"type": "ObjectProperty",
Expand All @@ -38,6 +38,142 @@
},
"value": 0
}
},
{
"type": "ObjectMethod",
"start":10,"end":17,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":17}},
"method": true,
"key": {
"type": "BigIntLiteral",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12}},
"extra": {
"rawValue": "1",
"raw": "1n"
},
"value": "1"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":15,"end":17,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":17}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":19,"end":29,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":29}},
"method": false,
"key": {
"type": "BigIntLiteral",
"start":23,"end":25,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":25}},
"extra": {
"rawValue": "2",
"raw": "2n"
},
"value": "2"
},
"computed": false,
"kind": "get",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":27,"end":29,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":29}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":31,"end":42,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":42}},
"method": false,
"key": {
"type": "BigIntLiteral",
"start":35,"end":37,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":37}},
"extra": {
"rawValue": "3",
"raw": "3n"
},
"value": "3"
},
"computed": false,
"kind": "set",
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":38,"end":39,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":39},"identifierName":"_"},
"name": "_"
}
],
"body": {
"type": "BlockStatement",
"start":40,"end":42,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":42}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":44,"end":57,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":57}},
"method": true,
"key": {
"type": "BigIntLiteral",
"start":50,"end":52,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":52}},
"extra": {
"rawValue": "4",
"raw": "4n"
},
"value": "4"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start":55,"end":57,"loc":{"start":{"line":1,"column":55},"end":{"line":1,"column":57}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":59,"end":67,"loc":{"start":{"line":1,"column":59},"end":{"line":1,"column":67}},
"method": true,
"key": {
"type": "BigIntLiteral",
"start":60,"end":62,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":62}},
"extra": {
"rawValue": "5",
"raw": "5n"
},
"value": "5"
},
"computed": false,
"kind": "method",
"id": null,
"generator": true,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":65,"end":67,"loc":{"start":{"line":1,"column":65},"end":{"line":1,"column":67}},
"body": [],
"directives": []
}
}
],
"extra": {
Expand Down