Skip to content

Commit

Permalink
refactor: Use Type::xx as constructor for readability (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunrabbit123 authored Sep 10, 2023
1 parent 90f7c04 commit 13696ae
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 174 deletions.
7 changes: 2 additions & 5 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,11 @@ impl Analyzer<'_, '_> {
let mut skip_check_null_or_undefined_of_rhs = false;
match op {
op!("*=") | op!("**=") | op!("/=") | op!("%=") | op!("-=") => {
if let Type::Keyword(KeywordType {
kind: TsKeywordTypeKind::TsUndefinedKeyword | TsKeywordTypeKind::TsNullKeyword,
..
}) = rhs
{
if rhs.is_null_or_undefined() {
if op == op!("**=") {
skip_check_null_or_undefined_of_rhs = true;
}

if op != op!("**=") && !self.rule().strict_null_checks && (l.is_num() || l.is_enum_variant()) {
skip_check_null_or_undefined_of_rhs = true;
} else {
Expand Down
10 changes: 1 addition & 9 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/type_el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,15 +1413,7 @@ impl Analyzer<'_, '_> {
l_index_ret_ty,
&Type::new_union(
span,
vec![
*r_prop_ty.clone(),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
],
vec![*r_prop_ty.clone(), Type::undefined(span, Default::default())],
),
opts,
)
Expand Down
7 changes: 1 addition & 6 deletions crates/stc_ts_file_analyzer/src/analyzer/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ impl Analyzer<'_, '_> {
.assign_with_opts(
&mut Default::default(),
ty,
&Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
&Type::undefined(span, Default::default()),
AssignOpts {
span,
..Default::default()
Expand Down
7 changes: 1 addition & 6 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,7 @@ impl Analyzer<'_, '_> {

if !errors.is_empty() {
if can_use_undefined && errors.len() != u.types.len() {
types.push(Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
types.push(Type::undefined(span, Default::default()));
types.dedup_type();
return Ok(Cow::Owned(Type::new_union(span, types)));
}
Expand Down
56 changes: 4 additions & 52 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,20 +426,7 @@ impl Analyzer<'_, '_> {
};

let exclude_types = if is_loose_comparison_with_null_or_undefined {
vec![
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
]
Type::null_and_undefined(span, Default::default())
} else {
exclude.freezed()
};
Expand Down Expand Up @@ -1874,16 +1861,7 @@ impl Analyzer<'_, '_> {
let actual = name.slice_to(name.len() - 1);

if has_undefined && candidates.is_empty() {
return Ok((
actual,
Type::Keyword(KeywordType {
span: u.span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
excluded.freezed(),
));
return Ok((actual, Type::undefined(span, Default::default()), excluded.freezed()));
}
let ty = Type::new_union(span, candidates).freezed();
return Ok((actual, ty, excluded.freezed()));
Expand Down Expand Up @@ -2280,20 +2258,7 @@ impl Analyzer<'_, '_> {
temp_vec.clone()
} else {
if is_loose_comparison {
vec![
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
]
Type::null_and_undefined(span, Default::default())
} else {
vec![]
}
Expand Down Expand Up @@ -2344,20 +2309,7 @@ impl Analyzer<'_, '_> {
temp_vec.clone()
} else {
if is_loose_comparison {
vec![
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
]
Type::null_and_undefined(span, Default::default())
} else {
vec![]
}
Expand Down
51 changes: 9 additions & 42 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2612,12 +2612,7 @@ impl Analyzer<'_, '_> {
}

if !opts.disallow_inexact && metadata.inexact {
return Ok(Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
return Ok(Type::undefined(span, Default::default()));
}

return Err(ErrorKind::NoSuchProperty {
Expand Down Expand Up @@ -2720,12 +2715,7 @@ impl Analyzer<'_, '_> {
} else {
if !errors.is_empty() {
if is_all_tuple && errors.len() != types.len() {
result_types.push(Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
result_types.push(Type::undefined(span, Default::default()));
result_types.dedup_type();
let ty = Type::new_union(span, result_types);
ty.assert_valid();
Expand Down Expand Up @@ -2828,13 +2818,9 @@ impl Analyzer<'_, '_> {

if v as usize >= elems.len() {
if opts.use_undefined_for_tuple_index_error {
return Ok(Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
return Ok(Type::undefined(span, Default::default()));
}

if opts.use_last_element_for_tuple_on_out_of_bound {
return Ok(*elems.last().unwrap().ty.clone());
}
Expand All @@ -2848,12 +2834,7 @@ impl Analyzer<'_, '_> {
}
.context("returning undefined because it's l-value context"),
);
return Ok(Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
return Ok(Type::undefined(span, Default::default()));
}

return Err(ErrorKind::TupleIndexError {
Expand Down Expand Up @@ -4783,13 +4764,9 @@ impl Analyzer<'_, '_> {
type_ann: Option<&Type>,
) -> VResult<Type> {
if i.sym == js_word!("undefined") {
return Ok(Type::Keyword(KeywordType {
span: i.span.with_ctxt(SyntaxContext::empty()),
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
return Ok(Type::undefined(i.span.with_ctxt(SyntaxContext::empty()), Default::default()));
}

let ty = self.type_of_var(i, mode, type_args)?;
if self.ctx.should_store_truthy_for_access && mode == TypeOfMode::RValue {
// `i` is truthy
Expand Down Expand Up @@ -4840,20 +4817,10 @@ impl Analyzer<'_, '_> {
RLit::Null(RNull { span }) => {
if self.ctx.in_export_default_expr {
// TODO(kdy1): strict mode
return Ok(Type::Keyword(KeywordType {
span: *span,
kind: TsKeywordTypeKind::TsAnyKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
return Ok(Type::any(*span, Default::default()));
}

Ok(Type::Keyword(KeywordType {
span: *span,
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
tracker: Default::default(),
}))
Ok(Type::null(*span, Default::default()))
}
RLit::Regex(v) => Ok(Type::Ref(Ref {
span: v.span,
Expand Down
9 changes: 4 additions & 5 deletions crates/stc_ts_file_analyzer/src/analyzer/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,10 @@ impl Analyzer<'_, '_> {
false
}

Type::Union(u) => u.types.iter().all(|ty| {
ty.is_kwd(TsKeywordTypeKind::TsNullKeyword)
|| ty.is_kwd(TsKeywordTypeKind::TsUndefinedKeyword)
|| self.is_type_valid_for_computed_key(span, ty)
}),
Type::Union(u) => u
.types
.iter()
.all(|ty| ty.is_null_or_undefined() || self.is_type_valid_for_computed_key(span, ty)),

_ => false,
}
Expand Down
10 changes: 4 additions & 6 deletions crates/stc_ts_file_analyzer/src/analyzer/stmt/return_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,10 @@ impl Analyzer<'_, '_> {

self.scope.return_values.yield_types.push(item_ty);
} else {
self.scope.return_values.yield_types.push(Type::Keyword(KeywordType {
span: e.span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}));
self.scope
.return_values
.yield_types
.push(Type::undefined(e.span, Default::default()));
}

Ok(Type::any(e.span, Default::default()))
Expand Down
21 changes: 3 additions & 18 deletions crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,7 @@ impl Analyzer<'_, '_> {

if sum >= 2 {
if sum == 2 && is_undefined && is_void {
return Ok(Some(Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: KeywordTypeMetadata { ..Default::default() },
tracker: Default::default(),
})));
return Ok(Some(Type::undefined(span, Default::default())));
}
return never!();
}
Expand Down Expand Up @@ -2498,18 +2493,8 @@ impl Analyzer<'_, '_> {
}

let mut unknown = vec![
Type::Keyword(KeywordType {
span: *span,
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span: *span,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::null(*span, Default::default()),
Type::undefined(*span, Default::default()),
Type::TypeLit(TypeLit {
span: *span,
members: vec![],
Expand Down
22 changes: 5 additions & 17 deletions crates/stc_ts_file_analyzer/src/ty/type_facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,13 @@ impl Analyzer<'_, '_> {
Type::new_union_without_dedup(
span,
vec![
Type::null(span, Default::default()),
Type::Keyword(KeywordType {
kind: TsKeywordTypeKind::TsObjectKeyword,
metadata: Default::default(),
span,
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
span,
tracker: Default::default(),
}),
],
),
],
Expand Down Expand Up @@ -467,25 +462,18 @@ impl Fold<Type> for TypeFactsHandler<'_, '_, '_> {
// typeof x === 'object'
// => x = {} | null
if ty.is_unknown() && self.facts.contains(TypeFacts::TypeofEQObject) {
ty = Type::Union(Union {
ty = Type::new_union(
span,
types: vec![
vec![
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsObjectKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::Keyword(KeywordType {
span,
kind: TsKeywordTypeKind::TsNullKeyword,
metadata: Default::default(),
tracker: Default::default(),
}),
Type::null(span, Default::default()),
],
metadata: Default::default(),
tracker: Default::default(),
})
)
.freezed();
}

Expand Down
11 changes: 3 additions & 8 deletions crates/stc_ts_type_ops/src/union_normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustc_hash::FxHashMap;
use stc_ts_ast_rnode::{RBindingIdent, RIdent, RPat};
use stc_ts_generics::type_param::replacer::TypeParamReplacer;
use stc_ts_types::{
CallSignature, FnParam, Function, FunctionMetadata, Key, KeywordType, PropertySignature, Type, TypeElement, TypeLit, TypeLitMetadata,
TypeParamDecl, Union,
CallSignature, FnParam, Function, FunctionMetadata, Key, PropertySignature, Type, TypeElement, TypeLit, TypeLitMetadata, TypeParamDecl,
Union,
};
use stc_utils::{cache::Freeze, dev_span, ext::TypeVecExt};
use swc_atoms::JsWord;
Expand Down Expand Up @@ -357,12 +357,7 @@ impl ObjectUnionNormalizer {
},
optional: true,
params: Default::default(),
type_ann: Some(Box::new(Type::Keyword(KeywordType {
span: DUMMY_SP,
kind: TsKeywordTypeKind::TsUndefinedKeyword,
metadata: Default::default(),
tracker: Default::default(),
}))),
type_ann: Some(Box::new(Type::undefined(DUMMY_SP, Default::default()))),
type_params: Default::default(),
metadata: Default::default(),
accessor: Default::default(),
Expand Down
Loading

0 comments on commit 13696ae

Please sign in to comment.