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 getESLintCoreRule_1 = require("../util/getESLintCoreRule");
|
24 | const util = __importStar(require("../util"));
|
25 | const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-loop-func');
|
26 | exports.default = util.createRule({
|
27 | name: 'no-loop-func',
|
28 | meta: {
|
29 | type: 'suggestion',
|
30 | docs: {
|
31 | description: 'Disallow function declarations that contain unsafe references inside loop statements',
|
32 | recommended: false,
|
33 | extendsBaseRule: true,
|
34 | },
|
35 | hasSuggestions: baseRule.meta.hasSuggestions,
|
36 | schema: [],
|
37 | messages: baseRule.meta.messages,
|
38 | },
|
39 | defaultOptions: [],
|
40 | create(context) {
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function checkForLoops(node) {
|
50 | const loopNode = getContainingLoopNode(node);
|
51 | if (!loopNode) {
|
52 | return;
|
53 | }
|
54 | const references = context.getScope().through;
|
55 | const unsafeRefs = references
|
56 | .filter(r => !isSafe(loopNode, r))
|
57 | .map(r => r.identifier.name);
|
58 | if (unsafeRefs.length > 0) {
|
59 | context.report({
|
60 | node,
|
61 | messageId: 'unsafeRefs',
|
62 | data: { varNames: `'${unsafeRefs.join("', '")}'` },
|
63 | });
|
64 | }
|
65 | }
|
66 | return {
|
67 | ArrowFunctionExpression: checkForLoops,
|
68 | FunctionExpression: checkForLoops,
|
69 | FunctionDeclaration: checkForLoops,
|
70 | };
|
71 | },
|
72 | });
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function getContainingLoopNode(node) {
|
83 | for (let currentNode = node; currentNode.parent; currentNode = currentNode.parent) {
|
84 | const parent = currentNode.parent;
|
85 | switch (parent.type) {
|
86 | case utils_1.AST_NODE_TYPES.WhileStatement:
|
87 | case utils_1.AST_NODE_TYPES.DoWhileStatement:
|
88 | return parent;
|
89 | case utils_1.AST_NODE_TYPES.ForStatement:
|
90 |
|
91 | if (parent.init !== currentNode) {
|
92 | return parent;
|
93 | }
|
94 | break;
|
95 | case utils_1.AST_NODE_TYPES.ForInStatement:
|
96 | case utils_1.AST_NODE_TYPES.ForOfStatement:
|
97 |
|
98 | if (parent.right !== currentNode) {
|
99 | return parent;
|
100 | }
|
101 | break;
|
102 | case utils_1.AST_NODE_TYPES.ArrowFunctionExpression:
|
103 | case utils_1.AST_NODE_TYPES.FunctionExpression:
|
104 | case utils_1.AST_NODE_TYPES.FunctionDeclaration:
|
105 |
|
106 | return null;
|
107 | default:
|
108 | break;
|
109 | }
|
110 | }
|
111 | return null;
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | function getTopLoopNode(node, excludedNode) {
|
121 | const border = excludedNode ? excludedNode.range[1] : 0;
|
122 | let retv = node;
|
123 | let containingLoopNode = node;
|
124 | while (containingLoopNode && containingLoopNode.range[0] >= border) {
|
125 | retv = containingLoopNode;
|
126 | containingLoopNode = getContainingLoopNode(containingLoopNode);
|
127 | }
|
128 | return retv;
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function isSafe(loopNode, reference) {
|
138 | var _a;
|
139 | const variable = reference.resolved;
|
140 | const definition = variable === null || variable === void 0 ? void 0 : variable.defs[0];
|
141 | const declaration = definition === null || definition === void 0 ? void 0 : definition.parent;
|
142 | const kind = (declaration === null || declaration === void 0 ? void 0 : declaration.type) === utils_1.AST_NODE_TYPES.VariableDeclaration
|
143 | ? declaration.kind
|
144 | : '';
|
145 |
|
146 |
|
147 | if (reference.isTypeReference) {
|
148 | return true;
|
149 | }
|
150 |
|
151 | if (kind === 'const') {
|
152 | return true;
|
153 | }
|
154 | |
155 |
|
156 |
|
157 |
|
158 | if (kind === 'let' &&
|
159 | declaration &&
|
160 | declaration.range[0] > loopNode.range[0] &&
|
161 | declaration.range[1] < loopNode.range[1]) {
|
162 | return true;
|
163 | }
|
164 | |
165 |
|
166 |
|
167 |
|
168 | const border = getTopLoopNode(loopNode, kind === 'let' ? declaration : null)
|
169 | .range[0];
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function isSafeReference(upperRef) {
|
183 | var _a;
|
184 | const id = upperRef.identifier;
|
185 | return (!upperRef.isWrite() ||
|
186 | (((_a = variable === null || variable === void 0 ? void 0 : variable.scope) === null || _a === void 0 ? void 0 : _a.variableScope) === upperRef.from.variableScope &&
|
187 | id.range[0] < border));
|
188 | }
|
189 | return (_a = variable === null || variable === void 0 ? void 0 : variable.references.every(isSafeReference)) !== null && _a !== void 0 ? _a : false;
|
190 | }
|
191 |
|
\ | No newline at end of file |