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
//! OXC-based parsing and statement processing.
//!
//! Contains the core parsing logic that processes AST statements
//! and extracts macro calls, bindings, and type definitions.
use oxc_allocator::Allocator;
use oxc_ast::ast::{Argument, BindingPattern, Expression, Statement, VariableDeclarationKind};
use oxc_parser::Parser;
use oxc_span::{GetSpan, SourceType};
use vize_carton::{String, ToCompactString};
use crate::types::BindingType;
use super::super::define_props_destructure::process_props_destructure;
use super::super::MacroCall;
use super::helpers::{
extract_args_from_call, extract_macro_from_expr, extract_type_args_from_call,
infer_binding_type, is_call_of, is_import_type_only,
};
use super::ScriptCompileContext;
use crate::script::build_interface_type_source;
impl ScriptCompileContext {
/// Parse the source with OXC and extract information
pub(super) fn parse_with_oxc(&mut self, source: &str) {
let allocator = Allocator::default();
let source_type = SourceType::from_path("script.ts").unwrap_or_default();
let ret = Parser::new(&allocator, source, source_type).parse();
if ret.panicked {
return;
}
let program = ret.program;
// First pass: collect all TypeScript interfaces and type aliases
// This ensures they're available when resolving type references in macros
for stmt in program.body.iter() {
match stmt {
Statement::TSInterfaceDeclaration(iface) => {
let name = iface.id.name.to_compact_string();
let body = build_interface_type_source(
source,
iface.id.span.end as usize,
iface.body.span.start as usize,
iface.body.span.end as usize,
);
self.interfaces.insert(name, body);
}
Statement::TSTypeAliasDeclaration(type_alias) => {
let name = type_alias.id.name.to_compact_string();
let type_start = type_alias.type_annotation.span().start as usize;
let type_end = type_alias.type_annotation.span().end as usize;
let type_body = String::from(&source[type_start..type_end]);
self.type_aliases.insert(name, type_body);
}
// Handle exported types: `export type X = ...` and `export interface X { ... }`
Statement::ExportNamedDeclaration(export_decl) => {
if let Some(ref decl) = export_decl.declaration {
match decl {
oxc_ast::ast::Declaration::TSInterfaceDeclaration(iface) => {
let name = iface.id.name.to_compact_string();
let body = build_interface_type_source(
source,
iface.id.span.end as usize,
iface.body.span.start as usize,
iface.body.span.end as usize,
);
self.interfaces.insert(name, body);
}
oxc_ast::ast::Declaration::TSTypeAliasDeclaration(type_alias) => {
let name = type_alias.id.name.to_compact_string();
let type_start = type_alias.type_annotation.span().start as usize;
let type_end = type_alias.type_annotation.span().end as usize;
let type_body = String::from(&source[type_start..type_end]);
self.type_aliases.insert(name, type_body);
}
_ => {}
}
}
}
_ => {}
}
}
// Second pass: process all statements (macros, bindings, etc.)
for stmt in program.body.iter() {
self.process_statement(stmt, source);
}
// Update flags
self.has_define_props_call = self.macros.define_props.is_some();
self.has_define_emits_call = self.macros.define_emits.is_some();
self.has_define_expose_call = self.macros.define_expose.is_some();
self.has_define_options_call = self.macros.define_options.is_some();
self.has_define_slots_call = self.macros.define_slots.is_some();
self.has_define_model_call = !self.macros.define_models.is_empty();
}
/// Collect type definitions (interfaces and type aliases) from additional source.
/// Used to merge types from the normal `<script>` block into the context
/// so that `defineProps<TypeRef>()` can resolve type references across blocks.
pub fn collect_types_from(&mut self, source: &str) {
let allocator = Allocator::default();
let source_type = SourceType::from_path("script.ts").unwrap_or_default();
let ret = Parser::new(&allocator, source, source_type).parse();
if ret.panicked {
return;
}
for stmt in ret.program.body.iter() {
match stmt {
Statement::TSInterfaceDeclaration(iface) => {
let name = iface.id.name.to_compact_string();
let body = build_interface_type_source(
source,
iface.id.span.end as usize,
iface.body.span.start as usize,
iface.body.span.end as usize,
);
self.interfaces.entry(name).or_insert(body);
}
Statement::TSTypeAliasDeclaration(type_alias) => {
let name = type_alias.id.name.to_compact_string();
let type_start = type_alias.type_annotation.span().start as usize;
let type_end = type_alias.type_annotation.span().end as usize;
let type_body = String::from(&source[type_start..type_end]);
self.type_aliases.entry(name).or_insert(type_body);
}
Statement::ExportNamedDeclaration(export_decl) => {
if let Some(ref decl) = export_decl.declaration {
match decl {
oxc_ast::ast::Declaration::TSInterfaceDeclaration(iface) => {
let name = iface.id.name.to_compact_string();
let body = build_interface_type_source(
source,
iface.id.span.end as usize,
iface.body.span.start as usize,
iface.body.span.end as usize,
);
self.interfaces.entry(name).or_insert(body);
}
oxc_ast::ast::Declaration::TSTypeAliasDeclaration(type_alias) => {
let name = type_alias.id.name.to_compact_string();
let type_start = type_alias.type_annotation.span().start as usize;
let type_end = type_alias.type_annotation.span().end as usize;
let type_body = String::from(&source[type_start..type_end]);
self.type_aliases.entry(name).or_insert(type_body);
}
_ => {}
}
}
}
_ => {}
}
}
}
/// Process a statement
fn process_statement(&mut self, stmt: &Statement<'_>, source: &str) {
match stmt {
Statement::ImportDeclaration(import_decl) => {
// Skip type-only import declarations: import type { ... } from '...'
if import_decl.import_kind.is_type() || is_import_type_only(import_decl, source) {
return;
}
// Process imports - add them to bindings so template knows about them
if let Some(specifiers) = &import_decl.specifiers {
for specifier in specifiers.iter() {
match specifier {
oxc_ast::ast::ImportDeclarationSpecifier::ImportSpecifier(spec) => {
// Named import: import { foo } from 'bar'
// Skip type-only imports: import { type Foo } from 'bar'
if !spec.import_kind.is_type() {
let name = spec.local.name.to_compact_string();
// Imports are treated as setup-maybe-ref since we don't know their type
self.bindings
.bindings
.insert(name, BindingType::SetupMaybeRef);
}
}
oxc_ast::ast::ImportDeclarationSpecifier::ImportDefaultSpecifier(
spec,
) => {
// Default import: import Foo from 'bar'
let name = spec.local.name.to_compact_string();
// Default imports of .vue files are typically components
self.bindings.bindings.insert(name, BindingType::SetupConst);
}
oxc_ast::ast::ImportDeclarationSpecifier::ImportNamespaceSpecifier(
spec,
) => {
// Namespace import: import * as foo from 'bar'
let name = spec.local.name.to_compact_string();
self.bindings.bindings.insert(name, BindingType::SetupConst);
}
}
}
}
}
Statement::VariableDeclaration(var_decl) => {
for decl in var_decl.declarations.iter() {
// Extract binding name if this is a simple identifier binding
let binding_name = match &decl.id {
BindingPattern::BindingIdentifier(id) => Some(id.name.to_compact_string()),
_ => None,
};
// Check if init is a macro call
if let Some(init) = &decl.init {
if let Some(mut macro_call) = extract_macro_from_expr(init, source) {
// Attach binding name to macro call
macro_call.1.binding_name = binding_name.as_deref().map(Into::into);
self.register_macro(¯o_call.0, macro_call.1);
}
// Check for withDefaults wrapping
if let Expression::CallExpression(call) = init {
if is_call_of(call, "withDefaults") {
self.macros.with_defaults = Some(MacroCall {
start: call.span.start as usize,
end: call.span.end as usize,
args: source[call.span.start as usize..call.span.end as usize]
.into(),
type_args: None,
binding_name: binding_name.as_deref().map(Into::into),
});
// Also extract the inner defineProps
if let Some(Argument::CallExpression(inner_call)) =
call.arguments.first()
{
if is_call_of(inner_call, "defineProps") {
let type_args =
extract_type_args_from_call(inner_call, source);
let props_call = MacroCall {
start: inner_call.span.start as usize,
end: inner_call.span.end as usize,
args: extract_args_from_call(inner_call, source),
type_args,
binding_name: binding_name.as_deref().map(String::from),
};
self.extract_props_bindings(&props_call);
self.macros.define_props = Some(props_call);
self.has_define_props_call = true;
}
}
}
}
}
// Extract binding name(s)
match &decl.id {
BindingPattern::BindingIdentifier(id) => {
let name = id.name.to_compact_string();
// Determine binding type
let binding_type = if let Some(init) = &decl.init {
infer_binding_type(init, var_decl.kind)
} else {
match var_decl.kind {
VariableDeclarationKind::Const => BindingType::SetupConst,
VariableDeclarationKind::Let => BindingType::SetupLet,
VariableDeclarationKind::Var => BindingType::SetupLet,
VariableDeclarationKind::Using
| VariableDeclarationKind::AwaitUsing => {
BindingType::SetupConst
}
}
};
self.bindings.bindings.insert(name, binding_type);
}
BindingPattern::ObjectPattern(obj_pat) => {
// Handle destructuring like: const { prop1, prop2 } = defineProps()
let mut is_props_destructure = false;
if let Some(init) = &decl.init {
if let Some((macro_name, macro_call)) =
extract_macro_from_expr(init, source)
{
if macro_name == "defineProps" {
is_props_destructure = true;
// Register defineProps macro (for type args / runtime props)
self.extract_props_bindings(¯o_call);
self.macros.define_props = Some(macro_call.clone());
self.has_define_props_call = true;
// Use the proper process_props_destructure function
let (destructure, binding_metadata, props_aliases) =
process_props_destructure(obj_pat, source);
// Merge binding metadata
for (name, binding_type) in binding_metadata {
self.bindings.bindings.insert(name, binding_type);
}
// Store props aliases
for (local, key) in props_aliases {
self.bindings.props_aliases.insert(local, key);
}
self.macros.props_destructure = Some(destructure);
}
}
}
// Register each destructured binding (skip for props destructure)
if !is_props_destructure {
// Infer binding type from the initializer.
// For `const { x, y } = useComposable()`, each destructured
// property might be a ref, so we use the same inference as
// non-destructured declarations. This ensures _unref() is
// applied in templates for composable returns.
let destructure_type = if let Some(init) = &decl.init {
infer_binding_type(init, var_decl.kind)
} else {
match var_decl.kind {
VariableDeclarationKind::Const => BindingType::SetupConst,
_ => BindingType::SetupLet,
}
};
for prop in obj_pat.properties.iter() {
if let BindingPattern::BindingIdentifier(id) = &prop.value {
self.bindings
.bindings
.insert(id.name.to_compact_string(), destructure_type);
}
}
}
}
BindingPattern::ArrayPattern(arr_pat) => {
let destructure_type = if let Some(init) = &decl.init {
infer_binding_type(init, var_decl.kind)
} else {
match var_decl.kind {
VariableDeclarationKind::Const => BindingType::SetupConst,
_ => BindingType::SetupLet,
}
};
for elem in arr_pat.elements.iter().flatten() {
if let BindingPattern::BindingIdentifier(id) = &elem {
self.bindings
.bindings
.insert(id.name.to_compact_string(), destructure_type);
}
}
}
BindingPattern::AssignmentPattern(assign_pat) => {
if let BindingPattern::BindingIdentifier(id) = &assign_pat.left {
self.bindings
.bindings
.insert(id.name.to_compact_string(), BindingType::SetupConst);
}
}
}
}
}
Statement::FunctionDeclaration(func) => {
if let Some(id) = &func.id {
self.bindings
.bindings
.insert(id.name.to_compact_string(), BindingType::SetupConst);
}
}
Statement::ClassDeclaration(class) => {
if let Some(id) = &class.id {
self.bindings
.bindings
.insert(id.name.to_compact_string(), BindingType::SetupConst);
}
}
Statement::ExpressionStatement(expr_stmt) => {
// Handle standalone macro calls like defineExpose({...})
if let Some(macro_call) = extract_macro_from_expr(&expr_stmt.expression, source) {
self.register_macro(¯o_call.0, macro_call.1);
}
// Handle standalone withDefaults(defineProps<...>(), {...})
if let Expression::CallExpression(call) = &expr_stmt.expression {
if is_call_of(call, "withDefaults") {
self.macros.with_defaults = Some(MacroCall {
start: call.span.start as usize,
end: call.span.end as usize,
args: source[call.span.start as usize..call.span.end as usize].into(),
type_args: None,
binding_name: None,
});
// Also extract the inner defineProps
if let Some(Argument::CallExpression(inner_call)) = call.arguments.first() {
if is_call_of(inner_call, "defineProps") {
let type_args = extract_type_args_from_call(inner_call, source);
let props_call = MacroCall {
start: inner_call.span.start as usize,
end: inner_call.span.end as usize,
args: extract_args_from_call(inner_call, source),
type_args,
binding_name: None,
};
self.extract_props_bindings(&props_call);
self.macros.define_props = Some(props_call);
self.has_define_props_call = true;
}
}
}
}
}
// TypeScript declarations are handled in the first pass
Statement::TSInterfaceDeclaration(_) | Statement::TSTypeAliasDeclaration(_) => {}
_ => {}
}
}
/// Register a macro call
pub(super) fn register_macro(&mut self, name: &str, call: MacroCall) {
match name {
"defineProps" => {
// Extract prop names and add to bindings
self.extract_props_bindings(&call);
self.macros.define_props = Some(call);
}
"defineEmits" => self.macros.define_emits = Some(call),
"defineExpose" => self.macros.define_expose = Some(call),
"defineOptions" => self.macros.define_options = Some(call),
"defineSlots" => self.macros.define_slots = Some(call),
"defineModel" => self.macros.define_models.push(call),
"withDefaults" => {
// Note: Props are extracted from the inner defineProps call
// in the separate withDefaults handling block in process_statement
self.macros.with_defaults = Some(call);
}
_ => {}
}
}
}