From c44cf0d0e62e24cadc08f50ae3d0039b30ca9982 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 31 Oct 2015 22:36:40 -0400 Subject: [PATCH] exclude constructors that are known to be pure --- src/utils/testForSideEffects.js | 14 ++++++++------ test/form/side-effect-f/_config.js | 6 ++++++ test/form/side-effect-f/_expected/amd.js | 7 +++++++ test/form/side-effect-f/_expected/cjs.js | 5 +++++ test/form/side-effect-f/_expected/es6.js | 3 +++ test/form/side-effect-f/_expected/iife.js | 7 +++++++ test/form/side-effect-f/_expected/umd.js | 11 +++++++++++ test/form/side-effect-f/main.js | 8 ++++++++ test/form/side-effect-g/_config.js | 6 ++++++ test/form/side-effect-g/_expected/amd.js | 7 +++++++ test/form/side-effect-g/_expected/cjs.js | 5 +++++ test/form/side-effect-g/_expected/es6.js | 3 +++ test/form/side-effect-g/_expected/iife.js | 7 +++++++ test/form/side-effect-g/_expected/umd.js | 11 +++++++++++ test/form/side-effect-g/main.js | 3 +++ 15 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 test/form/side-effect-f/_config.js create mode 100644 test/form/side-effect-f/_expected/amd.js create mode 100644 test/form/side-effect-f/_expected/cjs.js create mode 100644 test/form/side-effect-f/_expected/es6.js create mode 100644 test/form/side-effect-f/_expected/iife.js create mode 100644 test/form/side-effect-f/_expected/umd.js create mode 100644 test/form/side-effect-f/main.js create mode 100644 test/form/side-effect-g/_config.js create mode 100644 test/form/side-effect-g/_expected/amd.js create mode 100644 test/form/side-effect-g/_expected/cjs.js create mode 100644 test/form/side-effect-g/_expected/es6.js create mode 100644 test/form/side-effect-g/_expected/iife.js create mode 100644 test/form/side-effect-g/_expected/umd.js create mode 100644 test/form/side-effect-g/main.js diff --git a/src/utils/testForSideEffects.js b/src/utils/testForSideEffects.js index aeb2413..1a42eba 100644 --- a/src/utils/testForSideEffects.js +++ b/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 ); diff --git a/test/form/side-effect-f/_config.js b/test/form/side-effect-f/_config.js new file mode 100644 index 0000000..67f9fa4 --- /dev/null +++ b/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' + } +}; diff --git a/test/form/side-effect-f/_expected/amd.js b/test/form/side-effect-f/_expected/amd.js new file mode 100644 index 0000000..37d2571 --- /dev/null +++ b/test/form/side-effect-f/_expected/amd.js @@ -0,0 +1,7 @@ +define(function () { 'use strict'; + + var main = 42; + + return main; + +}); diff --git a/test/form/side-effect-f/_expected/cjs.js b/test/form/side-effect-f/_expected/cjs.js new file mode 100644 index 0000000..5a370cd --- /dev/null +++ b/test/form/side-effect-f/_expected/cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +var main = 42; + +module.exports = main; diff --git a/test/form/side-effect-f/_expected/es6.js b/test/form/side-effect-f/_expected/es6.js new file mode 100644 index 0000000..d862de8 --- /dev/null +++ b/test/form/side-effect-f/_expected/es6.js @@ -0,0 +1,3 @@ +var main = 42; + +export default main; diff --git a/test/form/side-effect-f/_expected/iife.js b/test/form/side-effect-f/_expected/iife.js new file mode 100644 index 0000000..b7b15ee --- /dev/null +++ b/test/form/side-effect-f/_expected/iife.js @@ -0,0 +1,7 @@ +var myBundle = (function () { 'use strict'; + + var main = 42; + + return main; + +})(); diff --git a/test/form/side-effect-f/_expected/umd.js b/test/form/side-effect-f/_expected/umd.js new file mode 100644 index 0000000..b5aa08a --- /dev/null +++ b/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; + +})); diff --git a/test/form/side-effect-f/main.js b/test/form/side-effect-f/main.js new file mode 100644 index 0000000..e75b2f3 --- /dev/null +++ b/test/form/side-effect-f/main.js @@ -0,0 +1,8 @@ +function foo () { + var a, b, c; + b = 1; +} + +foo(); + +export default 42; diff --git a/test/form/side-effect-g/_config.js b/test/form/side-effect-g/_config.js new file mode 100644 index 0000000..c821995 --- /dev/null +++ b/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' + } +}; diff --git a/test/form/side-effect-g/_expected/amd.js b/test/form/side-effect-g/_expected/amd.js new file mode 100644 index 0000000..37d2571 --- /dev/null +++ b/test/form/side-effect-g/_expected/amd.js @@ -0,0 +1,7 @@ +define(function () { 'use strict'; + + var main = 42; + + return main; + +}); diff --git a/test/form/side-effect-g/_expected/cjs.js b/test/form/side-effect-g/_expected/cjs.js new file mode 100644 index 0000000..5a370cd --- /dev/null +++ b/test/form/side-effect-g/_expected/cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +var main = 42; + +module.exports = main; diff --git a/test/form/side-effect-g/_expected/es6.js b/test/form/side-effect-g/_expected/es6.js new file mode 100644 index 0000000..d862de8 --- /dev/null +++ b/test/form/side-effect-g/_expected/es6.js @@ -0,0 +1,3 @@ +var main = 42; + +export default main; diff --git a/test/form/side-effect-g/_expected/iife.js b/test/form/side-effect-g/_expected/iife.js new file mode 100644 index 0000000..b7b15ee --- /dev/null +++ b/test/form/side-effect-g/_expected/iife.js @@ -0,0 +1,7 @@ +var myBundle = (function () { 'use strict'; + + var main = 42; + + return main; + +})(); diff --git a/test/form/side-effect-g/_expected/umd.js b/test/form/side-effect-g/_expected/umd.js new file mode 100644 index 0000000..b5aa08a --- /dev/null +++ b/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; + +})); diff --git a/test/form/side-effect-g/main.js b/test/form/side-effect-g/main.js new file mode 100644 index 0000000..75d2cf5 --- /dev/null +++ b/test/form/side-effect-g/main.js @@ -0,0 +1,3 @@ +var err = new Error( 'this will be ignored' ); + +export default 42;