Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Throw error when exporting non-declaration #241

Merged
merged 2 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/es2015/uncategorised/380/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var foo = 1;
121 changes: 121 additions & 0 deletions test/fixtures/es2015/uncategorised/380/expected.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
3 changes: 3 additions & 0 deletions test/fixtures/es2015/uncategorised/380/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
1 change: 1 addition & 0 deletions test/fixtures/es2015/uncategorised/381/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 1;
121 changes: 121 additions & 0 deletions test/fixtures/es2015/uncategorised/381/expected.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
3 changes: 3 additions & 0 deletions test/fixtures/es2015/uncategorised/381/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
1 change: 1 addition & 0 deletions test/fixtures/es2015/uncategorised/382/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let foo = 1;