UNPKG

7.54 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 tsutils = __importStar(require("tsutils"));
24const util = __importStar(require("../util"));
25const util_1 = require("../util");
26exports.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 // this shouldn't happen in correct code, but someone may attempt to parse bad code
62 // the parser won't error, so we shouldn't throw here
63 /* istanbul ignore next */ 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 /* istanbul ignore if */ if (!functionNode) {
70 return;
71 }
72 // function has an explicit return type, so ensure it's a safe return
73 const returnNodeType = util.getConstrainedTypeAtLocation(checker, esTreeNodeToTSNodeMap.get(returnNode));
74 const functionTSNode = esTreeNodeToTSNodeMap.get(functionNode);
75 // function expressions will not have their return type modified based on receiver typing
76 // so we have to use the contextual typing in these cases, i.e.
77 // const foo1: () => Set<string> = () => new Set<any>();
78 // the return type of the arrow function is Set<any> even though the variable is typed as Set<string>
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 // If there is an explicit type annotation *and* that type matches the actual
86 // function return type, we shouldn't complain (it's intentional, even if unsafe)
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 // Allow cases when the declared return type of the function is either unknown or unknown[]
96 // and the function is returning any or any[].
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 // `return this`
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 // If the function return type was not unknown/unknown[], mark usage as unsafeReturn.
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//# sourceMappingURL=no-unsafe-return.js.map
\No newline at end of file