question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: no-constructor-return

See original GitHub issue

Please describe what the rule should do:

Forbid return statements in constructors.

What category of rule is this? (place an “X” next to just one item)

[x] Suggests an alternate way of doing something (suggestion)

Provide 2-3 code examples that this rule will warn about:

class Foo {
  constructor () {
    return Math;
  }
}

class Goo {
  constructor () {
    return this;
  }
}

Why should this rule be included in ESLint (instead of a plugin)?

In Javascript return value in constructor is allowed, and the return value is the evaluated value when the class is new-ed. A class created with pattern behaves very differently from a normal class in the OOP paradigm, for example, in that every new instance should be a separate object. There seemed to be no practical usage of this pattern. Forbidding this pattern prevents mistake resulting from unfamiliarity with the language or copy-paste error.

Are you willing to submit a pull request to implement this rule?

Possibly.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Nov 6, 2019

ah, didn’t realize #12529 existed.

I’d call them legacy constructors.

1reaction
ljharbcommented, Oct 25, 2019

Would it be valuable to add an option, so that any named non-arrow non-generator non-async function also was warned by this rule? (it’d probably want to ignore any return new in that case)

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Proposed Rules
SEC Proposed Rules · Regulation NMS: Minimum Pricing Increments, Access Fees, and Transparency of Better Priced Orders · File No: S7-30-22 · Comments...
Read more >
Notice of Filing of a Proposed Rule Change To Adopt FINRA ...
Proposed new Rule 6470, entitled “Disclosure of Order Routing Information for OTC Equity Securities,” would require the publication of order ...
Read more >
ESLint equivalents in Elm - Elmcraft
228/263 (87%) of the core ESLint rules aren't necessary in Elm: ... no-constructor-return ... This is a proposal for elm-format .
Read more >
eslint | Yarn - Package Manager
Website | Configuring | Rules | Contributing | Reporting Bugs | Code of Conduct | Twitter | Mailing List | Chat Room. ESLint...
Read more >
Parsing error: Cannot find module '@babel/preset-react'
... "rules": { "prettier/prettier": "error", "consistent-return": "warn", ... "no-constructor-return": "error", "no-else-return": "error", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found