From a66cad653caeff0aec8eef4bc64e8840d7f59a08 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 4 Oct 2015 15:08:20 -0400 Subject: [PATCH] prevent illegal reassignments, part 2 --- src/Statement.js | 49 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Statement.js b/src/Statement.js index 0819671..4c63e6c 100644 --- a/src/Statement.js +++ b/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` }