rollup-plugin-commonjs
Advanced tools
Comparing version 4.0.1 to 4.1.0
# rollup-plugin-commonjs changelog | ||
## 4.1.0 | ||
* Ignore dead branches ([#93](https://github.com/rollup/rollup-plugin-commonjs/issues/93)) | ||
## 4.0.1 | ||
@@ -4,0 +8,0 @@ |
@@ -91,2 +91,41 @@ 'use strict'; | ||
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 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; | ||
} | ||
var operators = { | ||
'==': function (x) { | ||
return equals( x.left, x.right, false ); | ||
}, | ||
'!=': function (x) { return not( operators['==']( x ) ); }, | ||
'===': function (x) { | ||
return equals( x.left, x.right, true ); | ||
}, | ||
'!==': function (x) { return not( operators['===']( x ) ); }, | ||
'!': function (x) { return isFalsy( x.argument ); }, | ||
'&&': function (x) { return isTruthy( x.left ) && isTruthy( x.right ); }, | ||
'||': function (x) { return isTruthy( x.left ) || isTruthy( x.right ); } | ||
}; | ||
function getName ( id ) { | ||
@@ -151,2 +190,8 @@ return rollupPluginutils.makeLegalIdentifier( path.basename( id, path.extname( id ) ) ); | ||
enter: function enter ( node, parent ) { | ||
// skip dead branches | ||
if ( parent && ( parent.type === 'IfStatement' || parent.type === 'ConditionalExpression' ) ) { | ||
if ( node === parent.consequent && isFalsy( parent.test ) ) return this.skip(); | ||
if ( node === parent.alternate && isTruthy( parent.test ) ) return this.skip(); | ||
} | ||
if ( node.scope ) scope = node.scope; | ||
@@ -153,0 +198,0 @@ if ( /^Function/.test( node.type ) ) scopeDepth += 1; |
@@ -88,2 +88,41 @@ import { statSync } from 'fs'; | ||
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 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; | ||
} | ||
var operators = { | ||
'==': function (x) { | ||
return equals( x.left, x.right, false ); | ||
}, | ||
'!=': function (x) { return not( operators['==']( x ) ); }, | ||
'===': function (x) { | ||
return equals( x.left, x.right, true ); | ||
}, | ||
'!==': function (x) { return not( operators['===']( x ) ); }, | ||
'!': function (x) { return isFalsy( x.argument ); }, | ||
'&&': function (x) { return isTruthy( x.left ) && isTruthy( x.right ); }, | ||
'||': function (x) { return isTruthy( x.left ) || isTruthy( x.right ); } | ||
}; | ||
function getName ( id ) { | ||
@@ -148,2 +187,8 @@ return makeLegalIdentifier( basename( id, extname( id ) ) ); | ||
enter: function enter ( node, parent ) { | ||
// skip dead branches | ||
if ( parent && ( parent.type === 'IfStatement' || parent.type === 'ConditionalExpression' ) ) { | ||
if ( node === parent.consequent && isFalsy( parent.test ) ) return this.skip(); | ||
if ( node === parent.alternate && isTruthy( parent.test ) ) return this.skip(); | ||
} | ||
if ( node.scope ) scope = node.scope; | ||
@@ -150,0 +195,0 @@ if ( /^Function/.test( node.type ) ) scopeDepth += 1; |
{ | ||
"name": "rollup-plugin-commonjs", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Convert CommonJS modules to ES2015", | ||
@@ -8,2 +8,3 @@ "devDependencies": { | ||
"eslint": "^2.13.1", | ||
"locate-character": "^2.0.0", | ||
"mocha": "^2.5.3", | ||
@@ -10,0 +11,0 @@ "rollup": "^0.33.0", |
@@ -34,1 +34,40 @@ export function isReference ( node, parent ) { | ||
} | ||
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 ) | ||
}; |
@@ -5,3 +5,3 @@ import acorn from 'acorn'; | ||
import { attachScopes, makeLegalIdentifier } from 'rollup-pluginutils'; | ||
import { flatten, isReference } from './ast-utils.js'; | ||
import { flatten, isReference, isTruthy, isFalsy } from './ast-utils.js'; | ||
import { PREFIX, HELPERS_ID } from './helpers.js'; | ||
@@ -65,2 +65,8 @@ import { getName } from './utils.js'; | ||
enter ( node, parent ) { | ||
// skip dead branches | ||
if ( parent && ( parent.type === 'IfStatement' || parent.type === 'ConditionalExpression' ) ) { | ||
if ( node === parent.consequent && isFalsy( parent.test ) ) return this.skip(); | ||
if ( node === parent.alternate && isTruthy( parent.test ) ) return this.skip(); | ||
} | ||
if ( node.scope ) scope = node.scope; | ||
@@ -67,0 +73,0 @@ if ( /^Function/.test( node.type ) ) scopeDepth += 1; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122548
1192
9