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:
Huáng Jùnliàng 2021-04-20 13:00:05 -04:00 committed by Nicolò Ribaudo
parent b116865077
commit ceaab0bae7
481 changed files with 202 additions and 586 deletions

View File

@ -189,7 +189,8 @@ export const ErrorMessages = makeErrorTemplates(
"Leading decorators must be attached to a class declaration.",
UnexpectedLexicalDeclaration:
"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:
"A numeric separator is only allowed between two digits.",
UnexpectedPrivateField:

View File

@ -1283,7 +1283,6 @@ export default class ExpressionParser extends LValParser {
const isPrivate = this.match(tt.hash);
if (isPrivate) {
this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
if (!isPrivateNameAllowed) {
this.raise(this.state.pos, Errors.UnexpectedPrivateField);
}
@ -1520,15 +1519,7 @@ export default class ExpressionParser extends LValParser {
const metaProp = this.parseMetaProperty(node, meta, "target");
if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
const errorTemplate = { ...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 */
this.raise(metaProp.start, Errors.UnexpectedNewTarget);
}
return metaProp;

View File

@ -1564,8 +1564,6 @@ export default class StatementParser extends ExpressionParser {
classBody: N.ClassBody,
prop: N.ClassPrivateProperty,
) {
this.expectPlugin("classPrivateProperties", prop.key.start);
const node = this.parseClassPrivateProperty(prop);
classBody.body.push(node);
@ -1603,8 +1601,6 @@ export default class StatementParser extends ExpressionParser {
isGenerator: boolean,
isAsync: boolean,
): void {
this.expectPlugin("classPrivateMethods", method.key.start);
const node = this.parseMethod(
method,
isGenerator,
@ -1650,9 +1646,6 @@ export default class StatementParser extends ExpressionParser {
// https://tc39.es/proposal-class-fields/#prod-FieldDefinition
parseClassProperty(node: N.ClassProperty): N.ClassProperty {
if (!node.typeAnnotation || this.match(tt.eq)) {
this.expectPlugin("classProperties");
}
this.parseInitializer(node);
this.semicolon();
return this.finishNode(node, "ClassProperty");

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"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": {
"type": "Program",

View File

@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"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": {
"type": "Program",

View File

@ -1,3 +0,0 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'classProperties' (3:2)"
}

View 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": []
}
}

View File

@ -1,3 +0,0 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'classProperties' (3:2)"
}

View 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": []
}
}

View File

@ -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