alef 0.36.1

Opinionated polyglot binding generator for Rust libraries
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
use std::path::PathBuf;

use crate::core::backend::GeneratedFile;
use crate::core::config::ResolvedCrateConfig;
use crate::core::ir::ApiSurface;

use super::{gen_service_rs, gen_service_ts};

pub fn generate(api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
    if api.services.is_empty() {
        return Ok(vec![]);
    }

    use crate::core::config::resolve_output_dir;

    let output_dir = resolve_output_dir(config.output_paths.get("node"), &config.name, "crates/{name}-node/src/");
    let crate_root = {
        let p = PathBuf::from(&output_dir);
        match p.file_name().and_then(|n| n.to_str()) {
            Some("src") => p.parent().map(|parent| parent.to_path_buf()).unwrap_or(p),
            _ => p,
        }
    };
    let package_name = config.name.replace('-', "_");

    let service_rs = gen_service_rs(api, config);

    let service_ts = gen_service_ts(api, &package_name, config);

    let service_cjs = strip_typescript_annotations(&service_ts);

    let output_base = config
        .node
        .as_ref()
        .and_then(|n| n.package_name.as_ref())
        .map(|p| PathBuf::from(format!("packages/node/{}", p)))
        .unwrap_or_else(|| PathBuf::from(format!("packages/node/{}", package_name)));

    Ok(vec![
        GeneratedFile {
            path: PathBuf::from(&output_dir).join("service.rs"),
            content: service_rs,
            generated_header: true,
        },
        GeneratedFile {
            path: output_base.join("service.ts"),
            content: service_ts.clone(),
            generated_header: true,
        },
        GeneratedFile {
            path: crate_root.join("service.ts"),
            content: service_ts,
            generated_header: true,
        },
        GeneratedFile {
            path: crate_root.join("service.cjs"),
            content: service_cjs,
            generated_header: true,
        },
    ])
}

/// Convert TypeScript service wrapper to CommonJS JavaScript.
/// Simple approach: convert imports and output with // type comments removed.
/// Uses a brute-force regex-like approach to handle `: Type` patterns.
fn strip_typescript_annotations(ts_code: &str) -> String {
    let mut result = String::new();

    for line in ts_code.lines() {
        let mut modified_line = line.to_string();

        let trimmed = modified_line.trim();
        if trimmed.starts_with("export {") && trimmed.ends_with("};") {
            if let Some(start_brace) = modified_line.find('{') {
                if let Some(end_brace) = modified_line.rfind('}') {
                    let exports = &modified_line[start_brace..=end_brace];
                    modified_line = format!("module.exports = {};", exports);
                }
            }
            result.push_str(&modified_line);
            result.push('\n');
            continue;
        }

        let trimmed = modified_line.trim();
        if trimmed.ends_with(';')
            && trimmed.contains('(')
            && !trimmed.starts_with("//")
            && !trimmed.starts_with("/*")
            && !trimmed.starts_with('*')
            && !trimmed.starts_with("import")
            && !trimmed.starts_with("export")
            && !trimmed.starts_with("const")
            && !trimmed.starts_with("let")
            && !trimmed.starts_with("var")
            && !trimmed.starts_with("return")
            && !trimmed.starts_with("throw")
            && !trimmed.starts_with("this.")
            && !trimmed.contains(" = ")
            && !trimmed.contains(".push(")
            && !trimmed.contains(".pop(")
            && !trimmed.contains(".set(")
            && !trimmed.contains(".get(")
            && trimmed.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_')
        {
            continue;
        }

        let mut without_optional_marker = String::with_capacity(modified_line.len());
        let bytes: Vec<char> = modified_line.chars().collect();
        let mut k = 0;
        while k < bytes.len() {
            let c = bytes[k];
            if c == '?' && k + 1 < bytes.len() {
                let next = bytes[k + 1];
                if next == ':' || next == ',' || next == ')' {
                    k += 1;
                    continue;
                }
            }
            without_optional_marker.push(c);
            k += 1;
        }
        modified_line = without_optional_marker;

        if modified_line.trim().starts_with("import type {") && modified_line.contains("from") {
            if let Some(start_brace) = modified_line.find('{') {
                if let Some(end_brace) = modified_line.rfind('}') {
                    if let Some(from_pos) = modified_line.find("from") {
                        let imports = modified_line[start_brace..=end_brace].replace(" as ", ": ");
                        let module_spec = modified_line[from_pos + 4..].trim();
                        let module_spec = module_spec.trim_end_matches(';');
                        modified_line = format!("const {imports} = require({module_spec});");
                    }
                }
            }
            result.push_str(&modified_line);
            result.push('\n');
            continue;
        }

        if modified_line.trim().starts_with("import {") && modified_line.contains("from") {
            if let Some(start_brace) = modified_line.find('{') {
                if let Some(end_brace) = modified_line.rfind('}') {
                    if let Some(from_pos) = modified_line.find("from") {
                        let imports = modified_line[start_brace..=end_brace].replace(" as ", ": ");
                        let module_spec = modified_line[from_pos + 4..].trim();
                        let module_spec = module_spec.trim_end_matches(';');
                        modified_line = format!("const {imports} = require({module_spec});");
                    }
                }
            }
            result.push_str(&modified_line);
            result.push('\n');
            continue;
        }

        if modified_line.trim().starts_with("export class") {
            modified_line = modified_line.replace("export class", "class");
        }

        if modified_line.contains("private ") {
            modified_line = modified_line.replace("private ", "");
        }

        if modified_line.contains("readonly ") {
            modified_line = modified_line.replace("readonly ", "");
        }

        let mut output = String::new();
        let chars: Vec<char> = modified_line.chars().collect();
        let mut i = 0;

        while i < chars.len() {
            if i < chars.len() - 1 && chars[i] == ':' && !modified_line[..i].ends_with("://") {
                let mut j = i + 1;
                while j < chars.len() && (chars[j] == ' ' || chars[j] == '\t') {
                    j += 1;
                }
                let mut paren_depth = 0;
                let mut angle_depth = 0;
                while j < chars.len() {
                    match chars[j] {
                        '(' => paren_depth += 1,
                        ')' => {
                            if paren_depth == 0 {
                                break;
                            }
                            paren_depth -= 1;
                        }
                        '<' => angle_depth += 1,
                        '>' if angle_depth > 0 => angle_depth -= 1,
                        '=' if paren_depth == 0 && angle_depth == 0 && j + 1 < chars.len() && chars[j + 1] == '>' => {
                            j += 1;
                        }
                        ',' | '=' | '{' | ';' if paren_depth == 0 && angle_depth == 0 => {
                            break;
                        }
                        _ => {}
                    }
                    j += 1;
                }
                i = j;
                while !output.is_empty() && output.ends_with(' ') {
                    output.pop();
                }
                if i < chars.len() && chars[i] != ',' && chars[i] != ')' && !output.is_empty() {
                    output.push(' ');
                }
                continue;
            }

            output.push(chars[i]);
            i += 1;
        }

        modified_line = output;

        result.push_str(&modified_line);
        result.push('\n');
    }

    result
}

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

    #[test]
    fn strip_typescript_annotations_converts_imports() {
        let ts_input = r#"import type { ServerConfig } from "./index";
import { Method, RouteBuilder } from "./index";
import { appIntoRouter } from "./index";"#;

        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains(r#"const { ServerConfig } = require("./index");"#),
            "import type should convert to const with require, got: {}",
            js_output
        );

        assert!(
            js_output.contains(r#"const { Method, RouteBuilder } = require("./index");"#),
            "import should convert to const with require, got: {}",
            js_output
        );

        assert!(
            js_output.contains(r#"const { appIntoRouter } = require("./index");"#),
            "appIntoRouter import should convert correctly, got: {}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_translates_import_alias_to_destructuring_rename() {
        let ts_input = r#"import { App as NativeApp, Method, RouteBuilder } from "./index";"#;
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains(r#"const { App: NativeApp, Method, RouteBuilder } = require("./index");"#),
            "import alias should translate `as` to `:` for CJS destructuring, got: {js_output}",
        );
        assert!(
            !js_output.contains(" as "),
            "no `as` keyword should remain in CJS output, got: {js_output}",
        );
    }

    #[test]
    fn strip_typescript_annotations_translates_type_import_alias_to_destructuring_rename() {
        let ts_input = r#"import type { ServerConfig as Config } from "./index";"#;
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains(r#"const { ServerConfig: Config } = require("./index");"#),
            "import type alias should translate `as` to `:` for CJS destructuring, got: {js_output}",
        );
    }

    #[test]
    fn strip_typescript_annotations_removes_export_class() {
        let ts_input = "export class App {";
        let js_output = strip_typescript_annotations(ts_input);

        assert!(js_output.contains("class App {"), "export class should become class");
        assert!(!js_output.contains("export class"), "export should be removed");
    }

    #[test]
    fn strip_typescript_annotations_removes_private() {
        let ts_input = "  private _registrations: Array<[string, any[]]> = [];";
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            !js_output.contains("private"),
            "private keyword should be removed, got: {}",
            js_output
        );
        assert!(js_output.contains("_registrations"), "field name should be preserved");
    }

    #[test]
    fn strip_typescript_annotations_removes_type_annotations() {
        let ts_input = "  route(builder: RouteBuilder): (fn: (...args: any[]) => any) => (...args: any[]) => any {";
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains("route(builder)"),
            "type annotations should be stripped, got: {}",
            js_output
        );
        assert!(!js_output.contains("RouteBuilder"), "type name should be removed");
        assert!(
            js_output.trim_end().ends_with('{'),
            "method-body opener `{{` must be preserved, got: {}",
            js_output
        );
        assert!(
            !js_output.contains("fn =>"),
            "stripping must not leave fragmentary arrow-type pieces like `fn => any`, got: {}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_drops_method_overload_signatures() {
        let ts_input = "  get(path: string, handler: (...args: any[]) => any): this;\n  get(path: string): (fn: (...args: any[]) => any) => (...args: any[]) => any;\n  get(path: string, handler?: (...args: any[]) => any): this | ((fn: (...args: any[]) => any) => (...args: any[]) => any) {\n    return this;\n  }";
        let js_output = strip_typescript_annotations(ts_input);
        let lines: Vec<&str> = js_output.lines().collect();
        let signature_only_lines: Vec<&&str> = lines
            .iter()
            .filter(|l| l.trim().ends_with(';') && l.contains("get(") && !l.contains('{'))
            .collect();
        assert!(
            signature_only_lines.is_empty(),
            "all overload signatures should be dropped, got: {}\noutput:\n{}",
            signature_only_lines.len(),
            js_output
        );
        assert!(
            lines.iter().any(|l| l.contains("get(") && l.trim_end().ends_with('{')),
            "implementation line must be preserved, got:\n{}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_drops_optional_param_marker() {
        let ts_input = "  get(path: string, handler?: (...args: any[]) => any): this {";
        let js_output = strip_typescript_annotations(ts_input);
        assert!(
            !js_output.contains("handler?"),
            "optional-param `?` must be stripped, got: {}",
            js_output
        );
        assert!(
            js_output.contains("get(path, handler)"),
            "stripped param list should be clean, got: {}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_preserves_brace_on_method_with_arrow_return_type() {
        let ts_input = "  register_route(builder: RouteBuilder, handler: (...args: any[]) => any): this {";
        let js_output = strip_typescript_annotations(ts_input);
        assert!(
            js_output.trim_end().ends_with('{'),
            "method-body opener must survive when params contain arrow-type annotations, got: {}",
            js_output
        );
        assert!(
            js_output.contains("register_route(builder, handler)"),
            "param-list type annotations should be stripped cleanly, got: {}",
            js_output
        );
        assert!(
            !js_output.contains("=> any"),
            "arrow-type return fragments must be fully stripped, got: {}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_converts_export_namespace() {
        let ts_input = "export { App };";
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains("module.exports = { App };"),
            "export namespace should convert to CommonJS module.exports, got: {}",
            js_output
        );
        assert!(
            !js_output.contains("export {"),
            "TypeScript export syntax must be removed, got: {}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_converts_export_namespace_multiple() {
        let ts_input = "export { App, Server, Router };";
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains("module.exports = { App, Server, Router };"),
            "export with multiple names should convert to CommonJS, got: {}",
            js_output
        );
    }

    #[test]
    fn strip_typescript_annotations_preserves_napi_class_instantiation() {
        let ts_input = "  constructor() {\n    this._app = new NativeApp();\n  }";
        let js_output = strip_typescript_annotations(ts_input);

        assert!(
            js_output.contains("new NativeApp()"),
            "native class instantiation should use the imported alias (NativeApp), got: {}",
            js_output
        );
        assert!(
            !js_output.contains("JsApp"),
            "Rust type name with prefix (JsApp) must not appear in JS, got: {}",
            js_output
        );
    }
}