mirror of https://github.com/lukechilds/rollup.git
Rich Harris
9 years ago
38 changed files with 301 additions and 27 deletions
@ -0,0 +1,38 @@ |
|||
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 ); |
|||
} |
|||
|
|||
export function isFalsy ( node ) { |
|||
return not( isTruthy( node ) ); |
|||
} |
|||
|
|||
function not ( value ) { |
|||
return value === undefined ? value : !value; |
|||
} |
|||
|
|||
function equals ( a, b, strict ) { |
|||
if ( a.type !== b.type ) return undefined; |
|||
if ( a.type === 'Literal' ) return strict ? a.value === b.value : a.value == b.value; |
|||
} |
|||
|
|||
const operators = { |
|||
'==': x => { |
|||
return equals( x.left, x.right, false ); |
|||
}, |
|||
|
|||
'!=': x => not( operators['==']( x ) ), |
|||
|
|||
'===': x => { |
|||
return equals( x.left, x.right, true ); |
|||
}, |
|||
|
|||
'!==': x => not( operators['===']( x ) ), |
|||
|
|||
'!': x => isFalsy( x.argument ), |
|||
|
|||
'&&': x => isTruthy( x.left ) && isTruthy( x.right ), |
|||
|
|||
'||': x => isTruthy( x.left ) || isTruthy( x.right ) |
|||
}; |
@ -0,0 +1,7 @@ |
|||
export function emptyBlockStatement ( start, end ) { |
|||
return { |
|||
start, end, |
|||
type: 'BlockStatement', |
|||
body: [] |
|||
}; |
|||
} |
@ -0,0 +1,10 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 're-exports are kept up-to-date', |
|||
exports: function ( exports ) { |
|||
assert.equal( exports.count, 0 ); |
|||
exports.incr(); |
|||
assert.equal( exports.count, 1 ); |
|||
} |
|||
}; |
@ -0,0 +1,10 @@ |
|||
export var count = 0; |
|||
|
|||
export function conflict () { |
|||
var foo = 0, |
|||
count = 42; |
|||
} |
|||
|
|||
export function incr () { |
|||
count += 1; |
|||
} |
@ -0,0 +1 @@ |
|||
export {count, incr, conflict} from './count'; |
@ -0,0 +1,10 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 're-exports are kept up-to-date', |
|||
exports: function ( exports ) { |
|||
assert.equal( exports.count, 0 ); |
|||
exports.incr(); |
|||
assert.equal( exports.count, 1 ); |
|||
} |
|||
}; |
@ -0,0 +1,5 @@ |
|||
export var count = 0; |
|||
|
|||
export function incr () { |
|||
count += 1; |
|||
} |
@ -0,0 +1 @@ |
|||
export {count, incr} from './count'; |
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'export from does not create a local binding', |
|||
runtimeError: function ( err ) { |
|||
assert.ok( /foo is not defined/.test( err.message ) ); |
|||
} |
|||
}; |
@ -0,0 +1 @@ |
|||
export default function() {} |
@ -0,0 +1,3 @@ |
|||
export {default as foo} from './foo'; |
|||
|
|||
export var foo1 = foo(); // This should fail as foo lacks a local binding.
|
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'skips a dead branch (b)', |
|||
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 ( true ) bar(); |
|||
else obj.foo(); |
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'skips a dead branch (c)', |
|||
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 ( !true ) obj.foo(); |
|||
bar(); |
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'skips a dead branch (d)', |
|||
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 ( 'development' === 'production' ) obj.foo(); |
|||
bar(); |
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'skips a dead branch (e)', |
|||
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 ( 'production' !== 'production' ) obj.foo(); |
|||
bar(); |
@ -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(); |
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'skips a dead branch', |
|||
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 ( false ) obj.foo(); |
|||
else bar(); |
@ -0,0 +1,24 @@ |
|||
var Promise = require( 'es6-promise' ).Promise; |
|||
|
|||
module.exports = { |
|||
description: 'transformers can be asynchronous', |
|||
options: { |
|||
plugins: [ |
|||
{ |
|||
transform: function ( code ) { |
|||
return Promise.resolve( code.replace( 'x', 1 ) ); |
|||
} |
|||
}, |
|||
{ |
|||
transform: function ( code ) { |
|||
return code.replace( '1', 2 ); |
|||
} |
|||
}, |
|||
{ |
|||
transform: function ( code ) { |
|||
return Promise.resolve( code.replace( '2', 3 ) ); |
|||
} |
|||
} |
|||
] |
|||
} |
|||
}; |
@ -0,0 +1 @@ |
|||
assert.equal( x, 3 ); |
Loading…
Reference in new issue