Browse Source

Merge pull request #1143 from danez/recursive-functions

Improve tree-shaking of assignment expressions and unused functions
gh-1132
Rich Harris 8 years ago
committed by GitHub
parent
commit
3f39de8800
  1. 6
      src/ast/nodes/AssignmentExpression.js
  2. 12
      src/ast/nodes/CallExpression.js
  3. 10
      src/ast/utils/isProgramLevel.js
  4. 1
      test/form/side-effect-r/_config.js
  5. 5
      test/form/side-effect-r/_expected/amd.js
  6. 2
      test/form/side-effect-r/_expected/cjs.js
  7. 1
      test/form/side-effect-r/_expected/es.js
  8. 6
      test/form/side-effect-r/_expected/iife.js
  9. 9
      test/form/side-effect-r/_expected/umd.js
  10. 3
      test/form/side-effect-s/_config.js
  11. 5
      test/form/side-effect-s/_expected/amd.js
  12. 2
      test/form/side-effect-s/_expected/cjs.js
  13. 1
      test/form/side-effect-s/_expected/es.js
  14. 6
      test/form/side-effect-s/_expected/iife.js
  15. 9
      test/form/side-effect-s/_expected/umd.js
  16. 7
      test/form/side-effect-s/main.js
  17. 3
      test/form/skips-dead-branches-h/_config.js
  18. 8
      test/form/skips-dead-branches-h/_expected/amd.js
  19. 6
      test/form/skips-dead-branches-h/_expected/cjs.js
  20. 4
      test/form/skips-dead-branches-h/_expected/es.js
  21. 9
      test/form/skips-dead-branches-h/_expected/iife.js
  22. 12
      test/form/skips-dead-branches-h/_expected/umd.js
  23. 11
      test/form/skips-dead-branches-h/main.js
  24. 3
      test/form/skips-dead-branches-i/_config.js
  25. 8
      test/form/skips-dead-branches-i/_expected/amd.js
  26. 6
      test/form/skips-dead-branches-i/_expected/cjs.js
  27. 4
      test/form/skips-dead-branches-i/_expected/es.js
  28. 9
      test/form/skips-dead-branches-i/_expected/iife.js
  29. 12
      test/form/skips-dead-branches-i/_expected/umd.js
  30. 12
      test/form/skips-dead-branches-i/main.js

6
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 {
@ -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 );
}

12
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 {
@ -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
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
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
test/form/side-effect-r/_expected/amd.js

@ -0,0 +1,5 @@
define(function () { 'use strict';
});

2
test/form/side-effect-r/_expected/cjs.js

@ -0,0 +1,2 @@
'use strict';

1
test/form/side-effect-r/_expected/es.js

@ -0,0 +1 @@

6
test/form/side-effect-r/_expected/iife.js

@ -0,0 +1,6 @@
(function () {
'use strict';
}());

9
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
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
test/form/side-effect-s/_expected/amd.js

@ -0,0 +1,5 @@
define(function () { 'use strict';
});

2
test/form/side-effect-s/_expected/cjs.js

@ -0,0 +1,2 @@
'use strict';

1
test/form/side-effect-s/_expected/es.js

@ -0,0 +1 @@

6
test/form/side-effect-s/_expected/iife.js

@ -0,0 +1,6 @@
(function () {
'use strict';
}());

9
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
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
test/form/skips-dead-branches-h/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'skips a dead branch (h)'
};

8
test/form/skips-dead-branches-h/_expected/amd.js

@ -0,0 +1,8 @@
define(function () { 'use strict';
function baz() {
console.log("baz");
}
baz();
});

6
test/form/skips-dead-branches-h/_expected/cjs.js

@ -0,0 +1,6 @@
'use strict';
function baz() {
console.log("baz");
}
baz();

4
test/form/skips-dead-branches-h/_expected/es.js

@ -0,0 +1,4 @@
function baz() {
console.log("baz");
}
baz();

9
test/form/skips-dead-branches-h/_expected/iife.js

@ -0,0 +1,9 @@
(function () {
'use strict';
function baz() {
console.log("baz");
}
baz();
}());

12
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
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
test/form/skips-dead-branches-i/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'skips a dead branch (h)'
};

8
test/form/skips-dead-branches-i/_expected/amd.js

@ -0,0 +1,8 @@
define(function () { 'use strict';
function baz() {
console.log("baz");
}
baz();
});

6
test/form/skips-dead-branches-i/_expected/cjs.js

@ -0,0 +1,6 @@
'use strict';
function baz() {
console.log("baz");
}
baz();

4
test/form/skips-dead-branches-i/_expected/es.js

@ -0,0 +1,4 @@
function baz() {
console.log("baz");
}
baz();

9
test/form/skips-dead-branches-i/_expected/iife.js

@ -0,0 +1,9 @@
(function () {
'use strict';
function baz() {
console.log("baz");
}
baz();
}());

12
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
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();
Loading…
Cancel
Save