From 1db79be2904a050dcf73e7c61ddb42212374888c Mon Sep 17 00:00:00 2001 From: Jesse Jafa Date: Fri, 14 Apr 2023 15:25:30 +0300 Subject: [PATCH] handle different arity of type params for interfaces --- crates/stc_ts_errors/src/lib.rs | 7 ++++ .../src/analyzer/decl_merging.rs | 35 +++++++++++++++---- .../tests/conformance.pass.txt | 1 + ...icInterfaceWithTheSameName.error-diff.json | 6 +--- ...cInterfaceWithTheSameName.stats.rust-debug | 4 +-- ...SameNameButDifferentArity.error-diff.json} | 4 +-- ...SameNameButDifferentArity.stats.rust-debug | 6 ++-- .../conditionalTypes1.error-diff.json | 11 ++++-- .../conditionalTypes1.stats.rust-debug | 4 +-- ...tSignaturesWithOverloads2.stats.rust-debug | 4 +-- .../multipleNumericIndexers.error-diff.json | 7 +--- .../multipleNumericIndexers.stats.rust-debug | 4 +-- .../tests/tsc-stats.rust-debug | 6 ++-- 13 files changed, 62 insertions(+), 37 deletions(-) rename crates/stc_ts_type_checker/tests/conformance/{types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.error-diff.json => interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.error-diff.json} (88%) diff --git a/crates/stc_ts_errors/src/lib.rs b/crates/stc_ts_errors/src/lib.rs index 4144424f45..f4bb0e185f 100644 --- a/crates/stc_ts_errors/src/lib.rs +++ b/crates/stc_ts_errors/src/lib.rs @@ -1560,6 +1560,11 @@ pub enum ErrorKind { NotDeclaredInSuperClass { span: Span, }, + + /// TS2428 + InterfaceNonIdenticalTypeParams { + span: Span, + }, } #[cfg(target_pointer_width = "64")] @@ -2161,6 +2166,8 @@ impl ErrorKind { ErrorKind::NotDeclaredInSuperClass { .. } => 4113, + ErrorKind::InterfaceNonIdenticalTypeParams { .. } => 2428, + _ => 0, } } diff --git a/crates/stc_ts_file_analyzer/src/analyzer/decl_merging.rs b/crates/stc_ts_file_analyzer/src/analyzer/decl_merging.rs index d96d83ba76..4bcf3ada1d 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/decl_merging.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/decl_merging.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use fxhash::FxHashMap; -use stc_ts_errors::{debug::dump_type_as_string, DebugExt}; +use stc_ts_errors::{debug::dump_type_as_string, DebugExt, ErrorKind}; use stc_ts_types::{ClassDef, ClassMember, ClassProperty, Id, Interface, Method, Type, TypeElement, TypeParam, TypeParamDecl}; use stc_utils::cache::Freeze; use swc_common::{Span, Spanned}; @@ -97,16 +97,23 @@ impl Analyzer<'_, '_> { } (Type::Interface(a), Type::Interface(bi)) => { - // TODO: Handle the number of type parameters. let mut type_params = FxHashMap::default(); - if let Some(b_tps) = &bi.type_params { - if let Some(a_tp) = &a.type_params { + + match (&a.type_params, &bi.type_params) { + (Some(a_tps), Some(b_tps)) => { + if a_tps.params.len() != b_tps.params.len() { + self.storage + .report(ErrorKind::InterfaceNonIdenticalTypeParams { span: a.span() }.into()); + self.storage + .report(ErrorKind::InterfaceNonIdenticalTypeParams { span: b.span() }.into()); + } + for (idx, b_tp) in b_tps.params.iter().enumerate() { type_params.insert( b_tp.name.clone(), Type::Param(TypeParam { - span: a_tp.span, - name: a_tp.params[idx].name.clone(), + span: a_tps.span, + name: a_tps.params.get(idx).unwrap_or(b_tp).name.clone(), constraint: None, default: None, metadata: Default::default(), @@ -114,7 +121,13 @@ impl Analyzer<'_, '_> { }), ); } - } else { + } + (None, Some(b_tps)) => { + self.storage + .report(ErrorKind::InterfaceNonIdenticalTypeParams { span: a.span() }.into()); + self.storage + .report(ErrorKind::InterfaceNonIdenticalTypeParams { span: b.span() }.into()); + for (idx, b_tp) in b_tps.params.iter().enumerate() { type_params.insert( b_tp.name.clone(), @@ -129,7 +142,15 @@ impl Analyzer<'_, '_> { ); } } + (Some(a_tps), None) => { + self.storage + .report(ErrorKind::InterfaceNonIdenticalTypeParams { span: a.span() }.into()); + self.storage + .report(ErrorKind::InterfaceNonIdenticalTypeParams { span: b.span() }.into()); + } + (None, None) => {} } + let b_ty = self.expand_type_params(&type_params, b, Default::default())?.freezed(); let mut new_members = a.body.clone(); diff --git a/crates/stc_ts_type_checker/tests/conformance.pass.txt b/crates/stc_ts_type_checker/tests/conformance.pass.txt index 7a355fc8e4..9838080f00 100644 --- a/crates/stc_ts_type_checker/tests/conformance.pass.txt +++ b/crates/stc_ts_type_checker/tests/conformance.pass.txt @@ -2450,6 +2450,7 @@ types/objectTypeLiteral/callSignatures/typeParameterUsedAsTypeParameterConstrain types/objectTypeLiteral/callSignatures/typeParameterUsedAsTypeParameterConstraint4.ts types/objectTypeLiteral/constructSignatures/constructSignaturesWithIdenticalOverloads.ts types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads.ts +types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.ts types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloadsThatDifferOnlyByReturnType.ts types/objectTypeLiteral/indexSignatures/numericIndexingResults.ts types/objectTypeLiteral/indexSignatures/stringIndexingResults.ts diff --git a/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.error-diff.json index a176c5103a..2d10581df9 100644 --- a/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.error-diff.json @@ -1,13 +1,9 @@ { "required_errors": { - "TS2428": 6 + "TS2428": 2 }, "required_error_lines": { "TS2428": [ - 3, - 7, - 12, - 16, 34, 40 ] diff --git a/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.stats.rust-debug index e1dc08a8cb..0ec042eda0 100644 --- a/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/genericAndNonGenericInterfaceWithTheSameName.stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 6, - matched_error: 0, + required_error: 2, + matched_error: 4, extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.error-diff.json similarity index 88% rename from crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.error-diff.json rename to crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.error-diff.json index cc31700bf3..a685c83102 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.error-diff.json @@ -4,8 +4,8 @@ }, "required_error_lines": { "TS2428": [ - 27, - 32 + 32, + 38 ] }, "extra_errors": {}, diff --git a/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.stats.rust-debug index 86b030cdec..0ec042eda0 100644 --- a/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/interfaces/declarationMerging/twoGenericInterfacesWithTheSameNameButDifferentArity.stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 6, - matched_error: 0, + required_error: 2, + matched_error: 4, extra_error: 0, - panic: 1, + panic: 0, } \ 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 62036b763e..fd726daa27 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 @@ -1,8 +1,9 @@ { "required_errors": { "TS2322": 6, + "TS2540": 2, "TS2542": 1, - "TS2540": 1, + "TS2339": 1, "TS2403": 1 }, "required_error_lines": { @@ -14,11 +15,15 @@ 109, 111 ], + "TS2540": [ + 137, + 139 + ], "TS2542": [ 138 ], - "TS2540": [ - 139 + "TS2339": [ + 140 ], "TS2403": [ 266 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 d5dbbd0f4f..df5398b133 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: 9, - matched_error: 11, + required_error: 11, + matched_error: 9, extra_error: 3, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.stats.rust-debug index 485da44816..780225267c 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/constructSignatures/constructSignaturesWithOverloads2.stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 2, - matched_error: 0, + required_error: 0, + matched_error: 2, extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.error-diff.json index 1de1ed2e5b..f0edb04a9e 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.error-diff.json @@ -1,7 +1,6 @@ { "required_errors": { - "TS2374": 12, - "TS2428": 2 + "TS2374": 12 }, "required_error_lines": { "TS2374": [ @@ -17,10 +16,6 @@ 25, 29, 30 - ], - "TS2428": [ - 8, - 28 ] }, "extra_errors": {}, diff --git a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.stats.rust-debug index 0fb2a1f65b..9bd45a47d5 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/objectTypeLiteral/indexSignatures/multipleNumericIndexers.stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 14, - matched_error: 0, + required_error: 12, + matched_error: 2, 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 cb9e73b796..8b30e63e91 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: 3704, - matched_error: 6330, + required_error: 3694, + matched_error: 6340, extra_error: 747, - panic: 73, + panic: 72, } \ No newline at end of file