Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: new rule no-constructor-return (fixes #12481)
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Disallow returning value in constructor (no-constructor-return) | ||
|
||
In JavaScript returning value in constructor is allowed, however this looks meaningless. Forbidding this pattern prevents mistake resulting from unfamiliarity with the language or copy-paste error. | ||
|
||
Note that returning nothing with flow control is allowed. | ||
|
||
## Rule Details | ||
|
||
This rule disallows return statement in constructor of a class. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-constructor-return: "error"*/ | ||
|
||
class A { | ||
constructor() { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
return 'a'; | ||
} | ||
} | ||
|
||
class B { | ||
constructor(f) { | ||
if (!f) { | ||
return 'falsy'; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-constructor-return: "error"*/ | ||
|
||
class C { | ||
constructor() { } | ||
This comment has been minimized.
Sorry, something went wrong.
kaicataldo
Member
|
||
} | ||
|
||
class D { | ||
constructor(f) { | ||
if (!f) { | ||
return; // Flow control. | ||
} | ||
|
||
f(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @fileoverview Rule to disallow returning value from constructor. | ||
* @author Pig Fang <https://github.com/g-plane> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
|
||
docs: { | ||
description: "disallow returning value from constructor", | ||
category: "Best Practices", | ||
recommended: false, | ||
url: "https://eslint.org/docs/rules/no-constructor-return" | ||
}, | ||
|
||
schema: {}, | ||
|
||
fixable: "code", | ||
|
||
messages: { | ||
unexpected: "Unexpected return statement in constructor." | ||
} | ||
}, | ||
|
||
create(context) { | ||
return { | ||
"MethodDefinition[kind='constructor'] ReturnStatement"(node) { | ||
const ancestors = context.getAncestors(); | ||
const ancestor = ancestors[ancestors.length - 3]; | ||
|
||
if ( | ||
ancestor && | ||
ancestor.type === "MethodDefinition" && | ||
ancestor.kind === "constructor" || | ||
node.argument | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "unexpected" | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @fileoverview Tests for no-constructor-return rule. | ||
* @author Pig Fang <https://github.com/g-plane> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-constructor-return"), | ||
{ RuleTester } = require("../../../lib/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); | ||
|
||
const errors = [{ type: "ReturnStatement", messageId: "unexpected" }]; | ||
|
||
ruleTester.run("no-constructor-return", rule, { | ||
valid: [ | ||
"class C { }", | ||
"class C { constructor() {} }", | ||
"class C { constructor() { let v } }", | ||
"class C { method() { return '' } }", | ||
"class C { get value() { return '' } }", | ||
"class C { constructor(a) { if (!a) { return } else { a() } } }" | ||
], | ||
invalid: [ | ||
{ | ||
code: "class C { constructor() { return } }", | ||
errors | ||
}, | ||
{ | ||
code: "class C { constructor() { return '' } }", | ||
errors | ||
}, | ||
{ | ||
code: "class C { constructor(a) { if (!a) { return '' } else { a() } } }", | ||
errors | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I think this example would be clearer if the
constructor
had statements in the body. Something like: