Throw error when exporting non-declaration (#241)

* Throw error when exporting non-declaration

fixes #238

* Do check ahead of parsing export statement
This commit is contained in:
Kai Cataldo 2016-12-01 15:03:13 -05:00 committed by Daniel Tschinder
parent 5630380026
commit 5fb4353778
32 changed files with 852 additions and 3 deletions

View File

@ -848,7 +848,7 @@ pp.parseExport = function (node) {
if (needsSemi) this.semicolon();
this.checkExport(node, true, true);
return this.finishNode(node, "ExportDefaultDeclaration");
} else if (this.state.type.keyword || this.shouldParseExportDeclaration()) {
} else if (this.shouldParseExportDeclaration()) {
node.specifiers = [];
node.source = null;
node.declaration = this.parseExportDeclaration(node);
@ -901,8 +901,13 @@ pp.parseExportFrom = function (node, expect?) {
this.semicolon();
};
pp.shouldParseExportDeclaration = function () {
return this.isContextual("async");
pp.shouldParseExportDeclaration = function (): boolean {
return this.state.type.keyword === "var"
|| this.state.type.keyword === "const"
|| this.state.type.keyword === "let"
|| this.state.type.keyword === "function"
|| this.state.type.keyword === "class"
|| this.isContextual("async");
};
pp.checkExport = function (node, checkNames, isDefault) {

View File

@ -0,0 +1 @@
export var foo = 1;

View File

@ -0,0 +1,121 @@
{
"type": "File",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "VariableDeclaration",
"start": 7,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 19
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 11,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 18
}
},
"id": {
"type": "Identifier",
"start": 11,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "foo"
},
"name": "foo"
},
"init": {
"type": "NumericLiteral",
"start": 17,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 18
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "var"
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1 @@
export const foo = 1;

View File

@ -0,0 +1,121 @@
{
"type": "File",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 21
}
},
"program": {
"type": "Program",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 21
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 21
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "VariableDeclaration",
"start": 7,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 21
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 13,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 20
}
},
"id": {
"type": "Identifier",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "foo"
},
"name": "foo"
},
"init": {
"type": "NumericLiteral",
"start": 19,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 20
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "const"
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1 @@
export let foo = 1;

View File

@ -0,0 +1,121 @@
{
"type": "File",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "VariableDeclaration",
"start": 7,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 19
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 11,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 18
}
},
"id": {
"type": "Identifier",
"start": 11,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "foo"
},
"name": "foo"
},
"init": {
"type": "NumericLiteral",
"start": 17,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 18
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "let"
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

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

View File

@ -0,0 +1,100 @@
{
"type": "File",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "ClassDeclaration",
"start": 7,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 19
}
},
"id": {
"type": "Identifier",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 17,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 19
}
},
"body": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1 @@
export function foo() {}

View File

@ -0,0 +1,104 @@
{
"type": "File",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"program": {
"type": "Program",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "FunctionDeclaration",
"start": 7,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 24
}
},
"id": {
"type": "Identifier",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 22,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 24
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1 @@
export async function foo() {}

View File

@ -0,0 +1,104 @@
{
"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": "ExportNamedDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 30
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "FunctionDeclaration",
"start": 7,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 30
}
},
"id": {
"type": "Identifier",
"start": 22,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 25
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": false,
"expression": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 28,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 30
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -0,0 +1 @@
export typeof foo;

View File

@ -0,0 +1,5 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected { (1:7)"
}

View File

@ -0,0 +1 @@
export new Foo();

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected { (1:7)"
}

View File

@ -0,0 +1 @@
export function() {};

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token (1:15)"
}

View File

@ -0,0 +1 @@
export for (;;);

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected { (1:7)"
}

View File

@ -0,0 +1 @@
export while(foo);

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected { (1:7)"
}

View File

@ -0,0 +1 @@
export function* foo() {};

View File

@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"program": {
"type": "Program",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"sourceType": "module",
"body": [
{
"type": "ExportNamedDeclaration",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "FunctionDeclaration",
"start": 7,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 25
}
},
"id": {
"type": "Identifier",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 20
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": true,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 23,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 25
}
},
"body": [],
"directives": []
}
}
},
{
"type": "EmptyStatement",
"start": 25,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 26
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}