agent-file-tools 0.35.0

Agent File Tools — tree-sitter powered code analysis for AI agents
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
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
use super::{
    import_byte_range, ImportBlock, ImportForm, ImportGroup, ImportKind, ImportRequest,
    ImportStatement, ImportSyntax,
};
use tree_sitter::{Node, Tree};

const RUBY_REQUIRE_KIND: &str = "require";
const RUBY_REQUIRE_RELATIVE_KIND: &str = "require_relative";
const RUBY_LOAD_KIND: &str = "load";
const RUBY_QUOTE_SINGLE: &str = "quote:single";
const RUBY_QUOTE_DOUBLE: &str = "quote:double";

pub(crate) fn classify_group_ruby(_module_path: &str) -> ImportGroup {
    // Ruby has no conventional stdlib/external/internal declaration grouping for
    // require-like calls, so keep Phase 1 grouping neutral and stable.
    ImportGroup::External
}

pub(crate) fn parse_ruby_imports(source: &str, tree: &Tree) -> ImportBlock {
    let root = tree.root_node();
    let mut imports = Vec::new();

    let mut cursor = root.walk();
    if cursor.goto_first_child() {
        loop {
            let node = cursor.node();
            if node.kind() == "call" {
                if let Some(imp) = parse_ruby_require_call(source, &node) {
                    imports.push(imp);
                }
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    let byte_range = import_byte_range(&imports);
    ImportBlock {
        imports,
        byte_range,
    }
}

fn parse_ruby_require_call(source: &str, node: &Node) -> Option<ImportStatement> {
    let import_kind = ruby_top_level_require_method(source, node)?;
    let arg_list = find_direct_child(node, "argument_list")?;
    let string = find_direct_child(&arg_list, "string")?;
    let (module_path, quote) = ruby_string_literal_content(source, &string)?;
    if module_path.is_empty() {
        return None;
    }

    let raw_text = source[node.byte_range()].to_string();
    let byte_range = node.byte_range();
    let group = classify_group_ruby(&module_path);
    let quote_modifier = ruby_quote_modifier(quote).to_string();
    let flat_marker = ruby_flat_marker(&import_kind, quote);

    Some(ImportStatement {
        module_path,
        names: Vec::new(),
        // The flat marker preserves Ruby's require form and quote style through
        // legacy organize code paths until all readers consume `form` directly.
        default_import: Some(flat_marker),
        namespace_import: None,
        kind: ImportKind::SideEffect,
        group,
        byte_range,
        raw_text,
        form: ImportForm::Structured {
            named: Vec::new(),
            namespace: None,
            alias: None,
            modifiers: vec![quote_modifier],
            import_kind: Some(import_kind),
        },
    })
}

fn ruby_top_level_require_method(source: &str, node: &Node) -> Option<String> {
    let mut method_name = None;
    let mut saw_receiver_separator = false;

    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            match child.kind() {
                "." | "::" => saw_receiver_separator = true,
                "identifier" if method_name.is_none() => {
                    method_name = Some(source[child.byte_range()].trim().to_string());
                }
                _ => {}
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    if saw_receiver_separator {
        return None;
    }

    let method_name = method_name?;
    if is_ruby_require_kind(&method_name) {
        Some(method_name)
    } else {
        None
    }
}

fn is_ruby_require_kind(kind: &str) -> bool {
    matches!(
        kind,
        RUBY_REQUIRE_KIND | RUBY_REQUIRE_RELATIVE_KIND | RUBY_LOAD_KIND
    )
}

fn ruby_string_literal_content(source: &str, node: &Node) -> Option<(String, char)> {
    let raw = source[node.byte_range()].trim();
    let quote = raw.chars().next()?;
    if quote != '\'' && quote != '"' {
        return None;
    }
    if raw.chars().last()? != quote {
        return None;
    }

    // Interpolated requires are dynamic runtime expressions, not static import
    // declarations that AFT can safely sort/remove/regenerate.
    if raw.contains("#{") {
        return None;
    }

    let content = &raw[quote.len_utf8()..raw.len() - quote.len_utf8()];
    Some((content.to_string(), quote))
}

fn ruby_quote_modifier(quote: char) -> &'static str {
    if quote == '"' {
        RUBY_QUOTE_DOUBLE
    } else {
        RUBY_QUOTE_SINGLE
    }
}

fn ruby_flat_marker(import_kind: &str, quote: char) -> String {
    let quote_suffix = ruby_quote_suffix(quote);
    if quote_suffix.is_empty() {
        import_kind.to_string()
    } else {
        format!("{import_kind}|{quote_suffix}")
    }
}

fn ruby_quote_suffix(quote: char) -> &'static str {
    if quote == '"' {
        "double"
    } else {
        ""
    }
}

fn ruby_marker_parts(marker: &str) -> (Option<&str>, Option<char>) {
    let (kind, quote) = marker.split_once('|').unwrap_or((marker, ""));
    let kind = is_ruby_require_kind(kind).then_some(kind);
    let quote = match quote {
        "double" | RUBY_QUOTE_DOUBLE => Some('"'),
        "single" | RUBY_QUOTE_SINGLE => Some('\''),
        _ => None,
    };
    (kind, quote)
}

fn ruby_quote_from_modifiers(modifiers: &[String]) -> Option<char> {
    if modifiers
        .iter()
        .any(|modifier| modifier == RUBY_QUOTE_DOUBLE)
    {
        Some('"')
    } else if modifiers
        .iter()
        .any(|modifier| modifier == RUBY_QUOTE_SINGLE)
    {
        Some('\'')
    } else {
        None
    }
}

fn find_direct_child<'tree>(node: &Node<'tree>, kind: &str) -> Option<Node<'tree>> {
    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            if child.kind() == kind {
                return Some(child);
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }
    None
}

pub(crate) fn generate_ruby_import_line(req: &ImportRequest) -> String {
    let (marker_kind, marker_quote) = req
        .default_import
        .map(ruby_marker_parts)
        .unwrap_or((None, None));
    let import_kind = req
        .import_kind
        .filter(|kind| is_ruby_require_kind(kind))
        .or(marker_kind)
        .unwrap_or(RUBY_REQUIRE_KIND);
    let quote = ruby_quote_from_modifiers(req.modifiers)
        .or(marker_quote)
        .unwrap_or('\'');

    format!("{import_kind} {quote}{}{quote}", req.module_path)
}

pub struct RubySyntax;

impl ImportSyntax for RubySyntax {
    fn parse(&self, source: &str, tree: &Tree) -> ImportBlock {
        parse_ruby_imports(source, tree)
    }

    fn generate_line(&self, req: &ImportRequest) -> String {
        generate_ruby_import_line(req)
    }

    fn classify_group(&self, module_path: &str) -> ImportGroup {
        classify_group_ruby(module_path)
    }
}

pub static RUBY_SYNTAX: RubySyntax = RubySyntax;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::imports::{generate_import, parse_imports};
    use crate::parser::{grammar_for, LangId};
    use std::collections::BTreeSet;
    use tree_sitter::Parser;

    fn parse_ruby(source: &str) -> (Tree, ImportBlock) {
        let grammar = grammar_for(LangId::Ruby);
        let mut parser = Parser::new();
        parser.set_language(&grammar).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let block = parse_imports(source, &tree, LangId::Ruby);
        (tree, block)
    }

    fn structured(import: &ImportStatement) -> (&[String], Option<&str>) {
        match &import.form {
            ImportForm::Structured {
                named,
                namespace,
                alias,
                modifiers,
                import_kind,
            } => {
                assert!(named.is_empty());
                assert!(namespace.is_none());
                assert!(alias.is_none());
                (modifiers, import_kind.as_deref())
            }
            other => panic!("expected Ruby Structured import, got {other:?}"),
        }
    }

    /// Grammar fixture: lock the exact tree-sitter-ruby node kinds this parser
    /// depends on. The current grammar emits both bare `require 'x'` and
    /// parenthesized `require('x')` as top-level `call` nodes with an `identifier`
    /// method child and an `argument_list` containing a `string`.
    #[test]
    fn ruby_grammar_node_kinds_are_stable() {
        let src = "require 'foo'\nrequire(\"bar\")\nrequire_relative '../baz'\nload 'qux.rb'\nobj.require 'ignored'\nif true\n  require 'nested'\nend\n";
        let (tree, _) = parse_ruby(src);
        assert!(!tree.root_node().has_error());

        let mut kinds = BTreeSet::new();
        fn walk(node: Node, kinds: &mut BTreeSet<String>) {
            kinds.insert(node.kind().to_string());
            let mut cursor = node.walk();
            if cursor.goto_first_child() {
                loop {
                    walk(cursor.node(), kinds);
                    if !cursor.goto_next_sibling() {
                        break;
                    }
                }
            }
        }
        walk(tree.root_node(), &mut kinds);

        for required in [
            "program",
            "call",
            "identifier",
            "argument_list",
            "string",
            "string_content",
            ".",
            "(",
            ")",
        ] {
            assert!(
                kinds.contains(required),
                "ruby grammar missing node kind {required:?}; present: {kinds:?}"
            );
        }
    }

    #[test]
    fn parse_ruby_supported_top_level_forms() {
        let (_, block) = parse_ruby(
            "require 'foo'\nrequire \"foo/bar\"\nrequire('paren')\nrequire_relative '../baz'\nload 'qux.rb'\nautoload :Sym, 'path'\nobj.require 'not_top_level'\nif true\n  require 'nested'\nend\n",
        );
        assert_eq!(block.imports.len(), 5);

        assert_ruby_import(
            &block.imports[0],
            "foo",
            RUBY_REQUIRE_KIND,
            RUBY_QUOTE_SINGLE,
        );
        assert_ruby_import(
            &block.imports[1],
            "foo/bar",
            RUBY_REQUIRE_KIND,
            RUBY_QUOTE_DOUBLE,
        );
        assert_ruby_import(
            &block.imports[2],
            "paren",
            RUBY_REQUIRE_KIND,
            RUBY_QUOTE_SINGLE,
        );
        assert_ruby_import(
            &block.imports[3],
            "../baz",
            RUBY_REQUIRE_RELATIVE_KIND,
            RUBY_QUOTE_SINGLE,
        );
        assert_ruby_import(
            &block.imports[4],
            "qux.rb",
            RUBY_LOAD_KIND,
            RUBY_QUOTE_SINGLE,
        );
    }

    fn assert_ruby_import(
        imp: &ImportStatement,
        module_path: &str,
        import_kind: &str,
        quote_modifier: &str,
    ) {
        assert_eq!(imp.module_path, module_path);
        assert_eq!(imp.names, Vec::<String>::new());
        assert!(imp.default_import.is_some());
        assert_eq!(imp.namespace_import, None);
        assert_eq!(imp.kind, ImportKind::SideEffect);
        assert_eq!(imp.group, ImportGroup::External);

        let (modifiers, parsed_import_kind) = structured(imp);
        assert_eq!(modifiers, &[quote_modifier.to_string()]);
        assert_eq!(parsed_import_kind, Some(import_kind));
    }

    #[test]
    fn generate_ruby_supported_forms() {
        assert_eq!(
            generate_import(
                LangId::Ruby,
                &ImportRequest::legacy("foo", &[], None, None, false)
            ),
            "require 'foo'"
        );
        assert_eq!(
            generate_import(
                LangId::Ruby,
                &ImportRequest {
                    module_path: "../baz",
                    names: &[],
                    default_import: None,
                    namespace: None,
                    alias: None,
                    type_only: false,
                    modifiers: &[],
                    import_kind: Some(RUBY_REQUIRE_RELATIVE_KIND),
                }
            ),
            "require_relative '../baz'"
        );
        assert_eq!(
            generate_import(
                LangId::Ruby,
                &ImportRequest {
                    module_path: "qux.rb",
                    names: &[],
                    default_import: None,
                    namespace: None,
                    alias: None,
                    type_only: false,
                    modifiers: &[],
                    import_kind: Some(RUBY_LOAD_KIND),
                }
            ),
            "load 'qux.rb'"
        );
    }

    #[test]
    fn generate_ruby_preserves_organized_flat_markers() {
        assert_eq!(
            generate_import(
                LangId::Ruby,
                &ImportRequest::legacy(
                    "../baz",
                    &[],
                    Some(RUBY_REQUIRE_RELATIVE_KIND),
                    None,
                    false
                )
            ),
            "require_relative '../baz'"
        );
        assert_eq!(
            generate_import(
                LangId::Ruby,
                &ImportRequest::legacy("qux.rb", &[], Some("load|double"), None, false)
            ),
            "load \"qux.rb\""
        );
    }

    #[test]
    fn classify_group_ruby_is_neutral_external() {
        assert_eq!(classify_group_ruby("json"), ImportGroup::External);
        assert_eq!(classify_group_ruby("./local"), ImportGroup::External);
        assert_eq!(classify_group_ruby("../relative"), ImportGroup::External);
    }

    #[test]
    fn ruby_round_trips_through_parse_generate() {
        for src in [
            "require 'foo'",
            "require \"foo/bar\"",
            "require_relative '../baz'",
            "load 'qux.rb'",
            "load \"other.rb\"",
        ] {
            let (_, block) = parse_ruby(src);
            assert_eq!(block.imports.len(), 1, "parse {src:?}");
            let imp = &block.imports[0];
            let (modifiers, import_kind) = structured(imp);
            let regenerated = generate_import(
                LangId::Ruby,
                &ImportRequest {
                    module_path: &imp.module_path,
                    names: &imp.names,
                    default_import: imp.default_import.as_deref(),
                    namespace: None,
                    alias: None,
                    type_only: false,
                    modifiers,
                    import_kind,
                },
            );
            assert_eq!(regenerated, src, "round-trip mismatch for {src:?}");
        }
    }
}