Browse Source

distinguish between binding/property assignments for `export default foo` special case

contingency-plan
Rich-Harris 10 years ago
parent
commit
402cdc3360
  1. 2
      src/Statement.js
  2. 4
      test/form/unmodified-default-exports/_config.js
  3. 15
      test/form/unmodified-default-exports/_expected/amd.js
  4. 13
      test/form/unmodified-default-exports/_expected/cjs.js
  5. 11
      test/form/unmodified-default-exports/_expected/es6.js
  6. 15
      test/form/unmodified-default-exports/_expected/iife.js
  7. 19
      test/form/unmodified-default-exports/_expected/umd.js
  8. 11
      test/form/unmodified-default-exports/foo.js
  9. 3
      test/form/unmodified-default-exports/main.js

2
src/Statement.js

@ -179,7 +179,7 @@ export default class Statement {
// special case = `export default foo; foo += 1;` - we'll
// need to assign a new variable so that the exported
// value is not updated by the second statement
if ( this.module.exports.default && this.module.exports.default.identifier === node.name ) {
if ( this.module.exports.default && depth === 0 && this.module.exports.default.identifier === node.name ) {
// but only if this is a) inside a function body or
// b) after the export declaration
if ( !!scope.parent || node.start > this.module.exports.default.statement.node.start ) {

4
test/form/unmodified-default-exports/_config.js

@ -0,0 +1,4 @@
module.exports = {
description: 'does not treat property assignment as rebinding for sake of unbound default exports',
// solo: true
};

15
test/form/unmodified-default-exports/_expected/amd.js

@ -0,0 +1,15 @@
define(function () { 'use strict';
var Foo = function () {
this.isFoo = true;
};
Foo.prototype = {
answer: function () {
return 42;
}
};
var foo = new Foo();
});

13
test/form/unmodified-default-exports/_expected/cjs.js

@ -0,0 +1,13 @@
'use strict';
var Foo = function () {
this.isFoo = true;
};
Foo.prototype = {
answer: function () {
return 42;
}
};
var foo = new Foo();

11
test/form/unmodified-default-exports/_expected/es6.js

@ -0,0 +1,11 @@
var Foo = function () {
this.isFoo = true;
};
Foo.prototype = {
answer: function () {
return 42;
}
};
var foo = new Foo();

15
test/form/unmodified-default-exports/_expected/iife.js

@ -0,0 +1,15 @@
(function () { 'use strict';
var Foo = function () {
this.isFoo = true;
};
Foo.prototype = {
answer: function () {
return 42;
}
};
var foo = new Foo();
})();

19
test/form/unmodified-default-exports/_expected/umd.js

@ -0,0 +1,19 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(this, function () { 'use strict';
var Foo = function () {
this.isFoo = true;
};
Foo.prototype = {
answer: function () {
return 42;
}
};
var foo = new Foo();
}));

11
test/form/unmodified-default-exports/foo.js

@ -0,0 +1,11 @@
var Foo = function () {
this.isFoo = true;
};
export default Foo;
Foo.prototype = {
answer: function () {
return 42;
}
};

3
test/form/unmodified-default-exports/main.js

@ -0,0 +1,3 @@
import Foo from './foo';
var foo = new Foo();
Loading…
Cancel
Save