reject non-toplevel import/export (#2128)

fixes #2124
This commit is contained in:
Alex Lam S.L
2017-06-21 03:18:48 +08:00
committed by GitHub
parent 62d1fbf645
commit 11923e3ae8
5 changed files with 45 additions and 28 deletions

View File

@@ -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);

View File

@@ -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,

View File

@@ -0,0 +1,3 @@
{
export var V = 1;
}

View File

@@ -0,0 +1,3 @@
{
import A from "B";
}

View File

@@ -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,