From b3d8bfdf1ddedeccc60028a6c05f7fbb0d0079e9 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Mon, 24 Jun 2024 22:40:12 +0200 Subject: [PATCH] fix(useConsistentArrayType): ignore `Array` in `extends`/`implements` clauses (#3280) --- CHANGELOG.md | 6 +++++ .../lint/style/use_consistent_array_type.rs | 22 ++++++++++--------- .../lint/style/use_shorthand_array_type.rs | 19 +++++++++++----- .../style/useConsistentArrayType/valid.ts | 3 +++ .../useConsistentArrayType/valid.ts.snap | 4 ++-- .../style/useShorthandArrayType/valid.ts | 3 +++ .../style/useShorthandArrayType/valid.ts.snap | 4 ++-- 7 files changed, 41 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43289ff5e6ce..f92ede2d42fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/biome_js_analyze/src/lint/style/use_consistent_array_type.rs b/crates/biome_js_analyze/src/lint/style/use_consistent_array_type.rs index 5613fcd12266..8ac6a75676f5 100644 --- a/crates/biome_js_analyze/src/lint/style/use_consistent_array_type.rs +++ b/crates/biome_js_analyze/src/lint/style/use_consistent_array_type.rs @@ -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, } diff --git a/crates/biome_js_analyze/src/lint/style/use_shorthand_array_type.rs b/crates/biome_js_analyze/src/lint/style/use_shorthand_array_type.rs index b7b9e90b84b4..685b72e87127 100644 --- a/crates/biome_js_analyze/src/lint/style/use_shorthand_array_type.rs +++ b/crates/biome_js_analyze/src/lint/style/use_shorthand_array_type.rs @@ -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; @@ -73,12 +73,19 @@ impl Rule for UseShorthandArrayType { fn run(ctx: &RuleContext) -> Option { 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::State) -> Option { diff --git a/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts b/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts index ce533268ceee..17e084f7383b 100644 --- a/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts +++ b/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts @@ -26,3 +26,6 @@ type readonlyValid7 = ReadonlyArray string>; let readonlyValid8: ReadonlyArray; type readonlyValid9 = T extends ReadonlyArray ? R : any; type readonlyValid10 = { [K in keyof T]: T[K] }; + +interface Args extends Array {} +class Args2 extends (Array) implements Array {} \ No newline at end of file diff --git a/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts.snap b/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts.snap index 67d364480ff0..800dc472fe00 100644 --- a/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/style/useConsistentArrayType/valid.ts.snap @@ -33,6 +33,6 @@ let readonlyValid8: ReadonlyArray; type readonlyValid9 = T extends ReadonlyArray ? R : any; type readonlyValid10 = { [K in keyof T]: T[K] }; +interface Args extends Array {} +class Args2 extends (Array) implements Array {} ``` - - diff --git a/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts b/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts index ce533268ceee..17e084f7383b 100644 --- a/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts +++ b/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts @@ -26,3 +26,6 @@ type readonlyValid7 = ReadonlyArray string>; let readonlyValid8: ReadonlyArray; type readonlyValid9 = T extends ReadonlyArray ? R : any; type readonlyValid10 = { [K in keyof T]: T[K] }; + +interface Args extends Array {} +class Args2 extends (Array) implements Array {} \ No newline at end of file diff --git a/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts.snap b/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts.snap index 67d364480ff0..800dc472fe00 100644 --- a/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/style/useShorthandArrayType/valid.ts.snap @@ -33,6 +33,6 @@ let readonlyValid8: ReadonlyArray; type readonlyValid9 = T extends ReadonlyArray ? R : any; type readonlyValid10 = { [K in keyof T]: T[K] }; +interface Args extends Array {} +class Args2 extends (Array) implements Array {} ``` - -