Browse Source

dont assume recursive function calls have side-effects

rewrite
Rich-Harris 8 years ago
parent
commit
5e1e5f99c5
  1. 2
      src/ast/nodes/shared/callHasEffects.js
  2. 3
      test/form/self-calling-function-with-effects/_config.js
  3. 20
      test/form/self-calling-function-with-effects/_expected/amd.js
  4. 18
      test/form/self-calling-function-with-effects/_expected/cjs.js
  5. 16
      test/form/self-calling-function-with-effects/_expected/es.js
  6. 21
      test/form/self-calling-function-with-effects/_expected/iife.js
  7. 24
      test/form/self-calling-function-with-effects/_expected/umd.js
  8. 16
      test/form/self-calling-function-with-effects/main.js
  9. 3
      test/form/self-calling-function/_config.js
  10. 5
      test/form/self-calling-function/_expected/amd.js
  11. 2
      test/form/self-calling-function/_expected/cjs.js
  12. 0
      test/form/self-calling-function/_expected/es.js
  13. 6
      test/form/self-calling-function/_expected/iife.js
  14. 9
      test/form/self-calling-function/_expected/umd.js
  15. 14
      test/form/self-calling-function/main.js

2
src/ast/nodes/shared/callHasEffects.js

@ -6,7 +6,7 @@ import { UNKNOWN } from '../../values.js';
const currentlyCalling = new Set();
function fnHasEffects ( fn ) {
if ( currentlyCalling.has( fn ) ) return true; // prevent infinite loops... TODO there must be a better way
if ( currentlyCalling.has( fn ) ) return false; // prevent infinite loops... TODO there must be a better way
currentlyCalling.add( fn );
// handle body-less arrow functions

3
test/form/self-calling-function-with-effects/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'discards a self-calling function with side-effects'
};

20
test/form/self-calling-function-with-effects/_expected/amd.js

@ -0,0 +1,20 @@
define(function () { 'use strict';
function foo ( x ) {
effect( x );
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
effect( x );
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );
});

18
test/form/self-calling-function-with-effects/_expected/cjs.js

@ -0,0 +1,18 @@
'use strict';
function foo ( x ) {
effect( x );
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
effect( x );
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );

16
test/form/self-calling-function-with-effects/_expected/es.js

@ -0,0 +1,16 @@
function foo ( x ) {
effect( x );
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
effect( x );
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );

21
test/form/self-calling-function-with-effects/_expected/iife.js

@ -0,0 +1,21 @@
(function () {
'use strict';
function foo ( x ) {
effect( x );
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
effect( x );
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );
}());

24
test/form/self-calling-function-with-effects/_expected/umd.js

@ -0,0 +1,24 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
function foo ( x ) {
effect( x );
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
effect( x );
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );
})));

16
test/form/self-calling-function-with-effects/main.js

@ -0,0 +1,16 @@
function foo ( x ) {
effect( x );
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
effect( x );
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );

3
test/form/self-calling-function/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'discards a self-calling function without side-effects'
};

5
test/form/self-calling-function/_expected/amd.js

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

2
test/form/self-calling-function/_expected/cjs.js

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

0
test/form/self-calling-function/_expected/es.js

6
test/form/self-calling-function/_expected/iife.js

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

9
test/form/self-calling-function/_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';
})));

14
test/form/self-calling-function/main.js

@ -0,0 +1,14 @@
function foo ( x ) {
if ( x > 0 ) foo( x - 1 );
}
function bar ( x ) {
if ( x > 0 ) baz( x );
}
function baz ( x ) {
bar( x - 1 );
}
foo( 10 );
bar( 10 );
Loading…
Cancel
Save