Skip to content

[no-useless-constructor] / [no-empty-function] with mixed JS & TS codebase #48

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
scottohara opened this issue Dec 21, 2017 · 13 comments
Closed
Assignees
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@scottohara
Copy link
Contributor

(This issue was originally raised in eslint/typescript-eslint-parser#418; and it was suggested that it would be more appropriately logged here, as it is not something that the parser can handle)

What version of TypeScript are you using?
2.6.2

What version of typescript-eslint-parser are you using?
9.0.0

What code were you trying to parse?

export default class Foo {
  constructor(private name: string) {}

  get greeting() : string {
    return `Hello ${this.name}`;
  }
}

What did you expect to happen?
No errors/warnings about empty constructor.

What happened?

  2:2   error  Useless constructor           no-useless-constructor
  2:36  error  Unexpected empty constructor  no-empty-function

For now, I have turned off these two rules in my .ts projects; but for projects that mix *.ts and *.js code, it would be nice to be able to have these rules enabled to catch any useless constructors/empty functions in *.js code without it also warning about valid *.ts constructors.

@macklinu
Copy link
Contributor

I'm curious what the proper solution is here. Should these rules be implemented in this plugin to support TypeScript's constructor syntax, since constructor(private name: string) {} is the equivalent of:

class Foo {
  private name: string
  constructor(name: string) {
    this.name = name
  }
}

I'd like to help out, but I'm not sure I understand what the next steps should be.

@corbinu
Copy link
Contributor

corbinu commented Mar 11, 2018

@macklinu

Yes the eslint rule should be forked into this plugin and fixed to understand TypeScript just like https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/no-unused-vars.md

@macklinu
Copy link
Contributor

Cool, thanks for that info, @corbinu!

@corbinu
Copy link
Contributor

corbinu commented Mar 11, 2018

Really glad your helping out!

@bradzacher bradzacher changed the title no-useless-constructor / no-empty-function with mixed JS & TS codebase [no-useless-constructor] / [no-empty-function] with mixed JS & TS codebase Nov 16, 2018
@JamesHenry JamesHenry transferred this issue from bradzacher/eslint-plugin-typescript Jan 18, 2019
@bradzacher bradzacher added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin labels Jan 18, 2019
@armano2 armano2 self-assigned this Jan 31, 2019
@JamesHenry
Copy link
Member

Please try out 1.2.0 and see if it helps - it's possible that no-empty-function is not yet taken care of, but if that's the case please open a dedicate issue. Many thanks!

@armano2
Copy link
Collaborator

armano2 commented Feb 1, 2019

this issue is still valid for no-empty-function

  2:36  error  Unexpected empty constructor  no-empty-function

is still going to error

@scottohara
Copy link
Contributor Author

The new @typescript-eslint/no-useless-constructor rule works as described, and no longer warns for code as per the original issue description. (no-empty-function still warns, as expected).

Unfortunately the new rule won't help in mixed JS & TS codebases, as per the issue title.

I was hoping that the no-useless-constructor rule from eslint could remain enabled (to catch useless constructors in vanilla JS code), and that the new @typescript-eslint/no-useless-constructor rule would simply 'undo' any false positives in TS code that had been flagged by the core rule.

(I'm guessing that this simply isn't possible, and that once an error has been flagged by a rule, there's no way for that error to be 'unflagged'?)

A similar issue exists with a number of other @typescript-eslint/* rules that wrap an existing eslint rule (e.g. indent is another like this), where the core rule in eslint has to be disabled to prevent false positives.

I appreciate that mixed JS & TS codebases may not be the primary use case for most, but having to disable the older rules means developers working in mixed codebases (or are still in the process of migrating from JS to TS) have to choose which language they want these rules to apply to, and not both.

@armano2 armano2 reopened this Feb 4, 2019
@armano2
Copy link
Collaborator

armano2 commented Feb 4, 2019

@scottohara can you give me example of issue with @typescript-eslint/no-useless-constructor for js file?

@scottohara
Copy link
Contributor Author

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"]
  "rules": {
    "no-useless-constructor": "error",
    "@typescript-eslint/no-useless-constructor": "error"
  }
}
// no-useless-constructor.js

class Foo {
  constructor() {}
}
// no-useless-constructor.ts

class Foo {
  constructor(private name: string) {}
}
no-useless-constructor.js
  5:3  error  Useless constructor  no-useless-constructor
  5:3  error  Useless constructor  @typescript-eslint/no-useless-constructor

no-useless-constructor.ts
  5:3  error  Useless constructor  no-useless-constructor

The expectation here is that the constructor in the *.js file is useless (and should be warned about; but the one in the *.ts file is not useless. Therefore, there should be 1 error in total.

With both rules enabled, we get 3 errors:

  • 2 x errors for the JS file
  • 1 x error for the TS file

@armano2
Copy link
Collaborator

armano2 commented Feb 4, 2019

we are not able to augment how eslint rules are working, you should use only @typescript-eslint/no-useless-constructor -> this rule works for js and ts files

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/tests/lib/rules/no-useless-constructor.js

@JamesHenry
Copy link
Member

So I believe closing this was correct, and that a new focused issue should be created for no-empty-function?

@armano2
Copy link
Collaborator

armano2 commented Feb 5, 2019

i didn't see any issues for no-empty-function and i didn't wanted to forget about this one, if you feel its better to close it, i'm ok with that

@bradzacher
Copy link
Member

Spawned off into #426 so it's clearly tracked

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

6 participants