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
use std::cell::Cell;

use oxc_allocator::Box;
use oxc_allocator::CloneIn;
use oxc_allocator::Vec;
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_ast::visit::walk_mut::walk_ts_signatures;
use oxc_ast::{syntax_directed_operations::BoundNames, Visit, VisitMut};
use oxc_span::{GetSpan, SPAN};
use oxc_syntax::scope::ScopeFlags;

use crate::diagnostics::accessor_must_have_explicit_return_type;
use crate::{
    diagnostics::{
        binding_element_export, inferred_type_of_expression, signature_computed_property_name,
        variable_must_have_explicit_type,
    },
    IsolatedDeclarations,
};

impl<'a> IsolatedDeclarations<'a> {
    pub fn transform_variable_declaration(
        &self,
        decl: &VariableDeclaration<'a>,
        check_binding: bool,
    ) -> Option<Box<'a, VariableDeclaration<'a>>> {
        if decl.declare {
            None
        } else {
            let declarations =
                self.ast.vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
                    self.transform_variable_declarator(declarator, check_binding)
                }));
            Some(self.transform_variable_declaration_with_new_declarations(decl, declarations))
        }
    }

    pub fn transform_variable_declaration_with_new_declarations(
        &self,
        decl: &VariableDeclaration<'a>,
        declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>,
    ) -> Box<'a, VariableDeclaration<'a>> {
        self.ast.alloc_variable_declaration(
            decl.span,
            decl.kind,
            self.ast.vec_from_iter(declarations),
            self.is_declare(),
        )
    }

    pub fn transform_variable_declarator(
        &self,
        decl: &VariableDeclarator<'a>,
        check_binding: bool,
    ) -> Option<VariableDeclarator<'a>> {
        if decl.id.kind.is_destructuring_pattern() {
            decl.id.bound_names(&mut |id| {
                if !check_binding || self.scope.has_reference(&id.name) {
                    self.error(binding_element_export(id.span));
                }
            });
            return None;
        }

        if check_binding {
            if let Some(name) = decl.id.get_identifier() {
                if !self.scope.has_reference(&name) {
                    return None;
                }
            }
        }

        let mut binding_type = None;
        let mut init = None;
        if decl.id.type_annotation.is_none() {
            if let Some(init_expr) = &decl.init {
                // if kind is const and it doesn't need to infer type from expression
                if decl.kind.is_const() && !Self::is_need_to_infer_type_from_expression(init_expr) {
                    if let Expression::TemplateLiteral(lit) = init_expr {
                        init =
                            self.transform_template_to_string(lit).map(Expression::StringLiteral);
                    } else {
                        // SAFETY: `ast.copy` is unsound! We need to fix.
                        init = Some(unsafe { self.ast.copy(init_expr) });
                    }
                } else if !decl.kind.is_const()
                    || !matches!(init_expr, Expression::TemplateLiteral(_))
                {
                    // otherwise, we need to infer type from expression
                    binding_type = self.infer_type_from_expression(init_expr);
                }
            }
            if init.is_none() && binding_type.is_none() {
                binding_type = Some(self.ast.ts_type_unknown_keyword(SPAN));
                if !decl.init.as_ref().is_some_and(Expression::is_function) {
                    self.error(variable_must_have_explicit_type(decl.id.span()));
                }
            }
        }
        let id = binding_type.map_or_else(
            || {
                // SAFETY: `ast.copy` is unsound! We need to fix.
                unsafe { self.ast.copy(&decl.id) }
            },
            |ts_type| {
                self.ast.binding_pattern(
                    // SAFETY: `ast.copy` is unsound! We need to fix.
                    unsafe { self.ast.copy(&decl.id.kind) },
                    Some(self.ast.ts_type_annotation(SPAN, ts_type)),
                    decl.id.optional,
                )
            },
        );

        Some(self.ast.variable_declarator(decl.span, decl.kind, id, init, decl.definite))
    }

    pub fn transform_using_declaration(
        &self,
        decl: &VariableDeclaration<'a>,
        check_binding: bool,
    ) -> Box<'a, VariableDeclaration<'a>> {
        let declarations =
            self.ast.vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
                self.transform_variable_declarator(declarator, check_binding)
            }));
        self.transform_using_declaration_with_new_declarations(decl, declarations)
    }

    pub fn transform_using_declaration_with_new_declarations(
        &self,
        decl: &VariableDeclaration<'a>,
        declarations: oxc_allocator::Vec<'a, VariableDeclarator<'a>>,
    ) -> Box<'a, VariableDeclaration<'a>> {
        self.ast.alloc_variable_declaration(
            decl.span,
            VariableDeclarationKind::Const,
            declarations,
            self.is_declare(),
        )
    }

    fn transform_ts_module_block(
        &mut self,
        block: &Box<'a, TSModuleBlock<'a>>,
    ) -> Box<'a, TSModuleBlock<'a>> {
        // We need to enter a new scope for the module block, avoid add binding to the parent scope
        // TODO: doesn't have a scope_id!
        self.scope.enter_scope(ScopeFlags::TsModuleBlock, &Cell::default());
        let stmts = self.transform_statements_on_demand(&block.body);
        self.scope.leave_scope();
        self.ast.alloc_ts_module_block(SPAN, self.ast.vec(), stmts)
    }

    pub fn transform_ts_module_declaration(
        &mut self,
        decl: &Box<'a, TSModuleDeclaration<'a>>,
    ) -> Box<'a, TSModuleDeclaration<'a>> {
        if decl.declare {
            // SAFETY: `ast.copy` is unsound! We need to fix.
            return unsafe { self.ast.copy(decl) };
        }

        let Some(body) = &decl.body else {
            // SAFETY: `ast.copy` is unsound! We need to fix.
            return unsafe { self.ast.copy(decl) };
        };

        match body {
            TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
                let inner = self.transform_ts_module_declaration(decl);
                self.ast.alloc_ts_module_declaration(
                    decl.span,
                    // SAFETY: `ast.copy` is unsound! We need to fix.
                    unsafe { self.ast.copy(&decl.id) },
                    Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)),
                    decl.kind,
                    self.is_declare(),
                )
            }
            TSModuleDeclarationBody::TSModuleBlock(block) => {
                let body = self.transform_ts_module_block(block);
                self.ast.alloc_ts_module_declaration(
                    decl.span,
                    // SAFETY: `ast.copy` is unsound! We need to fix.
                    unsafe { self.ast.copy(&decl.id) },
                    Some(TSModuleDeclarationBody::TSModuleBlock(body)),
                    decl.kind,
                    self.is_declare(),
                )
            }
        }
    }

    pub fn transform_declaration(
        &mut self,
        decl: &Declaration<'a>,
        check_binding: bool,
    ) -> Option<Declaration<'a>> {
        match decl {
            Declaration::FunctionDeclaration(func) => {
                if !check_binding
                    || func.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
                {
                    self.transform_function(func, None).map(Declaration::FunctionDeclaration)
                } else {
                    None
                }
            }
            Declaration::VariableDeclaration(decl) => self
                .transform_variable_declaration(decl, check_binding)
                .map(Declaration::VariableDeclaration),
            Declaration::ClassDeclaration(decl) => {
                if !check_binding
                    || decl.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
                {
                    self.transform_class(decl, None).map(Declaration::ClassDeclaration)
                } else {
                    None
                }
            }
            Declaration::TSTypeAliasDeclaration(alias_decl) => {
                if !check_binding || self.scope.has_reference(&alias_decl.id.name) {
                    let mut decl = decl.clone_in(self.ast.allocator);
                    self.visit_declaration(&mut decl);
                    Some(decl)
                } else {
                    None
                }
            }
            Declaration::TSInterfaceDeclaration(interface_decl) => {
                if !check_binding || self.scope.has_reference(&interface_decl.id.name) {
                    let mut decl = decl.clone_in(self.ast.allocator);
                    self.visit_declaration(&mut decl);
                    Some(decl)
                } else {
                    None
                }
            }
            Declaration::TSEnumDeclaration(enum_decl) => {
                if !check_binding || self.scope.has_reference(&enum_decl.id.name) {
                    self.transform_ts_enum_declaration(enum_decl)
                } else {
                    None
                }
            }
            Declaration::TSModuleDeclaration(decl) => {
                if !check_binding
                    || matches!(
                        &decl.id,
                        TSModuleDeclarationName::Identifier(ident)
                            if self.scope.has_reference(&ident.name)
                    )
                {
                    Some(Declaration::TSModuleDeclaration(
                        self.transform_ts_module_declaration(decl),
                    ))
                } else {
                    None
                }
            }
            Declaration::TSImportEqualsDeclaration(decl) => {
                if !check_binding || self.scope.has_reference(&decl.id.name) {
                    // SAFETY: `ast.copy` is unsound! We need to fix.
                    Some(Declaration::TSImportEqualsDeclaration(unsafe { self.ast.copy(decl) }))
                } else {
                    None
                }
            }
        }
    }

    fn report_signature_property_key(&self, key: &PropertyKey<'a>, computed: bool) {
        if !computed {
            return;
        }

        let is_not_allowed = match key {
            PropertyKey::StaticIdentifier(_) | PropertyKey::Identifier(_) => false,
            PropertyKey::StaticMemberExpression(expr) => {
                !expr.get_first_object().is_identifier_reference()
            }
            key => !self.is_literal_key(key),
        };

        if is_not_allowed {
            self.error(signature_computed_property_name(key.span()));
        }
    }
}

impl<'a> VisitMut<'a> for IsolatedDeclarations<'a> {
    fn visit_ts_signatures(&mut self, signatures: &mut Vec<'a, TSSignature<'a>>) {
        self.transform_ts_signatures(signatures);
        walk_ts_signatures(self, signatures);
    }

    fn visit_ts_method_signature(&mut self, signature: &mut TSMethodSignature<'a>) {
        self.report_signature_property_key(&signature.key, signature.computed);
        if signature.return_type.is_none() {
            match signature.kind {
                TSMethodSignatureKind::Method => {
                    self.error(inferred_type_of_expression(signature.span));
                }
                TSMethodSignatureKind::Get => {
                    self.error(accessor_must_have_explicit_return_type(signature.key.span()));
                }
                TSMethodSignatureKind::Set => {
                    // setter method don't need return type
                }
            }
        }
    }

    fn visit_ts_property_signature(&mut self, signature: &mut TSPropertySignature<'a>) {
        self.report_signature_property_key(&signature.key, signature.computed);
    }
}