Browse Source

assignments to properties of bindings is permitted

contingency-plan
Rich Harris 10 years ago
parent
commit
5972bf8346
  1. 23
      src/Statement.js
  2. 3
      test/function/legal-import-modification/_config.js
  3. 1
      test/function/legal-import-modification/foo.js
  4. 5
      test/function/legal-import-modification/main.js

23
src/Statement.js

@ -156,16 +156,29 @@ export default class Statement {
checkForWrites ( scope, node ) { checkForWrites ( scope, node ) {
const addNode = ( node, disallowImportReassignments ) => { const addNode = ( node, disallowImportReassignments ) => {
let depth = 0; // determine whether we're illegally modifying a binding or namespace
while ( node.type === 'MemberExpression' ) { while ( node.type === 'MemberExpression' ) {
node = node.object; node = node.object;
depth += 1;
} }
// disallow assignments/updates to imported bindings and namespaces // disallow assignments/updates to imported bindings and namespaces
if ( disallowImportReassignments && has( this.module.imports, node.name ) && !scope.contains( node.name ) ) { if ( disallowImportReassignments ) {
const err = new Error( `Illegal reassignment to import '${node.name}'` ); const importSpecifier = this.module.imports[ node.name ];
err.file = this.module.path;
err.loc = getLocation( this.module.magicString.toString(), node.start ); if ( importSpecifier && !scope.contains( node.name ) ) {
throw err; const minDepth = importSpecifier.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 '${node.name}'` );
err.file = this.module.path;
err.loc = getLocation( this.module.magicString.toString(), node.start );
throw err;
}
}
} }
if ( node.type !== 'Identifier' ) { if ( node.type !== 'Identifier' ) {

3
test/function/legal-import-modification/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'assigning to properties of imported bindings is permitted'
};

1
test/function/legal-import-modification/foo.js

@ -0,0 +1 @@
export default {};

5
test/function/legal-import-modification/main.js

@ -0,0 +1,5 @@
import foo from './foo';
foo.modified = true;
export default foo;
Loading…
Cancel
Save