Browse Source

disallow reassignments to namespaces

declarations-and-references
Rich-Harris 9 years ago
parent
commit
c74c18dcc9
  1. 42
      src/Statement.js

42
src/Statement.js

@ -109,33 +109,31 @@ export default class Statement {
const reference = new Reference( node, scope );
references.push( reference );
if ( node.type === 'Identifier' ) {
if ( parent.type in modifierNodes ) {
let subject = parent[ modifierNodes[ parent.type ] ];
let depth = 0;
while ( subject.type === 'MemberExpression' ) {
subject = subject.object;
depth += 1;
}
if ( parent.type in modifierNodes ) {
let subject = parent[ modifierNodes[ parent.type ] ];
let depth = 0;
const importDeclaration = module.imports[ subject.name ];
while ( subject.type === 'MemberExpression' ) {
subject = subject.object;
depth += 1;
}
if ( !scope.contains( subject.name ) && importDeclaration ) {
const minDepth = importDeclaration.name === '*' ?
2 : // cannot do e.g. `namespace.foo = bar`
1; // cannot do e.g. `foo = bar`, but `foo.bar = bar` is fine
const importDeclaration = module.imports[ subject.name ];
if ( depth < minDepth ) {
const err = new Error( `Illegal reassignment to import '${subject.name}'` );
err.file = module.id;
err.loc = getLocation( module.magicString.toString(), subject.start );
throw err;
}
}
if ( !scope.contains( subject.name ) && importDeclaration ) {
const minDepth = importDeclaration.name === '*' ?
2 : // cannot do e.g. `namespace.foo = bar`
1; // cannot do e.g. `foo = bar`, but `foo.bar = bar` is fine
reference.isReassignment = true;
if ( depth < minDepth ) {
const err = new Error( `Illegal reassignment to import '${subject.name}'` );
err.file = module.id;
err.loc = getLocation( module.magicString.toString(), subject.start );
throw err;
}
}
reference.isReassignment = !depth;
}
reference.isImmediatelyUsed = !readDepth;

Loading…
Cancel
Save