Skip to content

Commit

Permalink
Bind calls involving conditional and logical expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 8, 2017
1 parent 4d1f946 commit 80dff0f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/ast/Node.js
@@ -1,9 +1,8 @@
/* eslint-disable no-unused-vars */

import { locate } from 'locate-character';
import { UNKNOWN_VALUE } from './values.js';
import { UNKNOWN_ASSIGNMENT, UNKNOWN_VALUE } from './values.js';
import ExecutionPathOptions from './ExecutionPathOptions';
import { UNKNOWN_ASSIGNMENT } from './values';

export default class Node {
constructor () {
Expand Down
24 changes: 17 additions & 7 deletions src/ast/nodes/ConditionalExpression.js
Expand Up @@ -3,16 +3,15 @@ import { UNKNOWN_VALUE } from '../values.js';

export default class ConditionalExpression extends Node {
bindAssignmentAtPath ( path, expression ) {
if ( this.testValue === UNKNOWN_VALUE ) {
this.consequent.bindAssignmentAtPath( path, expression );
this.alternate.bindAssignmentAtPath( path, expression );
} else {
this.testValue
? this.consequent.bindAssignmentAtPath( path, expression )
: this.alternate.bindAssignmentAtPath( path, expression );
if ( path.length > 0 ) {
this._forEachRelevantBranch( node => node.bindAssignmentAtPath( path, expression ) );
}
}

bindCallAtPath ( path, callOptions ) {
this._forEachRelevantBranch( node => node.bindCallAtPath( path, callOptions ) );
}

getValue () {
const testValue = this.test.getValue();
if ( testValue === UNKNOWN_VALUE ) return UNKNOWN_VALUE;
Expand Down Expand Up @@ -90,6 +89,17 @@ export default class ConditionalExpression extends Node {
node.someReturnExpressionWhenCalledAtPath( path, callOptions, predicateFunction, options ) );
}

_forEachRelevantBranch ( callback ) {
if ( this.testValue === UNKNOWN_VALUE ) {
callback( this.consequent );
callback( this.alternate );
} else {
this.testValue
? callback( this.consequent )
: callback( this.alternate );
}
}

_someRelevantBranch ( predicateFunction ) {
return this.testValue === UNKNOWN_VALUE
? predicateFunction( this.consequent ) || predicateFunction( this.alternate )
Expand Down
22 changes: 22 additions & 0 deletions src/ast/nodes/LogicalExpression.js
Expand Up @@ -2,6 +2,16 @@ import Node from '../Node.js';
import { UNKNOWN_VALUE } from '../values.js';

export default class LogicalExpression extends Node {
bindAssignmentAtPath ( path, expression ) {
if ( path.length > 0 ) {
this._forEachRelevantBranch( node => node.bindAssignmentAtPath( path, expression ) );
}
}

bindCallAtPath ( path, callOptions ) {
this._forEachRelevantBranch( node => node.bindCallAtPath( path, callOptions ) );
}

getValue () {
const leftValue = this.left.getValue();
if ( leftValue === UNKNOWN_VALUE ) return UNKNOWN_VALUE;
Expand Down Expand Up @@ -43,6 +53,18 @@ export default class LogicalExpression extends Node {
node.someReturnExpressionWhenCalledAtPath( path, callOptions, predicateFunction, options ) );
}

_forEachRelevantBranch ( callback ) {
const leftValue = this.left.getValue();
if ( leftValue === UNKNOWN_VALUE ) {
callback( this.left );
callback( this.right );
} else if ( (leftValue && this.operator === '||') || (!leftValue && this.operator === '&&') ) {
callback( this.left );
} else {
callback( this.right );
}
}

_someRelevantBranch ( predicateFunction ) {
const leftValue = this.left.getValue();
if ( leftValue === UNKNOWN_VALUE ) {
Expand Down
@@ -0,0 +1,8 @@
var assert = require( 'assert' );

module.exports = {
description: 'Associates parameters with their call arguments across other expressions',
exports: function ( exports ) {
assert.equal( exports.bar, 'present' );
}
};
@@ -0,0 +1,11 @@
const foo = { mightBeExported: {} };
const exported = {};

function assignExported ( obj ) {
obj.mightBeExported = exported;
}

(Math.random() < 0.5 ? true && assignExported : false || assignExported)( foo );
foo.mightBeExported.bar = 'present';

export default exported;

0 comments on commit 80dff0f

Please sign in to comment.