Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[no-useless-constructor] should not report on different constructor type #3820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
JounQin opened this issue Aug 30, 2021 · 12 comments · Fixed by #4377
Closed
3 tasks done

[no-useless-constructor] should not report on different constructor type #3820

JounQin opened this issue Aug 30, 2021 · 12 comments · Fixed by #4377
Assignees
Labels
accepting prs Go ahead, send a pull request that resolves this issue documentation Documentation ("docs") that needs adding/updating package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@JounQin
Copy link
Contributor

JounQin commented Aug 30, 2021

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/no-useless-constructor": [2]
  }
}
import { INTERNAL_SERVER_ERROR } from './constants'

import type { ApiError, ApiErrorOptions } from 'shared'

export class ResponseError extends Error implements ApiError {
  declare code: string
  declare reason: string
  declare extra: object | undefined
  declare details: unknown[] | undefined

  constructor(optionOrMsg: Partial<ApiErrorOptions> | string) {
    const options =
      typeof optionOrMsg === 'string' ? { message: optionOrMsg } : optionOrMsg
    super(options.message)
    this.code = options.code || String(INTERNAL_SERVER_ERROR)
    this.reason = options.reason || 'Internal Server Error'
    this.extra = options.extra
    this.details = options.details
  }
}

export class RedirectError extends ResponseError {
  constructor(url: string) { // error reported, unexpected, it changes the constructor type
    super(url)
  }
}

Expected Result

No error

Actual Result

error

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.29.3
@typescript-eslint/parser 4.29.3
TypeScript 4.4.2
ESLint 7.32.0
node 12.22.5
@JounQin JounQin added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 30, 2021
@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party unable to repro issues that a maintainer was not able to reproduce and removed triage Waiting for team members to take a look labels Aug 30, 2021
@bradzacher
Copy link
Member

Actual Result
error

Again, like your last issue, could you please be more specific?
What is the error? What line is it on?


I am unable to repro this against master
image

@JounQin
Copy link
Contributor Author

JounQin commented Aug 30, 2021

@bradzacher I've already added comment at RedirectError's constructor, did you scroll the snippet?

@bradzacher
Copy link
Member

Ah I apologise - I did not see the snippet had more. MacOS hides the scroll bars and the snippet ended perfectly.

This is working as expected - this lint rule does not use type information, only the AST.
And according to the AST - it's a useless constructor as all it does is pass the argument to the super.

Either use a disable comment or make the constructor not-useless

export class RedirectError extends ResponseError {
  constructor(url: string) {
    super({message: url})
  }
}

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed awaiting response Issues waiting for a reply from the OP or another party unable to repro issues that a maintainer was not able to reproduce labels Aug 30, 2021
@JounQin
Copy link
Contributor Author

JounQin commented Aug 30, 2021

@bradzacher That's OK, but again the limitation could be documented.

@bradzacher
Copy link
Member

Happy to accept a PR!
If things aren't documented it's usually because it wasn't recognised as a limitation at the time of authoring.
Then so few people run into the limitation that nobody's been motivated to PR it into the docs.

@JounQin
Copy link
Contributor Author

JounQin commented Aug 30, 2021

Can you reopen this issue and I'll raise a PR when I'm free, you can assign it to me.

@bradzacher bradzacher added the documentation Documentation ("docs") that needs adding/updating label Aug 30, 2021
@bradzacher bradzacher reopened this Aug 30, 2021
@bradzacher bradzacher assigned bradzacher and JounQin and unassigned bradzacher Aug 30, 2021
@JounQin
Copy link
Contributor Author

JounQin commented Sep 12, 2021

@bradzacher

Like #3796 (comment)

Can we ignore different constructor param names and types? Or at least an option? If you agree, I can try to raise a PR for it/them.

@bradzacher
Copy link
Member

You cannot do this because you don't have access to type information in this extension rule.
Without type information you cannot inspect the parent's constructor arguments.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 13, 2021

@bradzacher Well, can we include type information in this rule then? Just like unified-signatures.

It will be a breaking change.

@bradzacher
Copy link
Member

No.

Significant portions of our userbase does not use type-information. This means that introducing it into an existing rule hugely restricts the accessibility of the rule. It is a massive breaking change that likely locks users out of using the rule altogether.

So when we make the decision to add type information, it needs to be for a very good reason and add a lot of value.

In addition - adding it into an extension rule is something we need to make sure we're doing for the right reason. Extension rules exist to add support for TS code to the core rules (in this case - accessibility and parameter properties) - so we want them to be as accessible as possible.

Adding type information to handle this (frankly) very rare case is not worth the cost.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@JoshuaKGoldberg JoshuaKGoldberg moved this to Todo in Documentation Nov 9, 2021
@JoshuaKGoldberg
Copy link
Member

@JounQin are you still planning on sending a PR?

No worries if not, I will soon if you don't have time!

@JounQin
Copy link
Contributor Author

JounQin commented Dec 31, 2021

@JoshuaKGoldberg That would be nice! I have no time working on it temporarily.

@JoshuaKGoldberg JoshuaKGoldberg self-assigned this Dec 31, 2021
@JoshuaKGoldberg JoshuaKGoldberg moved this from Todo to In Progress in Documentation Dec 31, 2021
@JoshuaKGoldberg JoshuaKGoldberg moved this from In Progress to In Review in Documentation Dec 31, 2021
Repository owner moved this from In Review to Done in Documentation Dec 31, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue documentation Documentation ("docs") that needs adding/updating package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
No open projects
Status: Done
3 participants