1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = {
|
13 | meta: {
|
14 | type: "problem",
|
15 |
|
16 | docs: {
|
17 | description: "disallow returning value from constructor",
|
18 | category: "Best Practices",
|
19 | recommended: false,
|
20 | url: "https://eslint.org/docs/rules/no-constructor-return"
|
21 | },
|
22 |
|
23 | schema: {},
|
24 |
|
25 | fixable: null,
|
26 |
|
27 | messages: {
|
28 | unexpected: "Unexpected return statement in constructor."
|
29 | }
|
30 | },
|
31 |
|
32 | create(context) {
|
33 | const stack = [];
|
34 |
|
35 | return {
|
36 | onCodePathStart(_, node) {
|
37 | stack.push(node);
|
38 | },
|
39 | onCodePathEnd() {
|
40 | stack.pop();
|
41 | },
|
42 | ReturnStatement(node) {
|
43 | const last = stack[stack.length - 1];
|
44 |
|
45 | if (!last.parent) {
|
46 | return;
|
47 | }
|
48 |
|
49 | if (
|
50 | last.parent.type === "MethodDefinition" &&
|
51 | last.parent.kind === "constructor" &&
|
52 | (node.parent.parent === last || node.argument)
|
53 | ) {
|
54 | context.report({
|
55 | node,
|
56 | messageId: "unexpected"
|
57 | });
|
58 | }
|
59 | }
|
60 | };
|
61 | }
|
62 | };
|