1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const utils_1 = require("@typescript-eslint/utils");
|
23 | const tsutils = __importStar(require("tsutils"));
|
24 | const util = __importStar(require("../util"));
|
25 | const util_1 = require("../util");
|
26 | exports.default = util.createRule({
|
27 | name: 'no-unsafe-return',
|
28 | meta: {
|
29 | type: 'problem',
|
30 | docs: {
|
31 | description: 'Disallows returning any from a function',
|
32 | recommended: 'error',
|
33 | requiresTypeChecking: true,
|
34 | },
|
35 | messages: {
|
36 | unsafeReturn: 'Unsafe return of an `{{type}}` typed value.',
|
37 | unsafeReturnThis: [
|
38 | 'Unsafe return of an `{{type}}` typed value. `this` is typed as `any`.',
|
39 | 'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
|
40 | ].join('\n'),
|
41 | unsafeReturnAssignment: 'Unsafe return of type `{{sender}}` from function with return type `{{receiver}}`.',
|
42 | },
|
43 | schema: [],
|
44 | },
|
45 | defaultOptions: [],
|
46 | create(context) {
|
47 | const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context);
|
48 | const checker = program.getTypeChecker();
|
49 | const compilerOptions = program.getCompilerOptions();
|
50 | const isNoImplicitThis = tsutils.isStrictCompilerOptionEnabled(compilerOptions, 'noImplicitThis');
|
51 | function getParentFunctionNode(node) {
|
52 | let current = node.parent;
|
53 | while (current) {
|
54 | if (current.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
55 | current.type === utils_1.AST_NODE_TYPES.FunctionDeclaration ||
|
56 | current.type === utils_1.AST_NODE_TYPES.FunctionExpression) {
|
57 | return current;
|
58 | }
|
59 | current = current.parent;
|
60 | }
|
61 |
|
62 |
|
63 | return null;
|
64 | }
|
65 | function checkReturn(returnNode, reportingNode = returnNode) {
|
66 | const tsNode = esTreeNodeToTSNodeMap.get(returnNode);
|
67 | const anyType = util.isAnyOrAnyArrayTypeDiscriminated(tsNode, checker);
|
68 | const functionNode = getParentFunctionNode(returnNode);
|
69 | if (!functionNode) {
|
70 | return;
|
71 | }
|
72 |
|
73 | const returnNodeType = util.getConstrainedTypeAtLocation(checker, esTreeNodeToTSNodeMap.get(returnNode));
|
74 | const functionTSNode = esTreeNodeToTSNodeMap.get(functionNode);
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | let functionType = tsutils.isExpression(functionTSNode)
|
80 | ? util.getContextualType(checker, functionTSNode)
|
81 | : checker.getTypeAtLocation(functionTSNode);
|
82 | if (!functionType) {
|
83 | functionType = checker.getTypeAtLocation(functionTSNode);
|
84 | }
|
85 |
|
86 |
|
87 | if (functionTSNode.type) {
|
88 | for (const signature of functionType.getCallSignatures()) {
|
89 | if (returnNodeType === signature.getReturnType()) {
|
90 | return;
|
91 | }
|
92 | }
|
93 | }
|
94 | if (anyType !== util.AnyType.Safe) {
|
95 |
|
96 |
|
97 | for (const signature of functionType.getCallSignatures()) {
|
98 | const functionReturnType = signature.getReturnType();
|
99 | if (anyType === util.AnyType.Any &&
|
100 | util.isTypeUnknownType(functionReturnType)) {
|
101 | return;
|
102 | }
|
103 | if (anyType === util.AnyType.AnyArray &&
|
104 | util.isTypeUnknownArrayType(functionReturnType, checker)) {
|
105 | return;
|
106 | }
|
107 | }
|
108 | let messageId = 'unsafeReturn';
|
109 | if (!isNoImplicitThis) {
|
110 |
|
111 | const thisExpression = (0, util_1.getThisExpression)(returnNode);
|
112 | if (thisExpression &&
|
113 | util.isTypeAnyType(util.getConstrainedTypeAtLocation(checker, esTreeNodeToTSNodeMap.get(thisExpression)))) {
|
114 | messageId = 'unsafeReturnThis';
|
115 | }
|
116 | }
|
117 |
|
118 | return context.report({
|
119 | node: reportingNode,
|
120 | messageId,
|
121 | data: {
|
122 | type: anyType === util.AnyType.Any ? 'any' : 'any[]',
|
123 | },
|
124 | });
|
125 | }
|
126 | for (const signature of functionType.getCallSignatures()) {
|
127 | const functionReturnType = signature.getReturnType();
|
128 | const result = util.isUnsafeAssignment(returnNodeType, functionReturnType, checker, returnNode);
|
129 | if (!result) {
|
130 | return;
|
131 | }
|
132 | const { sender, receiver } = result;
|
133 | return context.report({
|
134 | node: reportingNode,
|
135 | messageId: 'unsafeReturnAssignment',
|
136 | data: {
|
137 | sender: checker.typeToString(sender),
|
138 | receiver: checker.typeToString(receiver),
|
139 | },
|
140 | });
|
141 | }
|
142 | }
|
143 | return {
|
144 | ReturnStatement(node) {
|
145 | const argument = node.argument;
|
146 | if (!argument) {
|
147 | return;
|
148 | }
|
149 | checkReturn(argument, node);
|
150 | },
|
151 | 'ArrowFunctionExpression > :not(BlockStatement).body': checkReturn,
|
152 | };
|
153 | },
|
154 | });
|
155 |
|
\ | No newline at end of file |