rust-anything 1.4.0

Rewrite your app in Rust, one piece at a time, checked against the original's recorded behavior at every step. TypeScript/JavaScript, Python, C, Go, Ruby and Java
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! Go: modules, imports, risks, the module-to-crate table, and exported functions with Go types
//! mapped into the TS-like type dialect. A unit is a package (a folder).

use std::collections::HashMap;
use std::path::Path;

use anyhow::Result;
use tree_sitter::Node;

use super::syntax::{self, named_children, text};
use crate::risks::Risk;
use crate::ts::deps::{CrateMatch, Fit};
use crate::ts::discover::{FileKind, Package, SourceFile, Workspace};
use crate::ts::exports::{ExportedFn, Param};
use crate::ts::scan::{FileFacts, Import};

fn language() -> tree_sitter::Language {
    tree_sitter_go::LANGUAGE.into()
}

const SKIP_DIRS: &[&str] = &["vendor", "testdata", "node_modules", "target", ".git"];

// ---- discovery -------------------------------------------------------------------------------

/// The `module` line of a `go.mod`.
pub fn module_path(go_mod: &str) -> Option<String> {
    go_mod.lines().find_map(|l| {
        l.trim()
            .strip_prefix("module ")
            .map(|m| m.trim().trim_matches('"').to_string())
    })
}

fn requirements(go_mod: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut in_block = false;
    for line in go_mod.lines() {
        let line = line.trim();
        if line.starts_with("require (") {
            in_block = true;
            continue;
        }
        if in_block && line == ")" {
            in_block = false;
            continue;
        }
        let spec = if in_block {
            Some(line)
        } else {
            line.strip_prefix("require ")
        };
        if let Some(spec) = spec
            && !line.contains("// indirect")
            && let Some(name) = spec.split_whitespace().next()
            && !name.is_empty()
            && !name.starts_with("//")
        {
            out.push(name.to_string());
        }
    }
    out
}

pub fn discover(root: &Path) -> Result<Workspace> {
    let mut modules = Vec::new();
    let mut files = Vec::new();
    let walker = crate::walk::filtered(root, |e| {
        let name = e.file_name().to_string_lossy();
        !(e.file_type().is_some_and(|t| t.is_dir())
            && (SKIP_DIRS.contains(&name.as_ref()) || name.starts_with('_')))
    })
    .hidden(true)
    .require_git(false)
    .build();
    for entry in walker.flatten() {
        if !entry.file_type().is_some_and(|t| t.is_file()) {
            continue;
        }
        let Ok(rel) = entry.path().strip_prefix(root) else {
            continue;
        };
        let rel = rel.to_string_lossy().replace('\\', "/");
        let name = rel.rsplit('/').next().unwrap_or(&rel);
        let dir = rel
            .rsplit_once('/')
            .map_or(String::new(), |(d, _)| d.to_string());
        if name == "go.mod" {
            let text = std::fs::read_to_string(entry.path()).unwrap_or_default();
            modules.push(Package {
                name: module_path(&text).unwrap_or_else(|| dir.clone()),
                src_root: dir.clone(),
                dir,
                deps: requirements(&text),
                dev_deps: Vec::new(),
            });
        } else if name.ends_with(".go") {
            let kind = if name.ends_with("_test.go") {
                FileKind::Test
            } else {
                FileKind::Source
            };
            files.push((rel, kind));
        }
    }
    if modules.is_empty() {
        modules.push(Package {
            name: root
                .file_name()
                .map_or_else(|| "root".into(), |n| n.to_string_lossy().into_owned()),
            dir: String::new(),
            src_root: String::new(),
            deps: Vec::new(),
            dev_deps: Vec::new(),
        });
    }
    modules.sort_by(|a, b| a.dir.cmp(&b.dir));
    files.sort_by(|a, b| a.0.cmp(&b.0));
    let files = files
        .into_iter()
        .map(|(path, kind)| {
            let package = modules
                .iter()
                .enumerate()
                .filter(|(_, p)| p.dir.is_empty() || path.starts_with(&format!("{}/", p.dir)))
                .max_by_key(|(_, p)| p.dir.len())
                .map_or(0, |(i, _)| i);
            SourceFile {
                path,
                package,
                kind,
            }
        })
        .collect();
    Ok(Workspace {
        packages: modules,
        files,
    })
}

// ---- risks and imports -----------------------------------------------------------------------

pub fn scan(source: &str) -> FileFacts {
    let mut facts = FileFacts {
        loc: syntax::loc(source),
        ..FileFacts::default()
    };
    let Some(tree) = syntax::parse(&language(), source) else {
        return facts;
    };
    let root = tree.root_node();
    facts.parse_errors = usize::from(root.has_error());
    let add = |facts: &mut FileFacts, risk: Risk, node: Node| {
        facts
            .risks
            .entry(risk)
            .or_default()
            .push(syntax::line(node))
    };
    syntax::walk(root, |node| match node.kind() {
        "import_spec" => {
            if let Some(path) = node.child_by_field_name("path") {
                let spec = text(path, source).trim_matches(|c| c == '"' || c == '`');
                match spec {
                    "unsafe" => add(&mut facts, Risk::Pointers, node),
                    "reflect" => add(&mut facts, Risk::Reflection, node),
                    "sync" | "sync/atomic" => add(&mut facts, Risk::Concurrency, node),
                    "regexp" => add(&mut facts, Risk::RegexSyntax, node),
                    "encoding/json" => add(&mut facts, Risk::Json, node),
                    _ => {}
                }
                facts.imports.push(Import {
                    specifier: spec.to_string(),
                    type_only: false,
                    dynamic: false,
                });
            }
        }
        "go_statement" | "channel_type" | "send_statement" | "select_statement" => {
            add(&mut facts, Risk::Concurrency, node)
        }
        "pointer_type" | "unary_expression" if text(node, source).starts_with('&') => {
            add(&mut facts, Risk::Pointers, node)
        }
        "pointer_type" => add(&mut facts, Risk::Pointers, node),
        "slice_expression" => add(&mut facts, Risk::StringOffsets, node),
        "binary_expression" => {
            let op = node
                .child_by_field_name("operator")
                .map_or("", |o| text(o, source));
            match op {
                "/" | "%" => add(&mut facts, Risk::DivisionSemantics, node),
                "<<" | ">>" => add(&mut facts, Risk::IntegerSemantics, node),
                _ => {}
            }
        }
        "type_conversion_expression" => add(&mut facts, Risk::IntegerSemantics, node),
        "call_expression" => {
            let callee = node
                .child_by_field_name("function")
                .map_or("", |f| text(f, source));
            match callee {
                "panic" | "recover" => add(&mut facts, Risk::Exceptions, node),
                "int" | "int32" | "int64" | "uint" | "uint32" | "uint64" | "uint8" | "byte"
                | "float64" => add(&mut facts, Risk::IntegerSemantics, node),
                _ => {}
            }
        }
        "nil" => add(&mut facts, Risk::NullReferences, node),
        "interface_type" | "type_switch_statement" | "type_assertion_expression" => {
            add(&mut facts, Risk::DynamicTyping, node)
        }
        "func_literal" | "function_type" => add(&mut facts, Risk::Callbacks, node),
        "defer_statement" => add(&mut facts, Risk::Exceptions, node),
        "range_clause" => {
            facts.loops += 1;
            if node
                .child_by_field_name("right")
                .is_some_and(|r| text(r, source).contains("map"))
            {
                add(&mut facts, Risk::UnorderedIteration, node);
            }
        }
        "for_statement" => facts.loops += 1,
        _ => {}
    });
    facts
}

/// Go standard-library import paths have no dot in their first element.
fn is_stdlib(spec: &str) -> bool {
    !spec.split('/').next().unwrap_or("").contains('.')
}

pub fn resolve(spec: &str, index: &HashMap<&str, usize>, ws: &Workspace) -> super::Resolution {
    // The longest module path that prefixes the import is the module it belongs to.
    if let Some(module) = ws
        .packages
        .iter()
        .filter(|p| spec == p.name || spec.starts_with(&format!("{}/", p.name)))
        .max_by_key(|p| p.name.len())
    {
        let sub = spec[module.name.len()..].trim_start_matches('/');
        let dir = match (module.dir.as_str(), sub) {
            ("", s) => s.to_string(),
            (d, "") => d.to_string(),
            (d, s) => format!("{d}/{s}"),
        };
        let mut best: Option<(&str, usize)> = None;
        for (path, i) in index {
            let parent = path.rsplit_once('/').map_or("", |(d, _)| d);
            if parent == dir
                && path.ends_with(".go")
                && !path.ends_with("_test.go")
                && best.is_none_or(|(p, _)| *path < p)
            {
                best = Some((path, *i));
            }
        }
        return best.map_or(super::Resolution::Unresolved, |(_, i)| {
            super::Resolution::File(i)
        });
    }
    if is_stdlib(spec) {
        return super::Resolution::Stdlib;
    }
    super::Resolution::External(spec.to_string())
}

const fn m(crates: &'static str, fit: Fit, note: &'static str) -> CrateMatch {
    CrateMatch { crates, fit, note }
}

/// Rust counterparts of common Go modules.
pub fn lookup(module: &str) -> Option<CrateMatch> {
    Some(match module {
        "github.com/spf13/cobra" | "github.com/urfave/cli" | "github.com/urfave/cli/v2" => {
            m("clap", Fit::Good, "")
        }
        "github.com/spf13/viper" => m("config, figment", Fit::Partial, ""),
        "github.com/sirupsen/logrus" | "go.uber.org/zap" | "github.com/rs/zerolog" => {
            m("tracing", Fit::Good, "")
        }
        "github.com/stretchr/testify" => m("std test, pretty_assertions", Fit::Good, ""),
        "github.com/google/uuid" => m("uuid", Fit::Good, ""),
        "github.com/gorilla/mux" | "github.com/gin-gonic/gin" | "github.com/labstack/echo/v4" => {
            m("axum", Fit::Partial, "routing and middleware differ")
        }
        "gopkg.in/yaml.v3" | "gopkg.in/yaml.v2" | "sigs.k8s.io/yaml" => {
            m("serde_yaml_ng", Fit::Good, "")
        }
        "github.com/BurntSushi/toml" | "github.com/pelletier/go-toml/v2" => {
            m("toml", Fit::Good, "")
        }
        "golang.org/x/text" => m("unicode-normalization, icu", Fit::Partial, ""),
        "golang.org/x/sync" => m("tokio, rayon", Fit::Partial, ""),
        "github.com/jackc/pgx/v5" | "github.com/lib/pq" => m("sqlx, tokio-postgres", Fit::Good, ""),
        "github.com/mattn/go-sqlite3" => m("rusqlite", Fit::Good, ""),
        "github.com/prometheus/client_golang" => m("prometheus", Fit::Good, ""),
        "google.golang.org/grpc" => m("tonic", Fit::Good, ""),
        "google.golang.org/protobuf" | "github.com/golang/protobuf" => m("prost", Fit::Good, ""),
        "github.com/dlclark/regexp2" => m("fancy-regex", Fit::Good, ""),
        _ => return None,
    })
}

// ---- exports ---------------------------------------------------------------------------------

/// Go builtin types the bridge carries, as the TS-like dialect.
fn builtin(t: &str) -> Option<&'static str> {
    Some(match t {
        "string" => "string",
        "bool" => "boolean",
        "int" | "int8" | "int16" | "int32" | "int64" | "uint" | "uint8" | "uint16" | "uint32"
        | "uint64" | "uintptr" | "byte" | "rune" => "int",
        "float32" | "float64" => "number",
        _ => return None,
    })
}

/// Exported struct types of a file: name to dialect object type, from their JSON field names.
fn structs(root: Node, source: &str) -> HashMap<String, String> {
    let mut out = HashMap::new();
    for decl in named_children(root)
        .into_iter()
        .filter(|n| n.kind() == "type_declaration")
    {
        for spec in named_children(decl)
            .into_iter()
            .filter(|n| n.kind() == "type_spec")
        {
            let (Some(name), Some(ty)) = (
                spec.child_by_field_name("name"),
                spec.child_by_field_name("type"),
            ) else {
                continue;
            };
            if ty.kind() != "struct_type" {
                continue;
            }
            let mut fields = Vec::new();
            let mut ok = true;
            syntax::walk(ty, |n| {
                if n.kind() != "field_declaration" {
                    return;
                }
                let field_type = n
                    .child_by_field_name("type")
                    .map_or("", |t| text(t, source));
                let tag = n.child_by_field_name("tag").map_or("", |t| text(t, source));
                let json_name = tag
                    .split("json:\"")
                    .nth(1)
                    .and_then(|r| r.split('"').next())
                    .map(|r| r.split(',').next().unwrap_or("").to_string());
                let mut cursor = n.walk();
                let names: Vec<String> = n
                    .children_by_field_name("name", &mut cursor)
                    .map(|f| text(f, source).to_string())
                    .collect();
                if names.is_empty() {
                    ok = false; // embedded field
                }
                for field in names {
                    if !field.starts_with(char::is_uppercase) || json_name.as_deref() == Some("-") {
                        continue;
                    }
                    match go_to_ts(field_type, &HashMap::new()) {
                        Ok(t) => {
                            let key = json_name.clone().filter(|j| !j.is_empty()).unwrap_or(field);
                            fields.push(format!("{key}: {t}"));
                        }
                        Err(_) => ok = false,
                    }
                }
            });
            if ok {
                out.insert(
                    text(name, source).to_string(),
                    format!("{{ {} }}", fields.join("; ")),
                );
            }
        }
    }
    out
}

/// A Go type in the TS-like dialect, or why the bridge cannot carry it.
pub fn go_to_ts(t: &str, structs: &HashMap<String, String>) -> Result<String, String> {
    let t = t.trim();
    if let Some(b) = builtin(t) {
        return Ok(b.to_string());
    }
    if let Some(inner) = t.strip_prefix("[]") {
        return go_to_ts(inner, structs).map(|i| {
            if i.contains(' ') || i.contains('|') {
                format!("({i})[]")
            } else {
                format!("{i}[]")
            }
        });
    }
    if let Some(rest) = t.strip_prefix("map[") {
        let mut depth = 1;
        let close = rest.char_indices().find_map(|(i, c)| {
            match c {
                '[' => depth += 1,
                ']' => depth -= 1,
                _ => {}
            }
            (depth == 0).then_some(i)
        });
        if let Some(close) = close {
            let (key, value) = (&rest[..close], &rest[close + 1..]);
            if key.trim() != "string" {
                return Err(format!("`{t}`: JSON object keys are strings"));
            }
            return go_to_ts(value, structs).map(|v| format!("Record<string, {v}>"));
        }
    }
    if t.starts_with('*') {
        return Err(format!("takes or returns a pointer (`{t}`)"));
    }
    if t.starts_with("func") {
        return Err("takes or returns a function".into());
    }
    if t.starts_with("chan") || t.starts_with("<-chan") {
        return Err("takes or returns a channel".into());
    }
    if t == "any" || t.starts_with("interface") {
        return Err(format!("`{t}`: values of any type"));
    }
    if t.starts_with('[') {
        return Err(format!("`{t}`: fixed-size array"));
    }
    if t == "error" {
        return Err("takes an `error` value".into());
    }
    if t.contains('.') {
        return Err(format!("uses `{t}` from another package"));
    }
    if !t.starts_with(char::is_uppercase) {
        return Err(format!("uses the unexported type `{t}`"));
    }
    Ok(structs.get(t).cloned().unwrap_or_else(|| "unknown".into()))
}

/// Exported top-level functions. Methods stay with their types: Go has no classes to bridge.
pub fn exports(path: &str, source: &str) -> Vec<ExportedFn> {
    if !path.ends_with(".go") || path.ends_with("_test.go") {
        return Vec::new();
    }
    let Some(tree) = syntax::parse(&language(), source) else {
        return Vec::new();
    };
    let root = tree.root_node();
    let structs = structs(root, source);
    let mut out = Vec::new();
    for node in named_children(root) {
        if node.kind() != "function_declaration" {
            continue;
        }
        let name = node
            .child_by_field_name("name")
            .map(|n| text(n, source).to_string())
            .unwrap_or_default();
        if !name.starts_with(char::is_uppercase) {
            continue;
        }
        let mut blocker = None;
        if node.child_by_field_name("type_parameters").is_some() {
            blocker = Some("generic: Go instantiates it per type".to_string());
        }
        let mut params = Vec::new();
        if let Some(list) = node.child_by_field_name("parameters") {
            for (i, p) in named_children(list).into_iter().enumerate() {
                if p.kind() == "variadic_parameter_declaration" {
                    blocker.get_or_insert_with(|| "is variadic".into());
                }
                let go_type = p
                    .child_by_field_name("type")
                    .map_or("", |t| text(t, source))
                    .to_string();
                let ts_type = go_to_ts(&go_type, &structs).unwrap_or_else(|reason| {
                    blocker.get_or_insert(reason);
                    "unknown".into()
                });
                let mut cursor = p.walk();
                let mut names: Vec<String> = p
                    .children_by_field_name("name", &mut cursor)
                    .map(|n| text(n, source).to_string())
                    .collect();
                if names.is_empty() {
                    names.push(format!("arg{i}"));
                }
                for n in names {
                    let n = if n == "_" {
                        format!("arg{}", params.len())
                    } else {
                        n
                    };
                    params.push(Param {
                        name: n,
                        ts_type: ts_type.clone(),
                        native_type: Some(go_type.clone()),
                        ..Default::default()
                    });
                }
            }
        }
        // Results: none, one type, or a list; a trailing `error` is the error channel.
        let mut results: Vec<String> = Vec::new();
        if let Some(r) = node.child_by_field_name("result") {
            if r.kind() == "parameter_list" {
                for p in named_children(r) {
                    let t = p
                        .child_by_field_name("type")
                        .map_or("", |t| text(t, source))
                        .to_string();
                    let mut cursor = p.walk();
                    let count = p.children_by_field_name("name", &mut cursor).count().max(1);
                    results.extend(std::iter::repeat_n(t, count));
                }
            } else {
                results.push(text(r, source).to_string());
            }
        }
        let fallible = results.last().is_some_and(|r| r == "error");
        if fallible {
            results.pop();
        }
        let mut dialect = Vec::new();
        for r in &results {
            match go_to_ts(r, &structs) {
                Ok(t) => dialect.push(t),
                Err(reason) => {
                    blocker.get_or_insert(reason);
                    dialect.push("unknown".into());
                }
            }
        }
        let returns = match dialect.len() {
            0 => "void".to_string(),
            1 => dialect.remove(0),
            _ => format!("[{}]", dialect.join(", ")),
        };
        let signature = node.child_by_field_name("body").map_or_else(
            || text(node, source),
            |b| source[node.start_byte()..b.start_byte()].trim(),
        );
        out.push(ExportedFn {
            name,
            file: path.to_string(),
            line: syntax::line(node),
            params,
            returns,
            native_return: Some(format!(
                "({}{})",
                results.join(", "),
                if fallible {
                    if results.is_empty() {
                        "error"
                    } else {
                        ", error"
                    }
                } else {
                    ""
                }
            )),
            prototype: Some(signature.split_whitespace().collect::<Vec<_>>().join(" ")),
            blocker,
            ..Default::default()
        });
    }
    out
}

/// The package clause of a Go file.
pub fn package_name(source: &str) -> Option<String> {
    source.lines().find_map(|l| {
        l.trim()
            .strip_prefix("package ")
            .map(|p| p.split_whitespace().next().unwrap_or("").to_string())
    })
}

/// The `type` declarations named `names`, as written, from the given sources.
pub fn type_decls(sources: &[String], names: &[String]) -> Vec<String> {
    let mut out = Vec::new();
    for source in sources {
        let Some(tree) = syntax::parse(&language(), source) else {
            continue;
        };
        for decl in named_children(tree.root_node())
            .into_iter()
            .filter(|n| n.kind() == "type_declaration")
        {
            for spec in named_children(decl)
                .into_iter()
                .filter(|n| n.kind() == "type_spec")
            {
                if spec
                    .child_by_field_name("name")
                    .is_some_and(|n| names.iter().any(|w| w == text(n, source)))
                {
                    out.push(format!("type {}", text(spec, source)));
                }
            }
        }
    }
    out
}

/// The exported fields of struct type `name`: (field, Go type, JSON key). `None` when the type
/// is not a plain struct (embedded fields, or not found).
pub fn struct_fields(sources: &[String], name: &str) -> Option<Vec<(String, String, String)>> {
    for source in sources {
        let Some(tree) = syntax::parse(&language(), source) else {
            continue;
        };
        for decl in named_children(tree.root_node())
            .into_iter()
            .filter(|n| n.kind() == "type_declaration")
        {
            for spec in named_children(decl)
                .into_iter()
                .filter(|n| n.kind() == "type_spec")
            {
                if spec.child_by_field_name("name").map(|n| text(n, source)) != Some(name) {
                    continue;
                }
                let ty = spec.child_by_field_name("type")?;
                if ty.kind() != "struct_type" {
                    return None;
                }
                let mut out = Vec::new();
                let mut ok = true;
                syntax::walk(ty, |n| {
                    if n.kind() != "field_declaration" {
                        return;
                    }
                    let field_type = n
                        .child_by_field_name("type")
                        .map_or("", |t| text(t, source))
                        .to_string();
                    let tag = n.child_by_field_name("tag").map_or("", |t| text(t, source));
                    let json = tag
                        .split("json:\"")
                        .nth(1)
                        .and_then(|r| r.split('"').next())
                        .map(|r| r.split(',').next().unwrap_or("").to_string());
                    let mut cursor = n.walk();
                    let names: Vec<String> = n
                        .children_by_field_name("name", &mut cursor)
                        .map(|f| text(f, source).to_string())
                        .collect();
                    if names.is_empty() {
                        ok = false;
                    }
                    for field in names {
                        if !field.starts_with(char::is_uppercase) || json.as_deref() == Some("-") {
                            continue;
                        }
                        let key = json
                            .clone()
                            .filter(|j| !j.is_empty())
                            .unwrap_or_else(|| field.clone());
                        out.push((field, field_type.clone(), key));
                    }
                });
                return ok.then_some(out);
            }
        }
    }
    None
}

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

    #[test]
    fn maps_types() {
        let none = HashMap::new();
        assert_eq!(go_to_ts("[]string", &none).unwrap(), "string[]");
        assert_eq!(
            go_to_ts("map[string][]int", &none).unwrap(),
            "Record<string, int[]>"
        );
        assert!(go_to_ts("*Point", &none).is_err());
        assert!(go_to_ts("map[int]string", &none).is_err());
    }

    #[test]
    fn finds_exports() {
        let src = "package x\n\ntype Point struct {\n\tX int `json:\"x\"`\n\tY float64\n\tz int\n}\n\n\
                   func Add(a, b int) int { return a + b }\n\
                   func Div(a float64, b float64) (float64, error) { return a / b, nil }\n\
                   func Many(xs ...string) []string { return xs }\n\
                   func (p Point) Norm() float64 { return 0 }\n\
                   func Gen[T any](x T) T { return x }\n\
                   func Move(p Point) Point { return p }\n\
                   func hidden() {}\n";
        let fns = exports("x/x.go", src);
        let names: Vec<&str> = fns.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, ["Add", "Div", "Many", "Gen", "Move"]);
        assert_eq!(fns[0].params.len(), 2);
        assert_eq!(fns[0].params[1].ts_type, "int");
        assert_eq!(fns[1].returns, "number");
        assert_eq!(fns[1].native_return.as_deref(), Some("(float64, error)"));
        assert!(fns[2].blocker.is_some() && fns[3].blocker.is_some());
        assert_eq!(fns[4].params[0].ts_type, "{ x: int; Y: number }");
        assert_eq!(
            fns[4].prototype.as_deref(),
            Some("func Move(p Point) Point")
        );
    }
}