Better error message for invalid decorators syntax (#7986)

This commit is contained in:
Nicolò Ribaudo
2018-05-23 21:26:35 +02:00
committed by Henry Zhu
parent 007bfb6565
commit b33823e7f8
7 changed files with 38 additions and 18 deletions

View File

@@ -243,13 +243,6 @@ export default class StatementParser extends ExpressionParser {
}
parseDecorators(allowExport?: boolean): void {
if (
this.hasPlugin("decorators") &&
!this.getPluginOption("decorators", "decoratorsBeforeExport")
) {
allowExport = false;
}
const currentContextDecorators = this.state.decoratorStack[
this.state.decoratorStack.length - 1
];
@@ -259,18 +252,22 @@ export default class StatementParser extends ExpressionParser {
}
if (this.match(tt._export)) {
if (allowExport) {
return;
} else {
if (!allowExport) {
this.unexpected();
}
if (
this.hasPlugin("decorators") &&
!this.getPluginOption("decorators", "decoratorsBeforeExport")
) {
this.raise(
this.state.start,
"Using the export keyword between a decorator and a class is not allowed. " +
"Please use `export @dec class` instead",
"Please use `export @dec class` instead, or set the " +
"'decoratorsBeforeExport' option to true.",
);
}
}
if (!this.canHaveLeadingDecorator()) {
} else if (!this.canHaveLeadingDecorator()) {
this.raise(
this.state.start,
"Leading decorators must be attached to a class declaration",
@@ -1442,7 +1439,12 @@ export default class StatementParser extends ExpressionParser {
this.hasPlugin("decorators") &&
this.getPluginOption("decorators", "decoratorsBeforeExport")
) {
this.unexpected();
this.unexpected(
this.state.start,
"Decorators must be placed *before* the 'export' keyword." +
" You can set the 'decoratorsBeforeExport' option to false to use" +
" the 'export @decorator class {}' syntax",
);
}
this.parseDecorators(false);
return this.parseClass(expr, true, true);
@@ -1544,7 +1546,12 @@ export default class StatementParser extends ExpressionParser {
this.expectOnePlugin(["decorators", "decorators-legacy"]);
if (this.hasPlugin("decorators")) {
if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
this.unexpected();
this.unexpected(
this.state.start,
"Decorators must be placed *before* the 'export' keyword." +
" You can set the 'decoratorsBeforeExport' option to false to use" +
" the 'export @decorator class {}' syntax",
);
} else {
return true;
}