Skip to content

Commit

Permalink
refactor(isolated-declarations): remove useless code from scope (#4420)
Browse files Browse the repository at this point in the history
The original logic was written to fix incorrect TypeScript ast scopes. Now the scopes are correct so they can be removed
  • Loading branch information
Dunqing committed Jul 23, 2024
1 parent 25dab7b commit 0e1ea90
Showing 1 changed file with 1 addition and 130 deletions.
131 changes: 1 addition & 130 deletions crates/oxc_isolated_declarations/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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<T>(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> = Type extends Array<infer Item> ? 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);
}
}

0 comments on commit 0e1ea90

Please sign in to comment.