1 | /**
|
2 | * @fileoverview Enforces that a return statement is present in property getters.
|
3 | * @author Aladdin-ADD(hh_2013@foxmail.com)
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Requirements
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | const astUtils = require("./utils/ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Helpers
|
16 | //------------------------------------------------------------------------------
|
17 | const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u;
|
18 |
|
19 | /**
|
20 | * Checks a given code path segment is reachable.
|
21 | *
|
22 | * @param {CodePathSegment} segment - A segment to check.
|
23 | * @returns {boolean} `true` if the segment is reachable.
|
24 | */
|
25 | function isReachable(segment) {
|
26 | return segment.reachable;
|
27 | }
|
28 |
|
29 | /**
|
30 | * Gets a readable location.
|
31 | *
|
32 | * - FunctionExpression -> the function name or `function` keyword.
|
33 | *
|
34 | * @param {ASTNode} node - A function node to get.
|
35 | * @returns {ASTNode|Token} The node or the token of a location.
|
36 | */
|
37 | function getId(node) {
|
38 | return node.id || node;
|
39 | }
|
40 |
|
41 | //------------------------------------------------------------------------------
|
42 | // Rule Definition
|
43 | //------------------------------------------------------------------------------
|
44 |
|
45 | module.exports = {
|
46 | meta: {
|
47 | type: "problem",
|
48 |
|
49 | docs: {
|
50 | description: "enforce `return` statements in getters",
|
51 | category: "Possible Errors",
|
52 | recommended: true,
|
53 | url: "https://eslint.org/docs/rules/getter-return"
|
54 | },
|
55 |
|
56 | fixable: null,
|
57 |
|
58 | schema: [
|
59 | {
|
60 | type: "object",
|
61 | properties: {
|
62 | allowImplicit: {
|
63 | type: "boolean",
|
64 | default: false
|
65 | }
|
66 | },
|
67 | additionalProperties: false
|
68 | }
|
69 | ],
|
70 |
|
71 | messages: {
|
72 | expected: "Expected to return a value in {{name}}.",
|
73 | expectedAlways: "Expected {{name}} to always return a value."
|
74 | }
|
75 | },
|
76 |
|
77 | create(context) {
|
78 |
|
79 | const options = context.options[0] || { allowImplicit: false };
|
80 |
|
81 | let funcInfo = {
|
82 | upper: null,
|
83 | codePath: null,
|
84 | hasReturn: false,
|
85 | shouldCheck: false,
|
86 | node: null
|
87 | };
|
88 |
|
89 | /**
|
90 | * Checks whether or not the last code path segment is reachable.
|
91 | * Then reports this function if the segment is reachable.
|
92 | *
|
93 | * If the last code path segment is reachable, there are paths which are not
|
94 | * returned or thrown.
|
95 | *
|
96 | * @param {ASTNode} node - A node to check.
|
97 | * @returns {void}
|
98 | */
|
99 | function checkLastSegment(node) {
|
100 | if (funcInfo.shouldCheck &&
|
101 | funcInfo.codePath.currentSegments.some(isReachable)
|
102 | ) {
|
103 | context.report({
|
104 | node,
|
105 | loc: getId(node).loc.start,
|
106 | messageId: funcInfo.hasReturn ? "expectedAlways" : "expected",
|
107 | data: {
|
108 | name: astUtils.getFunctionNameWithKind(funcInfo.node)
|
109 | }
|
110 | });
|
111 | }
|
112 | }
|
113 |
|
114 | /**
|
115 | * Checks whether a node means a getter function.
|
116 | * @param {ASTNode} node - a node to check.
|
117 | * @returns {boolean} if node means a getter, return true; else return false.
|
118 | */
|
119 | function isGetter(node) {
|
120 | const parent = node.parent;
|
121 |
|
122 | if (TARGET_NODE_TYPE.test(node.type) && node.body.type === "BlockStatement") {
|
123 | if (parent.kind === "get") {
|
124 | return true;
|
125 | }
|
126 | if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") {
|
127 |
|
128 | // Object.defineProperty()
|
129 | if (parent.parent.parent.type === "CallExpression" &&
|
130 | astUtils.getStaticPropertyName(parent.parent.parent.callee) === "defineProperty") {
|
131 | return true;
|
132 | }
|
133 |
|
134 | // Object.defineProperties()
|
135 | if (parent.parent.parent.type === "Property" &&
|
136 | parent.parent.parent.parent.type === "ObjectExpression" &&
|
137 | parent.parent.parent.parent.parent.type === "CallExpression" &&
|
138 | astUtils.getStaticPropertyName(parent.parent.parent.parent.parent.callee) === "defineProperties") {
|
139 | return true;
|
140 | }
|
141 | }
|
142 | }
|
143 | return false;
|
144 | }
|
145 | return {
|
146 |
|
147 | // Stacks this function's information.
|
148 | onCodePathStart(codePath, node) {
|
149 | funcInfo = {
|
150 | upper: funcInfo,
|
151 | codePath,
|
152 | hasReturn: false,
|
153 | shouldCheck: isGetter(node),
|
154 | node
|
155 | };
|
156 | },
|
157 |
|
158 | // Pops this function's information.
|
159 | onCodePathEnd() {
|
160 | funcInfo = funcInfo.upper;
|
161 | },
|
162 |
|
163 | // Checks the return statement is valid.
|
164 | ReturnStatement(node) {
|
165 | if (funcInfo.shouldCheck) {
|
166 | funcInfo.hasReturn = true;
|
167 |
|
168 | // if allowImplicit: false, should also check node.argument
|
169 | if (!options.allowImplicit && !node.argument) {
|
170 | context.report({
|
171 | node,
|
172 | messageId: "expected",
|
173 | data: {
|
174 | name: astUtils.getFunctionNameWithKind(funcInfo.node)
|
175 | }
|
176 | });
|
177 | }
|
178 | }
|
179 | },
|
180 |
|
181 | // Reports a given function if the last path is reachable.
|
182 | "FunctionExpression:exit": checkLastSegment,
|
183 | "ArrowFunctionExpression:exit": checkLastSegment
|
184 | };
|
185 | }
|
186 | };
|