[decorators] [typescript] Parse type parameters (#8767)

* [decorators] [typescript] Parse type parameters

* Add test for invalid code
This commit is contained in:
Nicolò Ribaudo 2018-10-01 22:04:19 +02:00 committed by GitHub
parent 07862e7272
commit 3c87401714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 383 additions and 9 deletions

View File

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

View File

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

View File

@ -0,0 +1,2 @@
@decorator<string>
class Test {}

View File

@ -0,0 +1,7 @@
{
"plugins": [
"typescript",
["decorators", { "decoratorsBeforeExport": true }]
],
"throws": "Unexpected token, expected \"(\" (2:0)"
}

View File

@ -0,0 +1,2 @@
@decorator<string>()
class Test {}

View File

@ -0,0 +1,6 @@
{
"plugins": [
"typescript",
["decorators", { "decoratorsBeforeExport": true }]
]
}

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

View File

@ -0,0 +1,2 @@
@decorator<string>()
class Test {}

View File

@ -0,0 +1,3 @@
{
"plugins": ["typescript", "decorators-legacy"]
}

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