Browse Source

Prune dead conditional expressions more carefully.

Fixes #534.
gh-669
Brian Donovan 9 years ago
parent
commit
ab76b617b4
  1. 15
      src/Module.js
  2. 3
      test/function/trim-conditional-branches-in-exports/_config.js
  3. 2
      test/function/trim-conditional-branches-in-exports/foo.js
  4. 2
      test/function/trim-conditional-branches-in-exports/main.js

15
src/Module.js

@ -304,7 +304,7 @@ export default class Module {
} }
walk( ast, { walk( ast, {
enter: (node, parent, prop) => { enter: node => {
// eliminate dead branches early // eliminate dead branches early
if ( node.type === 'IfStatement' ) { if ( node.type === 'IfStatement' ) {
if ( isFalsy( node.test ) ) { if ( isFalsy( node.test ) ) {
@ -314,7 +314,15 @@ export default class Module {
this.magicString.overwrite( node.alternate.start, node.alternate.end, '{}' ); this.magicString.overwrite( node.alternate.start, node.alternate.end, '{}' );
node.alternate = emptyBlockStatement( node.alternate.start, node.alternate.end ); node.alternate = emptyBlockStatement( node.alternate.start, node.alternate.end );
} }
} else if ( node.type === 'ConditionalExpression' ) { }
this.magicString.addSourcemapLocation( node.start );
this.magicString.addSourcemapLocation( node.end );
},
leave: ( node, parent, prop ) => {
// eliminate dead branches early
if ( node.type === 'ConditionalExpression' ) {
if ( isFalsy( node.test ) ) { if ( isFalsy( node.test ) ) {
this.magicString.remove( node.start, node.alternate.start ); this.magicString.remove( node.start, node.alternate.start );
parent[prop] = node.alternate; parent[prop] = node.alternate;
@ -324,9 +332,6 @@ export default class Module {
parent[prop] = node.consequent; parent[prop] = node.consequent;
} }
} }
this.magicString.addSourcemapLocation( node.start );
this.magicString.addSourcemapLocation( node.end );
} }
}); });

3
test/function/trim-conditional-branches-in-exports/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'trims conditional branches with a renamed default export'
};

2
test/function/trim-conditional-branches-in-exports/foo.js

@ -0,0 +1,2 @@
var foo = 0;
export default Math.random() < 0.5 ? foo : foo;

2
test/function/trim-conditional-branches-in-exports/main.js

@ -0,0 +1,2 @@
import foo from './foo.js';
console.log( true ? false ? foo : 0 : 1 );
Loading…
Cancel
Save