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

Commit

Permalink
Throw error when exporting non-declaration
Browse files Browse the repository at this point in the history
fixes #238
  • Loading branch information
kaicataldo committed Nov 29, 2016
1 parent 9b6bb3c commit bed0d87
Show file tree
Hide file tree
Showing 26 changed files with 628 additions and 6 deletions.
22 changes: 16 additions & 6 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 All @@ -861,8 +861,22 @@ pp.parseExport = function (node) {
return this.finishNode(node, "ExportNamedDeclaration");
};

pp.shouldParseExportDeclaration = function () {
return this.state.type.keyword || this.isContextual("async");
};

pp.isDeclaration = function (node): boolean {
return node.type === "VariableDeclaration"
|| node.type === "FunctionDeclaration"
|| node.type === "ClassDeclaration";
};

pp.parseExportDeclaration = function () {
return this.parseStatement(true);
const statement = this.parseStatement(true);
if (!this.isDeclaration(statement)) {
this.unexpected(statement.start);
}
return statement;
};

pp.isExportDefaultSpecifier = function () {
Expand Down Expand Up @@ -901,10 +915,6 @@ pp.parseExportFrom = function (node, expect?) {
this.semicolon();
};

pp.shouldParseExportDeclaration = function () {
return this.isContextual("async");
};

pp.checkExport = function (node, checkNames, isDefault) {
if (checkNames) {
// Check for duplicate exports
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;

0 comments on commit bed0d87

Please sign in to comment.