Browse Source

Throw if attempting to reassign namespace's name.

gh-109
Oskar Segersvärd 10 years ago
parent
commit
2aa575d0f5
  1. 16
      src/Statement.js

16
src/Statement.js

@ -15,6 +15,14 @@ function isFunctionDeclaration ( node, parent ) {
if ( node.type === 'FunctionExpression' && parent.type === 'VariableDeclarator' ) return true;
}
function chainedMemberExpression ( node ) {
if ( node.object.type === 'MemberExpression' ) {
return chainedMemberExpression( node.object ) + '.' + node.property.name;
}
return node.object.name + '.' + node.property.name;
}
export default class Statement {
constructor ( node, module, start, end ) {
this.node = node;
@ -163,6 +171,14 @@ export default class Statement {
namespace = id;
}
// If a namespace is the left hand side of an assignment, throw an error.
if ( parent.type === 'AssignmentExpression' && parent.left === node ) {
const err = new Error( `Illegal reassignment to import '${chainedMemberExpression( node )}'` );
err.file = this.module.id;
err.loc = getLocation( this.module.magicString.toString(), node.start );
throw err;
}
// Extract the name of the accessed property, from and Identifier or Literal.
// Any eventual Literal value is converted to a string.
const name = node.property.name ||

Loading…
Cancel
Save