forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpr.qll
More file actions
2778 lines (2474 loc) · 64.4 KB
/
Expr.qll
File metadata and controls
2778 lines (2474 loc) · 64.4 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Provides classes for working with expressions.
*/
import javascript
/**
* A program element that is either an expression or a type annotation.
*
* Examples:
*
* ```
* x + 1
* string[]
* ```
*/
class ExprOrType extends @exprortype, Documentable {
/** Gets the statement in which this expression or type appears. */
Stmt getEnclosingStmt() { enclosingStmt(this, result) }
/** Gets the function in which this expression or type appears, if any. */
Function getEnclosingFunction() { result = getContainer() }
/**
* Gets the JSDoc comment associated with this expression or type or its parent statement, if any.
*/
override JSDoc getDocumentation() {
result = getOwnDocumentation()
or
// if there is no JSDoc for the expression itself, check the enclosing property or statement
not exists(getOwnDocumentation()) and
(
exists(Property prop | prop = getParent() | result = prop.getDocumentation())
or
exists(MethodDeclaration decl | decl = getParent() | result = decl.getDocumentation())
or
exists(VariableDeclarator decl | decl = getParent() | result = decl.getDocumentation())
or
exists(DeclStmt stmt | this = stmt.getDecl(0) | result = stmt.getDocumentation())
or
exists(DotExpr dot | this = dot.getProperty() | result = dot.getDocumentation())
or
exists(AssignExpr e | this = e.getRhs() | result = e.getDocumentation())
or
exists(ParExpr p | this = p.getExpression() | result = p.getDocumentation())
)
}
/** Gets a JSDoc comment that is immediately before this expression or type (ignoring parentheses). */
private JSDoc getOwnDocumentation() {
exists(Token tk | tk = result.getComment().getNextToken() |
tk = this.getFirstToken()
or
exists(Expr p | p.getUnderlyingValue() = this | tk = p.getFirstToken())
)
}
/**
* Gets this expression or type, with any surrounding parentheses removed.
*
* Also see `getUnderlyingValue` and `getUnderlyingReference`.
*/
ExprOrType stripParens() { result = this }
/**
* Gets the innermost reference that this expression evaluates to, if any.
*
* Examples:
*
* - a variable or property access: the access itself.
* - a parenthesized expression `(e)`: the underlying reference of `e`.
* - a TypeScript type assertion `e as T`: the underlying reference of `e`.
*
* Also see `getUnderlyingValue` and `stripParens`.
*/
Expr getUnderlyingReference() { none() }
/**
* Gets the innermost expression that this expression evaluates to.
*
* Examples:
*
* - a parenthesised expression `(e)`: the underlying value of `e`.
* - a sequence expression `e1, e2`: the underlying value of `e2`.
* - an assignment expression `v = e`: the underlying value of `e`.
* - a TypeScript type assertion `e as T`: the underlying value of `e`.
* - any other expression: the expression itself.
*
* Also see `getUnderlyingReference` and `stripParens`.
*/
Expr getUnderlyingValue() { result = this }
}
/**
* An expression.
*
* Example:
*
* ```
* Math.sqrt(x*x + y*y)
* ```
*/
class Expr extends @expr, ExprOrStmt, ExprOrType, AST::ValueNode {
/** Gets this expression, with any surrounding parentheses removed. */
override Expr stripParens() { result = this }
/** Gets the constant integer value this expression evaluates to, if any. */
int getIntValue() { none() }
/** Gets the constant string value this expression evaluates to, if any. */
cached
string getStringValue() { result = getStringValue(this) }
/** Holds if this expression is impure, that is, its evaluation could have side effects. */
predicate isImpure() { any() }
/**
* Holds if this expression is pure, that is, is its evaluation is guaranteed to be
* side effect-free.
*/
predicate isPure() { not isImpure() }
/**
* Gets the kind of this expression, which is an integer value representing the expression's
* node type.
*
* _Note_: The mapping from node types to integers is considered an implementation detail
* and may change between versions of the extractor.
*/
int getKind() { exprs(this, result, _, _, _) }
override string toString() { exprs(this, _, _, _, result) }
/**
* Gets the expression that is the parent of this expression in the AST, if any.
*
* Note that for property names and property values the associated object expression or pattern
* is returned, skipping the property node itself (which is not an expression).
*/
Expr getParentExpr() {
this = result.getAChildExpr()
or
exists(Property prop |
result = prop.getParent() and
this = prop.getAChildExpr()
)
}
/**
* Holds if this expression accesses the global variable `g`, either directly
* or through the `window` object.
*/
predicate accessesGlobal(string g) { flow().accessesGlobal(g) }
/**
* Holds if this expression may evaluate to `s`.
*/
predicate mayHaveStringValue(string s) { flow().mayHaveStringValue(s) }
/**
* Holds if this expression may evaluate to `b`.
*/
predicate mayHaveBooleanValue(boolean b) { flow().mayHaveBooleanValue(b) }
/**
* Holds if this expression may refer to the initial value of parameter `p`.
*/
predicate mayReferToParameter(Parameter p) { flow().mayReferToParameter(p) }
/**
* Gets the static type of this expression, as determined by the TypeScript type system.
*
* Has no result if the expression is in a JavaScript file or in a TypeScript
* file that was extracted without type information.
*/
Type getType() { ast_node_type(this, result) }
/**
* Holds if the syntactic context that the expression appears in relies on the expression
* being non-null/non-undefined.
*
* A context relies on the subexpression being non-null/non-undefined if either...
*
* * Using null or undefined would cause a runtime error
* * Using null or undefined would cause no error due to type conversion, but the
* behavior in the broader context is sufficiently non-obvious to warrant explicitly
* converting to ensure that readers understand the intent
*/
predicate inNullSensitiveContext() {
exists(ExprOrStmt ctx |
// bases cases
this = ctx.(PropAccess).getBase()
or
this = ctx.(IndexExpr).getPropertyNameExpr()
or
this = ctx.(InvokeExpr).getCallee()
or
this = ctx.(BinaryExpr).getAnOperand() and
not ctx instanceof LogicalBinaryExpr and // x LOGOP y is fine because of implicit conversion
not ctx instanceof EqualityTest and // x EQOP y is fine because of implicit conversion and lack thereof
not ctx.(BitOrExpr).getAnOperand().(NumberLiteral).getIntValue() = 0 and // x | 0 is fine because it's used to convert to numbers
not ctx.(RShiftExpr).getRightOperand().(NumberLiteral).getIntValue() = 0 and // x >> 0 is fine because it's used to convert to numbers
not ctx.(URShiftExpr).getRightOperand().(NumberLiteral).getIntValue() = 0 // x >>> 0 is fine because it's used to convert to numbers
or
this = ctx.(UnaryExpr).getOperand() and
not ctx instanceof LogNotExpr and // !x is fine because of implicit conversion
not ctx instanceof PlusExpr and // +x is fine because of implicit conversion
not ctx instanceof VoidExpr // void x is fine because it explicitly is for capturing void things
or
this = ctx.(UpdateExpr).getOperand()
or
this = ctx.(CompoundAssignExpr).getLhs()
or
this = ctx.(CompoundAssignExpr).getRhs()
or
this = ctx.(AssignExpr).getRhs() and
ctx.(AssignExpr).getLhs() instanceof DestructuringPattern
or
this = ctx.(SpreadElement).getOperand()
or
this = ctx.(ForOfStmt).getIterationDomain()
or
// recursive cases
this = ctx.(ParExpr).getExpression() and
ctx.(ParExpr).inNullSensitiveContext()
or
this = ctx.(SeqExpr).getLastOperand() and
ctx.(SeqExpr).inNullSensitiveContext()
or
this = ctx.(LogicalBinaryExpr).getRightOperand() and
ctx.(LogicalBinaryExpr).inNullSensitiveContext()
or
this = ctx.(ConditionalExpr).getABranch() and
ctx.(ConditionalExpr).inNullSensitiveContext()
)
}
pragma[inline]
private Stmt getRawEnclosingStmt(Expr e) {
// For performance reasons, we need the enclosing statement without overrides
enclosingStmt(e, result)
}
/**
* Gets the data-flow node where exceptions thrown by this expression will
* propagate if this expression causes an exception to be thrown.
*/
pragma[inline]
DataFlow::Node getExceptionTarget() {
result = getCatchParameterFromStmt(getRawEnclosingStmt(this))
or
not exists(getCatchParameterFromStmt(getRawEnclosingStmt(this))) and
result =
any(DataFlow::FunctionNode f | f.getFunction() = this.getContainer()).getExceptionalReturn()
}
}
cached
private DataFlow::Node getCatchParameterFromStmt(Stmt stmt) {
result =
DataFlow::parameterNode(stmt.getEnclosingTryCatchStmt().getACatchClause().getAParameter())
}
/**
* An identifier.
*
* Example:
*
* ```
* x
* ```
*/
class Identifier extends @identifier, ExprOrType {
/** Gets the name of this identifier. */
string getName() { literals(result, _, this) }
}
/**
* A statement or property label, that is, an identifier that
* does not refer to a variable.
*
* Examples:
*
* ```
* outer: // `outer` is a statement label
* for(i=0; i<a.length; ++i) { // `length` is a property label
* // ...
* }
* ```
*/
class Label extends @label, Identifier, Expr {
override predicate isImpure() { none() }
}
/**
* A literal.
*
* Examples:
*
* ```
* null // null literal
* true // Boolean literal
* 2 // number literal
* 3n // BigInt literal
* "hello" // string literal
* /jsx?/ // regular-expression literal
* ```
*/
class Literal extends @literal, Expr {
/** Gets the value of this literal, as a string. */
string getValue() { literals(result, _, this) }
/**
* Gets the raw source text of this literal, including quotes for
* string literals.
*/
string getRawValue() { literals(_, result, this) }
override predicate isImpure() { none() }
}
/**
* A parenthesized expression.
*
* Example:
*
* ```
* (function() { console.log("Hello, world!"); }())
* ```
*/
class ParExpr extends @parexpr, Expr {
/** Gets the expression within parentheses. */
Expr getExpression() { result = this.getChildExpr(0) }
override Expr stripParens() { result = getExpression().stripParens() }
override int getIntValue() { result = getExpression().getIntValue() }
override predicate isImpure() { getExpression().isImpure() }
override Expr getUnderlyingValue() { result = getExpression().getUnderlyingValue() }
override Expr getUnderlyingReference() { result = getExpression().getUnderlyingReference() }
}
/**
* A `null` literal.
*
* Example:
*
* ```
* null
* ```
*/
class NullLiteral extends @nullliteral, Literal { }
/**
* A Boolean literal, that is, either `true` or `false`.
*
* Examples:
*
* ```
* true
* false
* ```
*/
class BooleanLiteral extends @booleanliteral, Literal { }
/**
* A numeric literal.
*
* Examples:
*
* ```
* 0b101
* 0o31
* 25
* 0xffff
* 6.626e-34
* ```
*/
class NumberLiteral extends @numberliteral, Literal {
/** Gets the integer value of this literal. */
override int getIntValue() { result = getValue().toInt() }
/** Gets the floating point value of this literal. */
float getFloatValue() { result = getValue().toFloat() }
}
/**
* A BigInt literal.
*
* Example:
*
* ```
* 9007199254740991n
* ```
*/
class BigIntLiteral extends @bigintliteral, Literal {
/**
* Gets the integer value of this literal if it can be represented
* as a QL integer value.
*/
override int getIntValue() { result = getValue().toInt() }
/**
* Gets the floating point value of this literal if it can be represented
* as a QL floating point value.
*/
float getFloatValue() { result = getValue().toFloat() }
}
/**
* A string literal, either single-quoted or double-quoted.
*
* Note that template literals are represented by a different class, `TemplateLiteral`.
*
* Examples:
*
* ```
* "Hello, world!\n"
* 'Hello, "world"!'
* ```
*/
class StringLiteral extends @stringliteral, Literal {
/**
* Gets the value of this string literal parsed as a regular expression, if possible.
*
* All string literals have an associated regular expression tree, provided they can
* be parsed without syntax errors.
*/
RegExpTerm asRegExp() { this = result.getParent() }
}
/**
* A regular expression literal.
*
* Note that this class does not cover regular expressions constructed by calling the built-in
* `RegExp` function.
*
* Example:
*
* ```
* /(?i)ab*c(d|e)$/
* ```
*/
class RegExpLiteral extends @regexpliteral, Literal, RegExpParent {
/** Gets the root term of this regular expression literal. */
RegExpTerm getRoot() { this = result.getParent() }
/** Gets the flags of this regular expression. */
string getFlags() { result = getValue().regexpCapture(".*/(\\w*)$", 1) }
/** Holds if this regular expression has an `m` flag. */
predicate isMultiline() { RegExp::isMultiline(getFlags()) }
/** Holds if this regular expression has a `g` flag. */
predicate isGlobal() { RegExp::isGlobal(getFlags()) }
/** Holds if this regular expression has an `i` flag. */
predicate isIgnoreCase() { RegExp::isIgnoreCase(getFlags()) }
/** Holds if this regular expression has an `s` flag. */
predicate isDotAll() { RegExp::isDotAll(getFlags()) }
}
/**
* A `this` expression.
*
* Example:
*
* ```
* this
* ```
*/
class ThisExpr extends @thisexpr, Expr {
override predicate isImpure() { none() }
/**
* Gets the function whose `this` binding this expression refers to,
* which is the nearest enclosing non-arrow function.
*/
Function getBinder() { result = getEnclosingFunction().getThisBinder() }
/**
* Gets the function or top-level whose `this` binding this expression refers to,
* which is the nearest enclosing non-arrow function or top-level.
*/
StmtContainer getBindingContainer() {
result = getContainer().(Function).getThisBindingContainer()
or
result = getContainer().(TopLevel)
}
}
/**
* An array literal.
*
* Examples:
*
* ```
* [ 1, , [ 3, 4 ] ]
* ```
*/
class ArrayExpr extends @arrayexpr, Expr {
/** Gets the `i`th element of this array literal. */
Expr getElement(int i) { result = this.getChildExpr(i) }
/** Gets an element of this array literal. */
Expr getAnElement() { result = this.getAChildExpr() }
/** Gets the number of elements in this array literal. */
int getSize() { arraySize(this, result) }
/**
* Holds if this array literal includes a trailing comma after the
* last element.
*/
predicate hasTrailingComma() { this.getLastToken().getPreviousToken().getValue() = "," }
/** Holds if the `i`th element of this array literal is omitted. */
predicate elementIsOmitted(int i) {
i in [0 .. getSize() - 1] and
not exists(getElement(i))
}
/** Holds if this array literal has an omitted element. */
predicate hasOmittedElement() { elementIsOmitted(_) }
override predicate isImpure() { getAnElement().isImpure() }
}
/**
* An object literal, containing zero or more property definitions.
*
* Example:
*
* ```
* var p = { // object literal containing five property definitions
* x: 1,
* y: 1,
* diag: function() { return this.x - this.y; },
* get area() { return this.x * this.y; },
* set area(a) { this.x = Math.sqrt(a); this.y = Math.sqrt(a); }
* };
* ```
*/
class ObjectExpr extends @objexpr, Expr {
/** Gets the `i`th property in this object literal. */
Property getProperty(int i) { properties(result, this, i, _, _) }
/** Gets a property in this object literal. */
Property getAProperty() { exists(int i | result = this.getProperty(i)) }
/** Gets the number of properties in this object literal. */
int getNumProperty() { result = count(this.getAProperty()) }
/** Gets the property with the given name, if any. */
Property getPropertyByName(string name) {
result = this.getAProperty() and
result.getName() = name
}
/**
* Holds if this object literal includes a trailing comma after the
* last property.
*/
predicate hasTrailingComma() { this.getLastToken().getPreviousToken().getValue() = "," }
override predicate isImpure() { getAProperty().isImpure() }
}
/**
* A property definition in an object literal, which may be either
* a value property, a property getter, or a property setter.
*
* Note that property definitions are not expressions.
*
* Examples:
*
* ```
* var p = {
* x: 1, // value property
* y: 1, // value property
* diag: function() { return this.x - this.y; }, // value property
* get area() { return this.x * this.y; }, // property getter
* set area(a) { this.x = Math.sqrt(a); this.y = Math.sqrt(a); } // property setter
* }
* ```
*/
class Property extends @property, Documentable {
Property() {
// filter out property patterns and JSX attributes
exists(ObjectExpr obj | properties(this, obj, _, _, _))
}
/**
* Gets the expression specifying the name of this property.
*
* For normal properties, this is either an identifier, a string literal, or a
* numeric literal; for computed properties it can be an arbitrary expression;
* for spread properties, it is not defined.
*/
Expr getNameExpr() { result = this.getChildExpr(0) }
/** Gets the expression specifying the initial value of this property. */
Expr getInit() { result = this.getChildExpr(1) }
/** Gets the name of this property. */
string getName() {
not isComputed() and result = getNameExpr().(Identifier).getName()
or
result = getNameExpr().(Literal).getValue()
}
/** Holds if the name of this property is computed. */
predicate isComputed() { isComputed(this) }
/** Holds if this property is defined using method syntax. */
predicate isMethod() { isMethod(this) }
/** Holds if this property is defined using shorthand syntax. */
predicate isShorthand() { getNameExpr().getLocation() = getInit().getLocation() }
/** Gets the object literal this property belongs to. */
ObjectExpr getObjectExpr() { properties(this, result, _, _, _) }
/** Gets the (0-based) index at which this property appears in its enclosing literal. */
int getIndex() { this = getObjectExpr().getProperty(result) }
/**
* Holds if this property is impure, that is, the evaluation of its name or
* its initializer expression could have side effects.
*/
predicate isImpure() {
isComputed() and getNameExpr().isImpure()
or
getInit().isImpure()
}
override string toString() { properties(this, _, _, _, result) }
override ControlFlowNode getFirstControlFlowNode() {
result = getNameExpr().getFirstControlFlowNode()
or
not exists(getNameExpr()) and result = getInit().getFirstControlFlowNode()
}
/**
* Gets the kind of this property, which is an opaque integer
* value indicating whether this property is a value property,
* a property getter, or a property setter.
*/
int getKind() { properties(this, _, _, result, _) }
/**
* Gets the `i`th decorator applied to this property.
*
* For example, the property `@A @B x: 42` has
* `@A` as its 0th decorator, and `@B` as its first decorator.
*/
Decorator getDecorator(int i) { result = getChildExpr(-(i + 1)) }
/**
* Gets a decorator applied to this property.
*
* For example, the property `@A @B x: 42` has
* decorators `@A` and `@B`.
*/
Decorator getADecorator() { result = getDecorator(_) }
}
/**
* A value property definition in an object literal.
*
* Examples:
*
* ```
* var p = {
* x: 1, // value property
* y: 1, // value property
* diag: function() { return this.x - this.y; }, // value property
* }
* ```
*/
class ValueProperty extends Property, @value_property { }
/**
* A property getter or setter in an object literal.
*
* Examples:
*
* ```
* var p = {
* x: 1,
* y: 1,
* diag: function() { return this.x - this.y; },
* get area() { return this.x * this.y; }, // property getter
* set area(a) { this.x = Math.sqrt(a); this.y = Math.sqrt(a); } // property setter
* }
* ```
*/
class PropertyAccessor extends Property, @property_accessor {
override FunctionExpr getInit() { result = Property.super.getInit() }
}
/**
* A property getter in an object literal.
*
* Example:
*
* ```
* var p = {
* x: 1,
* y: 1,
* diag: function() { return this.x - this.y; },
* get area() { return this.x * this.y; }, // property getter
* set area(a) { this.x = Math.sqrt(a); this.y = Math.sqrt(a); }
* }
* ```
*/
class PropertyGetter extends PropertyAccessor, @property_getter { }
/**
* A property setter in an object literal.
*
* Example:
*
* ```
* var p = {
* x: 1,
* y: 1,
* diag: function() { return this.x - this.y; },
* get area() { return this.x * this.y; },
* set area(a) { this.x = Math.sqrt(a); this.y = Math.sqrt(a); } // property setter
* }
* ```
*/
class PropertySetter extends PropertyAccessor, @property_setter { }
/**
* A spread property in an object literal.
*
* The initializer of a spread property is always
* a `SpreadElement`.
*
* Example:
*
* ```
* var options = {
* ...defaultOptions, // spread property
* password: pwd
* }
* ```
*/
class SpreadProperty extends Property {
SpreadProperty() { not exists(getNameExpr()) }
}
/**
* A (non-arrow) function expression.
*
* Examples:
*
* ```
* var greet =
* function g() { // function expression with name `g`
* console.log("Hi!");
* };
*
* class C {
* m() { // methods are (anonymous) function expressions
* return 1;
* }
* }
* ```
*/
class FunctionExpr extends @functionexpr, Expr, Function {
/** Holds if this function expression is a property setter. */
predicate isSetter() { exists(PropertySetter s | s.getInit() = this) }
/** Holds if this function expression is a property getter. */
predicate isGetter() { exists(PropertyGetter g | g.getInit() = this) }
/** Holds if this function expression is a property accessor. */
predicate isAccessor() { exists(PropertyAccessor acc | acc.getInit() = this) }
/** Gets the statement in which this function expression appears. */
override Stmt getEnclosingStmt() { result = Expr.super.getEnclosingStmt() }
override StmtContainer getEnclosingContainer() { result = Expr.super.getContainer() }
override predicate isImpure() { none() }
}
/**
* An arrow function expression.
*
* Examples:
*
* ```
* var greet =
* () => console.log("Hi!"); // arrow function expression
*/
class ArrowFunctionExpr extends @arrowfunctionexpr, Expr, Function {
/** Gets the statement in which this expression appears. */
override Stmt getEnclosingStmt() { result = Expr.super.getEnclosingStmt() }
override StmtContainer getEnclosingContainer() { result = Expr.super.getContainer() }
override predicate isImpure() { none() }
override Function getThisBinder() { result = getEnclosingContainer().(Function).getThisBinder() }
}
/**
* A sequence expression (also known as comma expression).
*
* Example:
*
* ```
* x++, y++
* ```
*/
class SeqExpr extends @seqexpr, Expr {
/** Gets the `i`th expression in this sequence. */
Expr getOperand(int i) { result = getChildExpr(i) }
/** Gets an expression in this sequence. */
Expr getAnOperand() { result = getOperand(_) }
/** Gets the number of expressions in this sequence. */
int getNumOperands() { result = count(getOperand(_)) }
/** Gets the last expression in this sequence. */
Expr getLastOperand() { result = getOperand(getNumOperands() - 1) }
override predicate isImpure() { getAnOperand().isImpure() }
override Expr getUnderlyingValue() { result = getLastOperand().getUnderlyingValue() }
}
/**
* A conditional expression.
*
* Example:
*
* ```
* x == 0 ? 0 : 1/x
* ```
*/
class ConditionalExpr extends @conditionalexpr, Expr {
/** Gets the condition expression of this conditional. */
Expr getCondition() { result = getChildExpr(0) }
/** Gets the 'then' expression of this conditional. */
Expr getConsequent() { result = getChildExpr(1) }
/** Gets the 'else' expression of this conditional. */
Expr getAlternate() { result = getChildExpr(2) }
/** Gets either the 'then' or the 'else' expression of this conditional. */
Expr getABranch() { result = getConsequent() or result = getAlternate() }
override predicate isImpure() {
getCondition().isImpure() or
getABranch().isImpure()
}
}
/**
* An invocation expression, that is, either a function call or
* a `new` expression.
*
* Examples:
*
* ```
* f(1)
* x.f(1, ...args)
* f.call(x, 1)
* new Array(16)
* ```
*/
class InvokeExpr extends @invokeexpr, Expr {
/** Gets the expression specifying the function to be called. */
Expr getCallee() { result = this.getChildExpr(-1) }
/** Gets the name of the function or method being invoked, if it can be determined. */
string getCalleeName() {
exists(Expr callee | callee = getCallee().getUnderlyingValue() |
result = callee.(Identifier).getName() or
result = callee.(PropAccess).getPropertyName()
)
}
/** Gets the `i`th argument of this invocation. */
Expr getArgument(int i) { i >= 0 and result = this.getChildExpr(i) }
/** Gets an argument of this invocation. */
Expr getAnArgument() { result = getArgument(_) }
/** Gets the last argument of this invocation, if any. */
Expr getLastArgument() { result = getArgument(getNumArgument() - 1) }
/** Gets the number of arguments of this invocation. */
int getNumArgument() { result = count(getAnArgument()) }
/** Gets the `i`th type argument of this invocation. */
TypeExpr getTypeArgument(int i) { i >= 0 and result = this.getChildTypeExpr(-i - 2) }
/** Gets a type argument of this invocation. */
TypeExpr getATypeArgument() { result = getTypeArgument(_) }
/** Gets the number of type arguments of this invocation. */
int getNumTypeArgument() { result = count(getATypeArgument()) }
override ControlFlowNode getFirstControlFlowNode() {
result = getCallee().getFirstControlFlowNode()
}
/** Holds if the argument list of this function has a trailing comma. */
predicate hasTrailingComma() {
// check whether the last token of this invocation is a closing
// parenthesis, which itself is preceded by a comma
exists(PunctuatorToken rparen | rparen.getValue() = ")" |
rparen = getLastToken() and
rparen.getPreviousToken().getValue() = ","
)
}
/**
* Holds if the `i`th argument of this invocation is a spread element.
*/
predicate isSpreadArgument(int i) { getArgument(i).stripParens() instanceof SpreadElement }
/**
* Holds if the `i`th argument of this invocation is an object literal whose property
* `name` is set to `value`.
*
* This predicate is an approximation, computed using only local data flow.
*/
predicate hasOptionArgument(int i, string name, Expr value) {
value = flow().(DataFlow::InvokeNode).getOptionArgument(i, name).asExpr()
}
/**
* Gets the call signature of the invoked function, as determined by the TypeScript
* type system, with overloading resolved and type parameters substituted.
*
* This predicate is only populated for files extracted with full TypeScript extraction.
*/
CallSignatureType getResolvedSignature() { invoke_expr_signature(this, result) }
/**
* Gets the index of the targeted call signature among the overload signatures
* on the invoked function.
*
* This predicate is only populated for files extracted with full TypeScript extraction.
*/
int getResolvedOverloadIndex() { invoke_expr_overload_index(this, result) }
/**
* Gets the canonical name of the static call target, as determined by the TypeScript type system.
*
* This predicate is only populated for files extracted with full TypeScript extraction.
*/
CanonicalFunctionName getResolvedCalleeName() { ast_node_symbol(this, result) }
/**
* Gets the statically resolved target function, as determined by the TypeScript type system, if any.
*
* This predicate is only populated for files extracted with full TypeScript extraction.
*
* Note that the resolved function may be overridden in a subclass and thus is not
* necessarily the actual target of this invocation at runtime.
*/
Function getResolvedCallee() { result = getResolvedCalleeName().getImplementation() }
}
/**
* A `new` expression.
*
* Example:
*
* ```
* new Array(16)
* ```
*/
class NewExpr extends @newexpr, InvokeExpr { }
/**
* A function call expression.
*
* Examples:
*
* ```
* f()
* require('express')()
* x.f()
* ```