repotoire 0.8.3

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
Documentation
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
//! AST node to `SymbolicValue` conversion.
//!
//! Contains the core `node_to_symbolic()` function that dispatches on
//! tree-sitter node kinds via the language config.

use super::super::configs::LanguageValueConfig;
use super::super::types::{LiteralValue, SymbolicValue};
use super::helpers::{
    extract_binary_op, node_text, parse_float, parse_integer, strip_numeric_suffix, unquote,
};

/// Extract the callee function name from a call expression node.
///
/// Handles simple identifiers (`foo(...)`) and attribute/member access
/// (`obj.method(...)`) across all supported languages.
///
/// Different grammars use different field names:
/// - Python: `function`
/// - JS/TS/Rust/Go/C/C++: `function`
/// - Java: `name` + `object` for method invocations
pub(super) fn extract_callee_name(node: tree_sitter::Node, source: &[u8]) -> String {
    // Most languages use "function" as the field name for the callee
    if let Some(func_node) = node.child_by_field_name("function") {
        return node_text(func_node, source).to_string();
    }

    // Java method_invocation: uses "name" for the method and "object" for the receiver
    if let Some(name_node) = node.child_by_field_name("name") {
        let name = node_text(name_node, source);
        if let Some(obj_node) = node.child_by_field_name("object") {
            let obj = node_text(obj_node, source);
            return format!("{obj}.{name}");
        }
        return name.to_string();
    }

    // Fallback: first named child
    if let Some(first) = node.named_child(0) {
        return node_text(first, source).to_string();
    }

    "<unknown>".to_string()
}

/// Convert a tree-sitter expression node to a `SymbolicValue`.
///
/// Walks the AST node and dispatches based on the language config's node kind
/// tables. Recurses into sub-expressions for compound values.
///
/// # Arguments
/// * `node` - The tree-sitter AST node to convert.
/// * `source` - The full source file as bytes.
/// * `config` - The language-specific configuration table.
/// * `_func_qn` - The qualified name of the enclosing function (for future use).
//
// `node_to_symbolic` dispatches to specialized `convert_*` helpers which
// recurse back through `node_to_symbolic` for child nodes. This is the
// textbook visitor pattern; it terminates because the AST is a finite tree.
// repotoire:ignore[mutual-recursion] — recursive descent over a finite AST.
pub fn node_to_symbolic(
    node: tree_sitter::Node,
    source: &[u8],
    config: &LanguageValueConfig,
    _func_qn: &str,
) -> SymbolicValue {
    let kind = node.kind();

    // --- Identifiers ---
    if LanguageValueConfig::matches(config.identifier_kinds, kind) {
        return SymbolicValue::Variable(node_text(node, source).to_string());
    }

    // --- Boolean literals (check before string since `true`/`false` are simple) ---
    // Some languages (Rust, C#) use the same node kind for both true and false
    // (e.g. `boolean_literal`). When true and false share a kind, disambiguate
    // by inspecting the node text.
    let matches_true = LanguageValueConfig::matches(config.bool_true_kinds, kind);
    let matches_false = LanguageValueConfig::matches(config.bool_false_kinds, kind);
    if matches_true || matches_false {
        if matches_true && matches_false {
            // Shared kind (e.g. Rust boolean_literal) — check text
            let text = node_text(node, source);
            return SymbolicValue::Literal(LiteralValue::Boolean(text == "true"));
        }
        if matches_true {
            return SymbolicValue::Literal(LiteralValue::Boolean(true));
        }
        return SymbolicValue::Literal(LiteralValue::Boolean(false));
    }

    // --- Null ---
    if LanguageValueConfig::matches(config.null_kinds, kind) {
        return SymbolicValue::Literal(LiteralValue::Null);
    }

    // --- Numeric literals ---
    // Some languages (C, C++, JS/TS) use a single node kind for both integers
    // and floats (e.g. `number_literal`, `number`). When the kind appears in
    // both lists, try integer parsing first, then fall back to float.
    let matches_int = LanguageValueConfig::matches(config.integer_literal_kinds, kind);
    let matches_float = LanguageValueConfig::matches(config.float_literal_kinds, kind);
    if matches_int || matches_float {
        let text = node_text(node, source);
        // Strip type suffixes common in C/C++/Rust/Java (e.g. 42L, 3.14f, 100u64)
        let cleaned = strip_numeric_suffix(text);
        if matches_int {
            if let Some(n) = parse_integer(cleaned) {
                return SymbolicValue::Literal(LiteralValue::Integer(n));
            }
        }
        if matches_float {
            if let Some(f) = parse_float(cleaned) {
                return SymbolicValue::Literal(LiteralValue::Float(f));
            }
        }
        return SymbolicValue::Unknown;
    }

    // --- String literals ---
    if LanguageValueConfig::matches(config.string_literal_kinds, kind) {
        return convert_string_literal(node, source, config, _func_qn);
    }

    // --- Binary operators ---
    if LanguageValueConfig::matches(config.binary_op_kinds, kind) {
        let op = extract_binary_op(node, source);

        // Left operand: named field "left" or first named child
        let lhs = node
            .child_by_field_name("left")
            .or_else(|| node.named_child(0))
            .map(|n| node_to_symbolic(n, source, config, _func_qn))
            .unwrap_or(SymbolicValue::Unknown);

        // Right operand: named field "right" or last named child
        let rhs = node
            .child_by_field_name("right")
            .or_else(|| {
                let count = node.named_child_count();
                if count >= 2 {
                    node.named_child(count - 1)
                } else {
                    None
                }
            })
            .map(|n| node_to_symbolic(n, source, config, _func_qn))
            .unwrap_or(SymbolicValue::Unknown);

        return SymbolicValue::BinaryOp(op, Box::new(lhs), Box::new(rhs));
    }

    // --- Function calls ---
    if LanguageValueConfig::matches(config.call_kinds, kind) {
        return convert_call_expr(node, source, config, _func_qn);
    }

    // --- Field/attribute access ---
    if LanguageValueConfig::matches(config.field_access_kinds, kind) {
        return convert_field_access(node, source, config, _func_qn);
    }

    // --- Subscript/index access ---
    if LanguageValueConfig::matches(config.subscript_kinds, kind) {
        return convert_subscript(node, source, config, _func_qn);
    }

    // --- List literals ---
    if LanguageValueConfig::matches(config.list_kinds, kind) {
        let mut items = Vec::new();
        let mut cursor = node.walk();
        for child in node.named_children(&mut cursor) {
            items.push(node_to_symbolic(child, source, config, _func_qn));
        }
        return SymbolicValue::Literal(LiteralValue::list(items));
    }

    // --- Dict literals ---
    if LanguageValueConfig::matches(config.dict_kinds, kind) {
        let mut entries = Vec::new();
        let mut cursor = node.walk();
        for child in node.named_children(&mut cursor) {
            if child.kind() == "pair" {
                let key = child
                    .child_by_field_name("key")
                    .map(|n| node_to_symbolic(n, source, config, _func_qn))
                    .unwrap_or(SymbolicValue::Unknown);
                let value = child
                    .child_by_field_name("value")
                    .map(|n| node_to_symbolic(n, source, config, _func_qn))
                    .unwrap_or(SymbolicValue::Unknown);
                entries.push((key, value));
            }
        }
        return SymbolicValue::Literal(LiteralValue::dict(entries));
    }

    // --- Conditional expressions (ternary) ---
    if LanguageValueConfig::matches(config.conditional_kinds, kind) {
        // Python: `x if cond else y` — tree-sitter has named children for the branches
        let mut arms = Vec::new();
        let mut cursor = node.walk();
        for child in node.named_children(&mut cursor) {
            arms.push(node_to_symbolic(child, source, config, _func_qn));
        }
        return SymbolicValue::phi(arms);
    }

    // --- Parenthesized expression: unwrap ---
    if kind == "parenthesized_expression" {
        if let Some(inner) = node.named_child(0) {
            return node_to_symbolic(inner, source, config, _func_qn);
        }
    }

    // --- Unary operator: limited support ---
    if kind == "unary_operator"
        || kind == "not_operator"
        || kind == "unary_expression"
        || kind == "prefix_unary_expression"
    {
        // For negation of a literal, produce the negated value
        if let Some(operand) = node
            .child_by_field_name("argument")
            .or_else(|| node.child_by_field_name("operand"))
            .or(node.named_child(0))
        {
            let op_text = node.child(0).map(|c| node_text(c, source)).unwrap_or("");
            let inner = node_to_symbolic(operand, source, config, _func_qn);
            if op_text == "-" {
                if let SymbolicValue::Literal(LiteralValue::Integer(n)) = &inner {
                    return SymbolicValue::Literal(LiteralValue::Integer(-n));
                }
                if let SymbolicValue::Literal(LiteralValue::Float(f)) = &inner {
                    return SymbolicValue::Literal(LiteralValue::Float(-f));
                }
            }
            return inner;
        }
    }

    // --- Expression statement: unwrap ---
    if kind == "expression_statement" {
        if let Some(inner) = node.named_child(0) {
            return node_to_symbolic(inner, source, config, _func_qn);
        }
    }

    // --- Fallback ---
    SymbolicValue::Unknown
}

/// Convert a string literal node (including concatenated and interpolated strings).
fn convert_string_literal(
    node: tree_sitter::Node,
    source: &[u8],
    config: &LanguageValueConfig,
    func_qn: &str,
) -> SymbolicValue {
    let kind = node.kind();

    // Concatenated string: join child strings
    if kind == "concatenated_string" {
        let mut parts = Vec::new();
        let mut cursor = node.walk();
        for child in node.named_children(&mut cursor) {
            parts.push(node_to_symbolic(child, source, config, func_qn));
        }
        return if parts.len() == 1 {
            parts.into_iter().next().expect("checked len == 1")
        } else {
            SymbolicValue::Concat(parts)
        };
    }

    // F-string (string with interpolation children)
    if has_interpolation_children(node) {
        let mut parts = Vec::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            let ck = child.kind();
            if ck == "interpolation" {
                if let Some(expr) = child.named_child(0) {
                    parts.push(node_to_symbolic(expr, source, config, func_qn));
                }
            } else if ck == "string_content" {
                parts.push(SymbolicValue::Literal(LiteralValue::String(
                    node_text(child, source).to_string(),
                )));
            }
        }
        return if parts.is_empty() {
            SymbolicValue::Literal(LiteralValue::String(unquote(node_text(node, source))))
        } else {
            SymbolicValue::Concat(parts)
        };
    }

    let text = node_text(node, source);
    SymbolicValue::Literal(LiteralValue::String(unquote(text)))
}

/// Convert a function call expression node.
fn convert_call_expr(
    node: tree_sitter::Node,
    source: &[u8],
    config: &LanguageValueConfig,
    func_qn: &str,
) -> SymbolicValue {
    let callee = extract_callee_name(node, source);
    let mut args = Vec::new();

    if let Some(args_node) = node.child_by_field_name("arguments") {
        let mut cursor = args_node.walk();
        for child in args_node.named_children(&mut cursor) {
            let ck = child.kind();
            if ck == "keyword_argument" || ck == "named_argument" {
                if let Some(val) = child.child_by_field_name("value") {
                    args.push(node_to_symbolic(val, source, config, func_qn));
                }
            } else if ck == "spread_element" || ck == "rest_pattern" {
                args.push(SymbolicValue::Unknown);
            } else {
                args.push(node_to_symbolic(child, source, config, func_qn));
            }
        }
    }

    SymbolicValue::Call(callee, args)
}

/// Convert a field/attribute access node.
fn convert_field_access(
    node: tree_sitter::Node,
    source: &[u8],
    config: &LanguageValueConfig,
    func_qn: &str,
) -> SymbolicValue {
    let obj = node
        .child_by_field_name("object")
        .or_else(|| node.child_by_field_name("value"))
        .or_else(|| node.child_by_field_name("operand"))
        .or_else(|| node.child_by_field_name("argument"))
        .or_else(|| node.child_by_field_name("expression"))
        .or_else(|| node.named_child(0))
        .map(|n| node_to_symbolic(n, source, config, func_qn))
        .unwrap_or(SymbolicValue::Unknown);

    let attr_name = node
        .child_by_field_name("attribute")
        .or_else(|| node.child_by_field_name("property"))
        .or_else(|| node.child_by_field_name("field"))
        .or_else(|| node.child_by_field_name("name"))
        .or_else(|| {
            let count = node.named_child_count();
            if count >= 2 {
                node.named_child(count - 1)
            } else {
                None
            }
        })
        .map(|n| node_text(n, source).to_string())
        .unwrap_or_default();

    SymbolicValue::FieldAccess(Box::new(obj), attr_name)
}

/// Convert a subscript/index access node.
fn convert_subscript(
    node: tree_sitter::Node,
    source: &[u8],
    config: &LanguageValueConfig,
    func_qn: &str,
) -> SymbolicValue {
    let obj = node
        .child_by_field_name("value")
        .or_else(|| node.child_by_field_name("object"))
        .or_else(|| node.child_by_field_name("operand"))
        .or_else(|| node.child_by_field_name("array"))
        .or_else(|| node.child_by_field_name("argument"))
        .or_else(|| node.child_by_field_name("expression"))
        .or_else(|| node.named_child(0))
        .map(|n| node_to_symbolic(n, source, config, func_qn))
        .unwrap_or(SymbolicValue::Unknown);

    let key = node
        .child_by_field_name("subscript")
        .or_else(|| node.child_by_field_name("index"))
        .or_else(|| {
            let count = node.named_child_count();
            if count >= 2 {
                node.named_child(count - 1)
            } else {
                None
            }
        })
        .map(|n| node_to_symbolic(n, source, config, func_qn))
        .unwrap_or(SymbolicValue::Unknown);

    SymbolicValue::Index(Box::new(obj), Box::new(key))
}

/// Check if a string node contains interpolation children (f-string).
fn has_interpolation_children(node: tree_sitter::Node) -> bool {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "interpolation" {
            return true;
        }
    }
    false
}