Skip to content

Commit

Permalink
fix(js_semantic_model): handle bogus conditional type (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Apr 27, 2024
1 parent 1e1f56d commit 1d52503
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
15 changes: 9 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

#### Bug fixes

- Fix [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) that missed redeclarations in functions ([#2394](https://github.com/biomejs/biome/issues/2394)).
- [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) now reports redeclarations of parameters in a functions body ([#2394](https://github.com/biomejs/biome/issues/2394)).

The rule was not able to detected redeclaraions of a parameter or a type parameter in the function body.
The rule was unable to detect redeclarations of a parameter or type parameter in the function body.
The following two redeclarations are now reported:

```ts
Expand All @@ -82,7 +82,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- Fix [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) that wrongly reported overloads in object types ([#2608](https://github.com/biomejs/biome/issues/2608)).
- [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) no longer reports overloads in object types ([#2608](https://github.com/biomejs/biome/issues/2608)).

The rule no longer report redeclarations in the following code:

Expand All @@ -95,7 +95,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- Fix [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) that didn't merge default function export declarations and types ([#2372](https://github.com/biomejs/biome/issues/2372)).
- [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) now merge default function export declarations and types ([#2372](https://github.com/biomejs/biome/issues/2372)).

The following code is no longer reported as a redeclaration:

Expand All @@ -106,6 +106,9 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- [noUnusedVariables] no longer crash Biome when encountering a malformed conditional type ([#1695](https://github.com/biomejs/biome/issues/1695)).
Contributed by @Conaclos

- [useConst](https://biomejs.dev/linter/rules/use-const/) now ignores a variable that is read before its assignment.

Previously, the rule reported the following example:
Expand All @@ -120,7 +123,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- Fix [useShorthandFunctionType](https://biomejs.dev/linter/rules/use-shorthand-function-type/) that suggested invalid code fixes when parentheses are required ([#2595](https://github.com/biomejs/biome/issues/2595)).
- [useShorthandFunctionType](https://biomejs.dev/linter/rules/use-shorthand-function-type/) now suggests correct code fixes when parentheses are required ([#2595](https://github.com/biomejs/biome/issues/2595)).

Previously, the rule didn't add parentheses when they were needed.
It now adds parentheses when the function signature is inside an array, a union, or an intersection.
Expand All @@ -132,7 +135,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- Fix [useTemplate](https://biomejs.dev/linter/rules/use-template/) that wrongly escaped strings in some edge cases ([#2580](https://github.com/biomejs/biome/issues/2580)).
- [useTemplate](https://biomejs.dev/linter/rules/use-template/) now correctly escapes strings ([#2580](https://github.com/biomejs/biome/issues/2580)).

Previously, the rule didn't correctly escape characters preceded by an escaped character.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ToPascalCase<S extends string> = S extends `${infer Prefix}` ? : ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: unusedInferBogusConditional.ts
---
# Input
```ts
export type ToPascalCase<S extends string> = S extends `${infer Prefix}` ? : ;
```

# Diagnostics
```
unusedInferBogusConditional.ts:1:65 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This variable is unused.
> 1 │ export type ToPascalCase<S extends string> = S extends `${infer Prefix}` ? : ;
│ ^^^^^^
i Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
```
23 changes: 15 additions & 8 deletions crates/biome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ impl SemanticEventExtractor {

fn enter_any_type(&mut self, node: &AnyTsType) {
if node.in_conditional_true_type() {
self.push_conditional_true_scope(node);
self.push_scope(
node.syntax().text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
false,
);
self.push_infers_in_scope();
return;
}
let node = node.syntax();
Expand Down Expand Up @@ -668,6 +673,14 @@ impl SemanticEventExtractor {
) {
self.pop_scope(node.text_range());
}
// FALLBACK
// If the conditional type has a bogus true type,
// then infer declarations was never pushed into any scope.
// To ensure that every declaration has a binding,
// we bind the declaration directly in the scope of the conditional type.
if matches!(node.kind(), JsSyntaxKind::TS_CONDITIONAL_TYPE) && !self.infers.is_empty() {
self.push_infers_in_scope()
}
}

/// Return any previous extracted [SemanticEvent].
Expand All @@ -676,13 +689,7 @@ impl SemanticEventExtractor {
self.stash.pop_front()
}

fn push_conditional_true_scope(&mut self, node: &AnyTsType) {
self.push_scope(
node.syntax().text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
false,
);

fn push_infers_in_scope(&mut self) {
let infers = mem::take(&mut self.infers);
for infer in infers {
if let Ok(name_token) = infer.ident_token() {
Expand Down

0 comments on commit 1d52503

Please sign in to comment.