Allow parsing of decorators with TypeScript abstract classes (#7850)

* Allow parsing of decorators with TypeScript abstract classes

* avoid lookahead
This commit is contained in:
Brian Ng
2018-05-02 13:49:46 -05:00
committed by GitHub
parent 0353ce9ed5
commit d608535719
5 changed files with 280 additions and 5 deletions

View File

@@ -225,6 +225,10 @@ export default class StatementParser extends ExpressionParser {
}
}
canHaveLeadingDecorator(): boolean {
return this.match(tt._class);
}
parseDecorators(allowExport?: boolean): void {
if (this.hasPlugin("decorators2")) {
allowExport = false;
@@ -250,7 +254,7 @@ export default class StatementParser extends ExpressionParser {
}
}
if (!this.match(tt._class)) {
if (!this.canHaveLeadingDecorator()) {
this.raise(
this.state.start,
"Leading decorators must be attached to a class declaration",

View File

@@ -1489,11 +1489,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
isAbstractClass(): boolean {
return (
this.isContextual("abstract") && this.lookahead().type === tt._class
);
}
parseExportDefaultExpression(): N.Expression | N.Declaration {
if (
this.isContextual("abstract") &&
this.lookahead().type === tt._class
) {
if (this.isAbstractClass()) {
const cls = this.startNode();
this.next(); // Skip "abstract"
this.parseClass(cls, true, true);
@@ -2084,4 +2087,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
shouldParseAsyncArrow(): boolean {
return this.match(tt.colon) || super.shouldParseAsyncArrow();
}
canHaveLeadingDecorator() {
// Avoid unnecessary lookahead in checking for abstract class unless needed!
return super.canHaveLeadingDecorator() || this.isAbstractClass();
}
};

View File

@@ -0,0 +1,6 @@
@decorate()
abstract class Test extends Object {
constructor() {
super();
}
}

View File

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

View File

@@ -0,0 +1,254 @@
{
"type": "File",
"start": 0,
"end": 85,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 85,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 85,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"abstract": true,
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"callee": {
"type": "CallExpression",
"start": 1,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 11
}
},
"callee": {
"type": "Identifier",
"start": 1,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "decorate"
},
"name": "decorate"
},
"arguments": []
}
}
],
"id": {
"type": "Identifier",
"start": 27,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 19
},
"identifierName": "Test"
},
"name": "Test"
},
"superClass": {
"type": "Identifier",
"start": 40,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 28
},
"end": {
"line": 2,
"column": 34
},
"identifierName": "Object"
},
"name": "Object"
},
"body": {
"type": "ClassBody",
"start": 47,
"end": 85,
"loc": {
"start": {
"line": 2,
"column": 35
},
"end": {
"line": 6,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 51,
"end": 83,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 5,
"column": 3
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 51,
"end": 62,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 13
},
"identifierName": "constructor"
},
"name": "constructor"
},
"computed": false,
"kind": "constructor",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 65,
"end": 83,
"loc": {
"start": {
"line": 3,
"column": 16
},
"end": {
"line": 5,
"column": 3
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 71,
"end": 79,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 12
}
},
"expression": {
"type": "CallExpression",
"start": 71,
"end": 78,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 11
}
},
"callee": {
"type": "Super",
"start": 71,
"end": 76,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 9
}
}
},
"arguments": []
}
}
],
"directives": []
}
}
]
}
}
],
"directives": []
}
}