Skip to content

Commit

Permalink
Materialize the class features in @babel/parser. (#13175)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Apr 22, 2021
1 parent b257210 commit 2d0b249
Show file tree
Hide file tree
Showing 481 changed files with 202 additions and 586 deletions.
3 changes: 2 additions & 1 deletion packages/babel-parser/src/parser/error-message.js
Expand Up @@ -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:
Expand Down
11 changes: 1 addition & 10 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 0 additions & 7 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
Expand Up @@ -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",
Expand Down
Expand Up @@ -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",
Expand Down

This file was deleted.

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

This file was deleted.

@@ -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)"
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:8)",
"plugins": ["classProperties"]
"plugins": []
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (2:5)",
"plugins": ["classPrivateProperties"]
"plugins": []
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:3)",
"plugins": ["classPrivateProperties"]
"plugins": []
}
@@ -1,6 +1,3 @@
{
"plugins": [
"classPrivateProperties"
],
"throws": "Unexpected digit after hash token. (2:2)"
}
}
@@ -1,6 +1,3 @@
{
"plugins": [
"classPrivateProperties"
],
"throws": "Unexpected digit after hash token. (2:2)"
}
}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (4:11)"
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (2:3)",
"plugins": ["classPrivateProperties"]
"plugins": []
}
@@ -0,0 +1,3 @@
{
"plugins": ["typescript"]
}
@@ -0,0 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]]
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:9)",
"plugins": ["classProperties"]
"plugins": []
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:8)",
"plugins": ["classPrivateProperties"]
"plugins": []
}
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (2:4)",
"plugins": ["classProperties"]
"plugins": []
}
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"errors": [
"SyntaxError: `new.target` can only be used in functions. or class properties (1:8)"
"SyntaxError: `new.target` can only be used in functions or class properties. (1:8)"
],
"program": {
"type": "Program",
Expand Down
@@ -0,0 +1,3 @@
{
"plugins": ["flow"]
}
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"errors": [
"SyntaxError: `new.target` can only be used in functions. (1:8)"
"SyntaxError: `new.target` can only be used in functions or class properties. (1:8)"
],
"program": {
"type": "Program",
Expand Down
@@ -1,8 +1,3 @@
{
"plugins": [
"flow",
"jsx",
["estree", { "classFeatures": true }],
"classPrivateMethods"
]
"plugins": ["flow", "jsx", ["estree", { "classFeatures": true }]]
}
@@ -1,3 +1,3 @@
{
"plugins": ["flow", "jsx", "estree", "classPrivateMethods"]
"plugins": ["flow", "jsx", "estree"]
}

0 comments on commit 2d0b249

Please sign in to comment.