diff --git a/src/Module.js b/src/Module.js index 455b98e..d9ba8be 100644 --- a/src/Module.js +++ b/src/Module.js @@ -438,11 +438,15 @@ export default class Module { const declaration = reference.declaration; if ( reference.declaration ) { - const { start } = reference.node; + const { start, end } = reference.node; const name = declaration.render( es6 ); if ( reference.name !== name ) { - magicString.overwrite( start, start + reference.name.length, name, true ); + if ( reference.isShorthandProperty ) { + magicString.insert( end, `: ${name}` ); + } else { + magicString.overwrite( start, start + reference.name.length, name, true ); + } } } }); diff --git a/src/Statement.js b/src/Statement.js index 171b177..5f5b5b5 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -89,6 +89,15 @@ export default class Statement { enter ( node, parent ) { if ( node._scope ) scope = node._scope; + // special case – shorthand properties. because node.key === node.value, + // we can't differentiate once we've descended into the node + if ( node.type === 'Property' && node.shorthand ) { + const reference = new Reference( node.key, scope ); + reference.isShorthandProperty = true; // TODO feels a bit kludgy + references.push( reference ); + return this.skip(); + } + if ( isReference( node, parent ) ) { const reference = new Reference( node, scope ); references.push( reference ); diff --git a/test/form/shorthand-properties/_expected/amd.js b/test/form/shorthand-properties/_expected/amd.js index da84ec6..73e20c0 100644 --- a/test/form/shorthand-properties/_expected/amd.js +++ b/test/form/shorthand-properties/_expected/amd.js @@ -1,19 +1,25 @@ define(function () { 'use strict'; - function bar$1 () { - return 'main-bar'; + function x () { + return 'foo'; } - function bar () { - return 'foo-bar'; + var foo = { x }; + + function x$1 () { + return 'bar'; + } + + var bar = { x: x$1 }; + + function x$2 () { + return 'baz'; } - var foo = { - bar, - baz: bar$1 - }; + var baz = { x: x$2 }; - assert.equal( bar$1(), 'main-bar' ); - assert.equal( foo.bar(), 'foo-bar' ); + assert.equal( foo.x(), 'foo' ); + assert.equal( bar.x(), 'bar' ); + assert.equal( baz.x(), 'baz' ); }); diff --git a/test/form/shorthand-properties/_expected/cjs.js b/test/form/shorthand-properties/_expected/cjs.js index a620970..ec18995 100644 --- a/test/form/shorthand-properties/_expected/cjs.js +++ b/test/form/shorthand-properties/_expected/cjs.js @@ -1,17 +1,23 @@ 'use strict'; -function bar$1 () { - return 'main-bar'; +function x () { + return 'foo'; } -function bar () { - return 'foo-bar'; +var foo = { x }; + +function x$1 () { + return 'bar'; +} + +var bar = { x: x$1 }; + +function x$2 () { + return 'baz'; } -var foo = { - bar, - baz: bar$1 -}; +var baz = { x: x$2 }; -assert.equal( bar$1(), 'main-bar' ); -assert.equal( foo.bar(), 'foo-bar' ); +assert.equal( foo.x(), 'foo' ); +assert.equal( bar.x(), 'bar' ); +assert.equal( baz.x(), 'baz' ); diff --git a/test/form/shorthand-properties/_expected/es6.js b/test/form/shorthand-properties/_expected/es6.js index 8d1e6b3..f6d31d4 100644 --- a/test/form/shorthand-properties/_expected/es6.js +++ b/test/form/shorthand-properties/_expected/es6.js @@ -1,15 +1,21 @@ -function bar$1 () { - return 'main-bar'; +function x () { + return 'foo'; } -function bar () { - return 'foo-bar'; +var foo = { x }; + +function x$1 () { + return 'bar'; +} + +var bar = { x: x$1 }; + +function x$2 () { + return 'baz'; } -var foo = { - bar, - baz: bar$1 -}; +var baz = { x: x$2 }; -assert.equal( bar$1(), 'main-bar' ); -assert.equal( foo.bar(), 'foo-bar' ); +assert.equal( foo.x(), 'foo' ); +assert.equal( bar.x(), 'bar' ); +assert.equal( baz.x(), 'baz' ); diff --git a/test/form/shorthand-properties/_expected/iife.js b/test/form/shorthand-properties/_expected/iife.js index 2fc24d7..d5ba3c7 100644 --- a/test/form/shorthand-properties/_expected/iife.js +++ b/test/form/shorthand-properties/_expected/iife.js @@ -1,19 +1,25 @@ (function () { 'use strict'; - function bar$1 () { - return 'main-bar'; + function x () { + return 'foo'; } - function bar () { - return 'foo-bar'; + var foo = { x }; + + function x$1 () { + return 'bar'; + } + + var bar = { x: x$1 }; + + function x$2 () { + return 'baz'; } - var foo = { - bar, - baz: bar$1 - }; + var baz = { x: x$2 }; - assert.equal( bar$1(), 'main-bar' ); - assert.equal( foo.bar(), 'foo-bar' ); + assert.equal( foo.x(), 'foo' ); + assert.equal( bar.x(), 'bar' ); + assert.equal( baz.x(), 'baz' ); })(); diff --git a/test/form/shorthand-properties/_expected/umd.js b/test/form/shorthand-properties/_expected/umd.js index e4b7bde..b027a94 100644 --- a/test/form/shorthand-properties/_expected/umd.js +++ b/test/form/shorthand-properties/_expected/umd.js @@ -4,20 +4,26 @@ factory(); }(this, function () { 'use strict'; - function bar$1 () { - return 'main-bar'; + function x () { + return 'foo'; } - function bar () { - return 'foo-bar'; + var foo = { x }; + + function x$1 () { + return 'bar'; + } + + var bar = { x: x$1 }; + + function x$2 () { + return 'baz'; } - var foo = { - bar, - baz: bar$1 - }; + var baz = { x: x$2 }; - assert.equal( bar$1(), 'main-bar' ); - assert.equal( foo.bar(), 'foo-bar' ); + assert.equal( foo.x(), 'foo' ); + assert.equal( bar.x(), 'bar' ); + assert.equal( baz.x(), 'baz' ); })); diff --git a/test/form/shorthand-properties/bar.js b/test/form/shorthand-properties/bar.js new file mode 100644 index 0000000..f8f3758 --- /dev/null +++ b/test/form/shorthand-properties/bar.js @@ -0,0 +1,7 @@ +function x () { + return 'bar'; +} + +var bar = { x }; + +export { bar }; diff --git a/test/form/shorthand-properties/baz.js b/test/form/shorthand-properties/baz.js index d826bfa..ee1d362 100644 --- a/test/form/shorthand-properties/baz.js +++ b/test/form/shorthand-properties/baz.js @@ -1,3 +1,7 @@ -export default function bar () { - return 'main-bar'; +function x () { + return 'baz'; } + +var baz = { x }; + +export { baz }; diff --git a/test/form/shorthand-properties/foo.js b/test/form/shorthand-properties/foo.js index 831360e..1fa770d 100644 --- a/test/form/shorthand-properties/foo.js +++ b/test/form/shorthand-properties/foo.js @@ -1,10 +1,7 @@ -import baz from './baz.js'; - -function bar () { - return 'foo-bar'; +function x () { + return 'foo'; } -export var foo = { - bar, - baz -}; +var foo = { x }; + +export { foo }; diff --git a/test/form/shorthand-properties/main.js b/test/form/shorthand-properties/main.js index 754fba8..6774a8e 100644 --- a/test/form/shorthand-properties/main.js +++ b/test/form/shorthand-properties/main.js @@ -1,5 +1,7 @@ -import bar from './baz.js'; import { foo } from './foo'; +import { bar } from './bar'; +import { baz } from './baz'; -assert.equal( bar(), 'main-bar' ); -assert.equal( foo.bar(), 'foo-bar' ); +assert.equal( foo.x(), 'foo' ); +assert.equal( bar.x(), 'bar' ); +assert.equal( baz.x(), 'baz' );