UNPKG

7.5 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 getESLintCoreRule_1 = require("../util/getESLintCoreRule");
24const util = __importStar(require("../util"));
25const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-loop-func');
26exports.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 * Reports functions which match the following condition:
43 * - has a loop node in ancestors.
44 * - has any references which refers to an unsafe variable.
45 *
46 * @param node The AST node to check.
47 * @returns Whether or not the node is within a loop.
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 * Gets the containing loop node of a specified node.
75 *
76 * We don't need to check nested functions, so this ignores those.
77 * `Scope.through` contains references of nested functions.
78 *
79 * @param node An AST node to get.
80 * @returns The containing loop node of the specified node, or `null`.
81 */
82function 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 // `init` is outside of the loop.
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 // `right` is outside of the loop.
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 // We don't need to check nested functions.
106 return null;
107 default:
108 break;
109 }
110 }
111 return null;
112}
113/**
114 * Gets the containing loop node of a given node.
115 * If the loop was nested, this returns the most outer loop.
116 * @param node A node to get. This is a loop node.
117 * @param excludedNode A node that the result node should not include.
118 * @returns The most outer loop node.
119 */
120function 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 * Checks whether a given reference which refers to an upper scope's variable is
132 * safe or not.
133 * @param loopNode A containing loop node.
134 * @param reference A reference to check.
135 * @returns `true` if the reference is safe or not.
136 */
137function 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 // type references are all safe
146 // this only really matters for global types that haven't been configured
147 if (reference.isTypeReference) {
148 return true;
149 }
150 // Variables which are declared by `const` is safe.
151 if (kind === 'const') {
152 return true;
153 }
154 /*
155 * Variables which are declared by `let` in the loop is safe.
156 * It's a different instance from the next loop step's.
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 * WriteReferences which exist after this border are unsafe because those
166 * can modify the variable.
167 */
168 const border = getTopLoopNode(loopNode, kind === 'let' ? declaration : null)
169 .range[0];
170 /**
171 * Checks whether a given reference is safe or not.
172 * The reference is every reference of the upper scope's variable we are
173 * looking now.
174 *
175 * It's safe if the reference matches one of the following condition.
176 * - is readonly.
177 * - doesn't exist inside a local function and after the border.
178 *
179 * @param upperRef A reference to check.
180 * @returns `true` if the reference is safe.
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//# sourceMappingURL=no-loop-func.js.map
\No newline at end of file