Browse Source

account for local scopes when checking for pure global functions

better-aggressive
Rich-Harris 9 years ago
parent
commit
fd0cbb7e76
  1. 3
      src/utils/testForSideEffects.js
  2. 3
      test/form/side-effect-e/_config.js
  3. 13
      test/form/side-effect-e/_expected/amd.js
  4. 13
      test/form/side-effect-e/_expected/cjs.js
  5. 13
      test/form/side-effect-e/_expected/es6.js
  6. 13
      test/form/side-effect-e/_expected/iife.js
  7. 13
      test/form/side-effect-e/_expected/umd.js
  8. 10
      test/form/side-effect-e/main.js

3
src/utils/testForSideEffects.js

@ -69,7 +69,8 @@ export default function testForSideEffects ( node, scope, statement, strongDepen
if ( flattened ) { if ( flattened ) {
// if we're calling e.g. Object.keys(thing), there are no side-effects // if we're calling e.g. Object.keys(thing), there are no side-effects
// TODO make pureFunctions configurable // TODO make pureFunctions configurable
const declaration = statement.module.trace( flattened.name ); const declaration = scope.findDeclaration( flattened.name ) || statement.module.trace( flattened.name );
if ( !!declaration || !pureFunctions[ flattened.keypath ] ) { if ( !!declaration || !pureFunctions[ flattened.keypath ] ) {
hasSideEffect = true; hasSideEffect = true;
} }

3
test/form/side-effect-e/_config.js

@ -1,6 +1,5 @@
module.exports = { module.exports = {
solo: true, description: 'excludes functions that are known to be pure',
description: 'disregards side-effects that are contained within a function',
options: { options: {
moduleName: 'myBundle' moduleName: 'myBundle'
} }

13
test/form/side-effect-e/_expected/amd.js

@ -1,5 +1,18 @@
define(function () { 'use strict'; define(function () { 'use strict';
function foo () {
var Object = {
keys: function () {
console.log( 'side-effect' );
}
};
var obj = { foo: 1, bar: 2 };
var keys = Object.keys( obj );
}
foo();
var main = 42; var main = 42;
return main; return main;

13
test/form/side-effect-e/_expected/cjs.js

@ -1,5 +1,18 @@
'use strict'; 'use strict';
function foo () {
var Object = {
keys: function () {
console.log( 'side-effect' );
}
};
var obj = { foo: 1, bar: 2 };
var keys = Object.keys( obj );
}
foo();
var main = 42; var main = 42;
module.exports = main; module.exports = main;

13
test/form/side-effect-e/_expected/es6.js

@ -1,3 +1,16 @@
function foo () {
var Object = {
keys: function () {
console.log( 'side-effect' );
}
};
var obj = { foo: 1, bar: 2 };
var keys = Object.keys( obj );
}
foo();
var main = 42; var main = 42;
export default main; export default main;

13
test/form/side-effect-e/_expected/iife.js

@ -1,5 +1,18 @@
var myBundle = (function () { 'use strict'; var myBundle = (function () { 'use strict';
function foo () {
var Object = {
keys: function () {
console.log( 'side-effect' );
}
};
var obj = { foo: 1, bar: 2 };
var keys = Object.keys( obj );
}
foo();
var main = 42; var main = 42;
return main; return main;

13
test/form/side-effect-e/_expected/umd.js

@ -4,6 +4,19 @@
global.myBundle = factory(); global.myBundle = factory();
}(this, function () { 'use strict'; }(this, function () { 'use strict';
function foo () {
var Object = {
keys: function () {
console.log( 'side-effect' );
}
};
var obj = { foo: 1, bar: 2 };
var keys = Object.keys( obj );
}
foo();
var main = 42; var main = 42;
return main; return main;

10
test/form/side-effect-e/main.js

@ -1,6 +1,12 @@
function foo () { function foo () {
var a, b, c; var Object = {
b = 1; keys: function () {
console.log( 'side-effect' );
}
};
var obj = { foo: 1, bar: 2 };
var keys = Object.keys( obj );
} }
foo(); foo();

Loading…
Cancel
Save