dioxus-js-bindgen-macro 0.1.0

Procedural macro engine for dioxus-js-bindgen FFI bridge
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::Path;
use heck::{ToLowerCamelCase, ToSnakeCase};
use proc_macro2::Span;
use quote::quote;
use swc_core::common::comments::{Comments, SingleThreadedComments};
use swc_core::common::sync::Lrc;
use swc_core::common::{FileName, Globals, Mark, SourceMap, GLOBALS};
use swc_core::ecma::ast::*;
use swc_core::ecma::codegen::text_writer::JsWriter;
use swc_core::ecma::codegen::{Config, Emitter};
use swc_core::ecma::parser::{lexer::Lexer, Parser, StringInput, Syntax, TsSyntax};
use swc_core::ecma::transforms::typescript::strip;

use crate::syntax::{ItemSpec, PillarAttr};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InferredPillar {
    Command,
    Query,
    Watcher,
}

#[derive(Debug, Clone)]
pub struct ParamInfo {
    pub name: String,
    pub rust_param_type: proc_macro2::TokenStream,
    pub is_callback: bool,
    pub callback_arg_type: Option<proc_macro2::TokenStream>,
}

#[derive(Debug, Clone)]
pub struct ExportedFunction {
    pub name: String,
    pub pillar: InferredPillar,
    pub params: Vec<ParamInfo>,
    pub return_rust_type: Option<proc_macro2::TokenStream>,
    pub doc_comment: Option<String>,
}

#[derive(Debug, Clone)]
pub struct AnalyzedModule {
    pub module_hash: String,
    pub inlined_js: String,
    pub exports: Vec<ExportedFunction>,
}

pub fn analyze_file(
    file_path: &Path,
    macro_items: &[ItemSpec],
    wildcard: bool,
    call_span: Span,
) -> syn::Result<AnalyzedModule> {
    let source_code = std::fs::read_to_string(file_path).map_err(|e| {
        syn::Error::new(
            call_span,
            format!("Failed to read bridge file '{}': {}", file_path.display(), e),
        )
    })?;

    analyze_source(&source_code, file_path.to_string_lossy().as_ref(), macro_items, wildcard, call_span)
}

pub fn analyze_source(
    source_code: &str,
    file_name: &str,
    macro_items: &[ItemSpec],
    wildcard: bool,
    call_span: Span,
) -> syn::Result<AnalyzedModule> {
    let cm: Lrc<SourceMap> = Default::default();
    let comments = SingleThreadedComments::default();

    let fm = cm.new_source_file(
        Lrc::new(FileName::Custom(file_name.to_string())),
        source_code.to_string(),
    );

    let lexer = Lexer::new(
        Syntax::Typescript(TsSyntax {
            tsx: false,
            decorators: false,
            no_early_errors: true,
            ..Default::default()
        }),
        Default::default(),
        StringInput::from(&*fm),
        Some(&comments),
    );

    let mut parser = Parser::new_from(lexer);
    let module = parser.parse_module().map_err(|e| {
        syn::Error::new(call_span, format!("TypeScript parse error in '{}': {:?}", file_name, e))
    })?;

    // 1. Check for top-level static import rejection (GD-03)
    for item in &module.body {
        if let ModuleItem::ModuleDecl(ModuleDecl::Import(_)) = item {
            return Err(syn::Error::new(
                call_span,
                "Top-level static import is not supported in v1 bindable files. Use preloaded globals (window.*) or dynamic import() inside an async query.",
            ));
        }
        if let ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(_) | ModuleDecl::ExportDefaultExpr(_)) = item {
            return Err(syn::Error::new(
                call_span,
                "Default exports are not supported in bind_js!. Use named exports ('export function name()') to avoid Rust identifier conflicts.",
            ));
        }
    }

    // 2. Extract exported functions and analyze signatures
    let mut exports = Vec::new();
    for item in &module.body {
        if let ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export_decl)) = item {
            match &export_decl.decl {
                Decl::Fn(fn_decl) => {
                    let is_target = is_item_targeted(&fn_decl.ident.sym, macro_items, wildcard);
                    let func = extract_fn_decl(fn_decl, export_decl.span, &comments, macro_items, is_target, call_span)?;
                    exports.push(func);
                }
                Decl::Var(var_decl) => {
                    for decl in &var_decl.decls {
                        let is_target = match &decl.name {
                            Pat::Ident(binding) => is_item_targeted(&binding.id.sym, macro_items, wildcard),
                            _ => false,
                        };
                        if let Some(func) = extract_var_decl(decl, export_decl.span, &comments, macro_items, is_target, call_span)? {
                            exports.push(func);
                        }
                    }
                }
                _ => {}
            }
        }
    }

    // 3. Compute deterministic module hash
    let mut hasher = DefaultHasher::new();
    file_name.hash(&mut hasher);
    source_code.hash(&mut hasher);
    let module_hash = format!("{:x}", hasher.finish());

    // 4. Strip TypeScript types into standard inlined JavaScript
    let inlined_js = strip_ts_types(cm, module);

    Ok(AnalyzedModule {
        module_hash,
        inlined_js,
        exports,
    })
}

fn extract_comment_pillar(
    comments: &SingleThreadedComments,
    spans: &[swc_core::common::Span],
) -> (Option<InferredPillar>, Option<String>) {
    let mut pillar = None;
    let mut doc_lines = Vec::new();

    for span in spans {
        if let Some(leading) = comments.get_leading(span.lo) {
            for c in leading {
                let text = c.text.trim();
                if text.contains("#[command]") {
                    pillar = Some(InferredPillar::Command);
                } else if text.contains("#[query]") {
                    pillar = Some(InferredPillar::Query);
                } else if text.contains("#[watcher]") || text.contains("#[monitor]") {
                    pillar = Some(InferredPillar::Watcher);
                } else if text.starts_with('*') || (!text.starts_with('#') && !text.is_empty()) {
                    doc_lines.push(text.trim_start_matches('*').trim().to_string());
                }
            }
        }
    }

    let doc = if doc_lines.is_empty() {
        None
    } else {
        Some(doc_lines.join("\n"))
    };

    (pillar, doc)
}

fn find_macro_override(name: &str, macro_items: &[ItemSpec]) -> Option<InferredPillar> {
    let name_camel = name.to_lower_camel_case();
    let name_snake = name.to_snake_case();
    macro_items
        .iter()
        .find(|item| {
            let item_camel = item.original_name.to_lower_camel_case();
            let item_snake = item.original_name.to_snake_case();
            item_camel == name_camel || item_snake == name_snake
        })
        .and_then(|item| {
            item.attr.map(|a| match a {
                PillarAttr::Command => InferredPillar::Command,
                PillarAttr::Query => InferredPillar::Query,
                PillarAttr::Watcher => InferredPillar::Watcher,
            })
        })
}

fn is_item_targeted(name: &str, macro_items: &[ItemSpec], wildcard: bool) -> bool {
    if wildcard {
        return true;
    }
    let name_camel = name.to_lower_camel_case();
    let name_snake = name.to_snake_case();
    macro_items.iter().any(|item| {
        let item_camel = item.original_name.to_lower_camel_case();
        let item_snake = item.original_name.to_snake_case();
        item_camel == name_camel || item_snake == name_snake
    })
}

fn extract_fn_decl(
    fn_decl: &FnDecl,
    export_span: swc_core::common::Span,
    comments: &SingleThreadedComments,
    macro_items: &[ItemSpec],
    is_target: bool,
    call_span: Span,
) -> syn::Result<ExportedFunction> {
    let name = fn_decl.ident.sym.to_string();
    let (comment_pillar, doc_comment) = extract_comment_pillar(
        comments,
        &[export_span, fn_decl.function.span, fn_decl.ident.span],
    );
    let macro_pillar = find_macro_override(&name, macro_items);

    let mut params = Vec::new();
    for param in &fn_decl.function.params {
        let (p_name, ts_type) = match &param.pat {
            Pat::Ident(binding) => {
                let name = binding.id.sym.to_string();
                let ty = binding.type_ann.as_ref().map(|ann| &*ann.type_ann);
                (name, ty)
            }
            _ => ("_".to_string(), None),
        };

        let param_info = parse_param_type(&p_name, ts_type, call_span)?;
        params.push(param_info);
    }

    let (return_type, is_promise, is_void, is_fn) = parse_return_type(
        fn_decl.function.return_type.as_ref().map(|ann| &*ann.type_ann),
        call_span,
    )?;

    let pillar = determine_pillar(
        macro_pillar,
        comment_pillar,
        fn_decl.function.is_async,
        is_promise,
        is_void,
        &params,
    );

    if is_target && is_fn && pillar != InferredPillar::Watcher {
        return Err(syn::Error::new(
            call_span,
            "Higher-order functions returning functions are not supported in bind_js!. Return concrete serializable data or use a Watcher callback (annotated with #[watcher]).",
        ));
    }

    Ok(ExportedFunction {
        name,
        pillar,
        params,
        return_rust_type: return_type,
        doc_comment,
    })
}

fn extract_var_decl(
    decl: &VarDeclarator,
    export_span: swc_core::common::Span,
    comments: &SingleThreadedComments,
    macro_items: &[ItemSpec],
    is_target: bool,
    call_span: Span,
) -> syn::Result<Option<ExportedFunction>> {
    let name = match &decl.name {
        Pat::Ident(binding) => binding.id.sym.to_string(),
        _ => return Ok(None),
    };

    let (comment_pillar, doc_comment) = extract_comment_pillar(comments, &[export_span, decl.span]);
    let macro_pillar = find_macro_override(&name, macro_items);

    let (params_ast, return_ann, is_async) = match &decl.init {
        Some(init_expr) => match &**init_expr {
            Expr::Arrow(arrow) => {
                let params = arrow
                    .params
                    .iter()
                    .map(|pat| match pat {
                        Pat::Ident(b) => (b.id.sym.to_string(), b.type_ann.as_ref().map(|ann| &*ann.type_ann)),
                        _ => ("_".to_string(), None),
                    })
                    .collect::<Vec<_>>();
                (params, arrow.return_type.as_ref().map(|ann| &*ann.type_ann), arrow.is_async)
            }
            Expr::Fn(fn_expr) => {
                let params = fn_expr
                    .function
                    .params
                    .iter()
                    .map(|p| match &p.pat {
                        Pat::Ident(b) => (b.id.sym.to_string(), b.type_ann.as_ref().map(|ann| &*ann.type_ann)),
                        _ => ("_".to_string(), None),
                    })
                    .collect::<Vec<_>>();
                (params, fn_expr.function.return_type.as_ref().map(|ann| &*ann.type_ann), fn_expr.function.is_async)
            }
            _ => return Ok(None),
        },
        None => return Ok(None),
    };

    let mut params = Vec::new();
    for (p_name, ts_type) in params_ast {
        let param_info = parse_param_type(&p_name, ts_type, call_span)?;
        params.push(param_info);
    }

    let (return_type, is_promise, is_void, is_fn) = parse_return_type(return_ann, call_span)?;

    let pillar = determine_pillar(
        macro_pillar,
        comment_pillar,
        is_async,
        is_promise,
        is_void,
        &params,
    );

    if is_target && is_fn && pillar != InferredPillar::Watcher {
        return Err(syn::Error::new(
            call_span,
            "Higher-order functions returning functions are not supported in bind_js!. Return concrete serializable data or use a Watcher callback (annotated with #[watcher]).",
        ));
    }

    Ok(Some(ExportedFunction {
        name,
        pillar,
        params,
        return_rust_type: return_type,
        doc_comment,
    }))
}

fn determine_pillar(
    macro_pillar: Option<InferredPillar>,
    comment_pillar: Option<InferredPillar>,
    is_async: bool,
    is_promise: bool,
    is_void: bool,
    _params: &[ParamInfo],
) -> InferredPillar {
    // 1. Macro invocation attribute (1st priority)
    if let Some(p) = macro_pillar {
        return p;
    }

    // 2. TS doc comment priority (2nd priority)
    if let Some(p) = comment_pillar {
        return p;
    }

    // 3. Return type AST inference (3rd priority, GD-02)
    // Per GD-02: Watcher strictly requires explicit annotation (#[watcher]).
    // Non-annotated functions are partitioned into Command or Query:
    if is_promise || (is_async && !is_void) {
        return InferredPillar::Query;
    }

    if is_void {
        return InferredPillar::Command;
    }

    InferredPillar::Query
}

fn parse_param_type(
    name: &str,
    ts_type: Option<&TsType>,
    _call_span: Span,
) -> syn::Result<ParamInfo> {
    if let Some(ty) = ts_type {
        match ty {
            // Callback: (event: T) => void
            TsType::TsFnOrConstructorType(TsFnOrConstructorType::TsFnType(fn_type)) => {
                let arg_type = if let Some(first_param) = fn_type.params.first() {
                    match first_param {
                        TsFnParam::Ident(b) => b.type_ann.as_ref().map(|ann| map_ts_to_rust_return(&ann.type_ann)),
                        _ => None,
                    }
                } else {
                    None
                };

                return Ok(ParamInfo {
                    name: name.to_string(),
                    rust_param_type: quote! {},
                    is_callback: true,
                    callback_arg_type: arg_type,
                });
            }
            _ => {
                let rust_type = map_ts_to_rust_param(ty);
                return Ok(ParamInfo {
                    name: name.to_string(),
                    rust_param_type: rust_type,
                    is_callback: false,
                    callback_arg_type: None,
                });
            }
        }
    }

    // Default untyped: ::dioxus_js_bindgen::serde_json::Value
    Ok(ParamInfo {
        name: name.to_string(),
        rust_param_type: quote! { ::dioxus_js_bindgen::serde_json::Value },
        is_callback: false,
        callback_arg_type: None,
    })
}

fn parse_return_type(
    ts_type: Option<&TsType>,
    _call_span: Span,
) -> syn::Result<(Option<proc_macro2::TokenStream>, bool, bool, bool)> {
    let ty = match ts_type {
        Some(t) => t,
        None => return Ok((None, false, true, false)),
    };

    match ty {
        TsType::TsKeywordType(kw) => match kw.kind {
            TsKeywordTypeKind::TsVoidKeyword | TsKeywordTypeKind::TsUndefinedKeyword => {
                Ok((None, false, true, false))
            }
            _ => {
                let r_ty = map_ts_to_rust_return(ty);
                Ok((Some(r_ty), false, false, false))
            }
        },
        TsType::TsTypeRef(type_ref) => {
            let type_name = match &type_ref.type_name {
                TsEntityName::Ident(id) => id.sym.to_string(),
                _ => "".to_string(),
            };

            if type_name == "Promise" {
                if let Some(type_args) = &type_ref.type_params {
                    if let Some(inner) = type_args.params.first() {
                        if let TsType::TsKeywordType(kw) = &**inner {
                            if kw.kind == TsKeywordTypeKind::TsVoidKeyword {
                                return Ok((None, true, true, false));
                            }
                        }
                        let inner_rust = map_ts_to_rust_return(inner);
                        return Ok((Some(inner_rust), true, false, false));
                    }
                }
                return Ok((None, true, true, false));
            }

            let r_ty = map_ts_to_rust_return(ty);
            Ok((Some(r_ty), false, false, false))
        }
        TsType::TsFnOrConstructorType(_) => {
            // Higher-order function returning a function
            Ok((None, false, false, true))
        }
        _ => {
            let r_ty = map_ts_to_rust_return(ty);
            Ok((Some(r_ty), false, false, false))
        }
    }
}

fn map_ts_to_rust_param(ty: &TsType) -> proc_macro2::TokenStream {
    match ty {
        TsType::TsKeywordType(kw) => match kw.kind {
            TsKeywordTypeKind::TsStringKeyword => quote! { &str },
            TsKeywordTypeKind::TsNumberKeyword => quote! { f64 },
            TsKeywordTypeKind::TsBooleanKeyword => quote! { bool },
            TsKeywordTypeKind::TsAnyKeyword | TsKeywordTypeKind::TsUnknownKeyword => {
                quote! { ::dioxus_js_bindgen::serde_json::Value }
            }
            _ => quote! { ::dioxus_js_bindgen::serde_json::Value },
        },
        TsType::TsArrayType(arr) => {
            let inner = map_ts_to_rust_param(&arr.elem_type);
            quote! { &[#inner] }
        }
        TsType::TsTupleType(tuple) => {
            let elem_types: Vec<_> = tuple.elem_types.iter().map(|e| map_ts_to_rust_param(&e.ty)).collect();
            let first = elem_types.first();
            let all_same = elem_types.iter().all(|t| format!("{}", t) == format!("{}", first.unwrap()));
            if all_same && !elem_types.is_empty() {
                let count = elem_types.len();
                let inner = &elem_types[0];
                quote! { [#inner; #count] }
            } else {
                quote! { (#(#elem_types),*) }
            }
        }
        TsType::TsUnionOrIntersectionType(TsUnionOrIntersectionType::TsUnionType(union)) => {
            // Check if string literal union
            let all_literals = union.types.iter().all(|t| matches!(&**t, TsType::TsLitType(TsLitType { lit: TsLit::Str(_), .. })));
            if all_literals {
                return quote! { &str };
            }
            // Check if Option (T | undefined | null)
            let non_null: Vec<_> = union
                .types
                .iter()
                .filter(|t| !matches!(&***t, TsType::TsKeywordType(kw) if kw.kind == TsKeywordTypeKind::TsUndefinedKeyword || kw.kind == TsKeywordTypeKind::TsNullKeyword))
                .collect();
            if non_null.len() == 1 {
                let inner = map_ts_to_rust_param(non_null[0]);
                return quote! { Option<#inner> };
            }
            quote! { ::dioxus_js_bindgen::serde_json::Value }
        }
        TsType::TsTypeRef(type_ref) => {
            if let TsEntityName::Ident(id) = &type_ref.type_name {
                let sym = id.sym.as_ref();
                if sym == "Array" {
                    if let Some(params) = &type_ref.type_params {
                        if let Some(first) = params.params.first() {
                            let inner = map_ts_to_rust_param(first);
                            return quote! { &[#inner] };
                        }
                    }
                }
                let ident = syn::Ident::new(&id.sym.to_string(), proc_macro2::Span::call_site());
                quote! { &#ident }
            } else {
                quote! { ::dioxus_js_bindgen::serde_json::Value }
            }
        }
        _ => quote! { ::dioxus_js_bindgen::serde_json::Value },
    }
}

fn map_ts_to_rust_return(ty: &TsType) -> proc_macro2::TokenStream {
    match ty {
        TsType::TsKeywordType(kw) => match kw.kind {
            TsKeywordTypeKind::TsStringKeyword => quote! { String },
            TsKeywordTypeKind::TsNumberKeyword => quote! { f64 },
            TsKeywordTypeKind::TsBooleanKeyword => quote! { bool },
            TsKeywordTypeKind::TsVoidKeyword => quote! { () },
            _ => quote! { ::dioxus_js_bindgen::serde_json::Value },
        },
        TsType::TsArrayType(arr) => {
            let inner = map_ts_to_rust_return(&arr.elem_type);
            quote! { Vec<#inner> }
        }
        TsType::TsTupleType(tuple) => {
            let elem_types: Vec<_> = tuple.elem_types.iter().map(|e| map_ts_to_rust_return(&e.ty)).collect();
            let first = elem_types.first();
            let all_same = elem_types.iter().all(|t| format!("{}", t) == format!("{}", first.unwrap()));
            if all_same && !elem_types.is_empty() {
                let count = elem_types.len();
                let inner = &elem_types[0];
                quote! { [#inner; #count] }
            } else {
                quote! { (#(#elem_types),*) }
            }
        }
        TsType::TsUnionOrIntersectionType(TsUnionOrIntersectionType::TsUnionType(union)) => {
            // Check if string literal union -> String default
            let all_literals = union.types.iter().all(|t| matches!(&**t, TsType::TsLitType(TsLitType { lit: TsLit::Str(_), .. })));
            if all_literals {
                return quote! { String };
            }
            let non_null: Vec<_> = union
                .types
                .iter()
                .filter(|t| !matches!(&***t, TsType::TsKeywordType(kw) if kw.kind == TsKeywordTypeKind::TsUndefinedKeyword || kw.kind == TsKeywordTypeKind::TsNullKeyword))
                .collect();
            if non_null.len() == 1 {
                let inner = map_ts_to_rust_return(non_null[0]);
                return quote! { Option<#inner> };
            }
            quote! { ::dioxus_js_bindgen::serde_json::Value }
        }
        TsType::TsTypeRef(type_ref) => {
            if let TsEntityName::Ident(id) = &type_ref.type_name {
                let sym = id.sym.as_ref();
                if sym == "Array" {
                    if let Some(params) = &type_ref.type_params {
                        if let Some(first) = params.params.first() {
                            let inner = map_ts_to_rust_return(first);
                            return quote! { Vec<#inner> };
                        }
                    }
                }
                let ident = syn::Ident::new(&id.sym.to_string(), proc_macro2::Span::call_site());
                quote! { #ident }
            } else {
                quote! { ::dioxus_js_bindgen::serde_json::Value }
            }
        }
        _ => quote! { ::dioxus_js_bindgen::serde_json::Value },
    }
}

pub fn strip_ts_types(cm: Lrc<SourceMap>, mut module: Module) -> String {
    GLOBALS.set(&Globals::default(), || {
        let unresolved_mark = Mark::new();
        let top_level_mark = Mark::new();

        // Convert export decls to standard statements so the inlined code is valid inside a function IIFE
        for item in &mut module.body {
            if let ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export_decl)) = item {
                *item = ModuleItem::Stmt(Stmt::Decl(export_decl.decl.clone()));
            }
        }

        let mut program = Program::Module(module);
        let mut pass = strip(unresolved_mark, top_level_mark);
        pass.process(&mut program);

        let mut buf = Vec::new();
        {
            let mut emitter = Emitter {
                cfg: Config::default(),
                cm: cm.clone(),
                comments: None,
                wr: Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None)),
            };
            emitter.emit_program(&program).expect("JS emission failed");
        }
        String::from_utf8(buf).expect("Invalid UTF-8 JS output")
    })
}

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

    #[test]
    fn test_reject_top_level_import() {
        let ts = r#"
            import { something } from 'other';
            export function test(): void {}
        "#;
        let res = analyze_source(ts, "test.ts", &[], true, Span::call_site());
        assert!(res.is_err());
        assert!(res.unwrap_err().to_string().contains("Top-level static import is not supported"));
    }

    #[test]
    fn test_reject_default_export() {
        let ts = r#"
            export default function test(): void {}
        "#;
        let res = analyze_source(ts, "test.ts", &[], true, Span::call_site());
        assert!(res.is_err());
        assert!(res.unwrap_err().to_string().contains("Default exports are not supported"));
    }

    #[test]
    fn test_reject_higher_order_function() {
        let ts = r#"
            export function curried(x: number): (y: number) => number {
                return (y) => x + y;
            }
        "#;
        let res = analyze_source(ts, "test.ts", &[], true, Span::call_site());
        assert!(res.is_err());
        assert!(res.unwrap_err().to_string().contains("Higher-order functions"));
    }

    #[test]
    fn test_selective_import_ignores_untargeted_higher_order_function() {
        let ts = r#"
            export function normalAction(): void {}
            export function curried(x: number): (y: number) => number {
                return (y) => x + y;
            }
        "#;
        let macro_items = vec![ItemSpec {
            attr: None,
            original_name: "normalAction".to_string(),
            rename_as: None,
        }];
        // wildcard = false: curried is not in macro_items, so it should NOT error!
        let res = analyze_source(ts, "test.ts", &macro_items, false, Span::call_site());
        assert!(res.is_ok(), "Untargeted higher-order function must be ignored when wildcard is false");
    }

    #[test]
    fn test_macro_watcher_override_permits_higher_order_function() {
        let ts = r#"
            export function watchCustom(x: number): () => void {
                return () => {};
            }
        "#;
        let macro_items = vec![ItemSpec {
            attr: Some(PillarAttr::Watcher),
            original_name: "watchCustom".to_string(),
            rename_as: None,
        }];
        let res = analyze_source(ts, "test.ts", &macro_items, true, Span::call_site());
        assert!(res.is_ok(), "Macro #[watcher] must permit higher-order cleanup return");
        let analyzed = res.unwrap();
        assert_eq!(analyzed.exports[0].pillar, InferredPillar::Watcher);
    }

    #[test]
    fn test_analyze_command_query_watcher() {
        let ts = r#"
            // #[command]
            export function focusElement(id: string): void {
                document.getElementById(id)?.focus();
            }

            export async function getBoundingRect(id: string): Promise<Rect> {
                return { x: 0, y: 0 };
            }

            // #[watcher]
            export function watchScroll(emit: (scrollY: number) => void): () => void {
                const handler = () => emit(window.scrollY);
                window.addEventListener('scroll', handler);
                return () => window.removeEventListener('scroll', handler);
            }
        "#;

        let res = analyze_source(ts, "test.ts", &[], true, Span::call_site()).unwrap();
        assert_eq!(res.exports.len(), 3);

        // Command
        assert_eq!(res.exports[0].name, "focusElement");
        assert_eq!(res.exports[0].pillar, InferredPillar::Command);
        assert_eq!(res.exports[0].params.len(), 1);
        assert_eq!(res.exports[0].params[0].name, "id");

        // Query
        assert_eq!(res.exports[1].name, "getBoundingRect");
        assert_eq!(res.exports[1].pillar, InferredPillar::Query);
        assert!(res.exports[1].return_rust_type.is_some());

        // Watcher
        assert_eq!(res.exports[2].name, "watchScroll");
        assert_eq!(res.exports[2].pillar, InferredPillar::Watcher);
        assert!(res.exports[2].params[0].is_callback);

        // Inlined JS contains pure JS without types
        println!("INLINED JS:\n{}", res.inlined_js);
    }

    #[test]
    fn test_array_generic_and_bracket_syntax_parity() {
        let ts = r#"
            export function processBrackets(items: string[]): number[] {
                return [items.length];
            }

            export function processGenerics(items: Array<string>): Array<number> {
                return [items.length];
            }

            export async function fetchArrayAsync(): Promise<Array<string>> {
                return ["a", "b"];
            }
        "#;

        let res = analyze_source(ts, "test.ts", &[], true, Span::call_site()).unwrap();
        assert_eq!(res.exports.len(), 3);

        // Bracket syntax (T[])
        assert_eq!(res.exports[0].name, "processBrackets");
        assert_eq!(res.exports[0].params[0].rust_param_type.to_string(), "& [& str]");
        assert_eq!(res.exports[0].return_rust_type.as_ref().unwrap().to_string(), "Vec < f64 >");

        // Generic syntax (Array<T>)
        assert_eq!(res.exports[1].name, "processGenerics");
        assert_eq!(res.exports[1].params[0].rust_param_type.to_string(), "& [& str]");
        assert_eq!(res.exports[1].return_rust_type.as_ref().unwrap().to_string(), "Vec < f64 >");

        // Async Query returning Promise<Array<T>>
        assert_eq!(res.exports[2].name, "fetchArrayAsync");
        assert_eq!(res.exports[2].return_rust_type.as_ref().unwrap().to_string(), "Vec < String >");
    }
}