-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ExecutionPathOptions.js
268 lines (236 loc) · 7.45 KB
/
ExecutionPathOptions.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import Immutable from 'immutable';
const OPTION_IGNORED_LABELS = 'IGNORED_LABELS';
const OPTION_ACCESSED_NODES = 'ACCESSED_NODES';
const OPTION_ARGUMENTS_VARIABLES = 'ARGUMENTS_VARIABLES';
const OPTION_ASSIGNED_NODES = 'ASSIGNED_NODES';
const OPTION_IGNORE_BREAK_STATEMENTS = 'IGNORE_BREAK_STATEMENTS';
const OPTION_IGNORE_RETURN_AWAIT_YIELD = 'IGNORE_RETURN_AWAIT_YIELD';
const OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS = 'NODES_CALLED_AT_PATH_WITH_OPTIONS';
const OPTION_REPLACED_VARIABLE_INITS = 'REPLACED_VARIABLE_INITS';
const OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH = 'RETURN_EXPRESSIONS_ACCESSED_AT_PATH';
const OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH = 'RETURN_EXPRESSIONS_ASSIGNED_AT_PATH';
const OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH = 'RETURN_EXPRESSIONS_CALLED_AT_PATH';
const RESULT_KEY = {};
/** Wrapper to ensure immutability */
export default class ExecutionPathOptions {
/**
* @returns {ExecutionPathOptions}
*/
static create () {
return new this( Immutable.Map() );
}
constructor ( optionValues ) {
this._optionValues = optionValues;
}
/**
* @param {string} option - The name of an option
* @returns {*} Its value
*/
get ( option ) {
return this._optionValues.get( option );
}
/**
* Returns a new ExecutionPathOptions instance with the given option removed.
* Does not mutate the current instance. Also works in sub-classes.
* @param {string} option - The name of an option
* @returns {*} Its value
*/
remove ( option ) {
return new this.constructor( this._optionValues.remove( option ) );
}
/**
* Returns a new ExecutionPathOptions instance with the given option set to a new value.
* Does not mutate the current instance. Also works in sub-classes.
* @param {string} option - The name of an option
* @param {*} value - The new value of the option
* @returns {ExecutionPathOptions} A new options instance
*/
set ( option, value ) {
return new this.constructor( this._optionValues.set( option, value ) );
}
setIn ( optionPath, value ) {
return new this.constructor( this._optionValues.setIn( optionPath, value ) );
}
/**
* @param {String[]} path
* @param {Node} node
* @return {ExecutionPathOptions}
*/
addAccessedNodeAtPath ( path, node ) {
return this.setIn( [ OPTION_ACCESSED_NODES, node, ...path, RESULT_KEY ], true );
}
/**
* @param {String[]} path
* @param {CallExpression|Property} callExpression
* @return {ExecutionPathOptions}
*/
addAccessedReturnExpressionAtPath ( path, callExpression ) {
return this.setIn( [ OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH, callExpression, ...path, RESULT_KEY ], true );
}
/**
* @param {String[]} path
* @param {Node} node
* @return {ExecutionPathOptions}
*/
addAssignedNodeAtPath ( path, node ) {
return this.setIn( [ OPTION_ASSIGNED_NODES, node, ...path, RESULT_KEY ], true );
}
/**
* @param {String[]} path
* @param {CallExpression|Property} callExpression
* @return {ExecutionPathOptions}
*/
addAssignedReturnExpressionAtPath ( path, callExpression ) {
return this.setIn( [ OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH, callExpression, ...path, RESULT_KEY ], true );
}
/**
* @param {String[]} path
* @param {Node} node
* @param {CallOptions} callOptions
* @return {ExecutionPathOptions}
*/
addCalledNodeAtPathWithOptions ( path, node, callOptions ) {
return this.setIn( [ OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS, node, ...path, RESULT_KEY, callOptions ], true );
}
/**
* @param {String[]} path
* @param {CallExpression|Property} callExpression
* @return {ExecutionPathOptions}
*/
addCalledReturnExpressionAtPath ( path, callExpression ) {
return this.setIn( [ OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH, callExpression, ...path, RESULT_KEY ], true );
}
/**
* @return {ParameterVariable[]}
*/
getArgumentsVariables () {
return this.get( OPTION_ARGUMENTS_VARIABLES ) || [];
}
/**
* @return {ExecutionPathOptions}
*/
getHasEffectsWhenCalledOptions () {
return this
.setIgnoreReturnAwaitYield()
.setIgnoreBreakStatements( false )
.setIgnoreNoLabels();
}
/**
* @param {ThisVariable|ParameterVariable} variable
* @returns {Node}
*/
getReplacedVariableInit ( variable ) {
return this._optionValues.getIn( [ OPTION_REPLACED_VARIABLE_INITS, variable ] );
}
/**
* @param {String[]} path
* @param {Node} node
* @return {boolean}
*/
hasNodeBeenAccessedAtPath ( path, node ) {
return this._optionValues.getIn( [ OPTION_ACCESSED_NODES, node, ...path, RESULT_KEY ] );
}
/**
* @param {String[]} path
* @param {Node} node
* @return {boolean}
*/
hasNodeBeenAssignedAtPath ( path, node ) {
return this._optionValues.getIn( [ OPTION_ASSIGNED_NODES, node, ...path, RESULT_KEY ] );
}
/**
* @param {String[]} path
* @param {Node} node
* @param {CallOptions} callOptions
* @return {boolean}
*/
hasNodeBeenCalledAtPathWithOptions ( path, node, callOptions ) {
const previousCallOptions = this._optionValues.getIn( [ OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS, node, ...path, RESULT_KEY ] );
return previousCallOptions && previousCallOptions.find( ( _, otherCallOptions ) => otherCallOptions.equals( callOptions ) );
}
/**
* @param {String[]} path
* @param {CallExpression|Property} callExpression
* @return {boolean}
*/
hasReturnExpressionBeenAccessedAtPath ( path, callExpression ) {
return this._optionValues.getIn( [ OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH, callExpression, ...path, RESULT_KEY ] );
}
/**
* @param {String[]} path
* @param {CallExpression|Property} callExpression
* @return {boolean}
*/
hasReturnExpressionBeenAssignedAtPath ( path, callExpression ) {
return this._optionValues.getIn( [ OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH, callExpression, ...path, RESULT_KEY ] );
}
/**
* @param {String[]} path
* @param {CallExpression|Property} callExpression
* @return {boolean}
*/
hasReturnExpressionBeenCalledAtPath ( path, callExpression ) {
return this._optionValues.getIn( [ OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH, callExpression, ...path, RESULT_KEY ] );
}
/**
* @return {boolean}
*/
ignoreBreakStatements () {
return this.get( OPTION_IGNORE_BREAK_STATEMENTS );
}
/**
* @param {string} labelName
* @return {boolean}
*/
ignoreLabel ( labelName ) {
return this._optionValues.getIn( [ OPTION_IGNORED_LABELS, labelName ] );
}
/**
* @return {boolean}
*/
ignoreReturnAwaitYield () {
return this.get( OPTION_IGNORE_RETURN_AWAIT_YIELD );
}
/**
* @param {ThisVariable|ParameterVariable} variable
* @param {Node} init
* @return {ExecutionPathOptions}
*/
replaceVariableInit ( variable, init ) {
return this.setIn( [ OPTION_REPLACED_VARIABLE_INITS, variable ], init );
}
/**
* @param {ParameterVariable[]} variables
* @return {ExecutionPathOptions}
*/
setArgumentsVariables ( variables ) {
return this.set( OPTION_ARGUMENTS_VARIABLES, variables );
}
/**
* @param {boolean} [value=true]
* @return {ExecutionPathOptions}
*/
setIgnoreBreakStatements ( value = true ) {
return this.set( OPTION_IGNORE_BREAK_STATEMENTS, value );
}
/**
* @param {string} labelName
* @return {ExecutionPathOptions}
*/
setIgnoreLabel ( labelName ) {
return this.setIn( [ OPTION_IGNORED_LABELS, labelName ], true );
}
/**
* @return {ExecutionPathOptions}
*/
setIgnoreNoLabels () {
return this.remove( OPTION_IGNORED_LABELS );
}
/**
* @param {boolean} [value=true]
* @return {ExecutionPathOptions}
*/
setIgnoreReturnAwaitYield ( value = true ) {
return this.set( OPTION_IGNORE_RETURN_AWAIT_YIELD, value );
}
}