From 94e345e0b242ac85c59ba57ca1d58d44b1aab986 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 2 Aug 2015 21:36:52 +0100 Subject: [PATCH] check valid function/class token when parsing export default before converting to a declaration - fixes #2145 --- src/parser/statement.js | 3 +- .../actual.js | 1 + .../expected.json | 98 ++++++++++++++++++ .../options.json | 3 + .../actual.js | 1 + .../expected.json | 99 +++++++++++++++++++ .../options.json | 3 + 7 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/harmony/modules/export-default-function-declaration/actual.js create mode 100644 test/fixtures/harmony/modules/export-default-function-declaration/expected.json create mode 100644 test/fixtures/harmony/modules/export-default-function-declaration/options.json create mode 100644 test/fixtures/harmony/modules/export-default-function-expression/actual.js create mode 100644 test/fixtures/harmony/modules/export-default-function-expression/expected.json create mode 100644 test/fixtures/harmony/modules/export-default-function-expression/options.json diff --git a/src/parser/statement.js b/src/parser/statement.js index b8822c3d77..18482415f8 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -648,9 +648,10 @@ pp.parseExport = function (node) { } this.parseExportFrom(node, true); } else if (this.eat(tt._default)) { // export default ... + let possibleDeclaration = this.match(tt._function) || this.match(tt._class); let expr = this.parseMaybeAssign(); let needsSemi = true; - if (expr.type === "FunctionExpression" || expr.type === "ClassExpression") { + if (possibleDeclaration) { needsSemi = false; if (expr.id) { expr.type = expr.type === "FunctionExpression" ? "FunctionDeclaration" : "ClassDeclaration"; diff --git a/test/fixtures/harmony/modules/export-default-function-declaration/actual.js b/test/fixtures/harmony/modules/export-default-function-declaration/actual.js new file mode 100644 index 0000000000..c33f5633d7 --- /dev/null +++ b/test/fixtures/harmony/modules/export-default-function-declaration/actual.js @@ -0,0 +1 @@ +export default function a() {} diff --git a/test/fixtures/harmony/modules/export-default-function-declaration/expected.json b/test/fixtures/harmony/modules/export-default-function-declaration/expected.json new file mode 100644 index 0000000000..182219d69c --- /dev/null +++ b/test/fixtures/harmony/modules/export-default-function-declaration/expected.json @@ -0,0 +1,98 @@ +{ + "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": "FunctionDeclaration", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "id": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "name": "a" + }, + "generator": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 28, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "body": [] + } + } + } + ] + } +} \ No newline at end of file diff --git a/test/fixtures/harmony/modules/export-default-function-declaration/options.json b/test/fixtures/harmony/modules/export-default-function-declaration/options.json new file mode 100644 index 0000000000..2104ca4328 --- /dev/null +++ b/test/fixtures/harmony/modules/export-default-function-declaration/options.json @@ -0,0 +1,3 @@ +{ + "sourceType": "module" +} diff --git a/test/fixtures/harmony/modules/export-default-function-expression/actual.js b/test/fixtures/harmony/modules/export-default-function-expression/actual.js new file mode 100644 index 0000000000..73760420da --- /dev/null +++ b/test/fixtures/harmony/modules/export-default-function-expression/actual.js @@ -0,0 +1 @@ +export default (function a() {}); diff --git a/test/fixtures/harmony/modules/export-default-function-expression/expected.json b/test/fixtures/harmony/modules/export-default-function-expression/expected.json new file mode 100644 index 0000000000..b518031244 --- /dev/null +++ b/test/fixtures/harmony/modules/export-default-function-expression/expected.json @@ -0,0 +1,99 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportDefaultDeclaration", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "declaration": { + "type": "FunctionExpression", + "start": 16, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "id": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "name": "a" + }, + "generator": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 29, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "body": [] + }, + "parenthesizedExpression": true + } + } + ] + } +} \ No newline at end of file diff --git a/test/fixtures/harmony/modules/export-default-function-expression/options.json b/test/fixtures/harmony/modules/export-default-function-expression/options.json new file mode 100644 index 0000000000..2104ca4328 --- /dev/null +++ b/test/fixtures/harmony/modules/export-default-function-expression/options.json @@ -0,0 +1,3 @@ +{ + "sourceType": "module" +}