From c74c18dcc9758a54ad5af6d77a19696970fc9cfc Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 4 Oct 2015 14:14:20 -0400 Subject: [PATCH] disallow reassignments to namespaces --- src/Statement.js | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/Statement.js b/src/Statement.js index 27f1f36..0819671 100644 --- a/src/Statement.js +++ b/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;