Provide better error message for invalid default export declaration (#7717)

This commit is contained in:
Dennis Czombera
2018-04-13 00:07:41 +02:00
committed by Brian Ng
parent 1e41f613bb
commit 61ec5ce957
11 changed files with 229 additions and 0 deletions

View File

@@ -1417,6 +1417,15 @@ export default class StatementParser extends ExpressionParser {
} else if (this.match(tt.at)) {
this.parseDecorators(false);
return this.parseClass(expr, true, true);
} else if (
this.match(tt._let) ||
this.match(tt._const) ||
this.match(tt._var)
) {
this.raise(
this.state.start,
"Only expressions, functions or classes are allowed as the `default` export.",
);
} else {
const res = this.parseMaybeAssign();
this.semicolon();

View File

@@ -0,0 +1 @@
export default class Foo {}

View File

@@ -0,0 +1,98 @@
{
"type": "File",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"program": {
"type": "Program",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"declaration": {
"type": "ClassDeclaration",
"start": 15,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 24
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 25,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 27
}
},
"body": []
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
export default (class Foo {});

View File

@@ -0,0 +1,102 @@
{
"type": "File",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"program": {
"type": "Program",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"declaration": {
"type": "ClassExpression",
"start": 16,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 28
}
},
"id": {
"type": "Identifier",
"start": 22,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 25
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 26,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 28
}
},
"body": []
},
"extra": {
"parenthesized": true,
"parenStart": 15
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
export default const Foo = () => {
return `<div class="bar">Hola</div>`;
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Only expressions, functions or classes are allowed as the `default` export. (1:15)"
}

View File

@@ -0,0 +1,3 @@
export default let Foo = () => {
return `<div class="bar">Hola</div>`;
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Only expressions, functions or classes are allowed as the `default` export. (1:15)"
}

View File

@@ -0,0 +1,3 @@
export default var Foo = () => {
return `<div class="bar">Hola</div>`;
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Only expressions, functions or classes are allowed as the `default` export. (1:15)"
}