Skip to content

Commit

Permalink
feat(ts/analyzer): Use never for unconditional throws (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon authored Dec 18, 2022
1 parent acd622f commit d6dc481
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
22 changes: 15 additions & 7 deletions crates/stc_ts_file_analyzer/src/analyzer/stmt/return_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ impl Analyzer<'_, '_> {
debug!("visit_stmts_for_return()");
debug_assert!(!self.is_builtin, "builtin: visit_stmts_for_return should not be called");

let mut unconditional_throw = None;
for stmt in stmts {
if let RStmt::Throw(throws) = stmt {
unconditional_throw = Some(throws.span);
break;
}
}

let cannot_fallback_to_iterable_iterator = self.rule().strict_null_checks && {
let mut v = YieldValueUsageFinder::default();

Expand All @@ -95,13 +103,7 @@ impl Analyzer<'_, '_> {

{
// Expand return types if no element references a type parameter
let can_expand = values.return_types.iter().all(|ty| {
if should_preserve_ref(ty) {
return false;
}

true
});
let can_expand = !values.return_types.iter().any(should_preserve_ref);

if can_expand {
values.return_types = values
Expand Down Expand Up @@ -149,6 +151,12 @@ impl Analyzer<'_, '_> {
}
}

{
if let Some(span) = unconditional_throw {
values.return_types.push(Type::never(span, Default::default()));
}
}

debug!("visit_stmts_for_return: types.len() = {}", values.return_types.len());

let mut actual = Vec::with_capacity(values.return_types.len());
Expand Down
38 changes: 38 additions & 0 deletions crates/stc_ts_file_analyzer/tests/pass/fn/impl/1.swc-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

x Type
,-[$DIR/tests/pass/fn/impl/1.ts:1:1]
1 | undefined === function(): number {
: ^^^^^^^^^
`----

Error:
> undefined

x Type
,-[$DIR/tests/pass/fn/impl/1.ts:2:3]
2 | throw undefined
: ^^^^^^^^^
`----

Error:
> undefined

x Type
,-[$DIR/tests/pass/fn/impl/1.ts:1:1]
1 | ,-> undefined === function(): number {
2 | | throw undefined
3 | `-> }
`----

Error:
> () => number

x Type
,-[$DIR/tests/pass/fn/impl/1.ts:1:1]
1 | ,-> undefined === function(): number {
2 | | throw undefined
3 | `-> }
`----

Error:
> boolean
3 changes: 3 additions & 0 deletions crates/stc_ts_file_analyzer/tests/pass/fn/impl/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
undefined === function(): number {
throw undefined
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 3,
matched_error: 1,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 1,
matched_error: 0,
extra_error: 4,
extra_error: 3,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 3,
extra_error: 2,
panic: 0,
}

0 comments on commit d6dc481

Please sign in to comment.