Enhancement: [parameter-properties] Support unnecessary-check under derived classes #10825
Open
4 tasks done
Labels
enhancement: plugin rule option
New rule option for an existing eslint-plugin rule
package: eslint-plugin
Issues related to @typescript-eslint/eslint-plugin
triage
Waiting for team members to take a look
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Link to the rule's documentation
https://typescript-eslint.io/rules/parameter-properties/
Description
TL;DR
This issue proposes the following changes to the rule
parameter-properties
:parameter-property
under the class inheritance:no-unnecessary-parameter-property-assignment
to this rule.Background
Currently, the rule
parameter-properties
strictly reports the following TS parameter property usage whenOptions.prefer
is set toparameter-property
and conditions marked in comments are met:which expects it to be refactored as the following (Though no auto-fix was provided) to prefer
parameter-property
:However, the TS feature
parameter-property
comes with an error-prone usage (duplication):A developer would not intentionally write code like this, so the rule
no-unnecessary-parameter-property-assignment
detects it.Motivation
During our feature usage analysis, we found another error-prone usage involving the
super
call (that is, the class inheritance):causing the assignment expression
this.a = a
appears twice in classFoo
and classBar
respectively (See JS code in this playground).This is not a niche proposal since a real-world case can be found in a famous repository
BabylonJS/Babylon.js
, wheredefaultViewer.ts
declares a classDefaultViewer
that extends from a parent classAbstractViewerWithTemplate
.DefaultViewer
can be traced back to classAbstractViewer
inviewer.ts
.AbstractViewer
declares a parameter property withpublic containerElement: Element
.DefaultViewer
also DECLARES a parameter property withpublic containerElement: Element
and sends it to thesuper()
call.no-unnecessary-parameter-property-assignment
detects.This case is considered a bad practice by us because, on the contrary, we also found the following best practice as demonstrated by the repository
glideapps/quicktype
, whereDart.ts
classDartRenderer
-extends->ConvenienceRenderer.ts
classConvenienceRenderer
-extends->Renderer.ts
classRenderer
.targetLanguage
is markedprotected readonly
in the parent class and not marked in the following derived classes.Proposal
Given the motivation described above, we would like to (mainly) propose the enhancement of the unnecessary-check under the class inheritance:
super
call.This check benefits a better use of the feature
parameter-property
.Optional Goal
Like the rule
no-unnecessary-parameter-property-assignment
, a developer would not intentionally write duplicated code once they determined toprefer: 'parameter-property'
. We consider violations of best practices like these should be implemented seamlessly and automatically without another rule to turn on.The base rule
parameter-properties
was discussed in 2019 and implemented in 2022, and the ruleno-unnecessary-parameter-property-assignment
was discussed in 2023 and introduced later in 2024. We have read all discussions in PRs and issues and found that merging them into one rule was not discussed previously. By the chance of enhancing the base rule, we would like to pose the discussion for a more concise and compact rule set.Fail
Pass
Additional Info
Current rules can not detect the bad practice discussed in this proposal.
The text was updated successfully, but these errors were encountered: