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("../ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Helpers
|
16 | //------------------------------------------------------------------------------
|
17 | const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/;
|
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 | docs: {
|
48 | description: "enforce `return` statements in getters",
|
49 | category: "Possible Errors",
|
50 | recommended: false,
|
51 | url: "https://eslint.org/docs/rules/getter-return"
|
52 | },
|
53 | fixable: null,
|
54 | schema: [
|
55 | {
|
56 | type: "object",
|
57 | properties: {
|
58 | allowImplicit: {
|
59 | type: "boolean"
|
60 | }
|
61 | },
|
62 | additionalProperties: false
|
63 | }
|
64 | ]
|
65 | },
|
66 |
|
67 | create(context) {
|
68 |
|
69 | const options = context.options[0] || { allowImplicit: false };
|
70 |
|
71 | let funcInfo = {
|
72 | upper: null,
|
73 | codePath: null,
|
74 | hasReturn: false,
|
75 | shouldCheck: false,
|
76 | node: null
|
77 | };
|
78 |
|
79 | /**
|
80 | * Checks whether or not the last code path segment is reachable.
|
81 | * Then reports this function if the segment is reachable.
|
82 | *
|
83 | * If the last code path segment is reachable, there are paths which are not
|
84 | * returned or thrown.
|
85 | *
|
86 | * @param {ASTNode} node - A node to check.
|
87 | * @returns {void}
|
88 | */
|
89 | function checkLastSegment(node) {
|
90 | if (funcInfo.shouldCheck &&
|
91 | funcInfo.codePath.currentSegments.some(isReachable)
|
92 | ) {
|
93 | context.report({
|
94 | node,
|
95 | loc: getId(node).loc.start,
|
96 | message: funcInfo.hasReturn
|
97 | ? "Expected {{name}} to always return a value."
|
98 | : "Expected to return a value in {{name}}.",
|
99 | data: {
|
100 | name: astUtils.getFunctionNameWithKind(funcInfo.node)
|
101 | }
|
102 | });
|
103 | }
|
104 | }
|
105 |
|
106 | /**
|
107 | * Checks whether a node means a getter function.
|
108 | * @param {ASTNode} node - a node to check.
|
109 | * @returns {boolean} if node means a getter, return true; else return false.
|
110 | */
|
111 | function isGetter(node) {
|
112 | const parent = node.parent;
|
113 |
|
114 | if (TARGET_NODE_TYPE.test(node.type) && node.body.type === "BlockStatement") {
|
115 | if (parent.kind === "get") {
|
116 | return true;
|
117 | }
|
118 | if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") {
|
119 |
|
120 | // Object.defineProperty()
|
121 | if (parent.parent.parent.type === "CallExpression" &&
|
122 | astUtils.getStaticPropertyName(parent.parent.parent.callee) === "defineProperty") {
|
123 | return true;
|
124 | }
|
125 |
|
126 | // Object.defineProperties()
|
127 | if (parent.parent.parent.type === "Property" &&
|
128 | parent.parent.parent.parent.type === "ObjectExpression" &&
|
129 | parent.parent.parent.parent.parent.type === "CallExpression" &&
|
130 | astUtils.getStaticPropertyName(parent.parent.parent.parent.parent.callee) === "defineProperties") {
|
131 | return true;
|
132 | }
|
133 | }
|
134 | }
|
135 | return false;
|
136 | }
|
137 | return {
|
138 |
|
139 | // Stacks this function's information.
|
140 | onCodePathStart(codePath, node) {
|
141 | funcInfo = {
|
142 | upper: funcInfo,
|
143 | codePath,
|
144 | hasReturn: false,
|
145 | shouldCheck: isGetter(node),
|
146 | node
|
147 | };
|
148 | },
|
149 |
|
150 | // Pops this function's information.
|
151 | onCodePathEnd() {
|
152 | funcInfo = funcInfo.upper;
|
153 | },
|
154 |
|
155 | // Checks the return statement is valid.
|
156 | ReturnStatement(node) {
|
157 | if (funcInfo.shouldCheck) {
|
158 | funcInfo.hasReturn = true;
|
159 |
|
160 | // if allowImplicit: false, should also check node.argument
|
161 | if (!options.allowImplicit && !node.argument) {
|
162 | context.report({
|
163 | node,
|
164 | message: "Expected to return a value in {{name}}.",
|
165 | data: {
|
166 | name: astUtils.getFunctionNameWithKind(funcInfo.node)
|
167 | }
|
168 | });
|
169 | }
|
170 | }
|
171 | },
|
172 |
|
173 | // Reports a given function if the last path is reachable.
|
174 | "FunctionExpression:exit": checkLastSegment,
|
175 | "ArrowFunctionExpression:exit": checkLastSegment
|
176 | };
|
177 | }
|
178 | };
|