-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ConditionalExpression.js
104 lines (87 loc) · 3.04 KB
/
ConditionalExpression.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Node from '../Node.js';
import { UNKNOWN_VALUE } from '../values.js';
export default class ConditionalExpression extends Node {
bindAssignmentAtPath ( path, expression, options ) {
path.length > 0
&& this._forEachRelevantBranch( node => node.bindAssignmentAtPath( path, expression, options ) );
}
forEachReturnExpressionWhenCalledAtPath ( path, callOptions, callback, options ) {
this._forEachRelevantBranch( node => node.forEachReturnExpressionWhenCalledAtPath( path, callOptions, callback, options ) );
}
getValue () {
const testValue = this.test.getValue();
if ( testValue === UNKNOWN_VALUE ) return UNKNOWN_VALUE;
return testValue ? this.consequent.getValue() : this.alternate.getValue();
}
hasEffects ( options ) {
return (
this.test.hasEffects( options )
|| this._someRelevantBranch( node => node.hasEffects( options ) )
);
}
hasEffectsWhenAccessedAtPath ( path, options ) {
return path.length > 0
&& this._someRelevantBranch( node => node.hasEffectsWhenAccessedAtPath( path, options ) );
}
hasEffectsWhenAssignedAtPath ( path, options ) {
return path.length === 0
|| this._someRelevantBranch( node => node.hasEffectsWhenAssignedAtPath( path, options ) );
}
hasEffectsWhenCalledAtPath ( path, callOptions, options ) {
return this._someRelevantBranch( node => node.hasEffectsWhenCalledAtPath( path, callOptions, options ) );
}
initialiseChildren ( parentScope ) {
super.initialiseChildren( parentScope );
if ( this.module.bundle.treeshake ) {
this.testValue = this.test.getValue();
if ( this.testValue === UNKNOWN_VALUE ) {
return;
} else if ( this.testValue ) {
this.alternate = null;
} else if ( this.alternate ) {
this.consequent = null;
}
}
}
render ( code, es ) {
if ( !this.module.bundle.treeshake ) {
super.render( code, es );
}
else {
if ( this.testValue === UNKNOWN_VALUE ) {
super.render( code, es );
}
else {
const branchToRetain = this.testValue ? this.consequent : this.alternate;
code.remove( this.start, branchToRetain.start );
code.remove( branchToRetain.end, this.end );
if ( branchToRetain.type === 'SequenceExpression' ) {
code.prependLeft( branchToRetain.start, '(' );
code.appendRight( branchToRetain.end, ')' );
}
branchToRetain.render( code, es );
}
}
}
someReturnExpressionWhenCalledAtPath ( path, callOptions, predicateFunction, options ) {
return this._someRelevantBranch( 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 )
: this.testValue
? predicateFunction( this.consequent )
: predicateFunction( this.alternate );
}
}