Browse Source

Merge pull request #63 from rollup/gh-61

expand shorthand properties (#61)
contingency-plan
Oskar Segersvärd 9 years ago
parent
commit
7978ade77c
  1. 22
      src/Statement.js
  2. 3
      test/function/shorthand-properties/_config.js
  3. 7
      test/function/shorthand-properties/foo.js
  4. 8
      test/function/shorthand-properties/main.js

22
src/Statement.js

@ -327,18 +327,28 @@ export default class Statement {
replacementStack.push( newNames );
}
// We want to rewrite identifiers (that aren't property names etc)
if ( node.type !== 'Identifier' ) return;
// if there's no replacement, or it's the same, there's nothing more to do
const name = names[ node.name ];
if ( !name || name === node.name ) return;
// shorthand properties (`obj = { foo }`) need to be expanded
if ( parent.type === 'Property' && parent.shorthand ) {
magicString.insert( node.end, `: ${name}` );
parent.key._skip = true;
parent.value._skip = true; // redundant, but defensive
return;
}
// property names etc can be disregarded
if ( parent.type === 'MemberExpression' && !parent.computed && node !== parent.object ) return;
if ( parent.type === 'Property' && node !== parent.value ) return;
if ( parent.type === 'MethodDefinition' && node === parent.key ) return;
// TODO others...?
const name = names[ node.name ];
if ( name && name !== node.name ) {
magicString.overwrite( node.start, node.end, name );
}
// all other identifiers should be overwritten
magicString.overwrite( node.start, node.end, name );
},
leave ( node ) {

3
test/function/shorthand-properties/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'expands shorthand properties as necessary (#61)'
};

7
test/function/shorthand-properties/foo.js

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

8
test/function/shorthand-properties/main.js

@ -0,0 +1,8 @@
import { foo } from './foo';
function bar () {
return 'main-bar';
}
assert.equal( bar(), 'main-bar' );
assert.equal( foo.bar(), 'foo-bar' );
Loading…
Cancel
Save