diff --git a/lib/scope.js b/lib/scope.js index 68dc2b5a..2b75df82 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -223,6 +223,15 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ })); node.thedef = sym; } + if (!(scope instanceof AST_Toplevel) && (node instanceof AST_Export || node instanceof AST_Import)) { + js_error( + node.TYPE + " statement may only appear at top level", + node.start.file, + node.start.line, + node.start.col, + node.start.pos + ); + } function mark_export(def, level) { var node = tw.parent(level); diff --git a/test/compress/export.js b/test/compress/export.js index b37bbfdf..d623e31d 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -40,34 +40,6 @@ issue_2038_2: { } } -issue_2124: { - options = { - unused: true, - } - input: { - { - export var V = 1; - } - { - export let L = 2; - } - { - export const C = 3; - } - } - expect: { - { - export var V = 1; - } - { - export let L = 2; - } - { - export const C = 3; - } - } -} - issue_2126: { mangle = { toplevel: true, diff --git a/test/input/invalid/export.js b/test/input/invalid/export.js new file mode 100644 index 00000000..2ccd3131 --- /dev/null +++ b/test/input/invalid/export.js @@ -0,0 +1,3 @@ +{ + export var V = 1; +} diff --git a/test/input/invalid/import.js b/test/input/invalid/import.js new file mode 100644 index 00000000..43a2e9cd --- /dev/null +++ b/test/input/invalid/import.js @@ -0,0 +1,3 @@ +{ + import A from "B"; +} diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 5ae0e5a0..c01c02dd 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -533,6 +533,36 @@ describe("bin/uglifyjs", function () { done(); }); }); + it("Should throw syntax error (block-level export)", function(done) { + var command = uglifyjscmd + ' test/input/invalid/export.js -m'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/export.js:2,4", + " export var V = 1;", + " ^", + "ERROR: Export statement may only appear at top level" + ].join("\n")); + done(); + }); + }); + it("Should throw syntax error (block-level import)", function(done) { + var command = uglifyjscmd + ' test/input/invalid/import.js -m'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/import.js:2,4", + ' import A from "B";', + " ^", + "ERROR: Import statement may only appear at top level" + ].join("\n")); + done(); + }); + }); it("Should handle literal string as source map input", function(done) { var command = [ uglifyjscmd,