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

Materialize the class features in @babel/parser. #13175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I would of expected these tests to be duplicated given they used to throw with the enable parser plugin error (and thus wouldn't be testing the syntax but the plugin), but I guess they are unique actually? Like we would of had a test for asi that didn't throw as well, so you could delete this one.

"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)"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also fixed unexpected . in the error messages lol.

],
"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"]
}