Browse Source

ensure reference is only marked as reassignment if it is the subject of the reassignment – fixes #648

gh-669
Rich Harris 9 years ago
parent
commit
82112f7528
  1. 37
      src/Statement.js
  2. 9
      test/function/export-two-ways-function/_config.js
  3. 8
      test/function/export-two-ways-function/main.js

37
src/Statement.js

@ -65,29 +65,32 @@ export default class Statement {
if ( parent && isModifierNode( parent ) ) {
let subject = parent[ modifierNodes[ parent.type ] ];
let depth = 0;
while ( subject.type === 'MemberExpression' ) {
subject = subject.object;
depth += 1;
}
if ( node === subject ) {
let depth = 0;
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.original, subject.start );
throw err;
}
}
}
isReassignment = !depth;
isReassignment = !depth;
}
}
if ( isReference( node, parent ) ) {

9
test/function/export-two-ways-function/_config.js

@ -0,0 +1,9 @@
var assert = require( 'assert' );
module.exports = {
description: 'exports the same function more than one way (#648)',
exports: function ( exports ) {
assert.equal( exports.foo, exports.bar );
assert.equal( exports.foo(), 42 );
}
};

8
test/function/export-two-ways-function/main.js

@ -0,0 +1,8 @@
var bar;
bar = foo;
function foo () {
return 42;
}
export { foo, bar };
Loading…
Cancel
Save