From f89e241cbb3ef7579f2ae3a169d06771c2686043 Mon Sep 17 00:00:00 2001 From: Jesse Jafa Date: Mon, 12 Jun 2023 19:20:03 +0300 Subject: [PATCH 1/5] init --- .../stc_ts_file_analyzer/src/analyzer/assign/mod.rs | 10 +++++++++- .../stc_ts_file_analyzer/src/analyzer/assign/tpl.rs | 5 ++--- .../stc_ts_type_checker/tests/conformance.pass.txt | 1 + .../literal/templateLiteralTypes3.error-diff.json | 12 ------------ .../literal/templateLiteralTypes3.stats.rust-debug | 4 ++-- .../stc_ts_type_checker/tests/tsc-stats.rust-debug | 6 +++--- 6 files changed, 17 insertions(+), 21 deletions(-) delete mode 100644 crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.error-diff.json diff --git a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs index 888bb721c0..07db2399d0 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs @@ -2074,7 +2074,7 @@ impl Analyzer<'_, '_> { _ => {} }, - TsKeywordTypeKind::TsNumberKeyword => match *rhs { + TsKeywordTypeKind::TsNumberKeyword => match rhs { Type::Lit(LitType { lit: RTsLit::Number(..), .. }) => return Ok(()), @@ -2103,6 +2103,14 @@ impl Analyzer<'_, '_> { fail!() } + Type::Tpl(tpl) => { + // if !tpl.types.is_empty() { + let t = tpl.types.clone(); + if t.clone().iter().any(|t| t.is_bool()) { + fail!() + } + // } + } _ => {} }, diff --git a/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs b/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs index bc02f629a3..ffbe6cd3fa 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs @@ -2,10 +2,10 @@ use stc_ts_ast_rnode::RTsLit; use stc_ts_errors::{debug::force_dump_type_as_string, ErrorKind}; -use stc_ts_types::{IntrinsicKind, LitType, StringMapping, TplType, Type}; +use stc_ts_types::{IntrinsicKind, KeywordType, LitType, StringMapping, TplType, Type}; use stc_utils::dev_span; use swc_common::{Span, TypeEq}; -use swc_ecma_ast::TsKeywordTypeKind; +use swc_ecma_ast::{TsKeywordType, TsKeywordTypeKind}; use crate::{ analyzer::{ @@ -58,7 +58,6 @@ impl Analyzer<'_, '_> { source = tracing::field::display(&force_dump_type_as_string(source)), target = tracing::field::display(&force_dump_type_as_string(target)), ); - if source.type_eq(target) || target.is_any() || target.is_kwd(TsKeywordTypeKind::TsStringKeyword) { return Ok(true); } diff --git a/crates/stc_ts_type_checker/tests/conformance.pass.txt b/crates/stc_ts_type_checker/tests/conformance.pass.txt index b3042bffa6..c10b102f43 100644 --- a/crates/stc_ts_type_checker/tests/conformance.pass.txt +++ b/crates/stc_ts_type_checker/tests/conformance.pass.txt @@ -2372,6 +2372,7 @@ types/literal/stringLiteralsWithSwitchStatements03.ts types/literal/stringLiteralsWithSwitchStatements04.ts types/literal/stringLiteralsWithTypeAssertions01.ts types/literal/templateLiteralTypes2.ts +types/literal/templateLiteralTypes3.ts types/literal/templateLiteralTypesPatternsPrefixSuffixAssignability.ts types/localTypes/localTypes1.ts types/localTypes/localTypes2.ts diff --git a/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.error-diff.json deleted file mode 100644 index 63498f3ab2..0000000000 --- a/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.error-diff.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "required_errors": { - "TS2322": 1 - }, - "required_error_lines": { - "TS2322": [ - 77 - ] - }, - "extra_errors": {}, - "extra_error_lines": {} -} \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.stats.rust-debug index 6a9f55f079..2733dbe65c 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/literal/templateLiteralTypes3.stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 1, - matched_error: 7, + required_error: 0, + matched_error: 8, extra_error: 0, 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 3237e92857..e1473dfca1 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: 3535, - matched_error: 6500, - extra_error: 771, + required_error: 3517, + matched_error: 6480, + extra_error: 744, panic: 74, } \ No newline at end of file From 8c86593f0dc0826e565b395cf67927a0762ce2f3 Mon Sep 17 00:00:00 2001 From: Jesse Jafa Date: Mon, 12 Jun 2023 20:37:43 +0300 Subject: [PATCH 2/5] use ref --- .../stc_ts_file_analyzer/src/analyzer/assign/mod.rs | 12 ++++++------ .../stc_ts_type_checker/tests/tsc-stats.rust-debug | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs index 3c67b585fa..c104d89d35 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs @@ -7,8 +7,8 @@ use stc_ts_errors::{ }; 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, + PropertySignature, QueryExpr, QueryType, Readonly, Ref, StringMapping, ThisType, TplType, Tuple, TupleElement, Type, TypeElement, + TypeLit, TypeParam, }; use stc_utils::{cache::Freeze, dev_span, ext::SpanExt, stack}; use swc_atoms::js_word; @@ -2072,7 +2072,7 @@ impl Analyzer<'_, '_> { _ => {} }, - TsKeywordTypeKind::TsNumberKeyword => match rhs { + TsKeywordTypeKind::TsNumberKeyword => match *rhs { Type::Lit(LitType { lit: RTsLit::Number(..), .. }) => return Ok(()), @@ -2101,10 +2101,10 @@ impl Analyzer<'_, '_> { fail!() } - Type::Tpl(tpl) => { + Type::Tpl(TplType { ref types, .. }) => { // if !tpl.types.is_empty() { - let t = tpl.types.clone(); - if t.clone().iter().any(|t| t.is_bool()) { + // let t = tpl.types.clone(); + if types.iter().any(|t| t.is_bool()) { fail!() } // } 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 e1473dfca1..78f7f398d0 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: 3517, - matched_error: 6480, - extra_error: 744, + required_error: 3533, + matched_error: 6502, + extra_error: 771, panic: 74, } \ No newline at end of file From c07525de90aaa26acfac47dade196daba2ccbece Mon Sep 17 00:00:00 2001 From: Jesse Jafa Date: Sat, 17 Jun 2023 08:06:43 +0300 Subject: [PATCH 3/5] improve code --- .../src/analyzer/assign/mod.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs index c104d89d35..d433477d29 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs @@ -2102,12 +2102,23 @@ impl Analyzer<'_, '_> { fail!() } Type::Tpl(TplType { ref types, .. }) => { - // if !tpl.types.is_empty() { - // let t = tpl.types.clone(); - if types.iter().any(|t| t.is_bool()) { + if types.iter().any(|t| match t.normalize() { + Type::Keyword(KeywordType { + kind: TsKeywordTypeKind::TsNumberKeyword, + .. + }) + | Type::Keyword(KeywordType { + kind: TsKeywordTypeKind::TsBigIntKeyword, + .. + }) => false, + Type::Lit(ty) => match ty.lit { + RTsLit::BigInt(..) | RTsLit::Number(..) => false, + _ => true, + }, + _ => true, + }) { fail!() } - // } } _ => {} }, From 3628c92d8c7f969bb5e991ef7045fd5af7c5180f Mon Sep 17 00:00:00 2001 From: Jesse Jafa Date: Sat, 17 Jun 2023 08:09:41 +0300 Subject: [PATCH 4/5] clippy --- crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs | 5 +---- crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs index d433477d29..b85a8728c0 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs @@ -2111,10 +2111,7 @@ impl Analyzer<'_, '_> { kind: TsKeywordTypeKind::TsBigIntKeyword, .. }) => false, - Type::Lit(ty) => match ty.lit { - RTsLit::BigInt(..) | RTsLit::Number(..) => false, - _ => true, - }, + Type::Lit(ty) => !matches!(ty.lit, RTsLit::BigInt(..) | RTsLit::Number(..)), _ => true, }) { fail!() diff --git a/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs b/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs index 92e8ad3544..23fae87133 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs @@ -5,7 +5,7 @@ use stc_ts_errors::{debug::force_dump_type_as_string, ErrorKind}; use stc_ts_types::{IntrinsicKind, KeywordType, LitType, StringMapping, TplType, Type}; use stc_utils::dev_span; use swc_common::{Span, TypeEq}; -use swc_ecma_ast::{TsKeywordType, TsKeywordTypeKind}; +use swc_ecma_ast::TsKeywordTypeKind; use crate::{ analyzer::{ From ba73ea7450c3515025b31b2588200a2d969bfb38 Mon Sep 17 00:00:00 2001 From: Jesse Jafa Date: Sat, 17 Jun 2023 08:32:18 +0300 Subject: [PATCH 5/5] merge --- crates/stc_ts_type_checker/tests/tsc-stats.rust-debug | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 992227ddc9..16216098c2 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: 3519, - matched_error: 6516, + required_error: 3518, + matched_error: 6517, extra_error: 771, panic: 74, } \ No newline at end of file