Skip to content

Commit

Permalink
fix(js_semantic): type value schism (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Oct 9, 2023
1 parent 3dc9327 commit 3df5f43
Show file tree
Hide file tree
Showing 26 changed files with 770 additions and 685 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [452](https://github.com/biomejs/biome/pull/452). The linter panicked when it met a malformed regex (a regex not ending with a slash).

- Fix [#104](https://github.com/biomejs/biome/issues/104). We now correctly handle types and values with the same name.

### Parser

- Enhance diagnostic for infer type handling in the parser. The 'infer' keyword can only be utilized within the 'extends' clause of a conditional type. Using it outside of this context will result in an error. Ensure that any type declarations using 'infer' are correctly placed within the conditional type structure to avoid parsing issues. Contributed by @denbezrukov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ fn suggested_fix_if_unused(binding: &AnyJsIdentifierBinding) -> Option<Suggested
| AnyJsBindingDeclaration::JsFunctionExpression(_) => None,

// Some parameters are ok to not be used
AnyJsBindingDeclaration::JsArrowFunctionExpression(_) => {
suggestion_for_binding(binding)
}
AnyJsBindingDeclaration::TsPropertyParameter(_) => None,
AnyJsBindingDeclaration::JsFormalParameter(parameter) => {
if is_function_that_is_ok_parameter_not_be_used(&parameter.parent_function()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ fn capture_needs_to_be_in_the_dependency_list(
}

// all others need to be in the dependency list
AnyJsBindingDeclaration::JsFormalParameter(_)
AnyJsBindingDeclaration::JsArrowFunctionExpression(_)
| AnyJsBindingDeclaration::JsFormalParameter(_)
| AnyJsBindingDeclaration::JsRestParameter(_)
| AnyJsBindingDeclaration::JsBogusParameter(_)
| AnyJsBindingDeclaration::TsIndexSignatureParameter(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,8 @@ impl Named {
AnyJsBindingDeclaration::JsVariableDeclarator(var) => {
Named::from_variable_declarator(var)
}
AnyJsBindingDeclaration::JsBogusParameter(_)
AnyJsBindingDeclaration::JsArrowFunctionExpression(_)
| AnyJsBindingDeclaration::JsBogusParameter(_)
| AnyJsBindingDeclaration::JsFormalParameter(_)
| AnyJsBindingDeclaration::JsRestParameter(_) => Some(Named::FunctionParameter),
AnyJsBindingDeclaration::JsCatchDeclaration(_) => Some(Named::CatchParameter),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = 0;
export type { a }
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalidTypeValueWithSameName.ts
---
# Input
```js
const a = 0;
export type { a }
```

# Diagnostics
```
invalidTypeValueWithSameName.ts:2:15 lint/correctness/noUndeclaredVariables ━━━━━━━━━━━━━━━━━━━━━━━━
! The a variable is undeclared
1 │ const a = 0;
> 2 │ export type { a }
│ ^
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

type a = number
export const a = 5;

function f() {}
export type f = () => {}

const b = true
type b = boolean
export { type b }
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalidTypeValueSameNames.ts
---
# Input
```js

type a = number
export const a = 5;

function f() {}
export type f = () => {}

const b = true
type b = boolean
export { type b }

```

# Diagnostics
```
invalidTypeValueSameNames.ts:2:6 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This type alias is unused.
> 2 │ type a = number
│ ^
3 │ export const a = 5;
4 │
i Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
```

```
invalidTypeValueSameNames.ts:5:10 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This function is unused.
3 │ export const a = 5;
4 │
> 5 │ function f() {}
│ ^
6 │ export type f = () => {}
7 │
i Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
```

```
invalidTypeValueSameNames.ts:8:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━
! This variable is unused.
6 │ export type f = () => {}
7 │
> 8 │ const b = true
│ ^
9 │ type b = boolean
10 │ export { type b }
i Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
i Unsafe fix: If this is intentional, prepend b with an underscore.
6 6 │ export type f = () => {}
7 7 │
8 │ - const·b·=·true
8 │ + const·_b·=·true
9 9 │ type b = boolean
10 10 │ export { type b }
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// See https://github.com/biomejs/biome/issues/104

import { X } from "mod"
export function f(X: X): X {
return X;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: issue104.ts
---
# Input
```js
// See https://github.com/biomejs/biome/issues/104

import { X } from "mod"
export function f(X: X): X {
return X;
}

```


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class C {}
enum E {}

export type { C }
export { type E }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: validValueExportType.ts
---
# Input
```js
class C {}
enum E {}

export type { C }
export { type E }
```


Loading

0 comments on commit 3df5f43

Please sign in to comment.