UNPKG

5.59 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 ts = __importStar(require("typescript"));
24const util_1 = require("../util");
25exports.default = (0, util_1.createRule)({
26 name: 'prefer-return-this-type',
27 defaultOptions: [],
28 meta: {
29 type: 'suggestion',
30 docs: {
31 description: 'Enforce that `this` is used when only `this` type is returned',
32 recommended: false,
33 requiresTypeChecking: true,
34 },
35 messages: {
36 useThisType: 'Use `this` type instead.',
37 },
38 schema: [],
39 fixable: 'code',
40 },
41 create(context) {
42 const parserServices = (0, util_1.getParserServices)(context);
43 const checker = parserServices.program.getTypeChecker();
44 function tryGetNameInType(name, typeNode) {
45 if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeReference &&
46 typeNode.typeName.type === utils_1.AST_NODE_TYPES.Identifier &&
47 typeNode.typeName.name === name) {
48 return typeNode;
49 }
50 if (typeNode.type === utils_1.AST_NODE_TYPES.TSUnionType) {
51 for (const type of typeNode.types) {
52 const found = tryGetNameInType(name, type);
53 if (found) {
54 return found;
55 }
56 }
57 }
58 return undefined;
59 }
60 function isThisSpecifiedInParameters(originalFunc) {
61 const firstArg = originalFunc.params[0];
62 return (firstArg &&
63 firstArg.type === utils_1.AST_NODE_TYPES.Identifier &&
64 firstArg.name === 'this');
65 }
66 function isFunctionReturningThis(originalFunc, originalClass) {
67 if (isThisSpecifiedInParameters(originalFunc)) {
68 return false;
69 }
70 const func = parserServices.esTreeNodeToTSNodeMap.get(originalFunc);
71 if (!func.body) {
72 return false;
73 }
74 const classType = checker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(originalClass));
75 if (func.body.kind !== ts.SyntaxKind.Block) {
76 const type = checker.getTypeAtLocation(func.body);
77 return classType.thisType === type;
78 }
79 let hasReturnThis = false;
80 let hasReturnClassType = false;
81 (0, util_1.forEachReturnStatement)(func.body, stmt => {
82 const expr = stmt.expression;
83 if (!expr) {
84 return;
85 }
86 // fast check
87 if (expr.kind === ts.SyntaxKind.ThisKeyword) {
88 hasReturnThis = true;
89 return;
90 }
91 const type = checker.getTypeAtLocation(expr);
92 if (classType === type) {
93 hasReturnClassType = true;
94 return true;
95 }
96 if (classType.thisType === type) {
97 hasReturnThis = true;
98 return;
99 }
100 return;
101 });
102 return !hasReturnClassType && hasReturnThis;
103 }
104 function checkFunction(originalFunc, originalClass) {
105 var _a;
106 const className = (_a = originalClass.id) === null || _a === void 0 ? void 0 : _a.name;
107 if (!className || !originalFunc.returnType) {
108 return;
109 }
110 const node = tryGetNameInType(className, originalFunc.returnType.typeAnnotation);
111 if (!node) {
112 return;
113 }
114 if (isFunctionReturningThis(originalFunc, originalClass)) {
115 context.report({
116 node,
117 messageId: 'useThisType',
118 fix: fixer => fixer.replaceText(node, 'this'),
119 });
120 }
121 }
122 return {
123 'ClassBody > MethodDefinition'(node) {
124 checkFunction(node.value, node.parent.parent);
125 },
126 'ClassBody > PropertyDefinition'(node) {
127 var _a, _b;
128 if (!(((_a = node.value) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.FunctionExpression ||
129 ((_b = node.value) === null || _b === void 0 ? void 0 : _b.type) === utils_1.AST_NODE_TYPES.ArrowFunctionExpression)) {
130 return;
131 }
132 checkFunction(node.value, node.parent.parent);
133 },
134 };
135 },
136});
137//# sourceMappingURL=prefer-return-this-type.js.map
\No newline at end of file