reluxscript 0.1.4

Write AST transformations once. Compile to Babel, SWC, and beyond.
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
use tower_lsp::lsp_types::*;

/// Generate all keyword completions
pub fn get_keyword_completions() -> Vec<CompletionItem> {
    vec![
        // Top-level declarations
        completion_keyword("plugin", "Define a transformation plugin"),
        completion_keyword("writer", "Define a code generation writer"),
        completion_keyword("module", "Define a module"),
        completion_keyword("interface", "Define an interface"),
        completion_keyword("use", "Import symbols from another module"),
        completion_keyword("struct", "Define a struct"),
        completion_keyword("enum", "Define an enum"),
        completion_keyword("impl", "Implement methods for a type"),
        completion_keyword("where", "Where clause for generics"),

        // Function-related
        completion_keyword("fn", "Define a function"),
        completion_keyword("pub", "Make item public"),
        completion_keyword("pre", "Pre-hook function"),
        completion_keyword("exit", "Exit-hook function"),

        // Variables
        completion_keyword("let", "Declare a variable"),
        completion_keyword("const", "Declare a constant"),
        completion_keyword("mut", "Make binding mutable"),

        // Control flow
        completion_keyword("if", "Conditional statement"),
        completion_keyword("else", "Alternative branch"),
        completion_keyword("match", "Pattern matching"),
        completion_keyword("return", "Return from function"),
        completion_keyword("break", "Break from loop"),
        completion_keyword("continue", "Continue to next iteration"),
        completion_keyword("for", "For loop"),
        completion_keyword("in", "Iterator keyword"),
        completion_keyword("while", "While loop"),

        // Special
        completion_keyword("self", "Reference to current instance"),
        completion_keyword("Self", "Current type"),
        completion_keyword("traverse", "AST traversal"),
        completion_keyword("using", "Import context"),

        // Literals
        completion_keyword("true", "Boolean true"),
        completion_keyword("false", "Boolean false"),
        completion_keyword("null", "Null literal"),
        completion_keyword("None", "Option none variant"),
        completion_keyword("Some", "Option some variant"),
        completion_keyword("Ok", "Result ok variant"),
        completion_keyword("Err", "Result error variant"),

        // Operators (as keywords)
        completion_keyword("and", "Logical AND"),
        completion_keyword("or", "Logical OR"),
        completion_keyword("not", "Logical NOT"),
    ]
}

/// Generate AST node type completions
pub fn get_ast_type_completions() -> Vec<CompletionItem> {
    vec![
        // Expression types
        completion_type("Expression", "Base expression type"),
        completion_type("Identifier", "Variable or property identifier"),
        completion_type("CallExpression", "Function/method call"),
        completion_type("MemberExpression", "Property access (obj.prop)"),
        completion_type("BinaryExpression", "Binary operation (a + b)"),
        completion_type("UnaryExpression", "Unary operation (-x, !x)"),
        completion_type("AssignmentExpression", "Assignment (a = b)"),
        completion_type("UpdateExpression", "Update (a++, --b)"),
        completion_type("LogicalExpression", "Logical operation (a && b)"),
        completion_type("ConditionalExpression", "Ternary (a ? b : c)"),
        completion_type("ArrowFunctionExpression", "Arrow function (() => {})"),
        completion_type("FunctionExpression", "Function expression"),
        completion_type("NewExpression", "Constructor call (new Foo())"),
        completion_type("SequenceExpression", "Comma expression (a, b)"),
        completion_type("ThisExpression", "This reference"),
        completion_type("ArrayExpression", "Array literal ([1, 2, 3])"),
        completion_type("ObjectExpression", "Object literal ({a: 1})"),
        completion_type("TemplateLiteral", "Template string (`foo ${x}`)"),
        completion_type("SpreadElement", "Spread (...args)"),
        completion_type("YieldExpression", "Yield expression"),
        completion_type("AwaitExpression", "Await expression"),

        // Statement types
        completion_type("Statement", "Base statement type"),
        completion_type("Stmt", "Statement (alias)"),
        completion_type("BlockStatement", "Block of statements"),
        completion_type("ExpressionStatement", "Expression as statement"),
        completion_type("IfStatement", "If/else statement"),
        completion_type("SwitchStatement", "Switch statement"),
        completion_type("ForStatement", "For loop"),
        completion_type("WhileStatement", "While loop"),
        completion_type("DoWhileStatement", "Do-while loop"),
        completion_type("ForInStatement", "For-in loop"),
        completion_type("ForOfStatement", "For-of loop"),
        completion_type("BreakStatement", "Break statement"),
        completion_type("ContinueStatement", "Continue statement"),
        completion_type("ReturnStatement", "Return statement"),
        completion_type("ThrowStatement", "Throw statement"),
        completion_type("TryStatement", "Try-catch statement"),
        completion_type("VariableDeclaration", "Variable declaration"),
        completion_type("FunctionDeclaration", "Function declaration"),
        completion_type("ClassDeclaration", "Class declaration"),

        // Pattern types
        completion_type("Pattern", "Base pattern type"),
        completion_type("ObjectPattern", "Object destructuring pattern"),
        completion_type("ArrayPattern", "Array destructuring pattern"),
        completion_type("RestElement", "Rest pattern (...rest)"),
        completion_type("AssignmentPattern", "Default value pattern"),

        // Literal types
        completion_type("Literal", "Base literal type"),
        completion_type("StringLiteral", "String literal"),
        completion_type("NumericLiteral", "Number literal"),
        completion_type("BooleanLiteral", "Boolean literal"),
        completion_type("NullLiteral", "Null literal"),
        completion_type("RegExpLiteral", "Regular expression literal"),

        // Other
        completion_type("Property", "Object property"),
        completion_type("Program", "Root program node"),
        completion_type("File", "File node"),

        // Context & State types
        completion_type("Context", "Visitor context"),
        completion_type("PluginState", "Plugin state"),
        completion_type("CodeBuilder", "Code builder for writers"),
    ]
}

/// Generate built-in type completions (Rust types)
pub fn get_builtin_type_completions() -> Vec<CompletionItem> {
    vec![
        completion_type("Vec", "Dynamic array (Vec<T>)"),
        completion_type("Option", "Optional value (Option<T>)"),
        completion_type("Result", "Result type (Result<T, E>)"),
        completion_type("HashMap", "Hash map (HashMap<K, V>)"),
        completion_type("HashSet", "Hash set (HashSet<T>)"),
        completion_type("String", "Owned string"),
        completion_type("str", "String slice"),
        completion_type("bool", "Boolean type"),
        completion_type("i32", "32-bit signed integer"),
        completion_type("u32", "32-bit unsigned integer"),
        completion_type("f64", "64-bit floating point"),
        completion_type("usize", "Pointer-sized unsigned integer"),
    ]
}

/// Generate macro completions
pub fn get_macro_completions() -> Vec<CompletionItem> {
    vec![
        completion_macro("format!", "Format string macro"),
        completion_macro("println!", "Print with newline macro"),
        completion_macro("print!", "Print macro"),
        completion_macro("panic!", "Panic macro"),
        completion_macro("assert!", "Assert macro"),
        completion_macro("debug!", "Debug macro"),
        completion_macro("matches!", "Pattern matching macro"),
        completion_macro("vec!", "Vec creation macro"),

        // Verbatim blocks
        completion_macro("babel!", "Babel-specific verbatim code block"),
        completion_macro("swc!", "SWC-specific verbatim code block"),
        completion_macro("js!", "JavaScript verbatim code block (alias for babel!)"),
        completion_macro("rust!", "Rust verbatim code block (alias for swc!)"),
    ]
}

/// Helper to create macro completion
fn completion_macro(label: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::FUNCTION),
        detail: Some(detail.to_string()),
        documentation: None,
        ..Default::default()
    }
}

/// Generate function snippet completions
pub fn get_snippet_completions() -> Vec<CompletionItem> {
    vec![
        completion_snippet(
            "visit_call_expression",
            "fn visit_call_expression(node: &mut CallExpression) {\n    $0\n}",
            "Visitor for call expressions"
        ),
        completion_snippet(
            "visit_identifier",
            "fn visit_identifier(node: &mut Identifier) {\n    $0\n}",
            "Visitor for identifiers"
        ),
        completion_snippet(
            "visit_member_expression",
            "fn visit_member_expression(node: &mut MemberExpression) {\n    $0\n}",
            "Visitor for member expressions"
        ),
        completion_snippet(
            "visit_binary_expression",
            "fn visit_binary_expression(node: &mut BinaryExpression) {\n    $0\n}",
            "Visitor for binary expressions"
        ),
        completion_snippet(
            "if-let",
            "if let ${1:pattern} = ${2:expr} {\n    $0\n}",
            "If-let pattern match"
        ),
        completion_snippet(
            "match",
            "match ${1:expr} {\n    ${2:pattern} => ${3:expr},\n    $0\n}",
            "Match expression"
        ),
        completion_snippet(
            "for-in",
            "for ${1:item} in ${2:collection} {\n    $0\n}",
            "For-in loop"
        ),
    ]
}

/// Helper to create keyword completion
fn completion_keyword(label: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::KEYWORD),
        detail: Some(detail.to_string()),
        documentation: None,
        ..Default::default()
    }
}

/// Helper to create type completion
fn completion_type(label: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::CLASS),
        detail: Some(detail.to_string()),
        documentation: None,
        ..Default::default()
    }
}

/// Helper to create snippet completion
fn completion_snippet(label: &str, snippet: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::SNIPPET),
        detail: Some(detail.to_string()),
        insert_text: Some(snippet.to_string()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: None,
        ..Default::default()
    }
}

/// Get field completions for a specific AST node type
pub fn get_field_completions_for_type(type_name: &str) -> Vec<CompletionItem> {
    match type_name {
        "CallExpression" => vec![
            completion_field("callee", "Expression", "The function being called"),
            completion_field("arguments", "Vec<Expression>", "The arguments"),
            completion_field("optional", "bool", "Optional chaining (?.())"),
        ],
        "MemberExpression" => vec![
            completion_field("object", "Expression", "The object being accessed"),
            completion_field("property", "Expression", "The property being accessed"),
            completion_field("computed", "bool", "Computed (obj[x]) vs static (obj.x)"),
            completion_field("optional", "bool", "Optional chaining (?.)"),
        ],
        "BinaryExpression" => vec![
            completion_field("left", "Expression", "Left operand"),
            completion_field("right", "Expression", "Right operand"),
            completion_field("operator", "String", "Binary operator (+, -, *, etc.)"),
        ],
        "UnaryExpression" => vec![
            completion_field("argument", "Expression", "The operand"),
            completion_field("operator", "String", "Unary operator (-, !, ~, etc.)"),
            completion_field("prefix", "bool", "Prefix vs postfix"),
        ],
        "Identifier" => vec![
            completion_field("name", "String", "The identifier name"),
            completion_field("sym", "String", "The symbol (SWC)"),
        ],
        "FunctionDeclaration" | "FunctionExpression" | "ArrowFunctionExpression" => vec![
            completion_field("params", "Vec<Pattern>", "Function parameters"),
            completion_field("body", "BlockStatement", "Function body"),
            completion_field("async", "bool", "Is async function"),
            completion_field("generator", "bool", "Is generator function"),
        ],
        "IfStatement" => vec![
            completion_field("test", "Expression", "The condition"),
            completion_field("consequent", "Statement", "Then branch"),
            completion_field("alternate", "Option<Statement>", "Else branch"),
        ],
        "VariableDeclaration" => vec![
            completion_field("declarations", "Vec<VariableDeclarator>", "Variable declarators"),
            completion_field("kind", "String", "var, let, or const"),
        ],
        _ => vec![],
    }
}

/// Helper to create field completion
fn completion_field(label: &str, type_name: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::FIELD),
        detail: Some(format!("{}: {}", label, type_name)),
        documentation: Some(Documentation::String(detail.to_string())),
        ..Default::default()
    }
}

/// Get method completions (built-in methods on types)
pub fn get_method_completions_for_type(type_name: &str) -> Vec<CompletionItem> {
    match type_name {
        "String" | "str" => vec![
            completion_method("starts_with", "(prefix: &str) -> bool", "Check if starts with prefix"),
            completion_method("ends_with", "(suffix: &str) -> bool", "Check if ends with suffix"),
            completion_method("contains", "(pattern: &str) -> bool", "Check if contains pattern"),
            completion_method("trim", "() -> String", "Remove leading/trailing whitespace"),
            completion_method("to_lowercase", "() -> String", "Convert to lowercase"),
            completion_method("to_uppercase", "() -> String", "Convert to uppercase"),
            completion_method("split", "(sep: &str) -> Vec<String>", "Split by separator"),
            completion_method("replace", "(from: &str, to: &str) -> String", "Replace substring"),
        ],
        "Vec" => vec![
            completion_method("push", "(item: T)", "Add item to end"),
            completion_method("pop", "() -> Option<T>", "Remove and return last item"),
            completion_method("len", "() -> usize", "Get length"),
            completion_method("is_empty", "() -> bool", "Check if empty"),
            completion_method("clear", "()", "Remove all items"),
            completion_method("contains", "(item: &T) -> bool", "Check if contains item"),
        ],
        "Option" => vec![
            completion_method("is_some", "() -> bool", "Check if Some"),
            completion_method("is_none", "() -> bool", "Check if None"),
            completion_method("unwrap", "() -> T", "Get value (panics if None)"),
            completion_method("unwrap_or", "(default: T) -> T", "Get value or default"),
        ],
        _ => vec![],
    }
}

/// Helper to create method completion
fn completion_method(label: &str, signature: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::METHOD),
        detail: Some(signature.to_string()),
        documentation: Some(Documentation::String(detail.to_string())),
        ..Default::default()
    }
}

/// Get pattern variant completions for specific types
pub fn get_pattern_completions_for_type(type_name: &str) -> Vec<CompletionItem> {
    match type_name {
        "Expression" => vec![
            completion_pattern("Identifier(id)", "Identifier pattern"),
            completion_pattern("CallExpression(call)", "Call expression pattern"),
            completion_pattern("MemberExpression(member)", "Member expression pattern"),
            completion_pattern("BinaryExpression(bin)", "Binary expression pattern"),
            completion_pattern("UnaryExpression(unary)", "Unary expression pattern"),
            completion_pattern("Literal(lit)", "Literal pattern"),
            completion_pattern("ArrayExpression(arr)", "Array expression pattern"),
            completion_pattern("ObjectExpression(obj)", "Object expression pattern"),
            completion_pattern("ArrowFunctionExpression(arrow)", "Arrow function pattern"),
            completion_pattern("FunctionExpression(func)", "Function expression pattern"),
            completion_pattern("ThisExpression", "This expression pattern"),
            completion_pattern("NewExpression(new_expr)", "New expression pattern"),
            completion_pattern("ConditionalExpression(cond)", "Conditional expression pattern"),
            completion_pattern("AssignmentExpression(assign)", "Assignment expression pattern"),
            completion_pattern("UpdateExpression(update)", "Update expression pattern"),
            completion_pattern("LogicalExpression(logical)", "Logical expression pattern"),
        ],
        "Statement" | "Stmt" => vec![
            completion_pattern("ExpressionStatement(expr_stmt)", "Expression statement pattern"),
            completion_pattern("BlockStatement(block)", "Block statement pattern"),
            completion_pattern("IfStatement(if_stmt)", "If statement pattern"),
            completion_pattern("ReturnStatement(ret)", "Return statement pattern"),
            completion_pattern("VariableDeclaration(var_decl)", "Variable declaration pattern"),
            completion_pattern("FunctionDeclaration(fn_decl)", "Function declaration pattern"),
            completion_pattern("ForStatement(for_stmt)", "For statement pattern"),
            completion_pattern("WhileStatement(while_stmt)", "While statement pattern"),
            completion_pattern("BreakStatement", "Break statement pattern"),
            completion_pattern("ContinueStatement", "Continue statement pattern"),
            completion_pattern("ThrowStatement(throw)", "Throw statement pattern"),
            completion_pattern("TryStatement(try_stmt)", "Try statement pattern"),
        ],
        "Pattern" => vec![
            completion_pattern("Identifier(id)", "Identifier pattern"),
            completion_pattern("ObjectPattern(obj_pat)", "Object pattern"),
            completion_pattern("ArrayPattern(arr_pat)", "Array pattern"),
            completion_pattern("RestElement(rest)", "Rest element pattern"),
            completion_pattern("AssignmentPattern(assign_pat)", "Assignment pattern"),
        ],
        "MemberProperty" | "MemberProp" => vec![
            completion_pattern("Identifier(id)", "Identifier property"),
            completion_pattern("Computed(expr)", "Computed property"),
        ],
        "Callee" => vec![
            completion_pattern("Expression(expr)", "Expression callee"),
            completion_pattern("Super", "Super callee"),
            completion_pattern("Import", "Import callee"),
        ],
        _ => vec![],
    }
}

/// Helper to create pattern completion
fn completion_pattern(label: &str, detail: &str) -> CompletionItem {
    CompletionItem {
        label: label.to_string(),
        kind: Some(CompletionItemKind::ENUM_MEMBER),
        detail: Some(detail.to_string()),
        insert_text: Some(label.to_string()),
        documentation: None,
        ..Default::default()
    }
}

/// Get all common pattern completions (used in match expressions)
pub fn get_common_pattern_completions() -> Vec<CompletionItem> {
    let mut completions = Vec::new();
    completions.extend(get_pattern_completions_for_type("Expression"));
    completions.extend(get_pattern_completions_for_type("Statement"));
    completions.extend(get_pattern_completions_for_type("Pattern"));
    completions
}