Provide better error message for invalid default export declaration (#7717)
This commit is contained in:
committed by
Brian Ng
parent
1e41f613bb
commit
61ec5ce957
@@ -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();
|
||||
|
||||
1
packages/babylon/test/fixtures/es2015/modules/export-default-class-declaration/input.js
vendored
Normal file
1
packages/babylon/test/fixtures/es2015/modules/export-default-class-declaration/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export default class Foo {}
|
||||
98
packages/babylon/test/fixtures/es2015/modules/export-default-class-declaration/output.json
vendored
Normal file
98
packages/babylon/test/fixtures/es2015/modules/export-default-class-declaration/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
packages/babylon/test/fixtures/es2015/modules/export-default-class-expression/input.js
vendored
Normal file
1
packages/babylon/test/fixtures/es2015/modules/export-default-class-expression/input.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export default (class Foo {});
|
||||
102
packages/babylon/test/fixtures/es2015/modules/export-default-class-expression/output.json
vendored
Normal file
102
packages/babylon/test/fixtures/es2015/modules/export-default-class-expression/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
3
packages/babylon/test/fixtures/es2015/modules/export-default-variable-declaration/input.js
vendored
Normal file
3
packages/babylon/test/fixtures/es2015/modules/export-default-variable-declaration/input.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export default const Foo = () => {
|
||||
return `<div class="bar">Hola</div>`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Only expressions, functions or classes are allowed as the `default` export. (1:15)"
|
||||
}
|
||||
3
packages/babylon/test/fixtures/es2015/modules/export-default-variable-declaration2/input.js
vendored
Normal file
3
packages/babylon/test/fixtures/es2015/modules/export-default-variable-declaration2/input.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export default let Foo = () => {
|
||||
return `<div class="bar">Hola</div>`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Only expressions, functions or classes are allowed as the `default` export. (1:15)"
|
||||
}
|
||||
3
packages/babylon/test/fixtures/es2015/modules/export-default-variable-declaration3/input.js
vendored
Normal file
3
packages/babylon/test/fixtures/es2015/modules/export-default-variable-declaration3/input.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export default var Foo = () => {
|
||||
return `<div class="bar">Hola</div>`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Only expressions, functions or classes are allowed as the `default` export. (1:15)"
|
||||
}
|
||||
Reference in New Issue
Block a user