From 0e1ea9028261d131f6fd6c34b96420a21ff6896a Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 23 Jul 2024 09:39:16 +0000 Subject: [PATCH] refactor(isolated-declarations): remove useless code from scope (#4420) The original logic was written to fix incorrect TypeScript ast scopes. Now the scopes are correct so they can be removed --- crates/oxc_isolated_declarations/src/scope.rs | 131 +----------------- 1 file changed, 1 insertion(+), 130 deletions(-) diff --git a/crates/oxc_isolated_declarations/src/scope.rs b/crates/oxc_isolated_declarations/src/scope.rs index e1a5c5fc3d65d..21c54807a11ce 100644 --- a/crates/oxc_isolated_declarations/src/scope.rs +++ b/crates/oxc_isolated_declarations/src/scope.rs @@ -50,8 +50,7 @@ impl<'a> ScopeTree<'a> { } pub fn has_reference(&self, name: &str) -> bool { - // XXX(lucab): this should probably unwrap? - let Some(scope) = self.levels.last() else { return false }; + let Some(scope) = self.levels.last() else { unreachable!() }; scope.value_references.contains(name) || scope.type_references.contains(name) } @@ -194,132 +193,4 @@ impl<'a> Visit<'a> for ScopeTree<'a> { } walk_declaration(self, declaration); } - - // ==================== TSTypeParameter ==================== - - fn visit_ts_type_parameter(&mut self, it: &TSTypeParameter<'a>) { - self.add_type_binding(it.name.name.clone()); - if let Some(constraint) = &it.constraint { - self.visit_ts_type(constraint); - } - if let Some(default) = &it.default { - self.visit_ts_type(default); - } - } - - /// ```ts - /// function foo(x: T): T { - /// ^^^ - /// `T` is a type parameter - /// return x; - /// } - /// ``` - /// We should create a new scope for TSTypeParameterDeclaration - /// Because the type parameter is can be used in following nodes - /// until the end of the function. So we leave the scope in the parent node (Function) - fn visit_ts_type_parameter_declaration(&mut self, decl: &TSTypeParameterDeclaration<'a>) { - // TODO: doesn't have a scope_id! - self.enter_scope(ScopeFlags::empty(), &Cell::default()); - decl.params.iter().for_each(|param| self.visit_ts_type_parameter(param)); - // exit scope in parent AST node - } - - fn visit_class(&mut self, class: &Class<'a>) { - walk_class(self, class); - if class.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_function(&mut self, func: &Function<'a>, flags: ScopeFlags) { - walk_function(self, func, flags); - if func.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_arrow_function_expression(&mut self, expr: &ArrowFunctionExpression<'a>) { - walk_arrow_function_expression(self, expr); - if expr.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_ts_type_alias_declaration(&mut self, decl: &TSTypeAliasDeclaration<'a>) { - walk_ts_type_alias_declaration(self, decl); - if decl.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_ts_interface_declaration(&mut self, decl: &TSInterfaceDeclaration<'a>) { - walk_ts_interface_declaration(self, decl); - if decl.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_ts_call_signature_declaration(&mut self, signature: &TSCallSignatureDeclaration<'a>) { - walk_ts_call_signature_declaration(self, signature); - if signature.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_ts_method_signature(&mut self, signature: &TSMethodSignature<'a>) { - walk_ts_method_signature(self, signature); - if signature.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_ts_construct_signature_declaration( - &mut self, - signature: &TSConstructSignatureDeclaration<'a>, - ) { - walk_ts_construct_signature_declaration(self, signature); - if signature.type_parameters.is_some() { - self.leave_scope(); - } - } - - fn visit_ts_function_type(&mut self, signature: &TSFunctionType<'a>) { - walk_ts_function_type(self, signature); - if signature.type_parameters.is_some() { - self.leave_scope(); - } - } - - /// `type D = { [key in keyof T]: K };` - /// ^^^^^^^^^^^^^^^^^^^^ - /// We need to add both `T` and `K` to the scope - fn visit_ts_mapped_type(&mut self, ty: &TSMappedType<'a>) { - // TODO: doesn't have a scope_id! - self.enter_scope(ScopeFlags::empty(), &Cell::default()); - // copy from walk_ts_mapped_type - self.visit_ts_type_parameter(&ty.type_parameter); - if let Some(name) = &ty.name_type { - self.visit_ts_type(name); - } - if let Some(type_annotation) = &ty.type_annotation { - self.visit_ts_type(type_annotation); - } - self.leave_scope(); - } - - /// `export type Flatten = Type extends Array ? Item : Type;` - /// ^^^^^^^^^^ - /// `Item` is a type parameter - /// We need to add `Item` to the scope - fn visit_conditional_expression(&mut self, expr: &ConditionalExpression<'a>) { - // TODO: doesn't have a scope_id! - self.enter_scope(ScopeFlags::empty(), &Cell::default()); - walk_conditional_expression(self, expr); - self.leave_scope(); - } - - fn visit_ts_infer_type(&mut self, ty: &TSInferType<'a>) { - // copy from walk_ts_infer_type - self.visit_ts_type_parameter(&ty.type_parameter); - } }