Browse Source

remove aggressive mode

gh-335
Rich-Harris 9 years ago
parent
commit
5beac6d85e
  1. 4
      src/Bundle.js
  2. 14
      src/Declaration.js
  3. 12
      src/utils/run.js
  4. 6
      test/form/aggressive/_config.js
  5. 9
      test/form/aggressive/_expected/amd.js
  6. 7
      test/form/aggressive/_expected/cjs.js
  7. 5
      test/form/aggressive/_expected/es6.js
  8. 9
      test/form/aggressive/_expected/iife.js
  9. 13
      test/form/aggressive/_expected/umd.js
  10. 9
      test/form/aggressive/foo.js
  11. 3
      test/form/aggressive/main.js
  12. 8
      test/function/aggressive-mode/_config.js
  13. 11
      test/function/aggressive-mode/foo.js
  14. 3
      test/function/aggressive-mode/main.js

4
src/Bundle.js

@ -82,14 +82,12 @@ export default class Bundle {
});
// mark statements that should appear in the bundle
const safe = !this.aggressive;
let settled = false;
while ( !settled ) {
settled = true;
this.modules.forEach( module => {
if ( module.run( safe || module === entryModule ) ) settled = false;
if ( module.run() ) settled = false;
});
}

14
src/Declaration.js

@ -39,14 +39,14 @@ export default class Declaration {
return `exports.${this.name}`;
}
run ( strongDependencies, safe ) {
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 {
this.hasSideEffects = run( this.functionNode.body, this.functionNode._scope, this.statement, strongDependencies, false, safe );
this.hasSideEffects = run( this.functionNode.body, this.functionNode._scope, this.statement, strongDependencies, false );
}
return this.hasSideEffects;
@ -93,13 +93,13 @@ export class SyntheticDefaultDeclaration {
this.original.render();
}
run ( strongDependencies, safe ) {
run ( strongDependencies ) {
if ( this.original ) {
return this.original.run( strongDependencies, safe );
return this.original.run( strongDependencies );
}
if ( /FunctionExpression/.test( this.node.declaration.type ) ) {
return run( this.node.declaration.body, this.statement.scope, this.statement, strongDependencies, false, safe );
return run( this.node.declaration.body, this.statement.scope, this.statement, strongDependencies, false );
}
}
@ -224,8 +224,8 @@ export class ExternalDeclaration {
return es6 ? this.name : `${this.module.name}.${this.name}`;
}
run ( strongDependencies, safe ) {
return safe;
run ( strongDependencies ) {
return true;
}
use () {

12
src/utils/run.js

@ -46,7 +46,7 @@ simdTypes.forEach( t => {
export default function run ( node, scope, statement, strongDependencies, force, safe ) {
export default function run ( node, scope, statement, strongDependencies, force ) {
let hasSideEffect = false;
walk( node, {
@ -83,10 +83,10 @@ export default function run ( node, scope, statement, strongDependencies, force,
statement.module.trace( node.callee.name );
if ( declaration ) {
if ( declaration.run( strongDependencies, safe ) ) {
if ( declaration.run( strongDependencies ) ) {
hasSideEffect = true;
}
} else if ( safe && !pureFunctions[ node.callee.name ] ) {
} else if ( !pureFunctions[ node.callee.name ] ) {
hasSideEffect = true;
}
}
@ -99,7 +99,7 @@ export default function run ( node, scope, statement, strongDependencies, force,
// TODO make pureFunctions configurable
const declaration = scope.findDeclaration( flattened.name ) || statement.module.trace( flattened.name );
if ( safe && ( !!declaration || !pureFunctions[ flattened.keypath ] ) ) {
if ( !!declaration || !pureFunctions[ flattened.keypath ] ) {
hasSideEffect = true;
}
} else {
@ -110,7 +110,7 @@ export default function run ( node, scope, statement, strongDependencies, force,
}
// otherwise we're probably dealing with a function expression
else if ( run( node.callee, scope, statement, strongDependencies, true, safe ) ) {
else if ( run( node.callee, scope, statement, strongDependencies, true ) ) {
hasSideEffect = true;
}
}
@ -126,7 +126,7 @@ export default function run ( node, scope, statement, strongDependencies, force,
} else {
declaration = statement.module.trace( subject.name );
if ( ( safe && ( !declaration || declaration.isExternal ) ) || declaration && declaration.isUsed ) {
if ( !declaration || declaration.isExternal || declaration.isUsed ) {
hasSideEffect = true;
}
}

6
test/form/aggressive/_config.js

@ -1,6 +0,0 @@
module.exports = {
description: 'ignores side-effects outside entry module in aggressive mode',
options: {
aggressive: true
}
}

9
test/form/aggressive/_expected/amd.js

@ -1,9 +0,0 @@
define(function () { 'use strict';
function foo () {
return 42;
}
assert.equal( foo(), 42 );
});

7
test/form/aggressive/_expected/cjs.js

@ -1,7 +0,0 @@
'use strict';
function foo () {
return 42;
}
assert.equal( foo(), 42 );

5
test/form/aggressive/_expected/es6.js

@ -1,5 +0,0 @@
function foo () {
return 42;
}
assert.equal( foo(), 42 );

9
test/form/aggressive/_expected/iife.js

@ -1,9 +0,0 @@
(function () { 'use strict';
function foo () {
return 42;
}
assert.equal( foo(), 42 );
})();

13
test/form/aggressive/_expected/umd.js

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

9
test/form/aggressive/foo.js

@ -1,9 +0,0 @@
function x () {
console.log( 'side-effect' );
}
x();
export function foo () {
return 42;
}

3
test/form/aggressive/main.js

@ -1,3 +0,0 @@
import { foo } from './foo';
assert.equal( foo(), 42 );

8
test/function/aggressive-mode/_config.js

@ -1,8 +0,0 @@
module.exports = {
// solo: true,
// show: true,
description: 'aggressive mode distinguishes between necessary and probably-not-necessary side-effects',
options: {
aggressive: true
}
};

11
test/function/aggressive-mode/foo.js

@ -1,11 +0,0 @@
function Foo () {}
Foo.prototype = {
answer: function () {
return 42;
}
};
export default function foo () {
return new Foo();
}

3
test/function/aggressive-mode/main.js

@ -1,3 +0,0 @@
import foo from './foo';
assert.equal( foo().answer(), 42 );
Loading…
Cancel
Save