Browse Source
Improves tree-shaking by only considering program-level
assignments as a Bundle's dependent expressions.
gh-1132
Daniel Tschinder
8 years ago
No known key found for this signature in database
GPG Key ID: 46A883CAEC8A94BD
30 changed files with
179 additions and
13 deletions
-
src/ast/nodes/AssignmentExpression.js
-
src/ast/nodes/CallExpression.js
-
src/ast/utils/isProgramLevel.js
-
test/form/side-effect-r/_config.js
-
test/form/side-effect-r/_expected/amd.js
-
test/form/side-effect-r/_expected/cjs.js
-
test/form/side-effect-r/_expected/es.js
-
test/form/side-effect-r/_expected/iife.js
-
test/form/side-effect-r/_expected/umd.js
-
test/form/side-effect-s/_config.js
-
test/form/side-effect-s/_expected/amd.js
-
test/form/side-effect-s/_expected/cjs.js
-
test/form/side-effect-s/_expected/es.js
-
test/form/side-effect-s/_expected/iife.js
-
test/form/side-effect-s/_expected/umd.js
-
test/form/side-effect-s/main.js
-
test/form/skips-dead-branches-h/_config.js
-
test/form/skips-dead-branches-h/_expected/amd.js
-
test/form/skips-dead-branches-h/_expected/cjs.js
-
test/form/skips-dead-branches-h/_expected/es.js
-
test/form/skips-dead-branches-h/_expected/iife.js
-
test/form/skips-dead-branches-h/_expected/umd.js
-
test/form/skips-dead-branches-h/main.js
-
test/form/skips-dead-branches-i/_config.js
-
test/form/skips-dead-branches-i/_expected/amd.js
-
test/form/skips-dead-branches-i/_expected/cjs.js
-
test/form/skips-dead-branches-i/_expected/es.js
-
test/form/skips-dead-branches-i/_expected/iife.js
-
test/form/skips-dead-branches-i/_expected/umd.js
-
test/form/skips-dead-branches-i/main.js
|
|
@ -1,6 +1,7 @@ |
|
|
|
import Node from '../Node.js'; |
|
|
|
import disallowIllegalReassignment from './shared/disallowIllegalReassignment.js'; |
|
|
|
import isUsedByBundle from './shared/isUsedByBundle.js'; |
|
|
|
import isProgramLevel from '../utils/isProgramLevel.js'; |
|
|
|
import { NUMBER, STRING } from '../values.js'; |
|
|
|
|
|
|
|
export default class AssignmentExpression extends Node { |
|
|
@ -36,7 +37,10 @@ export default class AssignmentExpression extends Node { |
|
|
|
initialise ( scope ) { |
|
|
|
this.scope = scope; |
|
|
|
|
|
|
|
this.module.bundle.dependentExpressions.push( this ); |
|
|
|
if ( isProgramLevel( this ) ) { |
|
|
|
this.module.bundle.dependentExpressions.push( this ); |
|
|
|
} |
|
|
|
|
|
|
|
super.initialise( scope ); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1,6 +1,7 @@ |
|
|
|
import getLocation from '../../utils/getLocation.js'; |
|
|
|
import error from '../../utils/error.js'; |
|
|
|
import Node from '../Node.js'; |
|
|
|
import isProgramLevel from '../utils/isProgramLevel.js'; |
|
|
|
import callHasEffects from './shared/callHasEffects.js'; |
|
|
|
|
|
|
|
export default class CallExpression extends Node { |
|
|
@ -40,14 +41,3 @@ export default class CallExpression extends Node { |
|
|
|
return this.hasEffects( this.findScope() ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function isProgramLevel ( node ) { |
|
|
|
do { |
|
|
|
if ( node.type === 'Program' ) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
node = node.parent; |
|
|
|
} while ( node && !/Function/.test( node.type ) ); |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
@ -0,0 +1,10 @@ |
|
|
|
export default function isProgramLevel ( node ) { |
|
|
|
do { |
|
|
|
if ( node.type === 'Program' ) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
node = node.parent; |
|
|
|
} while ( node && !/Function/.test( node.type ) ); |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
@ -1,4 +1,3 @@ |
|
|
|
module.exports = { |
|
|
|
skip: true, |
|
|
|
description: 'discards unused function expression assigned to a variable that calls itself and a global' |
|
|
|
}; |
|
|
|
|
|
@ -0,0 +1,5 @@ |
|
|
|
define(function () { 'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
@ -0,0 +1,2 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
@ -0,0 +1,6 @@ |
|
|
|
(function () { |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}()); |
|
|
@ -0,0 +1,9 @@ |
|
|
|
(function (global, factory) { |
|
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
|
|
|
typeof define === 'function' && define.amd ? define(factory) : |
|
|
|
(factory()); |
|
|
|
}(this, (function () { 'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}))); |
|
|
@ -0,0 +1,3 @@ |
|
|
|
module.exports = { |
|
|
|
description: 'discards unused function expression assigned to a variable that calls itself and has side effects' |
|
|
|
}; |
|
|
@ -0,0 +1,5 @@ |
|
|
|
define(function () { 'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
@ -0,0 +1,2 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
@ -0,0 +1,6 @@ |
|
|
|
(function () { |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}()); |
|
|
@ -0,0 +1,9 @@ |
|
|
|
(function (global, factory) { |
|
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
|
|
|
typeof define === 'function' && define.amd ? define(factory) : |
|
|
|
(factory()); |
|
|
|
}(this, (function () { 'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}))); |
|
|
@ -0,0 +1,7 @@ |
|
|
|
var foo = function foo(param) { |
|
|
|
if ( whatever ) { |
|
|
|
foo(param); |
|
|
|
} else { |
|
|
|
param.foo = 1; |
|
|
|
} |
|
|
|
}; |
|
|
@ -0,0 +1,3 @@ |
|
|
|
module.exports = { |
|
|
|
description: 'skips a dead branch (h)' |
|
|
|
}; |
|
|
@ -0,0 +1,8 @@ |
|
|
|
define(function () { 'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
|
|
|
|
|
}); |
|
|
@ -0,0 +1,6 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
@ -0,0 +1,4 @@ |
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
@ -0,0 +1,9 @@ |
|
|
|
(function () { |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
|
|
|
|
|
}()); |
|
|
@ -0,0 +1,12 @@ |
|
|
|
(function (global, factory) { |
|
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
|
|
|
typeof define === 'function' && define.amd ? define(factory) : |
|
|
|
(factory()); |
|
|
|
}(this, (function () { 'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
|
|
|
|
|
}))); |
|
|
@ -0,0 +1,11 @@ |
|
|
|
function bar(umm) { |
|
|
|
umm = hmm(); |
|
|
|
console.log("bar"); |
|
|
|
} |
|
|
|
function hmm() { |
|
|
|
return true; |
|
|
|
} |
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
@ -0,0 +1,3 @@ |
|
|
|
module.exports = { |
|
|
|
description: 'skips a dead branch (h)' |
|
|
|
}; |
|
|
@ -0,0 +1,8 @@ |
|
|
|
define(function () { 'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
|
|
|
|
|
}); |
|
|
@ -0,0 +1,6 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
@ -0,0 +1,4 @@ |
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
@ -0,0 +1,9 @@ |
|
|
|
(function () { |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
|
|
|
|
|
}()); |
|
|
@ -0,0 +1,12 @@ |
|
|
|
(function (global, factory) { |
|
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
|
|
|
typeof define === 'function' && define.amd ? define(factory) : |
|
|
|
(factory()); |
|
|
|
}(this, (function () { 'use strict'; |
|
|
|
|
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |
|
|
|
|
|
|
|
}))); |
|
|
@ -0,0 +1,12 @@ |
|
|
|
function bar() { |
|
|
|
var t; |
|
|
|
t = hmm(); |
|
|
|
console.log("bar"); |
|
|
|
} |
|
|
|
function hmm() { |
|
|
|
return true; |
|
|
|
} |
|
|
|
function baz() { |
|
|
|
console.log("baz"); |
|
|
|
} |
|
|
|
baz(); |