Browse Source

prevent illegal reassignments, part 2

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

49
src/Statement.js

@ -105,38 +105,41 @@ export default class Statement {
return this.skip();
}
if ( isReference( node, parent ) ) {
const reference = new Reference( node, scope );
references.push( reference );
let isReassignment;
if ( parent.type in modifierNodes ) {
let subject = parent[ modifierNodes[ parent.type ] ];
let depth = 0;
if ( parent && parent.type in modifierNodes ) {
let subject = parent[ modifierNodes[ parent.type ] ];
let depth = 0;
while ( subject.type === 'MemberExpression' ) {
subject = subject.object;
depth += 1;
}
while ( subject.type === 'MemberExpression' ) {
subject = subject.object;
depth += 1;
}
const importDeclaration = module.imports[ subject.name ];
const importDeclaration = module.imports[ subject.name ];
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
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
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 ( 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;
}
isReassignment = !depth;
}
if ( isReference( node, parent ) ) {
const reference = new Reference( node, scope );
references.push( reference );
reference.isImmediatelyUsed = !readDepth;
reference.isReassignment = isReassignment;
this.skip(); // don't descend from `foo.bar.baz` into `foo.bar`
}

Loading…
Cancel
Save