Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sunrabbit123 committed Aug 30, 2023
1 parent 36d90d7 commit ee6fe40
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use stc_ts_base_type_ops::bindings::BindingKind;
use stc_ts_env::ModuleConfig;
use stc_ts_errors::{
debug::{dump_type_as_string, force_dump_type_as_string},
DebugExt, ErrorKind, Errors,
DebugExt, Error, ErrorKind, Errors,
};
use stc_ts_generics::ExpandGenericOpts;
use stc_ts_type_ops::{generalization::prevent_generalize, is_str_lit_or_union, Fix};
Expand Down Expand Up @@ -2176,6 +2176,11 @@ impl Analyzer<'_, '_> {
{
let validate_type_param = |name: &Id| {
match prop.ty().normalize() {
Type::Index(Index {
ty: box Type::Param(TypeParam { name: constraint_name, .. }),
..
}) if !name.eq(constraint_name) => return Err::<(), Error>(ErrorKind::CannotBeUsedToIndexType { span }.into()),

Type::Param(TypeParam { constraint: Some(ty), .. }) => {
if let Type::Index(Index {
ty: box Type::Param(TypeParam { name: constraint_name, .. }),
Expand All @@ -2187,29 +2192,21 @@ impl Analyzer<'_, '_> {
}
}
}

Type::Index(Index {
ty: box Type::Param(TypeParam { name: constraint_name, .. }),
..
}) if !name.eq(constraint_name) => return Err(ErrorKind::CannotBeUsedToIndexType { span }.into()),
_ => {}
}
Ok(())
};

if let Some(ty) = constraint {
if let Type::Param(TypeParam { name: constraint_name, .. }) = ty.normalize() {
match validate_type_param(name) {
Ok(..) => {}
Err(err) => validate_type_param(constraint_name)?,
};
}
} else {
match validate_type_param(name) {
Ok(..) => {}
Err(err) => return Err(err),
};
}
constraint
.as_ref()
.and_then(|ty| {
if let Type::Param(TypeParam { name: constraint_name, .. }) = ty.normalize() {
Some(validate_type_param(name)).map(|res| res.or(validate_type_param(constraint_name)))
} else {
Some(Ok(()))
}
})
.unwrap_or(validate_type_param(name))?;
}

{
Expand Down

0 comments on commit ee6fe40

Please sign in to comment.