make import reassignment illegal @eventualbuddha
This commit is contained in:
parent
2a09c0a5a5
commit
1cc606d4d0
@ -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(
|
||||
"=",
|
||||
|
||||
2
test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/actual.js
vendored
Normal file
2
test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import { foo } from "foo";
|
||||
var foo;
|
||||
3
test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/options.json
vendored
Normal file
3
test/fixtures/transformation/es6-modules-common/disallow-import-remapping-2/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Illegal assignment of module import"
|
||||
}
|
||||
2
test/fixtures/transformation/es6-modules-common/disallow-import-remapping/actual.js
vendored
Normal file
2
test/fixtures/transformation/es6-modules-common/disallow-import-remapping/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import { foo } from "foo";
|
||||
foo = 1;
|
||||
3
test/fixtures/transformation/es6-modules-common/disallow-import-remapping/options.json
vendored
Normal file
3
test/fixtures/transformation/es6-modules-common/disallow-import-remapping/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Illegal assignment of module import"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user