-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ConditionalExpression.js
62 lines (53 loc) · 1.71 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
import Node from '../Node.js';
import { UNKNOWN_VALUE } from '../values.js';
export default class ConditionalExpression extends Node {
initialiseChildren ( parentScope ) {
if ( this.module.bundle.treeshake ) {
this.testValue = this.test.getValue();
if ( this.testValue === UNKNOWN_VALUE ) {
super.initialiseChildren( parentScope );
} else if ( this.testValue ) {
this.consequent.initialise( this.scope );
this.alternate = null;
} else if ( this.alternate ) {
this.alternate.initialise( this.scope );
this.consequent = null;
}
} else {
super.initialiseChildren( parentScope );
}
}
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.included
|| this.test.hasEffects( options )
|| (this.testValue === UNKNOWN_VALUE && (this.consequent.hasEffects( options ) || this.alternate.hasEffects( options )))
|| (this.testValue ? this.consequent.hasEffects( options ) : this.alternate.hasEffects( options ))
);
}
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 );
}
}
}
}