Parse class fields and private methods by default (#13175)
* feat: materialize class features * chore: move testcases to es2022 * chore: update test fixtures * chore: remove classPr\w+ from options.json * chore: remove empty options.json * update flow test allowlist * update typescript allowlist
This commit is contained in:
parent
b116865077
commit
ceaab0bae7
@ -189,7 +189,8 @@ export const ErrorMessages = makeErrorTemplates(
|
|||||||
"Leading decorators must be attached to a class declaration.",
|
"Leading decorators must be attached to a class declaration.",
|
||||||
UnexpectedLexicalDeclaration:
|
UnexpectedLexicalDeclaration:
|
||||||
"Lexical declaration cannot appear in a single-statement context.",
|
"Lexical declaration cannot appear in a single-statement context.",
|
||||||
UnexpectedNewTarget: "`new.target` can only be used in functions.",
|
UnexpectedNewTarget:
|
||||||
|
"`new.target` can only be used in functions or class properties.",
|
||||||
UnexpectedNumericSeparator:
|
UnexpectedNumericSeparator:
|
||||||
"A numeric separator is only allowed between two digits.",
|
"A numeric separator is only allowed between two digits.",
|
||||||
UnexpectedPrivateField:
|
UnexpectedPrivateField:
|
||||||
|
|||||||
@ -1283,7 +1283,6 @@ export default class ExpressionParser extends LValParser {
|
|||||||
const isPrivate = this.match(tt.hash);
|
const isPrivate = this.match(tt.hash);
|
||||||
|
|
||||||
if (isPrivate) {
|
if (isPrivate) {
|
||||||
this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
|
|
||||||
if (!isPrivateNameAllowed) {
|
if (!isPrivateNameAllowed) {
|
||||||
this.raise(this.state.pos, Errors.UnexpectedPrivateField);
|
this.raise(this.state.pos, Errors.UnexpectedPrivateField);
|
||||||
}
|
}
|
||||||
@ -1520,15 +1519,7 @@ export default class ExpressionParser extends LValParser {
|
|||||||
const metaProp = this.parseMetaProperty(node, meta, "target");
|
const metaProp = this.parseMetaProperty(node, meta, "target");
|
||||||
|
|
||||||
if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
|
if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
|
||||||
const errorTemplate = { ...Errors.UnexpectedNewTarget };
|
this.raise(metaProp.start, Errors.UnexpectedNewTarget);
|
||||||
|
|
||||||
if (this.hasPlugin("classProperties")) {
|
|
||||||
errorTemplate.template += " or class properties";
|
|
||||||
}
|
|
||||||
|
|
||||||
/* eslint-disable @babel/development-internal/dry-error-messages */
|
|
||||||
this.raise(metaProp.start, errorTemplate);
|
|
||||||
/* eslint-enable @babel/development-internal/dry-error-messages */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return metaProp;
|
return metaProp;
|
||||||
|
|||||||
@ -1564,8 +1564,6 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
classBody: N.ClassBody,
|
classBody: N.ClassBody,
|
||||||
prop: N.ClassPrivateProperty,
|
prop: N.ClassPrivateProperty,
|
||||||
) {
|
) {
|
||||||
this.expectPlugin("classPrivateProperties", prop.key.start);
|
|
||||||
|
|
||||||
const node = this.parseClassPrivateProperty(prop);
|
const node = this.parseClassPrivateProperty(prop);
|
||||||
classBody.body.push(node);
|
classBody.body.push(node);
|
||||||
|
|
||||||
@ -1603,8 +1601,6 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
isGenerator: boolean,
|
isGenerator: boolean,
|
||||||
isAsync: boolean,
|
isAsync: boolean,
|
||||||
): void {
|
): void {
|
||||||
this.expectPlugin("classPrivateMethods", method.key.start);
|
|
||||||
|
|
||||||
const node = this.parseMethod(
|
const node = this.parseMethod(
|
||||||
method,
|
method,
|
||||||
isGenerator,
|
isGenerator,
|
||||||
@ -1650,9 +1646,6 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
|
|
||||||
// https://tc39.es/proposal-class-fields/#prod-FieldDefinition
|
// https://tc39.es/proposal-class-fields/#prod-FieldDefinition
|
||||||
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
|
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
|
||||||
if (!node.typeAnnotation || this.match(tt.eq)) {
|
|
||||||
this.expectPlugin("classProperties");
|
|
||||||
}
|
|
||||||
this.parseInitializer(node);
|
this.parseInitializer(node);
|
||||||
this.semicolon();
|
this.semicolon();
|
||||||
return this.finishNode(node, "ClassProperty");
|
return this.finishNode(node, "ClassProperty");
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
"type": "File",
|
"type": "File",
|
||||||
"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
|
"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
|
||||||
"errors": [
|
"errors": [
|
||||||
"SyntaxError: `new.target` can only be used in functions. (2:2)"
|
"SyntaxError: `new.target` can only be used in functions or class properties. (2:2)"
|
||||||
],
|
],
|
||||||
"program": {
|
"program": {
|
||||||
"type": "Program",
|
"type": "Program",
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
"type": "File",
|
"type": "File",
|
||||||
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
|
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
|
||||||
"errors": [
|
"errors": [
|
||||||
"SyntaxError: `new.target` can only be used in functions. (1:0)"
|
"SyntaxError: `new.target` can only be used in functions or class properties. (1:0)"
|
||||||
],
|
],
|
||||||
"program": {
|
"program": {
|
||||||
"type": "Program",
|
"type": "Program",
|
||||||
|
|||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"throws": "This experimental syntax requires enabling the parser plugin: 'classProperties' (3:2)"
|
|
||||||
}
|
|
||||||
63
packages/babel-parser/test/fixtures/es2017/async-functions/no-method-asi/output.json
vendored
Normal file
63
packages/babel-parser/test/fixtures/es2017/async-functions/no-method-asi/output.json
vendored
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
|
||||||
|
"sourceType": "script",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassDeclaration",
|
||||||
|
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
|
||||||
|
"name": "A"
|
||||||
|
},
|
||||||
|
"superClass": null,
|
||||||
|
"body": {
|
||||||
|
"type": "ClassBody",
|
||||||
|
"start":8,"end":27,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassProperty",
|
||||||
|
"start":12,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7}},
|
||||||
|
"static": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":12,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7},"identifierName":"async"},
|
||||||
|
"name": "async"
|
||||||
|
},
|
||||||
|
"computed": false,
|
||||||
|
"value": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ClassMethod",
|
||||||
|
"start":20,"end":25,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":7}},
|
||||||
|
"static": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":20,"end":21,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":3},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"computed": false,
|
||||||
|
"kind": "method",
|
||||||
|
"id": null,
|
||||||
|
"generator": false,
|
||||||
|
"async": false,
|
||||||
|
"params": [],
|
||||||
|
"body": {
|
||||||
|
"type": "BlockStatement",
|
||||||
|
"start":23,"end":25,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":7}},
|
||||||
|
"body": [],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"throws": "This experimental syntax requires enabling the parser plugin: 'classProperties' (3:2)"
|
|
||||||
}
|
|
||||||
63
packages/babel-parser/test/fixtures/es2018/async-generators/class-method-no-asi/output.json
vendored
Normal file
63
packages/babel-parser/test/fixtures/es2018/async-generators/class-method-no-asi/output.json
vendored
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
|
||||||
|
"sourceType": "script",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassDeclaration",
|
||||||
|
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
|
||||||
|
"name": "A"
|
||||||
|
},
|
||||||
|
"superClass": null,
|
||||||
|
"body": {
|
||||||
|
"type": "ClassBody",
|
||||||
|
"start":8,"end":29,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassProperty",
|
||||||
|
"start":12,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7}},
|
||||||
|
"static": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":12,"end":17,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7},"identifierName":"async"},
|
||||||
|
"name": "async"
|
||||||
|
},
|
||||||
|
"computed": false,
|
||||||
|
"value": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ClassMethod",
|
||||||
|
"start":20,"end":27,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":9}},
|
||||||
|
"static": false,
|
||||||
|
"kind": "method",
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":22,"end":23,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":5},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"computed": false,
|
||||||
|
"id": null,
|
||||||
|
"generator": true,
|
||||||
|
"async": false,
|
||||||
|
"params": [],
|
||||||
|
"body": {
|
||||||
|
"type": "BlockStatement",
|
||||||
|
"start":25,"end":27,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":9}},
|
||||||
|
"body": [],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Unexpected token (3:3)"
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user