meta-ast 0.6.1

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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use crate::language::{DefaultVisibility, LanguageSpec};
use crate::model::Visibility;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

fn resolve_js_import(raw: &str, source_dir: &Path, _project_root: &Path) -> Option<PathBuf> {
    use crate::language::import_resolver::{JS_EXTS, resolve_js_family_import};
    resolve_js_family_import(raw, source_dir, JS_EXTS, &|p| p.is_file())
}

static JS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    crate::language::common::compile_query(
        &tree_sitter_javascript::LANGUAGE.into(),
        r#"
(function_declaration
  "async"? @async
  name: (identifier) @name
  parameters: (formal_parameters) @signature
) @kind.function

(generator_function_declaration
  "async"? @async
  name: (identifier) @name
  parameters: (formal_parameters) @signature
) @kind.function

(class_declaration
  name: (identifier) @name
) @kind.class

(method_definition
  "async"? @async
  name: [
    (property_identifier)
    (identifier)
  ] @name
  parameters: (formal_parameters) @signature
) @kind.method

(export_statement
  [
    (function_declaration
      "async"? @async
      name: (identifier) @name
      parameters: (formal_parameters) @signature
    ) @kind.function
    (class_declaration
      name: (identifier) @name
    ) @kind.class
  ]
)
"#,
        "JavaScript",
    )
});

fn js_query() -> &'static tree_sitter::Query {
    &JS_QUERY
}

const JS_IMPORT_QUERY_STR: &str = r#"
(import_statement
  source: (string) @import.path)
(import_statement
  (import_clause
    (named_imports
      (import_specifier
        name: (identifier) @import.symbol
        alias: (identifier)? @import.alias))))
(import_statement
  (import_clause
    (identifier) @import.symbol))
(import_statement
  (import_clause
    (namespace_import
      (identifier) @import.symbol)))
(call_expression
  function: (identifier) @call.name
  arguments: (arguments . (string) @import.path .)
  (#eq? @call.name "require"))
"#;

const JS_REFERENCE_QUERY_STR: &str = crate::language::typescript::TS_FAMILY_REFERENCE_QUERY;

static JS_IMPORT_REF_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    crate::language::common::compile_query(
        &tree_sitter_javascript::LANGUAGE.into(),
        &format!("{}\n{}", JS_IMPORT_QUERY_STR, JS_REFERENCE_QUERY_STR),
        "JavaScript combined import+ref",
    )
});

fn js_import_ref_query() -> &'static tree_sitter::Query {
    &JS_IMPORT_REF_QUERY
}

pub(crate) const JS_SPEC: LanguageSpec = LanguageSpec {
    extensions: &["js", "mjs", "cjs"],
    grammar_fn: || tree_sitter_javascript::LANGUAGE.into(),
    query_fn: js_query,
    import_path_resolver: resolve_js_import,
    import_ref_query_fn: js_import_ref_query,
    class_like_parents: &["class_declaration", "class"],
    ancestor_visibility_rules: &[("export_statement", Visibility::Public)],
    visibility_from_name: None,
    import_statement_kinds: &["import_statement"],
    default_visibility: DefaultVisibility::PrivateByDefault,
    doc_comment_config: Some(crate::language::C_LIKE_DOC_COMMENT),
};

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

/// tree-sitter query capturing def-use sites for the JavaScript grammar.
///
/// JavaScript parameters live directly inside `formal_parameters` (no
/// `required_parameter` wrapper as in TypeScript). The capture names
/// (`@def.var`, `@def.param`, `@use.var`) match the shared schema used
/// across the JS family.
#[cfg(feature = "dataflow")]
pub(crate) const JS_DATAFLOW_QUERY_STR: &str = r#"
; Variable declarator: name position in a `let/const/var` binding.
(variable_declarator
  name: (identifier) @def.var)

; Function parameters (JS grammar: identifier directly in formal_parameters,
; possibly wrapped by assignment_pattern for default values).
(formal_parameters
  (identifier) @def.param)
(formal_parameters
  (assignment_pattern
    left: (identifier) @def.param))

; Identifier references in expression position.
(call_expression
  function: (identifier) @use.var)
(call_expression
  arguments: (arguments
    (identifier) @use.var))
(binary_expression
  left: (identifier) @use.var)
(binary_expression
  right: (identifier) @use.var)
(member_expression
  object: (identifier) @use.var)
(return_statement
  (identifier) @use.var)
(assignment_expression
  right: (identifier) @use.var)
"#;

/// tree-sitter query capturing def-use sites for the TypeScript family.
///
/// TypeScript wraps each parameter in a `required_parameter` /
/// `optional_parameter` node. The rest of the schema is identical to JS.
///
/// Note: we deliberately do NOT use the `name:` field for parameters. The
/// typescript grammar advertises a `name` field on `required_parameter`,
/// but in the current grammar the field's match behavior is brittle;
/// matching the direct `identifier` child is more reliable across grammar
/// versions and avoids accidental double-capture.
#[cfg(feature = "dataflow")]
pub(crate) const TS_FAMILY_DATAFLOW_QUERY: &str = r#"
; Variable declarator: name position in a `let/const/var` binding.
(variable_declarator
  name: (identifier) @def.var)

; Function parameters (TS grammar: required_parameter / optional_parameter).
(required_parameter
  (identifier) @def.param)
(optional_parameter
  (identifier) @def.param)

; Identifier references in expression position.
(call_expression
  function: (identifier) @use.var)
(call_expression
  arguments: (arguments
    (identifier) @use.var))
(binary_expression
  left: (identifier) @use.var)
(binary_expression
  right: (identifier) @use.var)
(member_expression
  object: (identifier) @use.var)
(return_statement
  (identifier) @use.var)
(assignment_expression
  right: (identifier) @use.var)
"#;

/// Extract data nodes and flow edges from a JavaScript-family parse tree.
///
/// `function_kinds` lists the AST node kinds that introduce a new scope.
/// Delegates to the shared def-use engine in `common`.
#[cfg(feature = "dataflow")]
pub(crate) fn extract_js_family_dataflow_with_query(
    tree: &tree_sitter::Tree,
    source: &[u8],
    query: &tree_sitter::Query,
    function_kinds: &[&str],
    id_gen: &crate::model::IdGenerator<crate::model::DataNodeId>,
) -> (Vec<crate::model::DataNode>, Vec<crate::model::FlowEdge>) {
    crate::language::common::extract_def_use_dataflow(tree, source, query, function_kinds, id_gen)
}

#[cfg(feature = "dataflow")]
static JS_DATAFLOW_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
    crate::language::common::compile_query(
        &tree_sitter_javascript::LANGUAGE.into(),
        JS_DATAFLOW_QUERY_STR,
        "JavaScript dataflow",
    )
});

/// JavaScript AST node kinds that introduce a new intra-procedural scope.
#[cfg(feature = "dataflow")]
pub(crate) const JS_FUNCTION_KINDS: &[&str] = crate::language::common::JS_FAMILY_FUNCTION_KINDS;

/// Extract data nodes and flow edges from a JavaScript parse tree.
#[cfg(feature = "dataflow")]
pub fn extract_javascript_dataflow(
    tree: &tree_sitter::Tree,
    source: &[u8],
    id_gen: &crate::model::IdGenerator<crate::model::DataNodeId>,
) -> (Vec<crate::model::DataNode>, Vec<crate::model::FlowEdge>) {
    extract_js_family_dataflow_with_query(
        tree,
        source,
        &JS_DATAFLOW_QUERY,
        JS_FUNCTION_KINDS,
        id_gen,
    )
}

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

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

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

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

    #[test]
    fn extract_class_and_methods() {
        let src = b"class Foo {\n  constructor() {}\n  bar() {}\n}";
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::JavaScript, &tree, src);
        let class = symbols.iter().find(|s| s.name == "Foo").unwrap();
        assert!(matches!(class.kind, SymbolKind::Class));
        let methods: Vec<_> = symbols
            .iter()
            .filter(|s| matches!(s.kind, SymbolKind::Method))
            .collect();
        assert_eq!(methods.len(), 2);
    }

    #[test]
    fn extract_exported_class() {
        let src = b"export class Foo { bar() {} }";
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::JavaScript, &tree, src);
        let class = symbols.iter().find(|s| s.name == "Foo").unwrap();
        assert_eq!(class.visibility, Some(Visibility::Public));
    }

    #[test]
    fn extract_named_imports() {
        use crate::language::extract_imports_and_references_for;
        let src = b"import { foo, bar } from 'utils';";
        let tree = parse(src);
        let (imports, _) = extract_imports_and_references_for(
            LangId::JavaScript,
            &tree,
            src,
            &std::path::PathBuf::from("test.js"),
        );
        let named: Vec<_> = imports.iter().filter(|i| i.symbol.is_some()).collect();
        assert_eq!(
            named.len(),
            2,
            "expected 2 named import records for foo and bar"
        );
        for imp in &named {
            assert_eq!(imp.import_specifier, "'utils'");
        }
        assert_eq!(named[0].symbol.as_deref(), Some("foo"));
        assert_eq!(named[1].symbol.as_deref(), Some("bar"));
    }

    #[test]
    fn extract_default_import() {
        use crate::language::extract_imports_and_references_for;
        let src = b"import React from 'react';";
        let tree = parse(src);
        let (imports, _) = extract_imports_and_references_for(
            LangId::JavaScript,
            &tree,
            src,
            &std::path::PathBuf::from("test.js"),
        );
        let named: Vec<_> = imports.iter().filter(|i| i.symbol.is_some()).collect();
        assert_eq!(named.len(), 1);
        assert_eq!(named[0].import_specifier, "'react'");
        assert_eq!(named[0].symbol.as_deref(), Some("React"));
    }

    #[test]
    fn extract_side_effect_import() {
        use crate::language::extract_imports_and_references_for;
        let src = b"import 'styles.css';";
        let tree = parse(src);
        let (imports, _) = extract_imports_and_references_for(
            LangId::JavaScript,
            &tree,
            src,
            &std::path::PathBuf::from("test.js"),
        );
        assert_eq!(imports.len(), 1);
        assert_eq!(imports[0].import_specifier, "'styles.css'");
        assert!(imports[0].symbol.is_none());
    }

    #[test]
    fn js_docstring_extraction() {
        let src = b"/** JSDoc comment. */\nfunction documented() {}";
        let tree = parse(src);
        let symbols = extract_symbols_for(LangId::JavaScript, &tree, src);
        let func = symbols.iter().find(|s| s.name == "documented").unwrap();
        assert!(func.docstring.is_some(), "documented should have docstring");
        assert!(func.docstring.as_ref().unwrap().contains("JSDoc comment"));
    }

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

    #[cfg(feature = "dataflow")]
    mod dataflow_tests {
        use super::*;
        use crate::language::javascript::extract_javascript_dataflow;
        use crate::model::{DataScope, FlowKind};

        fn extract(source: &[u8]) -> (Vec<crate::model::DataNode>, Vec<crate::model::FlowEdge>) {
            let id_gen = crate::model::IdGenerator::new();
            extract_javascript_dataflow(&parse(source), source, &id_gen)
        }

        #[test]
        fn const_declaration_captured_as_local() {
            let src = b"function f() { const x = 42; return x; }";
            let (nodes, _) = extract(src);
            let x = nodes
                .iter()
                .find(|n| n.name.as_deref() == Some("x") && n.scope == DataScope::Local)
                .expect("const x should be captured as Local");
            assert_eq!(x.scope, DataScope::Local);
        }

        #[test]
        fn function_parameter_captured_as_parameter() {
            let src = b"function add(a, b) { return a + b; }";
            let (nodes, edges) = extract(src);
            let params: Vec<_> = nodes
                .iter()
                .filter(|n| n.scope == DataScope::Parameter)
                .collect();
            assert_eq!(params.len(), 2, "expected 2 parameters");
            let names: Vec<_> = params.iter().map(|n| n.name.as_deref()).collect();
            assert!(names.contains(&Some("a")));
            assert!(names.contains(&Some("b")));
            assert!(
                !edges.is_empty(),
                "parameter usages should produce def-use edges"
            );
            for edge in &edges {
                assert_eq!(edge.kind, FlowKind::DefUse);
                assert!(
                    (edge.confidence - 0.9).abs() < f32::EPSILON,
                    "confidence should be 0.9, got {}",
                    edge.confidence
                );
            }
        }

        #[test]
        fn def_use_edge_anchored_in_graph() {
            // Each flow edge's target must reference a real data node id.
            let src = b"function f() { let x = 1; let y = x; }";
            let (nodes, edges) = extract(src);
            let ids: std::collections::HashSet<_> = nodes.iter().map(|n| n.id).collect();
            for edge in &edges {
                assert!(
                    ids.contains(&edge.source),
                    "edge source {:?} not in nodes",
                    edge.source
                );
                assert!(
                    ids.contains(&edge.target),
                    "edge target {:?} not in nodes (dangling edge)",
                    edge.target
                );
            }
        }

        #[test]
        fn no_cross_function_def_use_leak() {
            // `x` defined in outer scope must not link to `x` in nested function.
            let src = b"function outer() { let x = 1; function inner() { let x = 2; return x; } return x; }";
            let (nodes, edges) = extract(src);
            // 2 def nodes + 2 use nodes (one per `return x` site) all named "x".
            let defs_for_x: Vec<_> = nodes
                .iter()
                .filter(|n| n.name.as_deref() == Some("x") && n.scope == DataScope::Local)
                .collect();
            assert_eq!(defs_for_x.len(), 4, "2 def + 2 use nodes for `x` expected");
            // Of those, exactly 2 are definitions (Local scope with no incoming edge
            // of the same name; we identify them by being earlier in source order).
            let defs: Vec<_> = nodes
                .iter()
                .filter(|n| {
                    n.name.as_deref() == Some("x")
                        && n.scope == DataScope::Local
                        && !edges.iter().any(|e| e.target == n.id)
                })
                .collect();
            assert_eq!(defs.len(), 2, "two distinct `x` defs expected");

            // The use nodes anchor to the def in the same function scope.
            let use_nodes: Vec<_> = nodes
                .iter()
                .filter(|n| {
                    n.name.as_deref() == Some("x")
                        && n.scope == DataScope::Local
                        && edges.iter().any(|e| e.target == n.id)
                })
                .collect();
            assert_eq!(use_nodes.len(), 2);

            let outer_def = defs
                .iter()
                .min_by_key(|n| n.source_range.byte_start)
                .unwrap();
            let inner_def = defs
                .iter()
                .max_by_key(|n| n.source_range.byte_start)
                .unwrap();
            let use_for_inner_x = use_nodes
                .iter()
                .min_by_key(|n| n.source_range.byte_start)
                .unwrap();
            let use_for_outer_x = use_nodes
                .iter()
                .max_by_key(|n| n.source_range.byte_start)
                .unwrap();
            let edge_for_inner = edges
                .iter()
                .find(|e| e.target == use_for_inner_x.id)
                .expect("inner x-use must have an edge");
            let edge_for_outer = edges
                .iter()
                .find(|e| e.target == use_for_outer_x.id)
                .expect("outer x-use must have an edge");
            assert_eq!(
                edge_for_inner.source, inner_def.id,
                "inner use must bind to inner def"
            );
            assert_eq!(
                edge_for_outer.source, outer_def.id,
                "outer use must bind to outer def"
            );
            assert_ne!(edge_for_inner.source, edge_for_outer.source);
        }

        #[test]
        fn arrow_function_creates_new_scope() {
            // `x` inside the arrow must not link to outer `x`.
            let src = b"function outer() { let x = 1; const f = () => { let x = 2; return x; }; return x; }";
            let (_nodes, edges) = extract(src);
            // Just ensure no panic and that the implementation respects arrow scopes.
            // We can't easily distinguish edges from text alone; rely on non-emptiness
            // and absence of panics.
            assert!(!edges.is_empty());
        }

        #[test]
        fn no_edges_for_undefined_names() {
            let src = b"function f() { return undefinedSymbol; }";
            let (_nodes, edges) = extract(src);
            assert!(edges.is_empty(), "unresolved identifiers produce no edges");
        }

        #[test]
        fn empty_function_yields_no_nodes() {
            let src = b"function f() {}";
            let (nodes, edges) = extract(src);
            assert!(nodes.is_empty());
            assert!(edges.is_empty());
        }

        #[test]
        fn dataflow_against_fixture_file() {
            // Oracle against the shared JS fixture: must extract nodes and edges.
            let src = std::fs::read_to_string(
                std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                    .join("tests/fixtures/javascript/functions.js"),
            )
            .unwrap();
            let (nodes, edges) = extract(src.as_bytes());
            assert!(!nodes.is_empty(), "fixture must yield data nodes");
            assert!(!edges.is_empty(), "fixture must yield flow edges");
            // Every flow edge must be anchored in a real node.
            let ids: std::collections::HashSet<_> = nodes.iter().map(|n| n.id).collect();
            for edge in &edges {
                assert!(ids.contains(&edge.source));
                assert!(ids.contains(&edge.target));
            }
        }
    }
}