]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-constructor-return.js
import 8.4.0 source
[pve-eslint.git] / eslint / lib / rules / no-constructor-return.js
1 /**
2 * @fileoverview Rule to disallow returning value from constructor.
3 * @author Pig Fang <https://github.com/g-plane>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 /** @type {import('../shared/types').Rule} */
13 module.exports = {
14 meta: {
15 type: "problem",
16
17 docs: {
18 description: "disallow returning value from constructor",
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 };