UNPKG

6.57 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const util = __importStar(require("../util"));
24const explicitReturnTypeUtils_1 = require("../util/explicitReturnTypeUtils");
25exports.default = util.createRule({
26 name: 'explicit-function-return-type',
27 meta: {
28 type: 'problem',
29 docs: {
30 description: 'Require explicit return types on functions and class methods',
31 recommended: false,
32 },
33 messages: {
34 missingReturnType: 'Missing return type on function.',
35 },
36 schema: [
37 {
38 type: 'object',
39 properties: {
40 allowExpressions: {
41 type: 'boolean',
42 },
43 allowTypedFunctionExpressions: {
44 type: 'boolean',
45 },
46 allowHigherOrderFunctions: {
47 type: 'boolean',
48 },
49 allowDirectConstAssertionInArrowFunctions: {
50 type: 'boolean',
51 },
52 allowConciseArrowFunctionExpressionsStartingWithVoid: {
53 type: 'boolean',
54 },
55 allowedNames: {
56 type: 'array',
57 items: {
58 type: 'string',
59 },
60 },
61 },
62 additionalProperties: false,
63 },
64 ],
65 },
66 defaultOptions: [
67 {
68 allowExpressions: false,
69 allowTypedFunctionExpressions: true,
70 allowHigherOrderFunctions: true,
71 allowDirectConstAssertionInArrowFunctions: true,
72 allowConciseArrowFunctionExpressionsStartingWithVoid: false,
73 allowedNames: [],
74 },
75 ],
76 create(context, [options]) {
77 const sourceCode = context.getSourceCode();
78 function isAllowedName(node) {
79 var _a;
80 if (!options.allowedNames || !options.allowedNames.length) {
81 return false;
82 }
83 if (node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
84 node.type === utils_1.AST_NODE_TYPES.FunctionExpression) {
85 const parent = node.parent;
86 let funcName;
87 if ((_a = node.id) === null || _a === void 0 ? void 0 : _a.name) {
88 funcName = node.id.name;
89 }
90 else if (parent) {
91 switch (parent.type) {
92 case utils_1.AST_NODE_TYPES.VariableDeclarator: {
93 if (parent.id.type === utils_1.AST_NODE_TYPES.Identifier) {
94 funcName = parent.id.name;
95 }
96 break;
97 }
98 case utils_1.AST_NODE_TYPES.MethodDefinition:
99 case utils_1.AST_NODE_TYPES.PropertyDefinition:
100 case utils_1.AST_NODE_TYPES.Property: {
101 if (parent.key.type === utils_1.AST_NODE_TYPES.Identifier &&
102 parent.computed === false) {
103 funcName = parent.key.name;
104 }
105 break;
106 }
107 }
108 }
109 if (!!funcName && !!options.allowedNames.includes(funcName)) {
110 return true;
111 }
112 }
113 if (node.type === utils_1.AST_NODE_TYPES.FunctionDeclaration &&
114 node.id &&
115 node.id.type === utils_1.AST_NODE_TYPES.Identifier &&
116 !!options.allowedNames.includes(node.id.name)) {
117 return true;
118 }
119 return false;
120 }
121 return {
122 'ArrowFunctionExpression, FunctionExpression'(node) {
123 if (options.allowConciseArrowFunctionExpressionsStartingWithVoid &&
124 node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression &&
125 node.expression &&
126 node.body.type === utils_1.AST_NODE_TYPES.UnaryExpression &&
127 node.body.operator === 'void') {
128 return;
129 }
130 if (isAllowedName(node)) {
131 return;
132 }
133 if (options.allowTypedFunctionExpressions &&
134 ((0, explicitReturnTypeUtils_1.isValidFunctionExpressionReturnType)(node, options) ||
135 (0, explicitReturnTypeUtils_1.ancestorHasReturnType)(node))) {
136 return;
137 }
138 (0, explicitReturnTypeUtils_1.checkFunctionReturnType)(node, options, sourceCode, loc => context.report({
139 node,
140 loc,
141 messageId: 'missingReturnType',
142 }));
143 },
144 FunctionDeclaration(node) {
145 if (isAllowedName(node)) {
146 return;
147 }
148 if (options.allowTypedFunctionExpressions && node.returnType) {
149 return;
150 }
151 (0, explicitReturnTypeUtils_1.checkFunctionReturnType)(node, options, sourceCode, loc => context.report({
152 node,
153 loc,
154 messageId: 'missingReturnType',
155 }));
156 },
157 };
158 },
159});
160//# sourceMappingURL=explicit-function-return-type.js.map
\No newline at end of file