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

Loading…
Cancel
Save