Browse Source

fix shorthand properties

declarations-and-references
Rich-Harris 9 years ago
parent
commit
fcd43fab4b
  1. 8
      src/Module.js
  2. 9
      src/Statement.js
  3. 26
      test/form/shorthand-properties/_expected/amd.js
  4. 26
      test/form/shorthand-properties/_expected/cjs.js
  5. 26
      test/form/shorthand-properties/_expected/es6.js
  6. 26
      test/form/shorthand-properties/_expected/iife.js
  7. 26
      test/form/shorthand-properties/_expected/umd.js
  8. 7
      test/form/shorthand-properties/bar.js
  9. 8
      test/form/shorthand-properties/baz.js
  10. 13
      test/form/shorthand-properties/foo.js
  11. 8
      test/form/shorthand-properties/main.js

8
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 );
}
}
}
});

9
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 );

26
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' );
});

26
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' );

26
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' );

26
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' );
})();

26
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' );
}));

7
test/form/shorthand-properties/bar.js

@ -0,0 +1,7 @@
function x () {
return 'bar';
}
var bar = { x };
export { bar };

8
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 };

13
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 };

8
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' );

Loading…
Cancel
Save