Skip to content

Commit

Permalink
fix(useConsistentArrayType): ignore Array in extends/implements
Browse files Browse the repository at this point in the history
… clauses (#3280)
  • Loading branch information
Conaclos authored Jun 24, 2024
1 parent a3c2b18 commit b3d8bfd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
- Fix [#3184](https://github.com/biomejs/biome/issues/3184) CSS formatter converts custom identifiers to lowercase. Contributed by @denbezrukov
- Fix [#3256](https://github.com/biomejs/biome/issues/3256) constant crashes when editing css files #3256. Contributed by @denbezrukov

### Linter

#### Bug fixes

- `useConsistentArrayType` and `useShorthandArrayType` now ignore `Array` in the `extends` and `implements` clauses. Fix [#3247](https://github.com/biomejs/biome/issues/3247). Contributed by @Conaclos

## v1.8.2 (2024-06-20)

### CLI
Expand Down
22 changes: 12 additions & 10 deletions crates/biome_js_analyze/src/lint/style/use_consistent_array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,25 @@ impl Rule for UseConsistentArrayType {
if options.syntax == ConsistentArrayType::Shorthand {
return None;
}
match get_array_kind_by_any_type(query) {
Some(array_kind) => transform_array_type(query.to_owned(), array_kind),
_ => None,
}
let array_kind = get_array_kind_by_any_type(query)?;
transform_array_type(query.to_owned(), array_kind)
}
AnyTsType::TsReferenceType(ty) => {
if options.syntax == ConsistentArrayType::Generic {
return None;
}

match get_array_kind_by_reference(ty) {
Some(array_kind) => {
let type_arguments = &ty.type_arguments()?;
convert_to_array_type(type_arguments, array_kind)
}
_ => None,
// Ignore `Array` in the `extends` and `implements` clauses.
let parent =
ty.syntax().ancestors().skip(1).find(|ancestor| {
ancestor.kind() != JsSyntaxKind::JS_PARENTHESIZED_EXPRESSION
});
if parent.kind() == Some(JsSyntaxKind::TS_TYPE_LIST) {
return None;
}

let array_kind = get_array_kind_by_reference(ty)?;
convert_to_array_type(&ty.type_arguments()?, array_kind)
}
_ => None,
}
Expand Down
19 changes: 13 additions & 6 deletions crates/biome_js_analyze/src/lint/style/use_shorthand_array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use biome_js_factory::make;
use biome_js_syntax::{
AnyTsType, JsSyntaxKind, JsSyntaxToken, TsReferenceType, TsTypeArguments, T,
};
use biome_rowan::{AstNode, AstSeparatedList, BatchMutationExt, TriviaPiece};
use biome_rowan::{AstNode, AstSeparatedList, BatchMutationExt, SyntaxNodeOptionExt, TriviaPiece};

use crate::JsRuleAction;

Expand Down Expand Up @@ -73,12 +73,19 @@ impl Rule for UseShorthandArrayType {

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let type_arguments = node.type_arguments()?;

match get_array_kind_by_reference(node) {
Some(array_kind) => convert_to_array_type(&type_arguments, array_kind),
None => None,
let array_kind = get_array_kind_by_reference(node)?;

// Ignore `Array` in the `extends` and `implements` clauses.
let parent = node
.syntax()
.ancestors()
.skip(1)
.find(|ancestor| ancestor.kind() != JsSyntaxKind::JS_PARENTHESIZED_EXPRESSION);
if parent.kind() == Some(JsSyntaxKind::TS_TYPE_LIST) {
return None;
}

convert_to_array_type(&node.type_arguments()?, array_kind)
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ type readonlyValid7 = ReadonlyArray<new (string, number) => string>;
let readonlyValid8: ReadonlyArray<string & number>;
type readonlyValid9<T> = T extends ReadonlyArray<infer R> ? R : any;
type readonlyValid10<T> = { [K in keyof T]: T[K] };

interface Args extends Array<string> {}
class Args2 extends (Array<string>) implements Array<string> {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ let readonlyValid8: ReadonlyArray<string & number>;
type readonlyValid9<T> = T extends ReadonlyArray<infer R> ? R : any;
type readonlyValid10<T> = { [K in keyof T]: T[K] };

interface Args extends Array<string> {}
class Args2 extends (Array<string>) implements Array<string> {}
```


Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ type readonlyValid7 = ReadonlyArray<new (string, number) => string>;
let readonlyValid8: ReadonlyArray<string & number>;
type readonlyValid9<T> = T extends ReadonlyArray<infer R> ? R : any;
type readonlyValid10<T> = { [K in keyof T]: T[K] };

interface Args extends Array<string> {}
class Args2 extends (Array<string>) implements Array<string> {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ let readonlyValid8: ReadonlyArray<string & number>;
type readonlyValid9<T> = T extends ReadonlyArray<infer R> ? R : any;
type readonlyValid10<T> = { [K in keyof T]: T[K] };

interface Args extends Array<string> {}
class Args2 extends (Array<string>) implements Array<string> {}
```


0 comments on commit b3d8bfd

Please sign in to comment.