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

feat: Validate inferred generic types #1053

Merged
merged 4 commits into from
Aug 7, 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: 2 additions & 5 deletions crates/stc_ts_file_analyzer/src/analyzer/generic/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use stc_ts_generics::{
ExpandGenericOpts,
};
use stc_ts_type_ops::Fix;
use stc_ts_types::{Id, Interface, KeywordType, Readonly, TypeElement, TypeParam, TypeParamDecl, TypeParamInstantiation};
use stc_ts_types::{Id, Index, Interface, KeywordType, Readonly, TypeElement, TypeParam, TypeParamDecl, TypeParamInstantiation};
use stc_utils::{cache::Freeze, dev_span, ext::SpanExt};
use swc_common::{Span, Spanned, TypeEq};
use swc_ecma_ast::*;
Expand Down Expand Up @@ -241,10 +241,7 @@ impl Analyzer<'_, '_> {

return self.extends(span, child, &parent, opts);
}
_ => {}
}

match parent {
Type::Index(Index { ty, .. }) if ty.is_any() && child.is_index() => return Some(true),
Type::Keyword(KeywordType {
kind: TsKeywordTypeKind::TsNullKeyword,
..
Expand Down
31 changes: 30 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rnode::{Fold, FoldWith, VisitMut, VisitMutWith, VisitWith};
use stc_ts_ast_rnode::{RBindingIdent, RIdent, RNumber, RPat, RTsEntityName, RTsLit};
use stc_ts_errors::{
debug::{dump_type_as_string, force_dump_type_as_string, print_backtrace, print_type},
DebugExt,
DebugExt, ErrorKind,
};
use stc_ts_generics::{
expander::InferTypeResult,
Expand Down Expand Up @@ -128,6 +128,11 @@ impl Analyzer<'_, '_> {
if let Some(base) = base {
for (param, type_param) in base.params.iter().zip(type_params) {
info!("User provided `{:?} = {:?}`", type_param.name, param.clone());

if self.validate_generic_argument(span, param, type_param) {
break;
}

inferred.type_params.insert(
type_param.name.clone(),
InferenceInfo {
Expand Down Expand Up @@ -2376,6 +2381,30 @@ impl Analyzer<'_, '_> {

Ok(())
}

/// ```ts
/// type A<T extends number> = 1;
/// A<string>;
/// ```
fn validate_generic_argument(&mut self, span: Span, param: &Type, type_param: &TypeParam) -> bool {
if let Some(tp) = &type_param.constraint {
if !param.span().is_dummy() && !param.is_type_param() {
if matches!(self.extends(span, param, tp, Default::default()), Some(false)) {
self.storage.report(
ErrorKind::NotSatisfyConstraint {
span: param.span(),
left: tp.clone(),
right: Box::new(param.clone()),
}
.into(),
);
return true;
}
}
}

false
}
}

fn array_elem_type(t: &Type) -> Option<&Type> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"required_errors": {
"TS2344": 1,
"TS2339": 1
},
"required_error_lines": {
"TS2344": [
81
],
"TS2339": [
91
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 2,
matched_error: 8,
required_error: 1,
matched_error: 9,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"required_errors": {
"TS2344": 1,
"TS2339": 1
},
"required_error_lines": {
"TS2344": [
88
],
"TS2339": [
100
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 2,
matched_error: 8,
required_error: 1,
matched_error: 9,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
{
"required_errors": {
"TS2344": 5,
"TS2322": 1,
"TS2403": 1,
"TS2345": 1
},
"required_error_lines": {
"TS2344": [
11,
16,
17,
35,
49
],
"TS2322": [
33
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 8,
matched_error: 7,
required_error: 3,
matched_error: 12,
extra_error: 2,
panic: 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
},
"extra_errors": {
"TS2322": 1,
"TS0": 1
"TS2344": 1
},
"extra_error_lines": {
"TS2322": [
21
],
"TS0": [
323
"TS2344": [
317
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required_errors": {
"TS2313": 1,
"TS2322": 4,
"TS2344": 7,
"TS2344": 5,
"TS2403": 4,
"TS2345": 4,
"TS2536": 1,
Expand All @@ -19,11 +19,9 @@
139
],
"TS2344": [
25,
28,
29,
31,
33,
36,
40
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 23,
matched_error: 4,
required_error: 21,
matched_error: 6,
extra_error: 16,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 3,
matched_error: 5,
required_error: 0,
matched_error: 8,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"TS2339": 1,
"TS2531": 3,
"TS2532": 3,
"TS2533": 1,
"TS2344": 3
"TS2533": 1
},
"required_error_lines": {
"TS2454": [
Expand All @@ -26,11 +25,6 @@
],
"TS2533": [
34
],
"TS2344": [
53,
54,
55
]
},
"extra_errors": {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 12,
matched_error: 6,
required_error: 9,
matched_error: 9,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"required_errors": {
"TS2344": 1,
"TS2322": 1
},
"required_error_lines": {
"TS2344": [
138
],
"TS2322": [
167
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 2,
matched_error: 0,
required_error: 1,
matched_error: 1,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@
]
},
"extra_errors": {
"TS2304": 5,
"TS2322": 3
"TS2304": 3,
"TS2322": 3,
"TS2344": 2
},
"extra_error_lines": {
"TS2304": [
1,
5,
9,
18,
25
9
],
"TS2322": [
7,
20,
26
],
"TS2344": [
18,
25
]
}
}
4 changes: 2 additions & 2 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: 3518,
matched_error: 6517,
required_error: 3502,
matched_error: 6533,
extra_error: 769,
panic: 74,
}
Loading