From 402cdc33608692d64ac8b0108e16fd49b69d68fa Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 6 Jun 2015 17:06:56 -0400 Subject: [PATCH] distinguish between binding/property assignments for `export default foo` special case --- src/Statement.js | 2 +- .../unmodified-default-exports/_config.js | 4 ++++ .../_expected/amd.js | 15 +++++++++++++++ .../_expected/cjs.js | 13 +++++++++++++ .../_expected/es6.js | 11 +++++++++++ .../_expected/iife.js | 15 +++++++++++++++ .../_expected/umd.js | 19 +++++++++++++++++++ test/form/unmodified-default-exports/foo.js | 11 +++++++++++ test/form/unmodified-default-exports/main.js | 3 +++ 9 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 test/form/unmodified-default-exports/_config.js create mode 100644 test/form/unmodified-default-exports/_expected/amd.js create mode 100644 test/form/unmodified-default-exports/_expected/cjs.js create mode 100644 test/form/unmodified-default-exports/_expected/es6.js create mode 100644 test/form/unmodified-default-exports/_expected/iife.js create mode 100644 test/form/unmodified-default-exports/_expected/umd.js create mode 100644 test/form/unmodified-default-exports/foo.js create mode 100644 test/form/unmodified-default-exports/main.js diff --git a/src/Statement.js b/src/Statement.js index b352eaf..85bb920 100644 --- a/src/Statement.js +++ b/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 ) { diff --git a/test/form/unmodified-default-exports/_config.js b/test/form/unmodified-default-exports/_config.js new file mode 100644 index 0000000..b933cae --- /dev/null +++ b/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 +}; diff --git a/test/form/unmodified-default-exports/_expected/amd.js b/test/form/unmodified-default-exports/_expected/amd.js new file mode 100644 index 0000000..2ddbff5 --- /dev/null +++ b/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(); + +}); diff --git a/test/form/unmodified-default-exports/_expected/cjs.js b/test/form/unmodified-default-exports/_expected/cjs.js new file mode 100644 index 0000000..0ab59dc --- /dev/null +++ b/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(); diff --git a/test/form/unmodified-default-exports/_expected/es6.js b/test/form/unmodified-default-exports/_expected/es6.js new file mode 100644 index 0000000..a298bb9 --- /dev/null +++ b/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(); diff --git a/test/form/unmodified-default-exports/_expected/iife.js b/test/form/unmodified-default-exports/_expected/iife.js new file mode 100644 index 0000000..2942fdd --- /dev/null +++ b/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(); + +})(); diff --git a/test/form/unmodified-default-exports/_expected/umd.js b/test/form/unmodified-default-exports/_expected/umd.js new file mode 100644 index 0000000..63cf07d --- /dev/null +++ b/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(); + +})); diff --git a/test/form/unmodified-default-exports/foo.js b/test/form/unmodified-default-exports/foo.js new file mode 100644 index 0000000..08ca0b2 --- /dev/null +++ b/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; + } +}; diff --git a/test/form/unmodified-default-exports/main.js b/test/form/unmodified-default-exports/main.js new file mode 100644 index 0000000..ebe4279 --- /dev/null +++ b/test/form/unmodified-default-exports/main.js @@ -0,0 +1,3 @@ +import Foo from './foo'; + +var foo = new Foo();