From 1a0011485e912295df28bea534fd69abc823b701 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Fri, 22 Jan 2016 15:27:15 -0500 Subject: [PATCH] Skip dead branches of a conditional expression. This allows replacement of inline conditions such as in `export const resolve = isWindows ? win32Resolve : posixResolve;`. --- src/Module.js | 11 ++++++++++- test/function/skips-dead-branches-f/_config.js | 2 +- test/function/skips-dead-branches-g/_config.js | 9 +++++++++ test/function/skips-dead-branches-g/main.js | 6 ++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/function/skips-dead-branches-g/_config.js create mode 100644 test/function/skips-dead-branches-g/main.js diff --git a/src/Module.js b/src/Module.js index 0e42f77..ec195a3 100644 --- a/src/Module.js +++ b/src/Module.js @@ -300,7 +300,7 @@ export default class Module { } walk( ast, { - enter: node => { + enter: (node, parent, prop) => { // eliminate dead branches early if ( node.type === 'IfStatement' ) { if ( isFalsy( node.test ) ) { @@ -310,6 +310,15 @@ export default class Module { this.magicString.overwrite( node.alternate.start, node.alternate.end, '{}' ); node.alternate = emptyBlockStatement( node.alternate.start, node.alternate.end ); } + } else if ( node.type === 'ConditionalExpression' ) { + if ( isFalsy( node.test ) ) { + this.magicString.remove( node.start, node.alternate.start ); + parent[prop] = node.alternate; + } else if ( isTruthy( node.test ) ) { + this.magicString.remove( node.start, node.consequent.start ); + this.magicString.remove( node.consequent.end, node.end ); + parent[prop] = node.consequent; + } } this.magicString.addSourcemapLocation( node.start ); diff --git a/test/function/skips-dead-branches-f/_config.js b/test/function/skips-dead-branches-f/_config.js index 428bd69..e43c622 100644 --- a/test/function/skips-dead-branches-f/_config.js +++ b/test/function/skips-dead-branches-f/_config.js @@ -1,7 +1,7 @@ var assert = require( 'assert' ); module.exports = { - description: 'skips a dead branch (g)', + description: 'skips a dead branch (f)', code: function ( code ) { assert.equal( code.indexOf( 'obj.foo = function' ), -1, code ); } diff --git a/test/function/skips-dead-branches-g/_config.js b/test/function/skips-dead-branches-g/_config.js new file mode 100644 index 0000000..d1e507d --- /dev/null +++ b/test/function/skips-dead-branches-g/_config.js @@ -0,0 +1,9 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'skips a dead conditional expression branch (g)', + code: function ( code ) { + assert.ok( code.indexOf( 'var c = a;' ) >= 0, code ); + assert.ok( code.indexOf( 'var d = b;' ) >= 0, code ); + } +}; diff --git a/test/function/skips-dead-branches-g/main.js b/test/function/skips-dead-branches-g/main.js new file mode 100644 index 0000000..0c271f4 --- /dev/null +++ b/test/function/skips-dead-branches-g/main.js @@ -0,0 +1,6 @@ +var a = 0; +var b = 1; +var c = true ? a : b; +var d = false ? a : b; + +console.log( c + d );