Browse Source

disregard effects in dead branches

rewrite
Rich-Harris 8 years ago
parent
commit
ef676b4081
  1. 12
      src/ast/nodes/ConditionalExpression.js
  2. 26
      src/ast/nodes/IfStatement.js
  3. 3
      test/form/side-effect-q/_config.js
  4. 5
      test/form/side-effect-q/_expected/amd.js
  5. 2
      test/form/side-effect-q/_expected/cjs.js
  6. 0
      test/form/side-effect-q/_expected/es.js
  7. 6
      test/form/side-effect-q/_expected/iife.js
  8. 9
      test/form/side-effect-q/_expected/umd.js
  9. 5
      test/form/side-effect-q/main.js

12
src/ast/nodes/ConditionalExpression.js

@ -4,13 +4,13 @@ import { UNKNOWN } from '../values.js';
export default class ConditionalExpression extends Node { export default class ConditionalExpression extends Node {
initialise ( scope ) { initialise ( scope ) {
if ( this.module.bundle.treeshake ) { if ( this.module.bundle.treeshake ) {
const testValue = this.test.getValue(); this.testValue = this.test.getValue();
if ( testValue === UNKNOWN ) { if ( this.testValue === UNKNOWN ) {
super.initialise( scope ); super.initialise( scope );
} }
else if ( testValue ) { else if ( this.testValue ) {
this.consequent.initialise( scope ); this.consequent.initialise( scope );
this.alternate = null; this.alternate = null;
} else if ( this.alternate ) { } else if ( this.alternate ) {
@ -47,13 +47,11 @@ export default class ConditionalExpression extends Node {
} }
else { else {
const testValue = this.test.getValue(); if ( this.testValue === UNKNOWN ) {
if ( testValue === UNKNOWN ) {
super.render( code, es ); super.render( code, es );
} }
else if ( testValue ) { else if ( this.testValue ) {
code.remove( this.start, this.consequent.start ); code.remove( this.start, this.consequent.start );
code.remove( this.consequent.end, this.end ); code.remove( this.consequent.end, this.end );
} else { } else {

26
src/ast/nodes/IfStatement.js

@ -3,26 +3,6 @@ import { UNKNOWN } from '../values.js';
// TODO DRY this out // TODO DRY this out
export default class IfStatement extends Node { export default class IfStatement extends Node {
bind ( scope ) {
if ( this.module.bundle.treeshake ) {
if ( this.testValue === UNKNOWN ) {
super.bind( scope );
}
else if ( this.testValue ) {
this.consequent.bind( scope );
this.alternate = null;
} else if ( this.alternate ) {
this.alternate.bind( scope );
this.consequent = null;
}
}
else {
super.bind( scope );
}
}
initialise ( scope ) { initialise ( scope ) {
this.testValue = this.test.getValue(); this.testValue = this.test.getValue();
@ -33,8 +13,10 @@ export default class IfStatement extends Node {
else if ( this.testValue ) { else if ( this.testValue ) {
this.consequent.initialise( scope ); this.consequent.initialise( scope );
} else if ( this.alternate ) { this.alternate = null;
this.alternate.initialise( scope ); } else {
if ( this.alternate ) this.alternate.initialise( scope );
this.consequent = null;
} }
} }

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

@ -0,0 +1,3 @@
module.exports = {
description: 'discards effects in conditional expressions with known test values'
};

5
test/form/side-effect-q/_expected/amd.js

@ -0,0 +1,5 @@
define(function () { 'use strict';
});

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

@ -0,0 +1,2 @@
'use strict';

0
test/form/side-effect-q/_expected/es.js

6
test/form/side-effect-q/_expected/iife.js

@ -0,0 +1,6 @@
(function () {
'use strict';
}());

9
test/form/side-effect-q/_expected/umd.js

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

5
test/form/side-effect-q/main.js

@ -0,0 +1,5 @@
var x = true ? foo () : bar();
function foo () {
return 'should be removed, because x is unused';
}
Loading…
Cancel
Save