fix: allow bigInt in method name and TSLiteralType (#11547)
* refactor: add isLiteralPropertyName to parser utils * address review comments [skip-ci] * refactor: keyword is valid identifierName * fix: allow bigint in TSLiteralType * update typescript test whitelist
This commit is contained in:
parent
de8264c8a7
commit
2f31ecf85d
@ -1553,11 +1553,8 @@ export default class ExpressionParser extends LValParser {
|
|||||||
!prop.computed &&
|
!prop.computed &&
|
||||||
prop.key.type === "Identifier" &&
|
prop.key.type === "Identifier" &&
|
||||||
prop.key.name === "async" &&
|
prop.key.name === "async" &&
|
||||||
(this.match(tt.name) ||
|
(this.isLiteralPropertyName() ||
|
||||||
this.match(tt.num) ||
|
|
||||||
this.match(tt.string) ||
|
|
||||||
this.match(tt.bracketL) ||
|
this.match(tt.bracketL) ||
|
||||||
this.state.type.keyword ||
|
|
||||||
this.match(tt.star)) &&
|
this.match(tt.star)) &&
|
||||||
!this.hasPrecedingLineBreak()
|
!this.hasPrecedingLineBreak()
|
||||||
);
|
);
|
||||||
@ -1646,11 +1643,8 @@ export default class ExpressionParser extends LValParser {
|
|||||||
!prop.computed &&
|
!prop.computed &&
|
||||||
prop.key.type === "Identifier" &&
|
prop.key.type === "Identifier" &&
|
||||||
(prop.key.name === "get" || prop.key.name === "set") &&
|
(prop.key.name === "get" || prop.key.name === "set") &&
|
||||||
(this.match(tt.string) || // get "string"() {}
|
(this.isLiteralPropertyName() || // get foo() {}
|
||||||
this.match(tt.num) || // get 1() {}
|
this.match(tt.bracketL)) // get ["string"]() {}
|
||||||
this.match(tt.bracketL) || // get ["string"]() {}
|
|
||||||
this.match(tt.name) || // get foo() {}
|
|
||||||
!!this.state.type.keyword) // get debugger() {}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -259,6 +259,25 @@ export default class UtilParser extends Tokenizer {
|
|||||||
this.raise(doubleProto, Errors.DuplicateProto);
|
this.raise(doubleProto, Errors.DuplicateProto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if current token is a literal property name
|
||||||
|
* https://tc39.es/ecma262/#prod-LiteralPropertyName
|
||||||
|
* LiteralPropertyName:
|
||||||
|
* 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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -691,6 +691,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
node.literal = (() => {
|
node.literal = (() => {
|
||||||
switch (this.state.type) {
|
switch (this.state.type) {
|
||||||
case tt.num:
|
case tt.num:
|
||||||
|
case tt.bigint:
|
||||||
case tt.string:
|
case tt.string:
|
||||||
case tt._true:
|
case tt._true:
|
||||||
case tt._false:
|
case tt._false:
|
||||||
@ -747,13 +748,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
}
|
}
|
||||||
case tt.string:
|
case tt.string:
|
||||||
case tt.num:
|
case tt.num:
|
||||||
|
case tt.bigint:
|
||||||
case tt._true:
|
case tt._true:
|
||||||
case tt._false:
|
case tt._false:
|
||||||
return this.tsParseLiteralTypeNode();
|
return this.tsParseLiteralTypeNode();
|
||||||
case tt.plusMin:
|
case tt.plusMin:
|
||||||
if (this.state.value === "-") {
|
if (this.state.value === "-") {
|
||||||
const node: N.TsLiteralType = this.startNode();
|
const node: N.TsLiteralType = this.startNode();
|
||||||
if (this.lookahead().type !== tt.num) {
|
const nextToken = this.lookahead();
|
||||||
|
if (nextToken.type !== tt.num && nextToken.type !== tt.bigint) {
|
||||||
throw this.unexpected();
|
throw this.unexpected();
|
||||||
}
|
}
|
||||||
node.literal = this.parseMaybeUnary();
|
node.literal = this.parseMaybeUnary();
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
({ 0n: 0 });
|
({ 0n: 0, 1n() {}, get 2n(){}, set 3n(_){}, async 4n() {}, *5n() {} });
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
{
|
{
|
||||||
"type": "File",
|
"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": {
|
"program": {
|
||||||
"type": "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",
|
"sourceType": "script",
|
||||||
"interpreter": null,
|
"interpreter": null,
|
||||||
"body": [
|
"body": [
|
||||||
{
|
{
|
||||||
"type": "ExpressionStatement",
|
"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": {
|
"expression": {
|
||||||
"type": "ObjectExpression",
|
"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": [
|
"properties": [
|
||||||
{
|
{
|
||||||
"type": "ObjectProperty",
|
"type": "ObjectProperty",
|
||||||
@ -38,6 +38,142 @@
|
|||||||
},
|
},
|
||||||
"value": 0
|
"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": {
|
"extra": {
|
||||||
|
|||||||
1
packages/babel-parser/test/fixtures/typescript/types/literal-bigint-negative/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/literal-bigint-negative/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let x: -1n;
|
||||||
53
packages/babel-parser/test/fixtures/typescript/types/literal-bigint-negative/output.json
vendored
Normal file
53
packages/babel-parser/test/fixtures/typescript/types/literal-bigint-negative/output.json
vendored
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}},
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start":4,"end":10,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":10}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":4,"end":10,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":10},"identifierName":"x"},
|
||||||
|
"name": "x",
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSLiteralType",
|
||||||
|
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
|
||||||
|
"literal": {
|
||||||
|
"type": "UnaryExpression",
|
||||||
|
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
|
||||||
|
"operator": "-",
|
||||||
|
"prefix": true,
|
||||||
|
"argument": {
|
||||||
|
"type": "BigIntLiteral",
|
||||||
|
"start":8,"end":10,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":10}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "1",
|
||||||
|
"raw": "1n"
|
||||||
|
},
|
||||||
|
"value": "1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"init": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "let"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/typescript/types/literal-bigint/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/literal-bigint/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let x: 0n;
|
||||||
47
packages/babel-parser/test/fixtures/typescript/types/literal-bigint/output.json
vendored
Normal file
47
packages/babel-parser/test/fixtures/typescript/types/literal-bigint/output.json
vendored
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start":4,"end":9,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":9}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":4,"end":9,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":9},"identifierName":"x"},
|
||||||
|
"name": "x",
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":5,"end":9,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":9}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSLiteralType",
|
||||||
|
"start":7,"end":9,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":9}},
|
||||||
|
"literal": {
|
||||||
|
"type": "BigIntLiteral",
|
||||||
|
"start":7,"end":9,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":9}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "0",
|
||||||
|
"raw": "0n"
|
||||||
|
},
|
||||||
|
"value": "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"init": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "let"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -307,6 +307,7 @@ defineType("TSLiteralType", {
|
|||||||
"NumericLiteral",
|
"NumericLiteral",
|
||||||
"StringLiteral",
|
"StringLiteral",
|
||||||
"BooleanLiteral",
|
"BooleanLiteral",
|
||||||
|
"BigIntLiteral",
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -47,9 +47,7 @@ augmentedTypesEnum2.ts
|
|||||||
augmentedTypesFunction.ts
|
augmentedTypesFunction.ts
|
||||||
augmentedTypesInterface.ts
|
augmentedTypesInterface.ts
|
||||||
augmentedTypesVar.ts
|
augmentedTypesVar.ts
|
||||||
bigIntWithTargetES3.ts
|
|
||||||
bigintIndex.ts
|
bigintIndex.ts
|
||||||
bigintWithLib.ts
|
|
||||||
cacheResolutions.ts
|
cacheResolutions.ts
|
||||||
cachedModuleResolution1.ts
|
cachedModuleResolution1.ts
|
||||||
cachedModuleResolution2.ts
|
cachedModuleResolution2.ts
|
||||||
@ -320,7 +318,6 @@ nodeResolution4.ts
|
|||||||
nodeResolution6.ts
|
nodeResolution6.ts
|
||||||
nodeResolution8.ts
|
nodeResolution8.ts
|
||||||
nonMergedOverloads.ts
|
nonMergedOverloads.ts
|
||||||
numberVsBigIntOperations.ts
|
|
||||||
objectLiteralMemberWithoutBlock1.ts
|
objectLiteralMemberWithoutBlock1.ts
|
||||||
outModuleConcatAmd.ts
|
outModuleConcatAmd.ts
|
||||||
outModuleConcatCommonjs.ts
|
outModuleConcatCommonjs.ts
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user