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

fix: Improve selection of TS2322 vs TS2741 #760

Merged
merged 20 commits into from
Feb 26, 2023
6 changes: 6 additions & 0 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ impl Analyzer<'_, '_> {
AssignOpts {
allow_unknown_rhs: Some(true),
is_assigning_to_class_members: true,
report_assign_failure_for_missing_properties: opts.report_assign_failure_for_missing_properties.or_else(|| {
Some(match r.normalize() {
Type::Interface(r) => !r.extends.is_empty(),
_ => false,
})
}),
..opts
},
)
Expand Down
20 changes: 19 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ pub(crate) struct AssignOpts {

/// Used to prevent recursion
pub do_not_normalize_intersection_on_rhs: bool,

/// Use `TS2322` on missing properties.
pub report_assign_failure_for_missing_properties: Option<bool>,
}

#[derive(Default)]
Expand Down Expand Up @@ -2394,7 +2397,22 @@ impl Analyzer<'_, '_> {

Type::TypeLit(TypeLit { ref members, metadata, .. }) => {
return self
.assign_to_type_elements(data, span, members, rhs, *metadata, opts)
.assign_to_type_elements(
data,
span,
members,
rhs,
*metadata,
AssignOpts {
report_assign_failure_for_missing_properties: opts.report_assign_failure_for_missing_properties.or_else(|| {
match rhs.normalize() {
Type::Interface(r) if !r.extends.is_empty() => Some(true),
_ => None,
}
}),
..opts
},
)
.context("tried to assign a type to type elements");
}

Expand Down
51 changes: 49 additions & 2 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/type_el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,34 @@ impl Analyzer<'_, '_> {
{
rty.freeze();
return self
.assign_to_type_elements(data, lhs_span, lhs, &rty, lhs_metadata, AssignOpts { ..opts })
.assign_to_type_elements(
data,
lhs_span,
lhs,
&rty,
lhs_metadata,
AssignOpts {
report_assign_failure_for_missing_properties: opts
.report_assign_failure_for_missing_properties
.or_else(|| {
Some(match rhs.normalize() {
Type::Interface(r) => {
r.extends.is_empty()
&& r.body.iter().all(|el| {
!matches!(
el,
TypeElement::Index(..)
| TypeElement::Property(PropertySignature { .. })
| TypeElement::Method(MethodSignature { .. })
)
})
}
_ => false,
})
}),
..opts
},
)
.context("tried to assign to type elements by converting rhs to a type literal");
}

Expand Down Expand Up @@ -848,7 +875,27 @@ impl Analyzer<'_, '_> {
}

if !missing_fields.is_empty() {
if self.should_report_properties(span, lhs, rhs) {
if opts.report_assign_failure_for_missing_properties.unwrap_or_default()
&& lhs.iter().all(|el| {
!matches!(
el,
TypeElement::Property(PropertySignature { key: Key::Private(..), .. })
| TypeElement::Method(MethodSignature { key: Key::Private(..), .. })
)
})
{
errors.push(
ErrorKind::ObjectAssignFailed {
span,
errors: vec![ErrorKind::MissingFields {
span,
fields: missing_fields,
}
.into()],
}
.into(),
)
} else if self.should_report_properties(span, lhs, rhs) {
errors.push(
ErrorKind::MissingFields {
span,
Expand Down
2 changes: 1 addition & 1 deletion crates/stc_ts_file_analyzer/tests/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn compare(input: PathBuf) {
.into_iter()
.map(|err| StcError {
line: err.line,
code: err.code,
code: ErrorKind::normalize_error_code(err.code),
})
.collect_vec();
expected.sort();
Expand Down
27 changes: 27 additions & 0 deletions crates/stc_ts_file_analyzer/tests/tsc/issues/0xxx/741/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface StringTo<T> {
[x: string]: T;
}

interface NumberTo<T> {
[x: number]: T;
}

interface StringAndNumberTo<T> extends StringTo<T>, NumberTo<T> { }

interface Obj {
hello: string;
world: number;
}

type NumberToNumber = NumberTo<number>;

interface StringToAnyNumberToNumber extends StringTo<any>, NumberToNumber { }

function f3(
sToAny: StringTo<any>,
nToNumber: NumberToNumber,
strToAnyNumToNum: StringToAnyNumberToNumber,
someObj: Obj
) {
someObj = sToAny;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"file": "tests/tsc/issues/0xxx/741/1.ts",
"line": 26,
"col": 5,
"code": 2739
}
]
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
{
"required_errors": {
"TS2322": 3
},
"required_error_lines": {
"TS2322": [
82,
100,
105
]
},
"required_errors": {},
"required_error_lines": {},
"extra_errors": {
"TS2741": 4
"TS2322": 1
},
"extra_error_lines": {
"TS2741": [
82,
87,
100,
105
"TS2322": [
87
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 3,
matched_error: 21,
extra_error: 4,
required_error: 0,
matched_error: 24,
extra_error: 1,
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: 3791,
matched_error: 6020,
extra_error: 1108,
required_error: 3788,
matched_error: 6023,
extra_error: 1105,
panic: 100,
}