mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
9 years ago
14 changed files with 177 additions and 0 deletions
@ -0,0 +1,65 @@ |
|||||
|
function isEqualTest ( node ) { |
||||
|
return node.type === 'BinaryExpression' && ( node.operator === '===' || node.operator === '==' ); |
||||
|
} |
||||
|
|
||||
|
function isNotEqualTest ( node ) { |
||||
|
return node.type === 'BinaryExpression' && ( node.operator === '!==' || node.operator === '!=' ); |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
export function isFalsy ( node ) { |
||||
|
if ( node.type === 'Literal' && !node.value ) return true; |
||||
|
if ( node.type === 'ParenthesizedExpression' ) return isFalsy( node.expression ); |
||||
|
|
||||
|
if ( isEqualTest( node ) ) return nodesAreNotEqual( node.left, node.right ); |
||||
|
if ( isNotEqualTest( node ) ) return nodesAreEqual( node.left, node.right ); |
||||
|
|
||||
|
if ( node.type === 'UnaryExpression' ) { |
||||
|
if ( node.operator === '!' ) return isTruthy( node.argument ); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
export function emptyBlockStatement ( start, end ) { |
||||
|
return { |
||||
|
start, end, |
||||
|
type: 'BlockStatement', |
||||
|
body: [] |
||||
|
}; |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'skips a dead branch (b)', |
||||
|
code: function ( code ) { |
||||
|
assert.equal( code.indexOf( 'function foo' ), -1, code ); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
function foo () { |
||||
|
console.log( 'this should be excluded' ); |
||||
|
} |
||||
|
|
||||
|
function bar () { |
||||
|
console.log( 'this should be included' ); |
||||
|
} |
||||
|
|
||||
|
if ( true ) bar(); |
||||
|
else foo(); |
@ -0,0 +1,8 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'skips a dead branch (c)', |
||||
|
code: function ( code ) { |
||||
|
assert.equal( code.indexOf( 'function foo' ), -1, code ); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
function foo () { |
||||
|
console.log( 'this should be excluded' ); |
||||
|
} |
||||
|
|
||||
|
function bar () { |
||||
|
console.log( 'this should be included' ); |
||||
|
} |
||||
|
|
||||
|
if ( !true ) foo(); |
||||
|
bar(); |
@ -0,0 +1,8 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'skips a dead branch (c)', |
||||
|
code: function ( code ) { |
||||
|
assert.equal( code.indexOf( 'function foo' ), -1, code ); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
function foo () { |
||||
|
console.log( 'this should be excluded' ); |
||||
|
} |
||||
|
|
||||
|
function bar () { |
||||
|
console.log( 'this should be included' ); |
||||
|
} |
||||
|
|
||||
|
if ( 'development' === 'production' ) foo(); |
||||
|
bar(); |
@ -0,0 +1,8 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'skips a dead branch (c)', |
||||
|
code: function ( code ) { |
||||
|
assert.equal( code.indexOf( 'function foo' ), -1, code ); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
function foo () { |
||||
|
console.log( 'this should be excluded' ); |
||||
|
} |
||||
|
|
||||
|
function bar () { |
||||
|
console.log( 'this should be included' ); |
||||
|
} |
||||
|
|
||||
|
if ( 'production' !== 'production' ) foo(); |
||||
|
bar(); |
@ -0,0 +1,8 @@ |
|||||
|
var assert = require( 'assert' ); |
||||
|
|
||||
|
module.exports = { |
||||
|
description: 'skips a dead branch', |
||||
|
code: function ( code ) { |
||||
|
assert.equal( code.indexOf( 'function foo' ), -1, code ); |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
function foo () { |
||||
|
console.log( 'this should be excluded' ); |
||||
|
} |
||||
|
|
||||
|
function bar () { |
||||
|
console.log( 'this should be included' ); |
||||
|
} |
||||
|
|
||||
|
if ( false ) foo(); |
||||
|
else bar(); |
Loading…
Reference in new issue