Rule proposal: no-constructor-return
See original GitHub issuePlease 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:
- Created 4 years ago
- Reactions:3
- Comments:6 (6 by maintainers)
Top GitHub Comments
ah, didn’t realize #12529 existed.
I’d call them legacy constructors.
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)