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

Commit

Permalink
Do check ahead of parsing export statement
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Nov 30, 2016
1 parent bed0d87 commit fb3f48c
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 26 deletions.
25 changes: 10 additions & 15 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,22 +861,8 @@ 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 () {
const statement = this.parseStatement(true);
if (!this.isDeclaration(statement)) {
this.unexpected(statement.start);
}
return statement;
return this.parseStatement(true);
};

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

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) {
if (checkNames) {
// Check for duplicate exports
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/385/actual.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export typeof foo;
export async function foo() {}
104 changes: 104 additions & 0 deletions test/fixtures/es2015/uncategorised/385/expected.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
3 changes: 1 addition & 2 deletions test/fixtures/es2015/uncategorised/385/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"sourceType": "module",
"throws": "Unexpected token (1:7)"
"sourceType": "module"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/386/actual.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export new Foo();
export typeof foo;
3 changes: 2 additions & 1 deletion test/fixtures/es2015/uncategorised/386/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"sourceType": "module",
"throws": "Unexpected token (1:7)"
"throws": "Unexpected token, expected { (1:7)"
}

2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/387/actual.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export function() {};
export new Foo();
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/387/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token (1:15)"
"throws": "Unexpected token, expected { (1:7)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/388/actual.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export for (;;);
export function() {};
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/388/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token (1:7)"
"throws": "Unexpected token (1:15)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/389/actual.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export while(foo);
export for (;;);
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/389/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token (1:7)"
"throws": "Unexpected token, expected { (1:7)"
}
1 change: 1 addition & 0 deletions test/fixtures/es2015/uncategorised/390/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export while(foo);
4 changes: 4 additions & 0 deletions test/fixtures/es2015/uncategorised/390/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected { (1:7)"
}
1 change: 1 addition & 0 deletions test/fixtures/es2015/uncategorised/391/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function* foo() {};
119 changes: 119 additions & 0 deletions test/fixtures/es2015/uncategorised/391/expected.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
3 changes: 3 additions & 0 deletions test/fixtures/es2015/uncategorised/391/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}

0 comments on commit fb3f48c

Please sign in to comment.