Skip to content

Commit

Permalink
feat: Improve arity check while assigning to tuples (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Apr 12, 2023
1 parent 51aed1c commit 31c5b05
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 52 deletions.
8 changes: 8 additions & 0 deletions crates/stc_ts_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,14 @@ impl ErrorKind {
// TS4117: Cannot have override with spelling suggestion
4113 | 4117 => 4112,

// TS2515: Missing an abstract member
// TS18052: Missing all abstract members
2515 | 18052 => 2515,

// TS5101: Deprecated
// TS5107: Deprecated with two args
5101 | 5107 => 5101,

_ => code,
}
}
Expand Down
49 changes: 39 additions & 10 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ impl Analyzer<'_, '_> {
}

if !errors.is_empty() {
if !opts.do_not_use_single_error_for_tuple_with_rest && l.elems.iter().any(|elem| elem.ty.is_rest()) {
// We should use single error for tuple with rest in some cases.
if (!opts.do_not_use_single_error_for_tuple_with_rest && l.elems.iter().any(|elem| elem.ty.is_rest()))
|| rhs.metadata().resolved_from_var
{
fail!(errors);
}

Expand Down Expand Up @@ -190,15 +193,41 @@ impl Analyzer<'_, '_> {
}
}

Type::Index(..)
| Type::Lit(..)
| Type::Interface(..)
| Type::TypeLit(..)
| Type::Keyword(..)
| Type::Class(..)
| Type::ClassDef(..)
if !opts.allow_iterable_on_rhs =>
{
Type::Interface(..) | Type::TypeLit(..) => {
if let Some(tuple) = self.convert_type_to_type_lit(span, Cow::Borrowed(l_type))? {
return self
.assign_to_type_elements(
data,
tuple.span,
&tuple.members,
rhs,
tuple.metadata,
AssignOpts {
allow_unknown_rhs: Some(false),
allow_missing_fields: false,
..opts
},
)
.convert_err(|err| match &err {
ErrorKind::Errors { span, errors }
if errors
.iter()
.all(|err| matches!(&**err, ErrorKind::UnknownPropertyInObjectLiteralAssignment { .. })) =>
{
ErrorKind::SimpleAssignFailed {
span: *span,
cause: Some(box err.context("union errors because we are assigning to tuple")),
}
}
_ => err,
})
.context("tried to assign to a type literal created from a tuple")
.map(Some);
}

fail!()
}
Type::Index(..) | Type::Lit(..) | Type::Keyword(..) | Type::Class(..) | Type::ClassDef(..) if !opts.allow_iterable_on_rhs => {
fail!()
}

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 @@ -2576,6 +2576,7 @@ types/thisType/thisTypeInTuples.ts
types/thisType/thisTypeInTypePredicate.ts
types/thisType/thisTypeOptionalCall.ts
types/thisType/thisTypeSyntacticContext.ts
types/tuple/arityAndOrderCompatibility01.ts
types/tuple/castingTuple.ts
types/tuple/contextualTypeWithTuple.ts
types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"required_errors": {
"TS2322": 6,
"TS2540": 2,
"TS2542": 1,
"TS2339": 1,
"TS2540": 1,
"TS2403": 1
},
"required_error_lines": {
Expand All @@ -15,15 +14,11 @@
109,
111
],
"TS2540": [
137,
139
],
"TS2542": [
138
],
"TS2339": [
140
"TS2540": [
139
],
"TS2403": [
266
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 11,
matched_error: 9,
required_error: 9,
matched_error: 11,
extra_error: 3,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 17,
extra_error: 3,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@
"required_errors": {},
"required_error_lines": {},
"extra_errors": {
"TS0": 2,
"TS2322": 9
"TS2322": 4
},
"extra_error_lines": {
"TS0": [
62,
63
],
"TS2322": [
71,
71,
71,
71,
71,
71,
71,
62,
63,
71,
72
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 11,
extra_error: 4,
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: 3706,
matched_error: 6328,
extra_error: 759,
required_error: 3704,
matched_error: 6330,
extra_error: 749,
panic: 73,
}

0 comments on commit 31c5b05

Please sign in to comment.