macroforge_ts 0.1.80

TypeScript macro expansion engine - write compile-time macros in Rust
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
use convert_case::{Case, Casing};

use crate::ts_syn::abi::{ClassIR, Patch, PatchCode, SpanIR};
#[cfg(feature = "swc")]
use swc_core::{
    common::{FileName, SourceMap, sync::Lrc},
    ecma::{
        ast::{ClassMember, EsVersion},
        parser::{Parser, StringInput, Syntax, TsSyntax, lexer::Lexer},
    },
};

use super::derive_targets::DeriveTargetIR;

/// Extracts exported function names from patch code.
/// Returns a vector of (full_fn_name, short_name) pairs.
pub(super) fn extract_function_names_from_patches(
    patches: &[Patch],
    type_name: &str,
) -> Vec<(String, String)> {
    let mut functions = Vec::new();
    let camel_type_name = type_name.to_case(Case::Camel);

    for patch in patches {
        let code = match patch {
            Patch::Insert {
                code: PatchCode::Text(text),
                ..
            } => text,
            Patch::Replace {
                code: PatchCode::Text(text),
                ..
            } => text,
            _ => continue,
        };

        // Find "export function <name>(" patterns
        let mut search_start = 0;
        while let Some(pos) = code[search_start..].find("export function ") {
            let start = search_start + pos + "export function ".len();
            if let Some(paren_pos) = code[start..].find('(') {
                let fn_name = code[start..start + paren_pos].trim();
                if !fn_name.is_empty()
                    && fn_name
                        .chars()
                        .all(|c| c.is_ascii_alphanumeric() || c == '_')
                    && let Some(short_name) = extract_short_name(fn_name, &camel_type_name)
                {
                    functions.push((fn_name.to_string(), short_name));
                }
                search_start = start + paren_pos;
            } else {
                break;
            }
        }
    }

    functions
}

/// Extracts the short function name from the full function name.
/// Uses Prefix naming style: userClone -> clone, userDefaultValue -> defaultValue
fn extract_short_name(full_name: &str, camel_type_name: &str) -> Option<String> {
    if let Some(rest) = full_name.strip_prefix(camel_type_name) {
        if rest.is_empty() {
            return None;
        }
        // Convert first char to lowercase (UserClone prefix removed, rest is Clone -> clone)
        Some(rest.to_case(Case::Camel))
    } else {
        None
    }
}

/// Generates a convenience export that groups all generated functions for a type.
/// For enums, uses namespace merging (valid TS). For other types, uses const object.
pub(super) fn generate_convenience_export(
    target: &DeriveTargetIR,
    type_name: &str,
    functions: &[(String, String)],
    is_exported: bool,
) -> String {
    if functions.is_empty() {
        return String::new();
    }

    let export_keyword = if is_exported { "export " } else { "" };

    match target {
        DeriveTargetIR::Enum(_) => {
            // Enums require namespace merging - const redeclaration is invalid TS
            let entries: Vec<String> = functions
                .iter()
                .map(|(full_name, short_name)| {
                    format!("  export const {} = {};", short_name, full_name)
                })
                .collect();

            format!(
                "{}namespace {} {{\n{}\n}}",
                export_keyword,
                type_name,
                entries.join("\n")
            )
        }
        _ => {
            // Interfaces and type aliases use const object
            let entries: Vec<String> = functions
                .iter()
                .map(|(full_name, short_name)| format!("  {}: {}", short_name, full_name))
                .collect();

            format!(
                "{}const {} = {{\n{}\n}} as const;",
                export_keyword,
                type_name,
                entries.join(",\n")
            )
        }
    }
}

/// Checks if the source already has a namespace or const declaration with the given name.
/// This prevents generating a convenience const that would conflict with existing declarations.
pub(super) fn has_existing_namespace_or_const(source: &str, type_name: &str) -> bool {
    // Check for `namespace TypeName` or `const TypeName`
    // Look for common patterns with various whitespace
    let namespace_patterns = [
        format!("namespace {}", type_name),
        format!("namespace  {}", type_name),
        format!("namespace\t{}", type_name),
        format!("namespace\n{}", type_name),
    ];

    let const_patterns = [
        format!("const {}", type_name),
        format!("const  {}", type_name),
        format!("const\t{}", type_name),
        format!("const\n{}", type_name),
    ];

    for pattern in &namespace_patterns {
        if let Some(pos) = source.find(pattern.as_str()) {
            // Check that this is followed by whitespace, { or end of string
            let after_pos = pos + pattern.len();
            if after_pos >= source.len() {
                return true;
            }
            let next_char = source[after_pos..].chars().next();
            if matches!(
                next_char,
                Some('{') | Some(' ') | Some('\t') | Some('\n') | Some('<')
            ) {
                return true;
            }
        }
    }

    for pattern in &const_patterns {
        if let Some(pos) = source.find(pattern.as_str()) {
            // Check that this is followed by whitespace, = or end of string
            let after_pos = pos + pattern.len();
            if after_pos >= source.len() {
                return true;
            }
            let next_char = source[after_pos..].chars().next();
            if matches!(
                next_char,
                Some('=') | Some(' ') | Some('\t') | Some('\n') | Some(':') | Some('<')
            ) {
                return true;
            }
        }
    }

    false
}

/// Gets the type name from a DeriveTargetIR.
/// Returns None for classes (they use instance methods).
pub(super) fn get_derive_target_name(target: &DeriveTargetIR) -> Option<&str> {
    match target {
        DeriveTargetIR::Class(_) => None,
        DeriveTargetIR::Interface(i) => Some(&i.name),
        DeriveTargetIR::Enum(e) => Some(&e.name),
        DeriveTargetIR::TypeAlias(t) => Some(&t.name),
    }
}

/// Gets the end span position for a DeriveTargetIR.
pub(super) fn get_derive_target_end_span(target: &DeriveTargetIR) -> u32 {
    match target {
        DeriveTargetIR::Class(c) => c.span.end,
        DeriveTargetIR::Interface(i) => i.span.end,
        DeriveTargetIR::Enum(e) => e.span.end,
        DeriveTargetIR::TypeAlias(t) => t.span.end,
    }
}

/// Gets the start span position for a DeriveTargetIR.
pub(super) fn get_derive_target_start_span(target: &DeriveTargetIR) -> u32 {
    match target {
        DeriveTargetIR::Class(c) => c.span.start,
        DeriveTargetIR::Interface(i) => i.span.start,
        DeriveTargetIR::Enum(e) => e.span.start,
        DeriveTargetIR::TypeAlias(t) => t.span.start,
    }
}

/// Checks if a declaration at the given position is exported.
/// Looks for the `export` keyword before the declaration start.
pub(super) fn is_declaration_exported(source: &str, decl_start: u32) -> bool {
    let start = decl_start as usize;
    if start == 0 || start > source.len() {
        return false;
    }

    // Look at the text before the declaration (up to 50 chars should be enough)
    let look_back = start.min(50);
    let prefix = &source[start - look_back..start];

    // Find "export" keyword - must be followed by whitespace and not be part of another word
    if let Some(pos) = prefix.rfind("export") {
        let after_export = pos + 6;
        // Check that "export" is followed by whitespace (or is at the end of prefix)
        if after_export >= prefix.len() {
            return true;
        }
        let next_char = prefix[after_export..].chars().next();
        if matches!(next_char, Some(' ') | Some('\t') | Some('\n')) {
            // Also check it's not part of a larger word (e.g., "reexport")
            if pos == 0 {
                return true;
            }
            let prev_char = prefix[..pos].chars().last();
            if !matches!(prev_char, Some(c) if c.is_ascii_alphanumeric() || c == '_') {
                return true;
            }
        }
    }

    false
}

pub(super) fn is_ident_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_' || c == '$'
}

pub(super) fn contains_identifier(haystack: &str, ident: &str) -> bool {
    if ident.is_empty() {
        return false;
    }

    let mut search_start = 0;
    while let Some(pos) = haystack[search_start..].find(ident) {
        let start = search_start + pos;
        let end = start + ident.len();

        let prev_ok = start == 0
            || !haystack[..start]
                .chars()
                .next_back()
                .is_some_and(is_ident_char);
        let next_ok =
            end >= haystack.len() || !haystack[end..].chars().next().is_some_and(is_ident_char);

        if prev_ok && next_ok {
            return true;
        }

        search_start = end;
    }

    false
}

pub(super) fn derive_insert_pos(class_ir: &ClassIR, source: &str) -> u32 {
    let end = class_ir.span.end as usize;
    let search = &source[..end.min(source.len())];
    search
        .rfind('}')
        .map(|idx| idx as u32 + 1)
        .unwrap_or_else(|| class_ir.body_span.end.max(class_ir.span.start))
}

pub(super) fn find_macro_comment_span(source: &str, target_start: u32) -> Option<SpanIR> {
    let start = target_start.saturating_sub(1) as usize;
    if start == 0 || start > source.len() {
        return None;
    }
    let search_area = &source[..start];
    let start_idx = search_area.rfind("/**")?;
    let rest = &search_area[start_idx..];
    let end_rel = rest.find("*/")?;
    let end_idx = start_idx + end_rel + 2;

    let between = &search_area[end_idx..];
    if !between.trim().is_empty() {
        return None;
    }

    Some(SpanIR::new(start_idx as u32 + 1, end_idx as u32 + 1))
}

/// Convert InsertPos enum to the string location used internally.
pub(super) fn insert_pos_to_location(pos: crate::ts_syn::InsertPos) -> &'static str {
    match pos {
        crate::ts_syn::InsertPos::Top => "top",
        crate::ts_syn::InsertPos::Above => "above",
        crate::ts_syn::InsertPos::Within => "body",
        crate::ts_syn::InsertPos::Below => "below",
        crate::ts_syn::InsertPos::Bottom => "bottom",
    }
}

pub(super) fn split_by_markers(
    source: &str,
    default_pos: crate::ts_syn::InsertPos,
) -> Vec<(&'static str, String)> {
    let markers = [
        ("top", "/* @macroforge:top */"),
        ("above", "/* @macroforge:above */"),
        ("below", "/* @macroforge:below */"),
        ("body", "/* @macroforge:body */"),
        ("bottom", "/* @macroforge:bottom */"),
        // Legacy markers for backward compatibility
        ("body", "/* @macroforge:signature */"),
    ];

    let mut occurrences = Vec::new();
    for (name, pattern) in markers {
        for (idx, _) in source.match_indices(pattern) {
            occurrences.push((idx, pattern.len(), name));
        }
    }
    occurrences.sort_by_key(|k| k.0);

    let default_location = insert_pos_to_location(default_pos);

    if occurrences.is_empty() {
        return vec![(default_location, source.to_string())];
    }

    let mut chunks = Vec::new();

    if occurrences[0].0 > 0 {
        let text = &source[0..occurrences[0].0];
        if !text.trim().is_empty() {
            chunks.push((default_location, text.to_string()));
        }
    }

    for i in 0..occurrences.len() {
        let (start, len, name) = occurrences[i];
        let content_start = start + len;
        let content_end = if i + 1 < occurrences.len() {
            occurrences[i + 1].0
        } else {
            source.len()
        };

        let content = &source[content_start..content_end];
        chunks.push((name, content.to_string()));
    }

    chunks
}

/// A class member with its associated leading JSDoc comment (if any).
#[cfg(feature = "swc")]
pub(super) struct MemberWithComment {
    /// The leading JSDoc comment text (without /** */)
    pub leading_comment: Option<String>,
    /// The class member AST node
    pub member: ClassMember,
}

#[cfg(feature = "swc")]
pub(super) fn parse_members_from_tokens(tokens: &str) -> anyhow::Result<Vec<MemberWithComment>> {
    // First, extract JSDoc comments and their associated code segments
    // The body! macro outputs: /** comment */code /** comment */code ...
    let segments = extract_jsdoc_segments(tokens);

    // Build class body without comments for parsing
    let code_only: String = segments.iter().map(|(_, code)| code.as_str()).collect();
    let wrapped_stmt = format!("class __Temp {{ {} }}", code_only);

    // Parse using standard SWC (comments stripped)
    let cm: Lrc<SourceMap> = Lrc::new(Default::default());
    let fm = cm.new_source_file(
        FileName::Custom("macro_body.ts".into()).into(),
        wrapped_stmt,
    );

    let syntax = Syntax::Typescript(TsSyntax {
        tsx: true,
        decorators: true,
        ..Default::default()
    });

    let lexer = Lexer::new(syntax, EsVersion::latest(), StringInput::from(&*fm), None);
    let mut parser = Parser::new_from(lexer);

    let module = parser
        .parse_module()
        .map_err(|e| anyhow::anyhow!("Failed to parse macro output: {:?}", e))?;

    let class_body = module
        .body
        .into_iter()
        .find_map(|item| {
            if let swc_core::ecma::ast::ModuleItem::Stmt(swc_core::ecma::ast::Stmt::Decl(
                swc_core::ecma::ast::Decl::Class(class_decl),
            )) = item
            {
                Some(class_decl.class.body)
            } else {
                None
            }
        })
        .ok_or_else(|| anyhow::anyhow!("Failed to parse macro output into class members"))?;

    // Match parsed members with their extracted JSDoc comments
    // Use enumerate instead of zip to handle cases where there are more members than segments
    // (e.g., when no JSDoc comments exist, all code is in one segment but parses to multiple members)
    let result = class_body
        .into_iter()
        .enumerate()
        .map(|(i, member)| MemberWithComment {
            leading_comment: segments.get(i).and_then(|(comment, _)| comment.clone()),
            member,
        })
        .collect();

    Ok(result)
}

/// Extract JSDoc comments and their following code segments from body! output.
/// Returns a vec of (Option<comment_text>, code_segment) pairs.
#[cfg(feature = "swc")]
pub(super) fn extract_jsdoc_segments(input: &str) -> Vec<(Option<String>, String)> {
    let mut result = Vec::new();
    let mut remaining = input;

    while !remaining.is_empty() {
        remaining = remaining.trim_start();
        if remaining.is_empty() {
            break;
        }

        // Check if starts with JSDoc comment
        if remaining.starts_with("/**") {
            // Find the end of the comment
            if let Some(end_idx) = remaining.find("*/") {
                let comment_text = &remaining[3..end_idx]; // Skip /** and exclude */
                remaining = &remaining[end_idx + 2..]; // Skip past */
                // Now find the code until the next JSDoc or end
                let code_end = remaining.find("/**").unwrap_or(remaining.len());
                let code = remaining[..code_end].to_string();
                remaining = &remaining[code_end..];

                if !code.trim().is_empty() {
                    result.push((Some(comment_text.to_string()), code));
                }
            } else {
                // Malformed comment, treat rest as code
                result.push((None, remaining.to_string()));
                break;
            }
        } else {
            // No JSDoc comment, find code until next JSDoc or end
            let code_end = remaining.find("/**").unwrap_or(remaining.len());
            let code = remaining[..code_end].to_string();
            remaining = &remaining[code_end..];

            if !code.trim().is_empty() {
                result.push((None, code));
            }
        }
    }

    result
}