Skip to content

Commit

Permalink
Merge pull request #1195 from rollup/gh-1061
Browse files Browse the repository at this point in the history
statically analyse LogicalExpression nodes
  • Loading branch information
Rich-Harris authored Dec 29, 2016
2 parents bda138c + 47d3daf commit 1c0aca6
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/ast/nodes/LogicalExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Node from '../Node.js';
import { UNKNOWN } from '../values.js';

const operators = {
'&&': ( left, right ) => left && right,
'||': ( left, right ) => left || right
};

export default class LogicalExpression extends Node {
getValue () {
const leftValue = this.left.getValue();
if ( leftValue === UNKNOWN ) return UNKNOWN;

const rightValue = this.right.getValue();
if ( rightValue === UNKNOWN ) return UNKNOWN;

return operators[ this.operator ]( leftValue, rightValue );
}
}
2 changes: 2 additions & 0 deletions src/ast/nodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Identifier from './Identifier.js';
import IfStatement from './IfStatement.js';
import ImportDeclaration from './ImportDeclaration.js';
import Literal from './Literal.js';
import LogicalExpression from './LogicalExpression.js';
import MemberExpression from './MemberExpression.js';
import NewExpression from './NewExpression.js';
import ObjectExpression from './ObjectExpression.js';
Expand Down Expand Up @@ -59,6 +60,7 @@ export default {
IfStatement,
ImportDeclaration,
Literal,
LogicalExpression,
MemberExpression,
NewExpression,
ObjectExpression,
Expand Down
2 changes: 1 addition & 1 deletion test/form/skips-dead-branches-i/_config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
description: 'skips a dead branch (h)'
description: 'skips a dead branch (i)'
};
3 changes: 3 additions & 0 deletions test/form/skips-dead-branches-j/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'skips a dead branch (j)'
};
7 changes: 7 additions & 0 deletions test/form/skips-dead-branches-j/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function () { 'use strict';

{
console.log( 'true' );
}

});
5 changes: 5 additions & 0 deletions test/form/skips-dead-branches-j/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

{
console.log( 'true' );
}
3 changes: 3 additions & 0 deletions test/form/skips-dead-branches-j/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
console.log( 'true' );
}
8 changes: 8 additions & 0 deletions test/form/skips-dead-branches-j/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function () {
'use strict';

{
console.log( 'true' );
}

}());
11 changes: 11 additions & 0 deletions test/form/skips-dead-branches-j/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

{
console.log( 'true' );
}

})));
5 changes: 5 additions & 0 deletions test/form/skips-dead-branches-j/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if ( true && true ) {
console.log( 'true' );
} else {
console.log( 'false' );
}

0 comments on commit 1c0aca6

Please sign in to comment.