Skip to content

Commit

Permalink
fix: Treat a primitive type differently from the interface for the pr…
Browse files Browse the repository at this point in the history
…imitive type (#983)
  • Loading branch information
sunrabbit123 authored Apr 12, 2023
1 parent 31c5b05 commit 32d80ed
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 54 deletions.
40 changes: 13 additions & 27 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, KeywordTypeMetadata,
LitType, Mapped, PropertySignature, QueryExpr, QueryType, Readonly, Ref, StringMapping, ThisType, Tuple, TupleElement, Type,
TypeElement, TypeLit, TypeParam,
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,
};
use stc_utils::{cache::Freeze, dev_span, ext::SpanExt, stack};
use swc_atoms::js_word;
Expand Down Expand Up @@ -503,29 +503,6 @@ impl Analyzer<'_, '_> {
}

match ty {
Type::Ref(Ref {
span,
type_name: RTsEntityName::Ident(type_name),
type_args: None,
metadata,
..
}) => {
// TODO(kdy1): Check if ref points global.
return Ok(Cow::Owned(Type::Keyword(KeywordType {
span: *span,
kind: match type_name.sym {
js_word!("Boolean") => TsKeywordTypeKind::TsBooleanKeyword,
js_word!("Number") => TsKeywordTypeKind::TsNumberKeyword,
js_word!("String") => TsKeywordTypeKind::TsStringKeyword,
_ => return Ok(Cow::Borrowed(ty)),
},
metadata: KeywordTypeMetadata {
common: metadata.common,
..Default::default()
},
tracker: Default::default(),
})));
}
Type::EnumVariant(e @ EnumVariant { name: Some(..), .. }) => {
if opts.ignore_enum_variant_name {
return Ok(Cow::Owned(Type::EnumVariant(EnumVariant { name: None, ..e.clone() })));
Expand Down Expand Up @@ -1014,6 +991,7 @@ impl Analyzer<'_, '_> {
if match rhs.normalize_instance() {
Type::Lit(..) => true,
Type::Interface(i) => matches!(&**i.name.sym(), "Boolean" | "String" | "Number" | "BigInt"),
Type::EnumVariant(..) => true,
_ => false,
} {
// Handle special cases.
Expand Down Expand Up @@ -1051,6 +1029,14 @@ impl Analyzer<'_, '_> {
{
match rhs {
Type::Keyword(ref k) if k.kind == *kwd => return Ok(()),
Type::EnumVariant(ref e) => {
let e = e.def.normalize();
if (e.has_num && !e.has_str && *interface == "Number")
|| (e.has_str && !e.has_num && *interface == "String")
{
return Ok(());
}
}
_ => {}
}
}
Expand Down Expand Up @@ -2156,7 +2142,7 @@ impl Analyzer<'_, '_> {
| TsKeywordTypeKind::TsBooleanKeyword
| TsKeywordTypeKind::TsNullKeyword
| TsKeywordTypeKind::TsUndefinedKeyword => match rhs {
Type::Lit(..) | Type::Interface(..) | Type::Function(..) | Type::Constructor(..) => fail!(),
Type::Lit(..) | Type::Function(..) | Type::Constructor(..) | Type::Interface(..) => fail!(),
Type::TypeLit(..) => {
let left = self.normalize(
Some(span),
Expand Down
14 changes: 14 additions & 0 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/type_el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,20 @@ impl Analyzer<'_, '_> {
}
}

// retain default method
if !missing_fields.is_empty() {
missing_fields.retain(|v| {
if let TypeElement::Method(MethodSignature {
key: Key::Normal { sym, .. },
..
}) = v
{
&**sym != "toLocaleString" && &**sym != "toString"
} else {
true
}
});
}
if !missing_fields.is_empty() {
if opts.report_assign_failure_for_missing_properties.unwrap_or_default()
&& lhs.iter().all(|el| {
Expand Down
2 changes: 2 additions & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2472,11 +2472,13 @@ types/primitives/enum/validEnumAssignments.ts
types/primitives/null/directReferenceToNull.ts
types/primitives/null/validNullAssignments.ts
types/primitives/number/assignFromNumberInterface.ts
types/primitives/number/assignFromNumberInterface2.ts
types/primitives/number/extendNumberInterface.ts
types/primitives/number/invalidNumberAssignments.ts
types/primitives/number/numberPropertyAccess.ts
types/primitives/number/validNumberAssignments.ts
types/primitives/string/assignFromStringInterface.ts
types/primitives/string/assignFromStringInterface2.ts
types/primitives/string/extendStringInterface.ts
types/primitives/string/invalidStringAssignments.ts
types/primitives/string/stringPropertyAccess.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: 2,
extra_error: 1,
extra_error: 0,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 2,
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: 3704,
matched_error: 6330,
extra_error: 749,
extra_error: 747,
panic: 73,
}

0 comments on commit 32d80ed

Please sign in to comment.