From cc9fda81dd753388141fcd6b0388105f95015101 Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Tue, 8 Aug 2023 10:51:49 -0700 Subject: [PATCH 1/6] Fix: not implement keyof this --- .../src/analyzer/types/keyof.rs | 13 ++++++++++--- .../src/analyzer/types/mod.rs | 12 ++++++------ .../types/conditional/conditionalTypes1/4.ts | 16 ++++++++++++++++ .../conditionalTypes1.error-diff.json | 6 +----- .../conditionalTypes1.stats.rust-debug | 2 +- .../tests/tsc-stats.rust-debug | 2 +- 6 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 crates/stc_ts_file_analyzer/tests/pass-only/conformance/types/conditional/conditionalTypes1/4.ts diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs index 63bfd0336a..a110e20221 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs @@ -5,16 +5,17 @@ use stc_ts_ast_rnode::{RIdent, RNumber, RStr, RTsEntityName, RTsLit}; use stc_ts_errors::{debug::force_dump_type_as_string, DebugExt, ErrorKind}; use stc_ts_type_ops::{is_str_lit_or_union, Fix}; use stc_ts_types::{ - Class, ClassMember, ClassProperty, Index, KeywordType, KeywordTypeMetadata, LitType, Method, MethodSignature, PropertySignature, Ref, - Type, TypeElement, Union, + Class, ClassMember, ClassProperty, Id, Index, KeywordType, KeywordTypeMetadata, LitType, Method, MethodSignature, PropertySignature, + Ref, Type, TypeElement, Union, }; use stc_utils::{cache::Freeze, ext::TypeVecExt, stack, try_cache}; use swc_atoms::js_word; use swc_common::{Span, SyntaxContext, TypeEq, DUMMY_SP}; use swc_ecma_ast::TsKeywordTypeKind; +use tracing::Instrument; use crate::{ - analyzer::{types::NormalizeTypeOpts, Analyzer}, + analyzer::{scope::ItemRef, types::NormalizeTypeOpts, Analyzer}, VResult, }; @@ -297,6 +298,12 @@ impl Analyzer<'_, '_> { .context("tried to get keys of Array (builtin)"); } + Type::This(this) => { + if let Some(ty) = self.scope.this().map(Cow::into_owned) { + return self.keyof(this.span, &ty); + } + } + Type::Interface(..) | Type::Enum(..) => { let ty = self .convert_type_to_type_lit(span, ty.freezed(), Default::default())? diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs index 244a0ef204..6917b1a667 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs @@ -578,12 +578,12 @@ impl Analyzer<'_, '_> { return Ok(ty); } - let ty = self - .normalize(span, Cow::Owned(prop_ty), opts) - .context("tried to normalize the type of property")? - .into_owned(); - - return Ok(Cow::Owned(ty)); + return self.with_child(super::ScopeKind::ObjectLit, Default::default(), |child| { + child.scope.this = Some(*obj_ty); + child + .normalize(span, Cow::Owned(prop_ty), opts) + .context("tried to normalize the type of property") + }); } // TODO(kdy1): diff --git a/crates/stc_ts_file_analyzer/tests/pass-only/conformance/types/conditional/conditionalTypes1/4.ts b/crates/stc_ts_file_analyzer/tests/pass-only/conformance/types/conditional/conditionalTypes1/4.ts new file mode 100644 index 0000000000..2d008c3e54 --- /dev/null +++ b/crates/stc_ts_file_analyzer/tests/pass-only/conformance/types/conditional/conditionalTypes1/4.ts @@ -0,0 +1,16 @@ +// @strict: true +// @declaration: true + +type OldDiff = ( + & { [P in T]: P; } + & { [P in U]: never; } + & { [x: string]: never; } +)[T]; +interface A { + a: 'a'; +} +interface B1 extends A { + b: 'b'; + c: OldDiff; +} +type c1 = B1['c']; // 'c' | 'b' \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.error-diff.json index 539bf5b803..b9748d69af 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.error-diff.json @@ -30,15 +30,11 @@ ] }, "extra_errors": { - "TS2322": 1, - "TS2344": 1 + "TS2322": 1 }, "extra_error_lines": { "TS2322": [ 21 - ], - "TS2344": [ - 317 ] } } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.stats.rust-debug index d84650c75c..5821a9121b 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/conditional/conditionalTypes1.stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 11, matched_error: 9, - extra_error: 2, + extra_error: 1, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug index b72223af99..c1f1543a3a 100644 --- a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 3502, matched_error: 6533, - extra_error: 769, + extra_error: 768, panic: 74, } \ No newline at end of file From 4591ec7ddd94b03b6b7b88122bd0b69809c271ee Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Tue, 8 Aug 2023 10:58:24 -0700 Subject: [PATCH 2/6] Docs: add comment about todo --- crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs index 6917b1a667..48bd4acdbe 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs @@ -578,6 +578,7 @@ impl Analyzer<'_, '_> { return Ok(ty); } + // TODO(sunrabbit123): scope of normalize should be obj_ty scope return self.with_child(super::ScopeKind::ObjectLit, Default::default(), |child| { child.scope.this = Some(*obj_ty); child From c742f180d35e2f000ab1f763159d812a8ca7f019 Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Tue, 8 Aug 2023 10:59:18 -0700 Subject: [PATCH 3/6] Fix: resolve clippy --- crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs index a110e20221..285bed5184 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs @@ -12,10 +12,9 @@ use stc_utils::{cache::Freeze, ext::TypeVecExt, stack, try_cache}; use swc_atoms::js_word; use swc_common::{Span, SyntaxContext, TypeEq, DUMMY_SP}; use swc_ecma_ast::TsKeywordTypeKind; -use tracing::Instrument; use crate::{ - analyzer::{scope::ItemRef, types::NormalizeTypeOpts, Analyzer}, + analyzer::{types::NormalizeTypeOpts, Analyzer}, VResult, }; From 547c3db86fe6f0b625a3fafec8b9ef70567627a9 Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Tue, 8 Aug 2023 10:59:41 -0700 Subject: [PATCH 4/6] Fix: resolve clippy 2 --- crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs index 285bed5184..dd37d5205c 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/keyof.rs @@ -5,8 +5,8 @@ use stc_ts_ast_rnode::{RIdent, RNumber, RStr, RTsEntityName, RTsLit}; use stc_ts_errors::{debug::force_dump_type_as_string, DebugExt, ErrorKind}; use stc_ts_type_ops::{is_str_lit_or_union, Fix}; use stc_ts_types::{ - Class, ClassMember, ClassProperty, Id, Index, KeywordType, KeywordTypeMetadata, LitType, Method, MethodSignature, PropertySignature, - Ref, Type, TypeElement, Union, + Class, ClassMember, ClassProperty, Index, KeywordType, KeywordTypeMetadata, LitType, Method, MethodSignature, PropertySignature, Ref, + Type, TypeElement, Union, }; use stc_utils::{cache::Freeze, ext::TypeVecExt, stack, try_cache}; use swc_atoms::js_word; From b56b053885a3b56f29c5b56cdf887c1ec4b4e92b Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Tue, 8 Aug 2023 11:02:06 -0700 Subject: [PATCH 5/6] Fix: remove name of todo author --- crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs index 48bd4acdbe..eb2d021158 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs @@ -578,7 +578,7 @@ impl Analyzer<'_, '_> { return Ok(ty); } - // TODO(sunrabbit123): scope of normalize should be obj_ty scope + // TODO(): scope of normalize should be obj_ty scope return self.with_child(super::ScopeKind::ObjectLit, Default::default(), |child| { child.scope.this = Some(*obj_ty); child From a1dccf71778e27a9523cca0b492e9e4dc17cc03f Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Wed, 9 Aug 2023 06:41:29 -0700 Subject: [PATCH 6/6] Fix: apply comment --- .../stc_ts_file_analyzer/src/analyzer/types/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs index eb2d021158..90dba21dcf 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs @@ -578,13 +578,14 @@ impl Analyzer<'_, '_> { return Ok(ty); } - // TODO(): scope of normalize should be obj_ty scope - return self.with_child(super::ScopeKind::ObjectLit, Default::default(), |child| { - child.scope.this = Some(*obj_ty); - child - .normalize(span, Cow::Owned(prop_ty), opts) + let prev_this = self.scope.this.take(); + self.scope.this = Some(*obj_ty); + let result = { + self.normalize(span, Cow::Owned(prop_ty), opts) .context("tried to normalize the type of property") - }); + }; + self.scope.this = prev_this; + return result; } // TODO(kdy1):