mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
9 years ago
4 changed files with 44 additions and 52 deletions
@ -1,65 +1,39 @@ |
|||
function isEqualTest ( node ) { |
|||
return node.type === 'BinaryExpression' && ( node.operator === '===' || node.operator === '==' ); |
|||
} |
|||
|
|||
function isNotEqualTest ( node ) { |
|||
return node.type === 'BinaryExpression' && ( node.operator === '!==' || node.operator === '!=' ); |
|||
export function isTruthy ( node ) { |
|||
if ( node.type === 'Literal' ) return !!node.value; |
|||
if ( node.type === 'ParenthesizedExpression' ) return isTruthy( node.expression ); |
|||
if ( node.operator in operators ) return operators[ node.operator ]( node ); |
|||
} |
|||
|
|||
function nodesAreEqual ( a, b ) { |
|||
if ( a.type !== b.type ) return false; |
|||
if ( a.type === 'Literal' ) return a.value === b.value |
|||
if ( a.type === 'Identifier' ) return a.name === b.name && a.name !== 'NaN'; |
|||
|
|||
return false; |
|||
export function isFalsy ( node ) { |
|||
return not( isTruthy( node ) ); |
|||
} |
|||
|
|||
function nodesAreNotEqual ( a, b ) { |
|||
if ( a.type !== b.type ) return false; |
|||
if ( a.type === 'Literal' ) return a.value != b.value; |
|||
if ( a.type === 'Identifier' ) return a.name != b.name || a.name === 'NaN'; |
|||
|
|||
return false; |
|||
function not ( value ) { |
|||
return value === undefined ? value : !value; |
|||
} |
|||
|
|||
export function isTruthy ( node ) { |
|||
if ( node.type === 'Literal' && node.value ) return true; |
|||
if ( node.type === 'ParenthesizedExpression' ) return isTruthy( node.expression ); |
|||
|
|||
if ( isEqualTest( node ) ) return nodesAreEqual( node.left, node.right ); |
|||
if ( isNotEqualTest( node ) ) return nodesAreNotEqual( node.left, node.right ); |
|||
|
|||
if ( node.type === 'UnaryExpression' ) { |
|||
if ( node.operator === '!' ) return isFalsy( node.argument ); |
|||
return false; |
|||
function equals ( a, b, strict ) { |
|||
if ( a.type !== b.type ) return undefined; |
|||
if ( a.type === 'Identifier' ) return a.name === b.name && a.name !== 'NaN'; |
|||
if ( a.type === 'Literal' ) return strict ? a.value === b.value : a.value == b.value; |
|||
} |
|||
|
|||
if ( node.type === 'LogicalExpression' ) { |
|||
if ( node.operator === '&&' ) return isTruthy( node.left ) && isTruthy( node.right ); |
|||
if ( node.operator === '||' ) return isTruthy( node.left ) || isTruthy( node.right ); |
|||
return false; |
|||
} |
|||
const operators = { |
|||
'==': x => { |
|||
return equals( x.left, x.right, false ); |
|||
}, |
|||
|
|||
return false; |
|||
} |
|||
'!=': x => not( operators['==']( x ) ), |
|||
|
|||
export function isFalsy ( node ) { |
|||
if ( node.type === 'Literal' && !node.value ) return true; |
|||
if ( node.type === 'ParenthesizedExpression' ) return isFalsy( node.expression ); |
|||
'===': x => { |
|||
return equals( x.left, x.right, true ); |
|||
}, |
|||
|
|||
if ( isEqualTest( node ) ) return nodesAreNotEqual( node.left, node.right ); |
|||
if ( isNotEqualTest( node ) ) return nodesAreEqual( node.left, node.right ); |
|||
'!==': x => not( operators['===']( x ) ), |
|||
|
|||
if ( node.type === 'UnaryExpression' ) { |
|||
if ( node.operator === '!' ) return isTruthy( node.argument ); |
|||
return false; |
|||
} |
|||
'!': x => isFalsy( x.argument ), |
|||
|
|||
if ( node.type === 'LogicalExpression' ) { |
|||
if ( node.operator === '&&' ) return isFalsy( node.left ) || isFalsy( node.right ); |
|||
if ( node.operator === '||' ) return isFalsy( node.left ) && isFalsy( node.right ); |
|||
return false; |
|||
} |
|||
'&&': x => isTruthy( x.left ) && isTruthy( x.right ), |
|||
|
|||
return false; |
|||
} |
|||
'||': x => isTruthy( x.left ) || isTruthy( x.right ) |
|||
}; |
|||
|
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'skips a dead branch (g)', |
|||
code: function ( code ) { |
|||
assert.equal( code.indexOf( 'obj.foo = function' ), -1, code ); |
|||
} |
|||
} |
@ -0,0 +1,11 @@ |
|||
var obj = {}; |
|||
obj.foo = function () { |
|||
console.log( 'this should be excluded' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'this should be included' ); |
|||
} |
|||
|
|||
if ( 42 != '42' ) obj.foo(); |
|||
bar(); |
Loading…
Reference in new issue