Skip to content

Commit

Permalink
Update: extract get key type function
Browse files Browse the repository at this point in the history
  • Loading branch information
sunrabbit123 committed Aug 13, 2023
1 parent e6e4c63 commit 813d328
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 79 deletions.
27 changes: 1 addition & 26 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2091,32 +2091,7 @@ impl Analyzer<'_, '_> {
ty => {
if let Err(err) = self.assign_with_opts(
&mut Default::default(),
&Type::Union(Union {
span,
types: vec![
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsStringKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNumberKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsSymbolKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
],
metadata: Default::default(),
tracker: Default::default(),
})
.freezed(),
&Type::get_any_key_type(span),
ty,
AssignOpts {
span: ls,
Expand Down
54 changes: 1 addition & 53 deletions crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,7 @@ impl Analyzer<'_, '_> {
.context("tried applying `keyof` to a literal by delegating to keyword type handler")
}
Type::Keyword(KeywordType { kind, .. }) => match kind {
TsKeywordTypeKind::TsAnyKeyword => {
let string = Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsStringKeyword,
metadata: Default::default(),
tracker: Default::default(),
});
let number = Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNumberKeyword,
metadata: Default::default(),
tracker: Default::default(),
});
let symbol = Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsSymbolKeyword,
metadata: Default::default(),
tracker: Default::default(),
});
return Ok(Type::Union(Union {
span,
types: vec![string, number, symbol],
metadata: Default::default(),
tracker: Default::default(),
}));
}
TsKeywordTypeKind::TsAnyKeyword | TsKeywordTypeKind::TsNeverKeyword => return Ok(Type::get_any_key_type(span)),
TsKeywordTypeKind::TsVoidKeyword
| TsKeywordTypeKind::TsUndefinedKeyword
| TsKeywordTypeKind::TsNullKeyword
Expand Down Expand Up @@ -156,33 +131,6 @@ impl Analyzer<'_, '_> {

TsKeywordTypeKind::TsBigIntKeyword => {}
TsKeywordTypeKind::TsSymbolKeyword => {}
TsKeywordTypeKind::TsNeverKeyword => {
return Ok(Type::Union(Union {
span,
types: vec![
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsStringKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNumberKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsSymbolKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
],
metadata: Default::default(),
tracker: Default::default(),
}))
}
TsKeywordTypeKind::TsIntrinsicKeyword => {}
},

Expand Down
29 changes: 29 additions & 0 deletions crates/stc_ts_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,35 @@ impl Type {
_ => None,
}
}

pub fn get_any_key_type(span: Span) -> Type {
let string = Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsStringKeyword,
metadata: Default::default(),
tracker: Default::default(),
});
let number = Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNumberKeyword,
metadata: Default::default(),
tracker: Default::default(),
});
let symbol = Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsSymbolKeyword,
metadata: Default::default(),
tracker: Default::default(),
});

Type::Union(Union {
span,
types: vec![string, number, symbol],
metadata: Default::default(),
tracker: Default::default(),
})
.freezed()
}
}

impl Type {
Expand Down

0 comments on commit 813d328

Please sign in to comment.