Browse Source

exclude constructors that are known to be pure

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

14
src/utils/testForSideEffects.js

@ -6,8 +6,12 @@ import flatten from '../ast/flatten';
let pureFunctions = {};
[
// TODO add others to this list
'Object.keys'
// TODO add others to this list from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
'Array', 'Array.isArray',
'Error', 'EvalError', 'InternalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
'Object', 'Object.keys'
].forEach( name => pureFunctions[ name ] = true );
export default function testForSideEffects ( node, scope, statement, strongDependencies, force ) {
@ -31,13 +35,11 @@ export default function testForSideEffects ( node, scope, statement, strongDepen
}
}
// If this is a top-level call expression, or an assignment to a global,
// this statement will need to be marked
else if ( node.type === 'NewExpression' ) {
else if ( node.type === 'ThrowStatement' ) {
hasSideEffect = true;
}
else if ( node.type === 'CallExpression' ) {
else if ( node.type === 'CallExpression' || node.type === 'NewExpression' ) {
if ( node.callee.type === 'Identifier' ) {
const declaration = scope.findDeclaration( node.callee.name ) ||
statement.module.trace( node.callee.name );

6
test/form/side-effect-f/_config.js

@ -0,0 +1,6 @@
module.exports = {
description: 'disregards side-effects that are contained within a function',
options: {
moduleName: 'myBundle'
}
};

7
test/form/side-effect-f/_expected/amd.js

@ -0,0 +1,7 @@
define(function () { 'use strict';
var main = 42;
return main;
});

5
test/form/side-effect-f/_expected/cjs.js

@ -0,0 +1,5 @@
'use strict';
var main = 42;
module.exports = main;

3
test/form/side-effect-f/_expected/es6.js

@ -0,0 +1,3 @@
var main = 42;
export default main;

7
test/form/side-effect-f/_expected/iife.js

@ -0,0 +1,7 @@
var myBundle = (function () { 'use strict';
var main = 42;
return main;
})();

11
test/form/side-effect-f/_expected/umd.js

@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.myBundle = factory();
}(this, function () { 'use strict';
var main = 42;
return main;
}));

8
test/form/side-effect-f/main.js

@ -0,0 +1,8 @@
function foo () {
var a, b, c;
b = 1;
}
foo();
export default 42;

6
test/form/side-effect-g/_config.js

@ -0,0 +1,6 @@
module.exports = {
description: 'excludes constructors that are known to be pure',
options: {
moduleName: 'myBundle'
}
};

7
test/form/side-effect-g/_expected/amd.js

@ -0,0 +1,7 @@
define(function () { 'use strict';
var main = 42;
return main;
});

5
test/form/side-effect-g/_expected/cjs.js

@ -0,0 +1,5 @@
'use strict';
var main = 42;
module.exports = main;

3
test/form/side-effect-g/_expected/es6.js

@ -0,0 +1,3 @@
var main = 42;
export default main;

7
test/form/side-effect-g/_expected/iife.js

@ -0,0 +1,7 @@
var myBundle = (function () { 'use strict';
var main = 42;
return main;
})();

11
test/form/side-effect-g/_expected/umd.js

@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.myBundle = factory();
}(this, function () { 'use strict';
var main = 42;
return main;
}));

3
test/form/side-effect-g/main.js

@ -0,0 +1,3 @@
var err = new Error( 'this will be ignored' );
export default 42;
Loading…
Cancel
Save