Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle different arity of type params for interfaces #985

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/stc_ts_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,11 @@ pub enum ErrorKind {
NotDeclaredInSuperClass {
span: Span,
},

/// TS2428
InterfaceNonIdenticalTypeParams {
span: Span,
},
}

#[cfg(target_pointer_width = "64")]
Expand Down Expand Up @@ -2161,6 +2166,8 @@ impl ErrorKind {

ErrorKind::NotDeclaredInSuperClass { .. } => 4113,

ErrorKind::InterfaceNonIdenticalTypeParams { .. } => 2428,

_ => 0,
}
}
Expand Down
35 changes: 28 additions & 7 deletions crates/stc_ts_file_analyzer/src/analyzer/decl_merging.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -97,24 +97,37 @@ 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());
Copy link
Contributor Author

@awareness481 awareness481 Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

span currently looks like this

interface A<T extends string> {}
────────────────────────────────

is there any way to only use the span of the name of the interface? (like tsc)

interface A<T extends string> {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a field like name_span to the Interface type

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(),
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
constraint: None,
default: None,
metadata: Default::default(),
tracker: Default::default(),
}),
);
}
} 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(),
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"required_errors": {
"TS2428": 6
"TS2428": 2
},
"required_error_lines": {
"TS2428": [
3,
7,
12,
16,
34,
40
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 6,
matched_error: 0,
required_error: 2,
matched_error: 4,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
},
"required_error_lines": {
"TS2428": [
27,
32
32,
38
]
},
"extra_errors": {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 6,
matched_error: 0,
required_error: 2,
matched_error: 4,
extra_error: 0,
panic: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"required_errors": {
"TS2322": 6,
"TS2540": 2,
"TS2542": 1,
"TS2540": 1,
"TS2339": 1,
"TS2403": 1
},
"required_error_lines": {
Expand All @@ -14,11 +15,15 @@
109,
111
],
"TS2540": [
137,
139
],
"TS2542": [
138
],
"TS2540": [
139
"TS2339": [
140
],
"TS2403": [
266
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 9,
matched_error: 11,
required_error: 11,
matched_error: 9,
extra_error: 3,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 2,
matched_error: 0,
required_error: 0,
matched_error: 2,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"required_errors": {
"TS2374": 12,
"TS2428": 2
"TS2374": 12
},
"required_error_lines": {
"TS2374": [
Expand All @@ -17,10 +16,6 @@
25,
29,
30
],
"TS2428": [
8,
28
]
},
"extra_errors": {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 14,
matched_error: 0,
required_error: 12,
matched_error: 2,
extra_error: 0,
panic: 0,
}
6 changes: 3 additions & 3 deletions 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,
required_error: 3694,
matched_error: 6340,
extra_error: 747,
panic: 73,
panic: 72,
}