[decorators] [typescript] Parse type parameters (#8767)
* [decorators] [typescript] Parse type parameters * Add test for invalid code
This commit is contained in:
parent
07862e7272
commit
3c87401714
@ -305,15 +305,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.eat(tt.parenL)) {
|
||||
const node = this.startNodeAt(startPos, startLoc);
|
||||
node.callee = expr;
|
||||
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
|
||||
this.toReferencedList(node.arguments);
|
||||
expr = this.finishNode(node, "CallExpression");
|
||||
}
|
||||
|
||||
node.expression = expr;
|
||||
node.expression = this.parseMaybeDecoratorArguments(expr);
|
||||
this.state.decoratorStack.pop();
|
||||
} else {
|
||||
node.expression = this.parseMaybeAssign();
|
||||
@ -321,6 +313,18 @@ export default class StatementParser extends ExpressionParser {
|
||||
return this.finishNode(node, "Decorator");
|
||||
}
|
||||
|
||||
parseMaybeDecoratorArguments(expr: N.Expression): N.Expression {
|
||||
if (this.eat(tt.parenL)) {
|
||||
const node = this.startNodeAtNode(expr);
|
||||
node.callee = expr;
|
||||
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
|
||||
this.toReferencedList(node.arguments);
|
||||
return this.finishNode(node, "CallExpression");
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
parseBreakContinueStatement(
|
||||
node: N.BreakStatement | N.ContinueStatement,
|
||||
keyword: string,
|
||||
|
||||
@ -2047,6 +2047,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
parseMaybeDecoratorArguments(expr: N.Expression): N.Expression {
|
||||
if (this.isRelational("<")) {
|
||||
const typeArguments = this.tsParseTypeArguments();
|
||||
|
||||
if (this.match(tt.parenL)) {
|
||||
const call = super.parseMaybeDecoratorArguments(expr);
|
||||
call.typeParameters = typeArguments;
|
||||
return call;
|
||||
}
|
||||
|
||||
this.unexpected(this.state.start, tt.parenL);
|
||||
}
|
||||
|
||||
return super.parseMaybeDecoratorArguments(expr);
|
||||
}
|
||||
|
||||
// === === === === === === === === === === === === === === === ===
|
||||
// Note: All below methods are duplicates of something in flow.js.
|
||||
// Not sure what the best way to combine these is.
|
||||
|
||||
2
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments-invalid/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments-invalid/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
@decorator<string>
|
||||
class Test {}
|
||||
@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
"typescript",
|
||||
["decorators", { "decoratorsBeforeExport": true }]
|
||||
],
|
||||
"throws": "Unexpected token, expected \"(\" (2:0)"
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
@decorator<string>()
|
||||
class Test {}
|
||||
6
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments/options.json
vendored
Normal file
6
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
"typescript",
|
||||
["decorators", { "decoratorsBeforeExport": true }]
|
||||
]
|
||||
}
|
||||
166
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments/output.json
vendored
Normal file
166
packages/babel-parser/test/fixtures/typescript/decorators/type-arguments/output.json
vendored
Normal file
@ -0,0 +1,166 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ClassDeclaration",
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"decorators": [
|
||||
{
|
||||
"type": "Decorator",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "CallExpression",
|
||||
"start": 1,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"callee": {
|
||||
"type": "Identifier",
|
||||
"start": 1,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"identifierName": "decorator"
|
||||
},
|
||||
"name": "decorator"
|
||||
},
|
||||
"arguments": [],
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterInstantiation",
|
||||
"start": 10,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSStringKeyword",
|
||||
"start": 11,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 17
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 27,
|
||||
"end": 31,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 10
|
||||
},
|
||||
"identifierName": "Test"
|
||||
},
|
||||
"name": "Test"
|
||||
},
|
||||
"superClass": null,
|
||||
"body": {
|
||||
"type": "ClassBody",
|
||||
"start": 32,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"body": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/typescript/legacy-decorators/type-arguments/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/typescript/legacy-decorators/type-arguments/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
@decorator<string>()
|
||||
class Test {}
|
||||
3
packages/babel-parser/test/fixtures/typescript/legacy-decorators/type-arguments/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/typescript/legacy-decorators/type-arguments/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["typescript", "decorators-legacy"]
|
||||
}
|
||||
166
packages/babel-parser/test/fixtures/typescript/legacy-decorators/type-arguments/output.json
vendored
Normal file
166
packages/babel-parser/test/fixtures/typescript/legacy-decorators/type-arguments/output.json
vendored
Normal file
@ -0,0 +1,166 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ClassDeclaration",
|
||||
"start": 0,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"decorators": [
|
||||
{
|
||||
"type": "Decorator",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "CallExpression",
|
||||
"start": 1,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"callee": {
|
||||
"type": "Identifier",
|
||||
"start": 1,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"identifierName": "decorator"
|
||||
},
|
||||
"name": "decorator"
|
||||
},
|
||||
"arguments": [],
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterInstantiation",
|
||||
"start": 10,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSStringKeyword",
|
||||
"start": 11,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 17
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 27,
|
||||
"end": 31,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 10
|
||||
},
|
||||
"identifierName": "Test"
|
||||
},
|
||||
"name": "Test"
|
||||
},
|
||||
"superClass": null,
|
||||
"body": {
|
||||
"type": "ClassBody",
|
||||
"start": 32,
|
||||
"end": 34,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"body": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user