From 8150c8ebdd9554f0e3869e04edef66954b5dfd67 Mon Sep 17 00:00:00 2001 From: ty <62130798+togami2864@users.noreply.github.com> Date: Mon, 4 Mar 2024 22:38:07 +0900 Subject: [PATCH] fix(analyzer): allow constructor with default parameters in noUselessConstructor (#1954) --- CHANGELOG.md | 2 ++ .../complexity/no_useless_constructor.rs | 21 +++++++++++++++++++ .../complexity/noUselessConstructor/valid.ts | 6 ++++++ .../noUselessConstructor/valid.ts.snap | 6 ++++++ .../src/content/docs/internals/changelog.md | 2 ++ .../linter/rules/no-useless-constructor.md | 13 ++++++++++++ 6 files changed, 50 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c22f545033d0..3e58e19edf4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -551,6 +551,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b - Fix [#1932](https://github.com/biomejs/biome/issues/1932) Allow redeclaration of type parameters in different declarations. Contributed by @keita-hino +- Fix [#1945](https://github.com/biomejs/biome/issues/1945) Allow constructor with default parameters in `noUselessConstructor` + ### Parser #### Bug fixes diff --git a/crates/biome_js_analyze/src/analyzers/complexity/no_useless_constructor.rs b/crates/biome_js_analyze/src/analyzers/complexity/no_useless_constructor.rs index cb008a0b7bc5..95c6877d42c2 100644 --- a/crates/biome_js_analyze/src/analyzers/complexity/no_useless_constructor.rs +++ b/crates/biome_js_analyze/src/analyzers/complexity/no_useless_constructor.rs @@ -97,6 +97,19 @@ declare_rule! { /// ``` /// /// ```ts + /// class D { + /// constructor(public arg: number){} + /// } + /// + /// class F extends D { + /// // constructor with default parameters are allowed. + /// constructor(arg = 4) { + /// super(arg) + /// } + /// } + /// ``` + /// + /// ```ts /// @Decorator /// class C { /// constructor (prop: number) {} @@ -227,6 +240,14 @@ fn is_delegating_initialization( let param = param.ok()?; let arg = arg.ok()?; match (param, arg) { + ( + AnyJsConstructorParameter::AnyJsFormalParameter( + AnyJsFormalParameter::JsFormalParameter(param), + ), + _, + ) if param.initializer().is_some() => { + return Some(false); + } ( AnyJsConstructorParameter::JsRestParameter(param), AnyJsCallArgument::JsSpread(arg), diff --git a/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts b/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts index 37441ed1fb9f..01f5fb25e24e 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts +++ b/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts @@ -25,3 +25,9 @@ class G extends B { super(bar); } } + +class H extends B { + constructor(name = "default") { + super(name); + } +} diff --git a/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts.snap b/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts.snap index a415fd6a4a3b..37988ba35683 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/noUselessConstructor/valid.ts.snap @@ -32,6 +32,12 @@ class G extends B { } } +class H extends B { + constructor(name = "default") { + super(name); + } +} + ``` diff --git a/website/src/content/docs/internals/changelog.md b/website/src/content/docs/internals/changelog.md index 774e7dc7b937..1b924ff52ed1 100644 --- a/website/src/content/docs/internals/changelog.md +++ b/website/src/content/docs/internals/changelog.md @@ -557,6 +557,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b - Fix [#1932](https://github.com/biomejs/biome/issues/1932) Allow redeclaration of type parameters in different declarations. Contributed by @keita-hino +- Fix [#1945](https://github.com/biomejs/biome/issues/1945) Allow constructor with default parameters in `noUselessConstructor` + ### Parser #### Bug fixes diff --git a/website/src/content/docs/linter/rules/no-useless-constructor.md b/website/src/content/docs/linter/rules/no-useless-constructor.md index 4c49d37512ce..4f622eaa815f 100644 --- a/website/src/content/docs/linter/rules/no-useless-constructor.md +++ b/website/src/content/docs/linter/rules/no-useless-constructor.md @@ -171,6 +171,19 @@ class C { } ``` +```ts +class D { + constructor(public arg: number){} +} + +class F extends D { + // constructor with default parameters are allowed. + constructor(arg = 4) { + super(arg) + } +} +``` + ```ts @Decorator class C {