Merge pull request #590 from peey/decorators-followup

Follow-up on Decorators PR
This commit is contained in:
Henry Zhu
2017-06-27 15:34:44 -04:00
committed by GitHub
9 changed files with 839 additions and 12 deletions

View File

@@ -150,12 +150,13 @@ export default class StatementParser extends ExpressionParser {
}
takeDecorators(node: N.HasDecorators): void {
if (this.state.decorators.length) {
node.decorators = this.state.decorators;
const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
if (decorators.length) {
node.decorators = decorators;
if (this.hasPlugin("decorators2")) {
this.resetStartLocationFromNode(node, this.state.decorators[0]);
this.resetStartLocationFromNode(node, decorators[0]);
}
this.state.decorators = [];
this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
}
}
@@ -164,13 +165,18 @@ export default class StatementParser extends ExpressionParser {
allowExport = false;
}
const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
while (this.match(tt.at)) {
const decorator = this.parseDecorator();
this.state.decorators.push(decorator);
currentContextDecorators.push(decorator);
}
if (allowExport && this.match(tt._export)) {
return;
if (this.match(tt._export)) {
if (allowExport) {
return;
} else {
this.raise(this.state.start, "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead");
}
}
if (!this.match(tt._class)) {
@@ -202,7 +208,11 @@ export default class StatementParser extends ExpressionParser {
if (this.eat(tt.parenL)) {
const node = this.startNodeAt(startPos, startLoc);
node.callee = expr;
// Every time a decorator class expression is evaluated, a new empty array is pushed onto the stack
// So that the decorators of any nested class expressions will be dealt with separately
this.state.decoratorStack.push([]);
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
this.state.decoratorStack.pop();
expr = this.finishNode(node, "CallExpression");
this.toReferencedList(expr.arguments);
}
@@ -1038,7 +1048,8 @@ export default class StatementParser extends ExpressionParser {
}
}
if (this.state.decorators.length) {
const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
if (currentContextDecorators.length) {
const isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
if (!node.declaration || !isClass) {
throw this.raise(node.start, "You can only use decorators on an export when exporting a class");

View File

@@ -32,7 +32,7 @@ export default class State {
this.labels = [];
this.decorators = [];
this.decoratorStack = [[]];
this.tokens = [];
@@ -92,8 +92,10 @@ export default class State {
// Labels in scope.
labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>;
// Leading decorators.
decorators: Array<N.Decorator>;
// Leading decorators. Last element of the stack represents the decorators in current context.
// Supports nesting of decorators, e.g. @foo(@bar class inner {}) class outer {}
// where @foo belongs to the outer class and @bar to the inner
decoratorStack: Array<Array<N.Decorator>>;
// Token store.
tokens: Array<Token | N.Comment>;