From 1cc606d4d0fca262e309c778896cbc5787511dd9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 7 Jan 2015 07:58:04 +1100 Subject: [PATCH] make import reassignment illegal @eventualbuddha --- lib/6to5/transformation/modules/_default.js | 49 +++++++++++++++++++ .../disallow-import-remapping-2/actual.js | 2 + .../disallow-import-remapping-2/options.json | 3 ++ .../disallow-import-remapping/actual.js | 2 + .../disallow-import-remapping/options.json | 3 ++ 5 files changed, 59 insertions(+) create mode 100644 test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/actual.js create mode 100644 test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/options.json create mode 100644 test/fixtures/transformation/es6-modules-common/disallow-import-remapping/actual.js create mode 100644 test/fixtures/transformation/es6-modules-common/disallow-import-remapping/options.json diff --git a/lib/6to5/transformation/modules/_default.js b/lib/6to5/transformation/modules/_default.js index d067eece2a..391ceda34f 100644 --- a/lib/6to5/transformation/modules/_default.js +++ b/lib/6to5/transformation/modules/_default.js @@ -9,7 +9,11 @@ function DefaultFormatter(file) { this.file = file; this.localExports = this.getLocalExports(); + this.localImports = this.getLocalImports(); + this.remapAssignments(); + + this.checkImportAssignments(); } DefaultFormatter.prototype.getLocalExports = function () { @@ -27,6 +31,51 @@ DefaultFormatter.prototype.getLocalExports = function () { return localExports; }; +DefaultFormatter.prototype.getLocalImports = function () { + var localImports = {}; + + traverse(this.file.ast, { + enter: function (node) { + if (t.isImportDeclaration(node)) { + _.extend(localImports, t.getIds(node, true)); + } + } + }); + + return localImports; +}; + +DefaultFormatter.prototype.checkImportAssignments = function () { + var localImports = this.localImports; + var file = this.file; + + var isLocalReference = function (node, scope) { + return t.isIdentifier(node) && localImports[node.name]; + }; + + var check = function (node) { + if (isLocalReference(node)) { + throw file.errorWithNode(node, "Illegal assignment of module import"); + } + }; + + traverse(file.ast, { + enter: function (node, parent, scope) { + if (t.isAssignmentExpression(node)) { + + var left = node.left; + if (t.isMemberExpression(left)) { + while (left.object) left = left.object; + } + + check(left); + } else if (t.isDeclaration(node) && !t.isImportDeclaration(node)) { + _.each(t.getIds(node, true), check); + } + } + }); +}; + DefaultFormatter.prototype.remapExportAssignment = function (node) { return t.assignmentExpression( "=", diff --git a/test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/actual.js b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/actual.js new file mode 100644 index 0000000000..1d832634ed --- /dev/null +++ b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/actual.js @@ -0,0 +1,2 @@ +import { foo } from "foo"; +var foo; diff --git a/test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/options.json b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/options.json new file mode 100644 index 0000000000..a9357a58d2 --- /dev/null +++ b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Illegal assignment of module import" +} diff --git a/test/fixtures/transformation/es6-modules-common/disallow-import-remapping/actual.js b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping/actual.js new file mode 100644 index 0000000000..e1b913ccf6 --- /dev/null +++ b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping/actual.js @@ -0,0 +1,2 @@ +import { foo } from "foo"; +foo = 1; diff --git a/test/fixtures/transformation/es6-modules-common/disallow-import-remapping/options.json b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping/options.json new file mode 100644 index 0000000000..a9357a58d2 --- /dev/null +++ b/test/fixtures/transformation/es6-modules-common/disallow-import-remapping/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Illegal assignment of module import" +}