meta-ast 0.5.0

Polyglot static-analysis engine: extract symbols and cross-language dependency graphs from 9 supported source languages, with optional MetaCall deployment manifest generation.
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use crate::language::{DefaultVisibility, LanguageSpec};
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

fn resolve_python_import(raw: &str, source_dir: &Path, project_root: &Path) -> Option<PathBuf> {
    let raw = raw.trim_matches(|c| c == '"' || c == '\'');
    if raw.is_empty() {
        return None;
    }

    if raw.starts_with('.') {
        let relative = raw.trim_start_matches('.');
        if relative.is_empty() {
            return Some(source_dir.join("__init__.py"));
        }
        let path = source_dir.join(relative.replace('.', std::path::MAIN_SEPARATOR_STR));
        if path.join("__init__.py").exists() {
            return Some(path.join("__init__.py"));
        }
        Some(path.with_extension("py"))
    } else {
        let path = project_root.join(raw.replace('.', std::path::MAIN_SEPARATOR_STR));
        if path.join("__init__.py").exists() {
            return Some(path.join("__init__.py"));
        }
        Some(path.with_extension("py"))
    }
}

static PYTHON_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    crate::language::common::compile_query(
        &tree_sitter_python::LANGUAGE.into(),
        r#"
(function_definition
  "async"? @async
  name: (identifier) @name
  parameters: (parameters) @signature
  body: (block (expression_statement (string) @docstring)?)
) @kind.function

(class_definition
  name: (identifier) @name
  body: (block (expression_statement (string) @docstring)?)
) @kind.class

(decorated_definition
  definition: [
    (function_definition
      "async"? @async
      name: (identifier) @name
      parameters: (parameters) @signature
      body: (block (expression_statement (string) @docstring)?)
    ) @kind.function
    (class_definition
      name: (identifier) @name
      body: (block (expression_statement (string) @docstring)?)
    ) @kind.class
  ]
)
"#,
        "Python",
    )
});

fn python_query() -> &'static tree_sitter::Query {
    &PYTHON_QUERY
}

const PYTHON_IMPORT_QUERY_STR: &str = r#"
(import_statement
  (dotted_name) @import.path)
(import_statement
  (aliased_import
    name: (dotted_name) @import.path
    alias: (identifier) @import.alias))
(import_from_statement
  module_name: (dotted_name) @import.path
  name: (_) @import.symbol)
(import_from_statement
  module_name: (dotted_name) @import.path
  (aliased_import name: (_) @import.symbol alias: (identifier) @import.alias))
(import_from_statement
  module_name: (dotted_name) @import.path
  (wildcard_import) @import.star)
"#;

const PYTHON_REFERENCE_QUERY_STR: &str = r#"
(call
  function: (identifier) @reference.name)
(call
  function: (attribute
    attribute: (identifier) @reference.name))
"#;

static PYTHON_IMPORT_REF_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    crate::language::common::compile_query(
        &tree_sitter_python::LANGUAGE.into(),
        &format!(
            "{}\n{}",
            PYTHON_IMPORT_QUERY_STR, PYTHON_REFERENCE_QUERY_STR
        ),
        "Python combined import+ref",
    )
});

fn python_import_ref_query() -> &'static tree_sitter::Query {
    &PYTHON_IMPORT_REF_QUERY
}

pub(crate) const PYTHON_SPEC: LanguageSpec = LanguageSpec {
    extensions: &["py", "pyi"],
    grammar_fn: || tree_sitter_python::LANGUAGE.into(),
    query_fn: python_query,
    import_path_resolver: resolve_python_import,
    import_ref_query_fn: python_import_ref_query,
    class_like_parents: &["class_definition"],
    ancestor_visibility_rules: &[],
    visibility_from_name: None,
    import_statement_kinds: &["import_statement", "import_from_statement"],
    default_visibility: DefaultVisibility::PublicByDefault,
    doc_comment_config: None,
};

// ── Dataflow extraction ─────────────────────────────────────────────

#[cfg(feature = "dataflow")]
static PYTHON_DATAFLOW_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    crate::language::common::compile_query(
        &tree_sitter_python::LANGUAGE.into(),
        r#"
; Assignments
(assignment
  left: (identifier) @def.var)
(augmented_assignment
  left: (identifier) @def.var)
(for_statement
  left: (identifier) @def.var)

; Function parameters
(parameters
  (identifier) @def.param)
(parameters
  (default_parameter
    name: (identifier) @def.param))

; Usages in expression context
(call
  function: (identifier) @use.var)
(argument_list
  (identifier) @use.var)
(binary_operator
  (identifier) @use.var)
(return_statement
  (identifier) @use.var)
(assignment
  right: (identifier) @use.var)
(expression_statement
  (identifier) @use.var)
(attribute
  object: (identifier) @use.var)
(subscript
  value: (identifier) @use.var)
"#,
        "Python dataflow",
    )
});

/// Extract data nodes and flow edges from a Python parse tree.
#[cfg(feature = "dataflow")]
pub fn extract_python_dataflow(
    tree: &tree_sitter::Tree,
    source: &[u8],
    id_gen: &crate::model::IdGenerator<crate::model::DataNodeId>,
) -> (Vec<crate::model::DataNode>, Vec<crate::model::FlowEdge>) {
    use crate::model::{DataNode, DataNodeId, DataScope, FlowEdge, FlowKind};
    use tree_sitter::StreamingIterator;

    let query = &*PYTHON_DATAFLOW_QUERY;
    let mut cursor = tree_sitter::QueryCursor::new();

    let mut defs: Vec<(String, usize, tree_sitter::Node, bool)> = Vec::new();
    let mut uses: Vec<(String, usize, tree_sitter::Node, usize)> = Vec::new();

    let mut matches = cursor.matches(query, tree.root_node(), source);
    while let Some(m) = matches.next() {
        for capture in m.captures {
            let capture_name = query.capture_names()[capture.index as usize];
            let node = capture.node;
            let byte_pos = node.start_byte();
            let name = match node.utf8_text(source) {
                Ok(t) => t.to_string(),
                Err(_) => continue,
            };

            match capture_name {
                "def.var" => {
                    defs.push((name, byte_pos, node, false));
                }
                "def.param" => {
                    defs.push((name, byte_pos, node, true));
                }
                "use.var" => {
                    let func_start = enclosing_function_start(node);
                    uses.push((name, byte_pos, node, func_start));
                }
                _ => {}
            }
        }
    }

    let mut nodes = Vec::new();
    let mut def_ids: Vec<(String, usize, DataNodeId, bool)> = Vec::new();

    for (name, byte_pos, node, is_param) in &defs {
        let scope = if *is_param {
            DataScope::Parameter
        } else {
            DataScope::Local
        };
        let dn = DataNode {
            id: id_gen.next(),
            symbol_id: None,
            name: Some(name.clone()),
            scope,
            type_hint: None,
            source_range: source_range_from_node(node),
        };
        def_ids.push((name.clone(), *byte_pos, dn.id, *is_param));
        nodes.push(dn);
    }

    let mut edges = Vec::new();

    for (use_name, use_pos, use_node, use_func_start) in &uses {
        let mut best_def: Option<&(String, usize, DataNodeId, bool)> = None;

        for def in &def_ids {
            if def.0 == *use_name && def.1 < *use_pos {
                let def_func_start = enclosing_function_start_for_id(def.1, tree);
                if def_func_start == *use_func_start {
                    match best_def {
                        None => best_def = Some(def),
                        Some(best) => {
                            if def.1 > best.1 {
                                best_def = Some(def);
                            }
                        }
                    }
                }
            }
        }

        if let Some(def) = best_def {
            let use_dn = DataNode {
                id: id_gen.next(),
                symbol_id: None,
                name: Some(use_name.clone()),
                scope: DataScope::Local,
                type_hint: None,
                source_range: source_range_from_node(use_node),
            };
            let target_id = use_dn.id;
            nodes.push(use_dn);

            edges.push(FlowEdge {
                source: def.2,
                target: target_id,
                kind: FlowKind::DefUse,
                confidence: 0.9,
            });
        }
    }

    (nodes, edges)
}

#[cfg(feature = "dataflow")]
fn enclosing_function_start(node: tree_sitter::Node) -> usize {
    let mut current = node.parent();
    while let Some(parent) = current {
        if parent.kind() == "function_definition" {
            return parent.start_byte();
        }
        current = parent.parent();
    }
    0
}

#[cfg(feature = "dataflow")]
fn enclosing_function_start_for_id(byte_pos: usize, tree: &tree_sitter::Tree) -> usize {
    let node = tree.root_node();
    find_enclosing_function(node, byte_pos)
}

#[cfg(feature = "dataflow")]
fn find_enclosing_function(node: tree_sitter::Node, byte_pos: usize) -> usize {
    if node.start_byte() <= byte_pos && byte_pos < node.end_byte() {
        if node.kind() == "function_definition" {
            return node.start_byte();
        }
        for i in 0..node.named_child_count() {
            if let Some(child) = node.named_child(i as u32) {
                let result = find_enclosing_function(child, byte_pos);
                if result != 0 {
                    return result;
                }
            }
        }
    }
    0
}

#[cfg(feature = "dataflow")]
fn source_range_from_node(node: &tree_sitter::Node) -> crate::model::SourceRange {
    use crate::model::{LineColumn, SourceRange};
    SourceRange {
        byte_start: node.start_byte(),
        byte_end: node.end_byte(),
        start: LineColumn {
            line: node.start_position().row,
            column: node.start_position().column,
        },
        end: LineColumn {
            line: node.end_position().row,
            column: node.end_position().column,
        },
    }
}

#[cfg(test)]
mod tests {
    use crate::language::{LangId, extract_symbols_for, grammar_for};
    use crate::model::SymbolKind;

    fn parse(source: &[u8]) -> tree_sitter::Tree {
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&grammar_for(LangId::Python)).unwrap();
        parser.parse(source, None).unwrap()
    }

    #[test]
    fn python_grammar_loads() {
        let _ = grammar_for(LangId::Python);
    }

    #[test]
    fn extract_simple_function() {
        let tree = parse(b"def hello(): pass");
        let symbols = extract_symbols_for(LangId::Python, &tree, b"def hello(): pass");
        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].name, "hello");
        assert!(matches!(symbols[0].kind, SymbolKind::Function));
    }

    #[test]
    fn extract_async_function() {
        let tree = parse(b"async def fetch(): pass");
        let symbols = extract_symbols_for(LangId::Python, &tree, b"async def fetch(): pass");
        assert_eq!(symbols.len(), 1);
        assert!(symbols[0].is_async);
    }

    #[test]
    fn extract_class_and_methods() {
        let src =
            "class Foo:\n    def __init__(self):\n        pass\n    def bar(self):\n        pass\n";
        let tree = parse(src.as_bytes());
        let symbols = extract_symbols_for(LangId::Python, &tree, src.as_bytes());
        let foo = symbols.iter().find(|s| s.name == "Foo").unwrap();
        assert!(matches!(foo.kind, SymbolKind::Class));
        let bar = symbols.iter().find(|s| s.name == "bar").unwrap();
        assert!(matches!(bar.kind, SymbolKind::Method));
    }

    #[test]
    fn extract_decorated_function() {
        let src = "@decorator\ndef decorated_func(x):\n    return x * 2\n";
        let tree = parse(src.as_bytes());
        let symbols = extract_symbols_for(LangId::Python, &tree, src.as_bytes());
        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].name, "decorated_func");
    }

    #[test]
    fn extract_function_with_docstring() {
        let src = br#"def greet():
    """Say hello."""
    pass
"#;
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::Python, &tree, src);
        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].docstring.as_deref(), Some("Say hello."));
    }

    #[test]
    fn docstring_does_not_eat_content_starting_with_quote() {
        let src = br#"def f():
    """"Quoted" at start."""
    pass
"#;
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::Python, &tree, src);
        assert_eq!(symbols.len(), 1);
        let ds = symbols[0].docstring.as_deref().unwrap();
        assert!(
            ds.starts_with('"'),
            "docstring content should start with a quote char, got: {ds:?}"
        );
        assert!(
            ds.contains("Quoted"),
            "docstring should contain 'Quoted', got: {ds:?}"
        );
    }

    #[test]
    fn docstring_does_not_eat_content_ending_with_quote() {
        let src = br#"def f():
    '''She said "hello"'''
    pass
"#;
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::Python, &tree, src);
        assert_eq!(symbols.len(), 1);
        let ds = symbols[0].docstring.as_deref().unwrap();
        assert!(
            ds.contains("hello"),
            "docstring should contain 'hello', got: {ds:?}"
        );
        assert!(
            ds.contains(r#"""#),
            "docstring should preserve inner double quotes, got: {ds:?}"
        );
    }

    #[test]
    fn docstring_multiline_strips_delimiters_not_content() {
        let src = br#"def f():
    """
    She said "hi".
    """
    pass
"#;
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::Python, &tree, src);
        assert_eq!(symbols.len(), 1);
        let ds = symbols[0].docstring.as_deref().unwrap();
        assert!(
            !ds.starts_with('"'),
            "docstring should not start with delimiter quote, got: {ds:?}"
        );
        assert!(
            ds.contains(r#""hi""#),
            "docstring should preserve inner quotes, got: {ds:?}"
        );
    }

    #[test]
    fn python_insta_snapshot() {
        let src = std::fs::read_to_string(
            std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                .join("tests/fixtures/python/simple_functions.py"),
        )
        .unwrap();
        let tree = parse(src.as_bytes());
        let symbols = extract_symbols_for(LangId::Python, &tree, src.as_bytes());
        insta::assert_json_snapshot!(symbols);
    }

    #[cfg(feature = "dataflow")]
    #[test]
    fn python_dataflow_extracts_assignments_and_parameters() {
        let src = b"def add(x, y):\n    result = x + y\n    return result\n";
        let tree = parse(src);
        let id_gen = crate::model::IdGenerator::new();
        let (nodes, edges) = super::extract_python_dataflow(&tree, src, &id_gen);

        let names: Vec<Option<&str>> = nodes.iter().map(|n| n.name.as_deref()).collect();
        assert!(names.contains(&Some("x")));
        assert!(names.contains(&Some("y")));
        assert!(names.contains(&Some("result")));

        assert!(!edges.is_empty(), "should extract def-use flow edges");
        assert_eq!(edges[0].kind, crate::model::FlowKind::DefUse);
    }

    #[cfg(feature = "dataflow")]
    #[test]
    fn python_dataflow_for_loop_assignment() {
        let src = b"def calc():\n    total = 0\n    for i in items:\n        total += i\n";
        let tree = parse(src);
        let id_gen = crate::model::IdGenerator::new();
        let (nodes, _edges) = super::extract_python_dataflow(&tree, src, &id_gen);

        let names: Vec<Option<&str>> = nodes.iter().map(|n| n.name.as_deref()).collect();
        assert!(names.contains(&Some("total")));
        assert!(names.contains(&Some("i")));
    }
}