Browse Source

statically analyse LogicalExpression nodes (#1061)

gh-786
Rich-Harris 8 years ago
parent
commit
47d3daf9a5
  1. 19
      src/ast/nodes/LogicalExpression.js
  2. 2
      src/ast/nodes/index.js
  3. 2
      test/form/skips-dead-branches-i/_config.js
  4. 3
      test/form/skips-dead-branches-j/_config.js
  5. 7
      test/form/skips-dead-branches-j/_expected/amd.js
  6. 5
      test/form/skips-dead-branches-j/_expected/cjs.js
  7. 3
      test/form/skips-dead-branches-j/_expected/es.js
  8. 8
      test/form/skips-dead-branches-j/_expected/iife.js
  9. 11
      test/form/skips-dead-branches-j/_expected/umd.js
  10. 5
      test/form/skips-dead-branches-j/main.js

19
src/ast/nodes/LogicalExpression.js

@ -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
src/ast/nodes/index.js

@ -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';
@ -59,6 +60,7 @@ export default {
IfStatement,
ImportDeclaration,
Literal,
LogicalExpression,
MemberExpression,
NewExpression,
ObjectExpression,

2
test/form/skips-dead-branches-i/_config.js

@ -1,3 +1,3 @@
module.exports = {
description: 'skips a dead branch (h)'
description: 'skips a dead branch (i)'
};

3
test/form/skips-dead-branches-j/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'skips a dead branch (j)'
};

7
test/form/skips-dead-branches-j/_expected/amd.js

@ -0,0 +1,7 @@
define(function () { 'use strict';
{
console.log( 'true' );
}
});

5
test/form/skips-dead-branches-j/_expected/cjs.js

@ -0,0 +1,5 @@
'use strict';
{
console.log( 'true' );
}

3
test/form/skips-dead-branches-j/_expected/es.js

@ -0,0 +1,3 @@
{
console.log( 'true' );
}

8
test/form/skips-dead-branches-j/_expected/iife.js

@ -0,0 +1,8 @@
(function () {
'use strict';
{
console.log( 'true' );
}
}());

11
test/form/skips-dead-branches-j/_expected/umd.js

@ -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
test/form/skips-dead-branches-j/main.js

@ -0,0 +1,5 @@
if ( true && true ) {
console.log( 'true' );
} else {
console.log( 'false' );
}
Loading…
Cancel
Save