Browse Source

fix side-effect detection in cyclical function calls (#397)

gh-438-b
Rich-Harris 9 years ago
parent
commit
742cf9255c
  1. 8
      src/Declaration.js
  2. 2
      src/Module.js
  3. 1
      test/form/side-effect-m/_config.js
  4. 2
      test/form/side-effect-m/_expected/amd.js
  5. 2
      test/form/side-effect-m/_expected/cjs.js
  6. 2
      test/form/side-effect-m/_expected/es6.js
  7. 2
      test/form/side-effect-m/_expected/iife.js
  8. 2
      test/form/side-effect-m/_expected/umd.js

8
src/Declaration.js

@ -43,14 +43,20 @@ export default class Declaration {
run ( strongDependencies ) {
if ( this.tested ) return this.hasSideEffects;
this.tested = true;
if ( !this.functionNode ) {
this.hasSideEffects = true; // err on the side of caution. TODO handle unambiguous `var x; x = y => z` cases
} else {
if ( this.running ) return true; // short-circuit infinite loop
this.running = true;
this.hasSideEffects = run( this.functionNode.body, this.functionNode._scope, this.statement, strongDependencies, false );
this.running = false;
}
this.tested = true;
return this.hasSideEffects;
}

2
src/Module.js

@ -570,7 +570,7 @@ export default class Module {
let marked = false;
this.statements.forEach( statement => {
marked = marked || statement.run( this.strongDependencies, safe );
marked = statement.run( this.strongDependencies, safe ) || marked;
});
return marked;

1
test/form/side-effect-m/_config.js

@ -1,4 +1,3 @@
module.exports = {
solo: true,
description: 'detects side-effects with cyclical dependencies'
};

2
test/form/side-effect-m/_expected/amd.js

@ -7,7 +7,7 @@ define(function () { 'use strict';
var counter = 0;
// This should be in the output
export var foo = odd( 12 );
var foo = odd( 12 );
function even ( n ) {
alert( counter++ )

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

@ -7,7 +7,7 @@ function odd ( n ) {
var counter = 0;
// This should be in the output
export var foo = odd( 12 );
var foo = odd( 12 );
function even ( n ) {
alert( counter++ )

2
test/form/side-effect-m/_expected/es6.js

@ -5,7 +5,7 @@ function odd ( n ) {
var counter = 0;
// This should be in the output
export var foo = odd( 12 );
var foo = odd( 12 );
function even ( n ) {
alert( counter++ )

2
test/form/side-effect-m/_expected/iife.js

@ -7,7 +7,7 @@
var counter = 0;
// This should be in the output
export var foo = odd( 12 );
var foo = odd( 12 );
function even ( n ) {
alert( counter++ )

2
test/form/side-effect-m/_expected/umd.js

@ -11,7 +11,7 @@
var counter = 0;
// This should be in the output
export var foo = odd( 12 );
var foo = odd( 12 );
function even ( n ) {
alert( counter++ )

Loading…
Cancel
Save