Skip to content

Commit

Permalink
Adds the @init: modifier to decorator plugins (#13859)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
pzuraq and nicolo-ribaudo committed Nov 24, 2021
1 parent e6f6d3b commit e292ecc
Show file tree
Hide file tree
Showing 62 changed files with 789 additions and 15 deletions.
288 changes: 288 additions & 0 deletions packages/babel-core/test/fixtures/parse/output-legacy.json
@@ -0,0 +1,288 @@
{
"type": "File",
"start": 0,
"end": 91,
"errors": [],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 0
}
},
"program": {
"type": "Program",
"start": 0,
"end": 91,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 0
}
},
"interpreter": null,
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 17
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"expression": {
"type": "Identifier",
"start": 1,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 11
},
"identifierName": "annotation"
},
"name": "annotation"
}
}
],
"id": {
"type": "Identifier",
"start": 18,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "MyClass"
},
"name": "MyClass"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 17
}
},
"body": []
}
},
{
"type": "FunctionDeclaration",
"start": 31,
"end": 90,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 40,
"end": 50,
"loc": {
"start": {
"line": 4,
"column": 9
},
"end": {
"line": 4,
"column": 19
},
"identifierName": "annotation"
},
"name": "annotation"
},
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 51,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 20
},
"end": {
"line": 4,
"column": 26
},
"identifierName": "target"
},
"name": "target"
}
],
"body": {
"type": "BlockStatement",
"start": 59,
"end": 90,
"loc": {
"start": {
"line": 4,
"column": 28
},
"end": {
"line": 6,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 64,
"end": 88,
"loc": {
"start": {
"line": 5,
"column": 3
},
"end": {
"line": 5,
"column": 27
}
},
"expression": {
"type": "AssignmentExpression",
"start": 64,
"end": 87,
"loc": {
"start": {
"line": 5,
"column": 3
},
"end": {
"line": 5,
"column": 26
}
},
"operator": "=",
"left": {
"type": "MemberExpression",
"start": 64,
"end": 80,
"loc": {
"start": {
"line": 5,
"column": 3
},
"end": {
"line": 5,
"column": 19
}
},
"object": {
"type": "Identifier",
"start": 64,
"end": 70,
"loc": {
"start": {
"line": 5,
"column": 3
},
"end": {
"line": 5,
"column": 9
},
"identifierName": "target"
},
"name": "target"
},
"property": {
"type": "Identifier",
"start": 71,
"end": 80,
"loc": {
"start": {
"line": 5,
"column": 10
},
"end": {
"line": 5,
"column": 19
},
"identifierName": "annotated"
},
"name": "annotated"
},
"computed": false
},
"right": {
"type": "BooleanLiteral",
"start": 83,
"end": 87,
"loc": {
"start": {
"line": 5,
"column": 22
},
"end": {
"line": 5,
"column": 26
}
},
"value": true
}
}
}
],
"directives": []
}
}
],
"directives": []
},
"comments": []
}
1 change: 1 addition & 0 deletions packages/babel-core/test/fixtures/parse/output.json
Expand Up @@ -49,6 +49,7 @@
"type": "Decorator",
"start": 0,
"end": 11,
"init": false,
"loc": {
"start": {
"line": 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/test/parse.js
Expand Up @@ -18,7 +18,7 @@ function fixture(...args) {
describe("parse", function () {
it("should parse using configuration from .babelrc when a filename is provided", function () {
const input = fs.readFileSync(fixture("input.js"), "utf8");
const output = require(fixture("output"));
const output = require(fixture("output-legacy.json"));

const result = parse(input, {
filename: fixture("input.js"),
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/ast/spec.md
Expand Up @@ -598,6 +598,7 @@ A variable declarator.
interface Decorator <: Node {
type: "Decorator";
expression: Expression;
init: boolean;
}
```

Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -223,6 +223,8 @@ export const ErrorMessages = makeErrorTemplates(
UnsupportedBind: "Binding should be performed on object property.",
UnsupportedDecoratorExport:
"A decorated export must export a class declaration.",
UnsupportedDecoratorModifier:
"Unsupported decorator modifier. `@init:` is the only modifier currently supported for decorators.",
UnsupportedDefaultExport:
"Only expressions, functions or classes are allowed as the `default` export.",
UnsupportedImport:
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -32,6 +32,7 @@ import {
tokenOperatorPrecedence,
tt,
type TokenType,
tokenIsDecorator,
} from "../tokenizer/types";
import * as N from "../types";
import LValParser from "./lval";
Expand Down Expand Up @@ -1143,6 +1144,7 @@ export default class ExpressionParser extends LValParser {
return this.parseFunctionOrFunctionSent();

case tt.at:
case tt.atInit:
this.parseDecorators();
// fall through
case tt._class:
Expand Down Expand Up @@ -1955,7 +1957,7 @@ export default class ExpressionParser extends LValParser {
refExpressionErrors?: ?ExpressionErrors,
): N.ObjectMember | N.SpreadElement {
let decorators = [];
if (this.match(tt.at)) {
if (tokenIsDecorator(this.state.type)) {
if (this.hasPlugin("decorators")) {
this.raise(this.state.start, Errors.UnsupportedPropertyDecorator);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/lval.js
Expand Up @@ -2,7 +2,7 @@

/*:: declare var invariant; */
import * as charCodes from "charcodes";
import { tt, type TokenType } from "../tokenizer/types";
import { tt, tokenIsDecorator, type TokenType } from "../tokenizer/types";
import type {
TSParameterProperty,
Decorator,
Expand Down Expand Up @@ -390,7 +390,7 @@ export default class LValParser extends NodeUtils {
break;
} else {
const decorators = [];
if (this.match(tt.at) && this.hasPlugin("decorators")) {
if (tokenIsDecorator(this.state.type) && this.hasPlugin("decorators")) {
this.raise(this.state.start, Errors.UnsupportedParameterDecorator);
}
// invariant: hasPlugin("decorators-legacy")
Expand Down

0 comments on commit e292ecc

Please sign in to comment.