Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(isolated-declarations): eliminate imports incorrectly when they are used in TSInferType #4043

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions crates/oxc_isolated_declarations/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ impl<'a> Visit<'a> for ScopeTree<'a> {

// ==================== 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 {
/// ^^^
Expand All @@ -191,7 +201,7 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
/// 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>) {
self.enter_scope(ScopeFlags::empty());
walk_ts_type_parameter_declaration(self, decl);
decl.params.iter().for_each(|param| self.visit_ts_type_parameter(param));
// exit scope in parent AST node
}

Expand Down Expand Up @@ -267,13 +277,10 @@ impl<'a> Visit<'a> for ScopeTree<'a> {
fn visit_ts_mapped_type(&mut self, ty: &TSMappedType<'a>) {
// copy from walk_ts_mapped_type
self.enter_scope(ScopeFlags::empty());
self.add_type_binding(ty.type_parameter.name.name.clone());
self.visit_ts_type_parameter(&ty.type_parameter);
if let Some(name) = &ty.name_type {
self.visit_ts_type(name);
}
if let Some(constraint) = &ty.type_parameter.constraint {
self.visit_ts_type(constraint);
}
if let Some(type_annotation) = &ty.type_annotation {
self.visit_ts_type(type_annotation);
}
Expand All @@ -292,6 +299,6 @@ impl<'a> Visit<'a> for ScopeTree<'a> {

fn visit_ts_infer_type(&mut self, ty: &TSInferType<'a>) {
// copy from walk_ts_infer_type
self.add_type_binding(ty.type_parameter.name.name.clone());
self.visit_ts_type_parameter(&ty.type_parameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export interface A extends AExtend<Type> {}
export class B extends BExtend<Type> {}
export class C implements CImplements1<CType>, CImplements2<CType> {}
export function foo(this: ThisType1): void {}
export const bar: (this: ThisType2) => void = function() {}
export const bar: (this: ThisType2) => void = function() {}

import { type InferType1, type InferType2 } from 'infer';

export type F<X extends InferType1> = X extends infer U extends InferType2 ? U : never
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export declare class B extends BExtend<Type> {}
export declare class C implements CImplements1<CType>, CImplements2<CType> {}
export declare function foo(this: ThisType1 ): void;
export declare const bar: (this: ThisType2 ) => void;
import { type InferType1, type InferType2 } from 'infer';
export type F<X extends InferType1> = X extends infer U extends InferType2 ? U : never;
Loading