Skip to content

Commit 034fce0

Browse files
committed
Ruby: show constant value type in tests
1 parent 0613fda commit 034fce0

File tree

3 files changed

+1772
-1761
lines changed

3 files changed

+1772
-1761
lines changed

ruby/ql/lib/codeql/ruby/ast/Constant.qll

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,37 @@ private import internal.TreeSitter
88
/** A constant value. */
99
class ConstantValue extends TConstantValue {
1010
/** Gets a textual representation of this constant value. */
11-
final string toString() {
12-
result = this.getInt().toString()
11+
final string toString() { this.hasValueWithType(result, _) }
12+
13+
/** Gets a string describing the type of this constant value. */
14+
string getValueType() { this.hasValueWithType(_, result) }
15+
16+
private predicate hasValueWithType(string value, string type) {
17+
value = this.getInt().toString() and type = "int"
1318
or
14-
result = this.getFloat().toString()
19+
value = this.getFloat().toString() and type = "float"
1520
or
1621
exists(int numerator, int denominator |
1722
this.isRational(numerator, denominator) and
18-
result = numerator + "/" + denominator
23+
value = numerator + "/" + denominator and
24+
type = "rational"
1925
)
2026
or
2127
exists(float real, float imaginary |
2228
this.isComplex(real, imaginary) and
23-
result = real + "+" + imaginary + "i"
29+
value = real + "+" + imaginary + "i" and
30+
type = "complex"
2431
)
2532
or
26-
result = this.getString()
33+
value = this.getString() and type = "string"
2734
or
28-
result = ":" + this.getSymbol()
35+
value = ":" + this.getSymbol() and type = "symbol"
2936
or
30-
result = this.getRegExp()
37+
value = this.getRegExp() and type = "regexp"
3138
or
32-
result = this.getBoolean().toString()
39+
value = this.getBoolean().toString() and type = "boolean"
3340
or
34-
this.isNil() and result = "nil"
41+
this.isNil() and value = "nil" and type = "nil"
3542
}
3643

3744
/** Gets the integer value, if this is an integer. */

0 commit comments

Comments
 (0)