Skip to content

Commit

Permalink
Improves tree-shaking by only considering program-level
Browse files Browse the repository at this point in the history
assignments as a Bundle's dependent expressions.
  • Loading branch information
danez committed Dec 18, 2016
1 parent fcf3928 commit 583b13d
Show file tree
Hide file tree
Showing 30 changed files with 179 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/ast/nodes/AssignmentExpression.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 {
Expand Down Expand Up @@ -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 );
}

Expand Down
12 changes: 1 addition & 11 deletions src/ast/nodes/CallExpression.js
@@ -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 {
Expand Down Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions src/ast/utils/isProgramLevel.js
@@ -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 change: 0 additions & 1 deletion test/form/side-effect-r/_config.js
@@ -1,4 +1,3 @@
module.exports = {
skip: true,
description: 'discards unused function expression assigned to a variable that calls itself and a global'
};
5 changes: 5 additions & 0 deletions test/form/side-effect-r/_expected/amd.js
@@ -0,0 +1,5 @@
define(function () { 'use strict';



});
2 changes: 2 additions & 0 deletions test/form/side-effect-r/_expected/cjs.js
@@ -0,0 +1,2 @@
'use strict';

1 change: 1 addition & 0 deletions test/form/side-effect-r/_expected/es.js
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions test/form/side-effect-r/_expected/iife.js
@@ -0,0 +1,6 @@
(function () {
'use strict';



}());
9 changes: 9 additions & 0 deletions test/form/side-effect-r/_expected/umd.js
@@ -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';



})));
3 changes: 3 additions & 0 deletions test/form/side-effect-s/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'discards unused function expression assigned to a variable that calls itself and has side effects'
};
5 changes: 5 additions & 0 deletions test/form/side-effect-s/_expected/amd.js
@@ -0,0 +1,5 @@
define(function () { 'use strict';



});
2 changes: 2 additions & 0 deletions test/form/side-effect-s/_expected/cjs.js
@@ -0,0 +1,2 @@
'use strict';

1 change: 1 addition & 0 deletions test/form/side-effect-s/_expected/es.js
@@ -0,0 +1 @@

6 changes: 6 additions & 0 deletions test/form/side-effect-s/_expected/iife.js
@@ -0,0 +1,6 @@
(function () {
'use strict';



}());
9 changes: 9 additions & 0 deletions test/form/side-effect-s/_expected/umd.js
@@ -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';



})));
7 changes: 7 additions & 0 deletions test/form/side-effect-s/main.js
@@ -0,0 +1,7 @@
var foo = function foo(param) {
if ( whatever ) {
foo(param);
} else {
param.foo = 1;
}
};
3 changes: 3 additions & 0 deletions test/form/skips-dead-branches-h/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'skips a dead branch (h)'
};
8 changes: 8 additions & 0 deletions test/form/skips-dead-branches-h/_expected/amd.js
@@ -0,0 +1,8 @@
define(function () { 'use strict';

function baz() {
console.log("baz");
}
baz();

});
6 changes: 6 additions & 0 deletions test/form/skips-dead-branches-h/_expected/cjs.js
@@ -0,0 +1,6 @@
'use strict';

function baz() {
console.log("baz");
}
baz();
4 changes: 4 additions & 0 deletions test/form/skips-dead-branches-h/_expected/es.js
@@ -0,0 +1,4 @@
function baz() {
console.log("baz");
}
baz();
9 changes: 9 additions & 0 deletions test/form/skips-dead-branches-h/_expected/iife.js
@@ -0,0 +1,9 @@
(function () {
'use strict';

function baz() {
console.log("baz");
}
baz();

}());
12 changes: 12 additions & 0 deletions test/form/skips-dead-branches-h/_expected/umd.js
@@ -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();

})));
11 changes: 11 additions & 0 deletions test/form/skips-dead-branches-h/main.js
@@ -0,0 +1,11 @@
function bar(umm) {
umm = hmm();
console.log("bar");
}
function hmm() {
return true;
}
function baz() {
console.log("baz");
}
baz();
3 changes: 3 additions & 0 deletions test/form/skips-dead-branches-i/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'skips a dead branch (h)'
};
8 changes: 8 additions & 0 deletions test/form/skips-dead-branches-i/_expected/amd.js
@@ -0,0 +1,8 @@
define(function () { 'use strict';

function baz() {
console.log("baz");
}
baz();

});
6 changes: 6 additions & 0 deletions test/form/skips-dead-branches-i/_expected/cjs.js
@@ -0,0 +1,6 @@
'use strict';

function baz() {
console.log("baz");
}
baz();
4 changes: 4 additions & 0 deletions test/form/skips-dead-branches-i/_expected/es.js
@@ -0,0 +1,4 @@
function baz() {
console.log("baz");
}
baz();
9 changes: 9 additions & 0 deletions test/form/skips-dead-branches-i/_expected/iife.js
@@ -0,0 +1,9 @@
(function () {
'use strict';

function baz() {
console.log("baz");
}
baz();

}());
12 changes: 12 additions & 0 deletions test/form/skips-dead-branches-i/_expected/umd.js
@@ -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();

})));
12 changes: 12 additions & 0 deletions test/form/skips-dead-branches-i/main.js
@@ -0,0 +1,12 @@
function bar() {
var t;
t = hmm();
console.log("bar");
}
function hmm() {
return true;
}
function baz() {
console.log("baz");
}
baz();

0 comments on commit 583b13d

Please sign in to comment.