make import reassignment illegal @eventualbuddha

This commit is contained in:
Sebastian McKenzie 2015-01-07 07:58:04 +11:00
parent 2a09c0a5a5
commit 1cc606d4d0
5 changed files with 59 additions and 0 deletions

View File

@ -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(
"=",

View File

@ -0,0 +1,2 @@
import { foo } from "foo";
var foo;

View File

@ -0,0 +1,3 @@
{
"throws": "Illegal assignment of module import"
}

View File

@ -0,0 +1,2 @@
import { foo } from "foo";
foo = 1;

View File

@ -0,0 +1,3 @@
{
"throws": "Illegal assignment of module import"
}