repotoire 0.5.3

Graph-powered code analysis CLI. 106 detectors for security, architecture, 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
//! Non-code masking using tree-sitter
//!
//! Replaces comments, docstrings, and string literals with spaces
//! to prevent regex-based detectors from matching inside non-code regions.
//! Preserves newlines so line numbers and column offsets remain stable.

use std::ops::Range;
use tree_sitter::{Node, Parser};

/// Main entry point: mask non-code regions in source code.
///
/// Parses `source` using the tree-sitter grammar for `language` (file extension),
/// identifies comments, string literals, and docstrings, and replaces their
/// content with spaces (preserving newlines for line-count stability).
///
/// Returns the original source unchanged if the language is unsupported or
/// parsing fails.
pub fn mask_non_code(source: &str, language: &str) -> String {
    if source.is_empty() {
        return String::new();
    }

    let ts_lang = match get_ts_language(language) {
        Some(lang) => lang,
        None => return source.to_string(),
    };

    let mut parser = Parser::new();
    if parser.set_language(&ts_lang).is_err() {
        return source.to_string();
    }

    let tree = match parser.parse(source, None) {
        Some(t) => t,
        None => return source.to_string(),
    };

    let ranges = get_non_code_ranges(source.as_bytes(), language, &tree.root_node());
    if ranges.is_empty() {
        return source.to_string();
    }

    mask_ranges(source, &ranges)
}

/// Mask non-code regions using an EXISTING tree-sitter tree.
///
/// Identical to `mask_non_code()` but skips the tree-sitter parse — reuses the
/// tree from the main parse phase. Saves ~1-2ms per file (3,415 re-parses
/// eliminated for CPython).
pub fn mask_non_code_with_tree(source: &str, language: &str, tree: &tree_sitter::Tree) -> String {
    if source.is_empty() {
        return String::new();
    }

    let ranges = get_non_code_ranges(source.as_bytes(), language, &tree.root_node());
    if ranges.is_empty() {
        return source.to_string();
    }

    mask_ranges(source, &ranges)
}

/// Replace bytes in the given ranges with spaces, preserving `\n` characters.
///
/// This is safe for UTF-8 because:
/// - We only replace non-newline bytes with ASCII space (0x20)
/// - We preserve newlines (0x0A)
/// - The ranges come from tree-sitter which works on valid byte boundaries
fn mask_ranges(source: &str, ranges: &[Range<usize>]) -> String {
    let mut bytes = source.as_bytes().to_vec();
    for range in ranges {
        for i in range.start..range.end.min(bytes.len()) {
            if bytes[i] != b'\n' {
                bytes[i] = b' ';
            }
        }
    }
    // Safety: we only replaced non-newline bytes with spaces (valid ASCII/UTF-8)
    String::from_utf8(bytes).unwrap_or_else(|_| source.to_string())
}

/// Parse with tree-sitter and collect byte ranges for non-code regions.
fn get_non_code_ranges(source: &[u8], language: &str, root: &Node) -> Vec<Range<usize>> {
    let mut ranges = Vec::new();
    collect_non_code_ranges(root, source, language, &mut ranges);
    ranges
}

/// Recursively walk the CST and collect byte ranges for comments, strings,
/// and Python docstrings.
fn collect_non_code_ranges(
    node: &Node,
    source: &[u8],
    language: &str,
    ranges: &mut Vec<Range<usize>>,
) {
    let kind = node.kind();

    if is_comment_node(kind) {
        ranges.push(node.start_byte()..node.end_byte());
        return; // Comments have no interesting children
    }

    if is_string_node(kind) {
        // For Python, check if this is a docstring (expression_statement > string
        // as the first statement in a block or module)
        if language == "py" && is_python_docstring(node, source) {
            ranges.push(node.start_byte()..node.end_byte());
            return;
        }

        // All string literals get masked
        ranges.push(node.start_byte()..node.end_byte());
        return;
    }

    // For TypeScript/JavaScript template strings (template_string)
    if kind == "template_string" {
        ranges.push(node.start_byte()..node.end_byte());
        return;
    }

    // Recurse into children
    let child_count = node.child_count();
    for i in 0..child_count {
        if let Some(child) = node.child(i) {
            collect_non_code_ranges(&child, source, language, ranges);
        }
    }
}

/// Check if a node kind represents a comment.
fn is_comment_node(kind: &str) -> bool {
    matches!(kind, "comment" | "line_comment" | "block_comment")
}

/// Check if a node kind represents a string literal.
fn is_string_node(kind: &str) -> bool {
    matches!(
        kind,
        "string"
            | "string_literal"
            | "raw_string_literal"
            | "interpreted_string_literal"
            | "char_literal"
            | "verbatim_string_literal"
            | "string_content"
    )
}

/// Check if a string node is a Python docstring.
///
/// A docstring is an `expression_statement` containing a `string` that is
/// the first statement in a `block` (function/class body) or `module`.
fn is_python_docstring(node: &Node, _source: &[u8]) -> bool {
    // The string itself should be inside an expression_statement
    let parent = match node.parent() {
        Some(p) => p,
        None => return false,
    };

    if parent.kind() != "expression_statement" {
        return false;
    }

    // The expression_statement should be the first statement in a block or module
    let grandparent = match parent.parent() {
        Some(gp) => gp,
        None => return false,
    };

    match grandparent.kind() {
        "module" => {
            // First statement in module
            is_first_statement(&parent, &grandparent)
        }
        "block" => {
            // First statement in function/class body
            is_first_statement(&parent, &grandparent)
        }
        _ => false,
    }
}

/// Check if `stmt` is the first non-comment statement in `container`.
fn is_first_statement(stmt: &Node, container: &Node) -> bool {
    let child_count = container.child_count();
    for i in 0..child_count {
        if let Some(child) = container.child(i) {
            let kind = child.kind();
            // Skip comments and whitespace-only nodes
            if is_comment_node(kind) || kind == "newline" || kind == "\n" {
                continue;
            }
            // The first real statement should be our expression_statement
            return child.id() == stmt.id();
        }
    }
    false
}

/// Map file extensions to tree-sitter languages.
fn get_ts_language(ext: &str) -> Option<tree_sitter::Language> {
    match ext {
        "py" => Some(tree_sitter_python::LANGUAGE.into()),
        "js" | "jsx" | "mjs" | "cjs" => Some(tree_sitter_javascript::LANGUAGE.into()),
        "ts" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
        "tsx" => Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
        "rs" => Some(tree_sitter_rust::LANGUAGE.into()),
        "go" => Some(tree_sitter_go::LANGUAGE.into()),
        "java" => Some(tree_sitter_java::LANGUAGE.into()),
        "cs" => Some(tree_sitter_c_sharp::LANGUAGE.into()),
        "c" | "h" => Some(tree_sitter_c::LANGUAGE.into()),
        "cpp" | "cc" | "hpp" | "cxx" => Some(tree_sitter_cpp::LANGUAGE.into()),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mask_python_single_line_comment() {
        let source = "x = 1  # this is a comment\ny = 2\n";
        let result = mask_non_code(source, "py");

        // Code should be preserved
        assert!(result.contains("x = 1"));
        assert!(result.contains("y = 2"));

        // Comment should be masked
        assert!(!result.contains("this is a comment"));
    }

    #[test]
    fn test_mask_python_docstring() {
        let source = r#"def foo():
    """password=secret"""
    return 1
"#;
        let result = mask_non_code(source, "py");

        // Code should be preserved
        assert!(result.contains("def foo():"));
        assert!(result.contains("return 1"));

        // Docstring content should be masked
        assert!(!result.contains("password=secret"));
    }

    #[test]
    fn test_mask_python_multiline_docstring() {
        let source = r#"def analyze():
    """
    This function has debug keywords
    and hardcoded IP 192.168.1.1
    password = secret123
    """
    return True
"#;
        let result = mask_non_code(source, "py");

        // Code should be preserved
        assert!(result.contains("def analyze():"));
        assert!(result.contains("return True"));

        // Docstring content should be masked
        assert!(!result.contains("debug keywords"));
        assert!(!result.contains("192.168.1.1"));
        assert!(!result.contains("password = secret123"));
    }

    #[test]
    fn test_mask_python_string_literals() {
        let source = r#"x = "password=secret123"
y = 'another string'
"#;
        let result = mask_non_code(source, "py");

        // Variable assignments should be preserved
        assert!(result.contains("x ="));
        assert!(result.contains("y ="));

        // String contents should be masked
        assert!(!result.contains("password=secret123"));
        assert!(!result.contains("another string"));
    }

    #[test]
    fn test_mask_javascript_comments() {
        let source = r#"// single line comment
let x = 1;
/* multi-line
   comment with password=secret */
let y = 2;
"#;
        let result = mask_non_code(source, "js");

        // Code should be preserved
        assert!(result.contains("let x = 1;"));
        assert!(result.contains("let y = 2;"));

        // Comments should be masked
        assert!(!result.contains("single line comment"));
        assert!(!result.contains("password=secret"));
    }

    #[test]
    fn test_mask_typescript_template_literal() {
        let source = "let msg = `hello ${name} password=test`;\n";
        let result = mask_non_code(source, "ts");

        // Variable declaration should be preserved
        assert!(result.contains("let msg ="));

        // Template literal content should be masked
        assert!(!result.contains("password=test"));
    }

    #[test]
    fn test_mask_rust_comments() {
        let source = r#"// regular comment
/// doc comment with secret
fn main() {
    let x = 1;
}
"#;
        let result = mask_non_code(source, "rs");

        // Code should be preserved
        assert!(result.contains("fn main()"));
        assert!(result.contains("let x = 1;"));

        // Comments should be masked
        assert!(!result.contains("regular comment"));
        assert!(!result.contains("doc comment with secret"));
    }

    #[test]
    fn test_mask_go_comments() {
        let source = r#"// single line comment
package main
/* block comment
   with password */
func main() {}
"#;
        let result = mask_non_code(source, "go");

        // Code should be preserved
        assert!(result.contains("package main"));
        assert!(result.contains("func main()"));

        // Comments should be masked
        assert!(!result.contains("single line comment"));
        assert!(!result.contains("with password"));
    }

    #[test]
    fn test_mask_preserves_line_count() {
        let source = r#"# comment line 1
# comment line 2
x = 1
"""
multi
line
docstring
"""
y = 2
"#;
        let result = mask_non_code(source, "py");

        let original_lines = source.lines().count();
        let masked_lines = result.lines().count();
        assert_eq!(
            original_lines, masked_lines,
            "Line count should be preserved after masking"
        );
    }

    #[test]
    fn test_mask_unknown_language_returns_unchanged() {
        let source = "some code here # comment\n";
        let result = mask_non_code(source, "xyz");
        assert_eq!(result, source);
    }

    #[test]
    fn test_mask_empty_source() {
        let result = mask_non_code("", "py");
        assert_eq!(result, "");
    }

    #[test]
    fn test_mask_ranges_basic() {
        let source = "hello world";
        let ranges = [6..11]; // "world"
        let result = mask_ranges(source, &ranges);
        assert_eq!(result, "hello      ");
    }

    #[test]
    fn test_mask_ranges_preserves_newlines() {
        let source = "hello\nworld\nagain";
        let ranges = [0..17]; // entire string
        let result = mask_ranges(source, &ranges);
        assert_eq!(result, "     \n     \n     ");
    }
}