Skip to content

Commit

Permalink
Fix: add assign feature about indexed access type
Browse files Browse the repository at this point in the history
  • Loading branch information
sunrabbit123 committed Aug 19, 2023
1 parent d5a01ed commit fad6707
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
82 changes: 78 additions & 4 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use stc_ts_errors::{
DebugExt, ErrorKind,
};
use stc_ts_types::{
Array, Conditional, EnumVariant, Index, Instance, Interface, Intersection, IntrinsicKind, Key, KeywordType, LitType, Mapped,
PropertySignature, QueryExpr, QueryType, Readonly, Ref, StringMapping, ThisType, Tuple, TupleElement, Type, TypeElement, TypeLit,
TypeParam,
Array, Conditional, EnumVariant, Index, IndexedAccessType, Instance, Interface, Intersection, IntrinsicKind, Key, KeywordType, LitType,
Mapped, PropertySignature, QueryExpr, QueryType, Readonly, Ref, StringMapping, ThisType, Tuple, TupleElement, Type, TypeElement,
TypeLit, TypeParam,
};
use stc_utils::{cache::Freeze, dev_span, ext::SpanExt, stack};
use swc_atoms::js_word;
Expand Down Expand Up @@ -521,12 +521,72 @@ impl Analyzer<'_, '_> {
}
}

match ty {
match ty.normalize() {
Type::EnumVariant(e @ EnumVariant { name: Some(..), .. }) => {
if opts.ignore_enum_variant_name {
return Ok(Cow::Owned(Type::EnumVariant(EnumVariant { name: None, ..e.clone() })));
}
}
Type::IndexedAccessType(IndexedAccessType {
obj_type:
obj @ box Type::Param(TypeParam {
span: p_span,
name,
constraint,
..
}),
index_type:
box Type::Index(Index {
ty:
box Type::Param(TypeParam {
constraint: None,
default: None,
name: rhs_param_name,
..
}),
..
}),
..
}) if name.eq(rhs_param_name) => {
let create_index_accessed_type = |ty: Box<Type>, obj| {
Type::IndexedAccessType(IndexedAccessType {
span,
readonly: false,
obj_type: obj,
index_type: ty,
metadata: Default::default(),
tracker: Default::default(),
})
};

let gen_keyword_type = |kind| {
Type::Keyword(KeywordType {
span,
kind,
metadata: Default::default(),
tracker: Default::default(),
})
};

return Ok(Cow::Owned(Type::new_union(
span,
[
create_index_accessed_type(
Box::new(gen_keyword_type(TsKeywordTypeKind::TsStringKeyword)),
Box::new(*obj.clone()),
),
create_index_accessed_type(
Box::new(gen_keyword_type(TsKeywordTypeKind::TsNumberKeyword)),
Box::new(*obj.clone()),
),
create_index_accessed_type(
Box::new(gen_keyword_type(TsKeywordTypeKind::TsSymbolKeyword)),
Box::new(*obj.clone()),
),
],
)));
}

Type::Conditional(..)
| Type::IndexedAccessType(..)
| Type::Alias(..)
Expand Down Expand Up @@ -1965,10 +2025,23 @@ impl Analyzer<'_, '_> {
if results.iter().any(Result::is_ok) {
return Ok(());
}

if let Type::Param(TypeParam {
constraint: Some(box c), ..
}) = rhs
{
if let Ok(result) = self.normalize(Some(span), Cow::Borrowed(c), Default::default()) {
return self
.assign_with_opts(data, to, result.normalize(), opts)
.context("tried to assign a type_param at union");
}
}

let normalized = lu.types.iter().any(|ty| match ty.normalize() {
Type::TypeLit(ty) => ty.metadata.normalized,
_ => false,
});

let errors = results.into_iter().map(Result::unwrap_err).collect();
let should_use_single_error = normalized
|| lu.types.iter().all(|ty| {
Expand All @@ -1984,6 +2057,7 @@ impl Analyzer<'_, '_> {
|| ty.is_type_param()
|| ty.is_class()
|| ty.is_class_def()
|| ty.is_indexed_access_type()
});

if should_use_single_error {
Expand Down
1 change: 1 addition & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ jsdoc/parseLinkTag.ts
jsdoc/parseThrowsTag.ts
jsdoc/seeTag1.ts
jsdoc/seeTag2.ts
jsdoc/typeParameterExtendsUnionConstraintDistributed.ts
jsx/tsxEmitSpreadAttribute.ts
moduleResolution/importFromDot.ts
moduleResolution/packageJsonImportsExportsOptionCompat.ts
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
2 changes: 1 addition & 1 deletion crates/stc_ts_type_checker/tests/tsc-stats.rust-debug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 3502,
matched_error: 6533,
extra_error: 764,
extra_error: 763,
panic: 73,
}

0 comments on commit fad6707

Please sign in to comment.