@typescript-eslint/eslint-plugin 8.8.2-alpha.3 → 8.8.2-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,126 @@ exports.default = (0, util_1.createRule)({
|
|
18
18
|
},
|
19
19
|
defaultOptions: [],
|
20
20
|
create(context) {
|
21
|
+
const SKIPPED_IIFE_NODES = new Set();
|
22
|
+
/**
|
23
|
+
* Gets the containing loop node of a specified node.
|
24
|
+
*
|
25
|
+
* We don't need to check nested functions, so this ignores those.
|
26
|
+
* `Scope.through` contains references of nested functions.
|
27
|
+
*
|
28
|
+
* @param node An AST node to get.
|
29
|
+
* @returns The containing loop node of the specified node, or `null`.
|
30
|
+
*/
|
31
|
+
function getContainingLoopNode(node) {
|
32
|
+
for (let currentNode = node; currentNode.parent; currentNode = currentNode.parent) {
|
33
|
+
const parent = currentNode.parent;
|
34
|
+
switch (parent.type) {
|
35
|
+
case utils_1.AST_NODE_TYPES.WhileStatement:
|
36
|
+
case utils_1.AST_NODE_TYPES.DoWhileStatement:
|
37
|
+
return parent;
|
38
|
+
case utils_1.AST_NODE_TYPES.ForStatement:
|
39
|
+
// `init` is outside of the loop.
|
40
|
+
if (parent.init !== currentNode) {
|
41
|
+
return parent;
|
42
|
+
}
|
43
|
+
break;
|
44
|
+
case utils_1.AST_NODE_TYPES.ForInStatement:
|
45
|
+
case utils_1.AST_NODE_TYPES.ForOfStatement:
|
46
|
+
// `right` is outside of the loop.
|
47
|
+
if (parent.right !== currentNode) {
|
48
|
+
return parent;
|
49
|
+
}
|
50
|
+
break;
|
51
|
+
case utils_1.AST_NODE_TYPES.ArrowFunctionExpression:
|
52
|
+
case utils_1.AST_NODE_TYPES.FunctionExpression:
|
53
|
+
case utils_1.AST_NODE_TYPES.FunctionDeclaration:
|
54
|
+
// We don't need to check nested functions.
|
55
|
+
// We need to check nested functions only in case of IIFE.
|
56
|
+
if (SKIPPED_IIFE_NODES.has(parent)) {
|
57
|
+
break;
|
58
|
+
}
|
59
|
+
return null;
|
60
|
+
default:
|
61
|
+
break;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
return null;
|
65
|
+
}
|
66
|
+
/**
|
67
|
+
* Gets the containing loop node of a given node.
|
68
|
+
* If the loop was nested, this returns the most outer loop.
|
69
|
+
* @param node A node to get. This is a loop node.
|
70
|
+
* @param excludedNode A node that the result node should not include.
|
71
|
+
* @returns The most outer loop node.
|
72
|
+
*/
|
73
|
+
function getTopLoopNode(node, excludedNode) {
|
74
|
+
const border = excludedNode ? excludedNode.range[1] : 0;
|
75
|
+
let retv = node;
|
76
|
+
let containingLoopNode = node;
|
77
|
+
while (containingLoopNode && containingLoopNode.range[0] >= border) {
|
78
|
+
retv = containingLoopNode;
|
79
|
+
containingLoopNode = getContainingLoopNode(containingLoopNode);
|
80
|
+
}
|
81
|
+
return retv;
|
82
|
+
}
|
83
|
+
/**
|
84
|
+
* Checks whether a given reference which refers to an upper scope's variable is
|
85
|
+
* safe or not.
|
86
|
+
* @param loopNode A containing loop node.
|
87
|
+
* @param reference A reference to check.
|
88
|
+
* @returns `true` if the reference is safe or not.
|
89
|
+
*/
|
90
|
+
function isSafe(loopNode, reference) {
|
91
|
+
const variable = reference.resolved;
|
92
|
+
const definition = variable?.defs[0];
|
93
|
+
const declaration = definition?.parent;
|
94
|
+
const kind = declaration?.type === utils_1.AST_NODE_TYPES.VariableDeclaration
|
95
|
+
? declaration.kind
|
96
|
+
: '';
|
97
|
+
// type references are all safe
|
98
|
+
// this only really matters for global types that haven't been configured
|
99
|
+
if (reference.isTypeReference) {
|
100
|
+
return true;
|
101
|
+
}
|
102
|
+
// Variables which are declared by `const` is safe.
|
103
|
+
if (kind === 'const') {
|
104
|
+
return true;
|
105
|
+
}
|
106
|
+
/*
|
107
|
+
* Variables which are declared by `let` in the loop is safe.
|
108
|
+
* It's a different instance from the next loop step's.
|
109
|
+
*/
|
110
|
+
if (kind === 'let' &&
|
111
|
+
declaration &&
|
112
|
+
declaration.range[0] > loopNode.range[0] &&
|
113
|
+
declaration.range[1] < loopNode.range[1]) {
|
114
|
+
return true;
|
115
|
+
}
|
116
|
+
/*
|
117
|
+
* WriteReferences which exist after this border are unsafe because those
|
118
|
+
* can modify the variable.
|
119
|
+
*/
|
120
|
+
const border = getTopLoopNode(loopNode, kind === 'let' ? declaration : null).range[0];
|
121
|
+
/**
|
122
|
+
* Checks whether a given reference is safe or not.
|
123
|
+
* The reference is every reference of the upper scope's variable we are
|
124
|
+
* looking now.
|
125
|
+
*
|
126
|
+
* It's safe if the reference matches one of the following condition.
|
127
|
+
* - is readonly.
|
128
|
+
* - doesn't exist inside a local function and after the border.
|
129
|
+
*
|
130
|
+
* @param upperRef A reference to check.
|
131
|
+
* @returns `true` if the reference is safe.
|
132
|
+
*/
|
133
|
+
function isSafeReference(upperRef) {
|
134
|
+
const id = upperRef.identifier;
|
135
|
+
return (!upperRef.isWrite() ||
|
136
|
+
(variable?.scope.variableScope === upperRef.from.variableScope &&
|
137
|
+
id.range[0] < border));
|
138
|
+
}
|
139
|
+
return variable?.references.every(isSafeReference) ?? false;
|
140
|
+
}
|
21
141
|
/**
|
22
142
|
* Reports functions which match the following condition:
|
23
143
|
* - has a loop node in ancestors.
|
@@ -31,8 +151,19 @@ exports.default = (0, util_1.createRule)({
|
|
31
151
|
return;
|
32
152
|
}
|
33
153
|
const references = context.sourceCode.getScope(node).through;
|
154
|
+
if (!(node.async || node.generator) && isIIFE(node)) {
|
155
|
+
const isFunctionExpression = node.type === utils_1.AST_NODE_TYPES.FunctionExpression;
|
156
|
+
// Check if the function is referenced elsewhere in the code
|
157
|
+
const isFunctionReferenced = isFunctionExpression && node.id
|
158
|
+
? references.some(r => r.identifier.name === node.id?.name)
|
159
|
+
: false;
|
160
|
+
if (!isFunctionReferenced) {
|
161
|
+
SKIPPED_IIFE_NODES.add(node);
|
162
|
+
return;
|
163
|
+
}
|
164
|
+
}
|
34
165
|
const unsafeRefs = references
|
35
|
-
.filter(r => !isSafe(loopNode, r))
|
166
|
+
.filter(r => r.resolved && !isSafe(loopNode, r))
|
36
167
|
.map(r => r.identifier.name);
|
37
168
|
if (unsafeRefs.length > 0) {
|
38
169
|
context.report({
|
@@ -49,120 +180,8 @@ exports.default = (0, util_1.createRule)({
|
|
49
180
|
};
|
50
181
|
},
|
51
182
|
});
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
* We don't need to check nested functions, so this ignores those.
|
56
|
-
* `Scope.through` contains references of nested functions.
|
57
|
-
*
|
58
|
-
* @param node An AST node to get.
|
59
|
-
* @returns The containing loop node of the specified node, or `null`.
|
60
|
-
*/
|
61
|
-
function getContainingLoopNode(node) {
|
62
|
-
for (let currentNode = node; currentNode.parent; currentNode = currentNode.parent) {
|
63
|
-
const parent = currentNode.parent;
|
64
|
-
switch (parent.type) {
|
65
|
-
case utils_1.AST_NODE_TYPES.WhileStatement:
|
66
|
-
case utils_1.AST_NODE_TYPES.DoWhileStatement:
|
67
|
-
return parent;
|
68
|
-
case utils_1.AST_NODE_TYPES.ForStatement:
|
69
|
-
// `init` is outside of the loop.
|
70
|
-
if (parent.init !== currentNode) {
|
71
|
-
return parent;
|
72
|
-
}
|
73
|
-
break;
|
74
|
-
case utils_1.AST_NODE_TYPES.ForInStatement:
|
75
|
-
case utils_1.AST_NODE_TYPES.ForOfStatement:
|
76
|
-
// `right` is outside of the loop.
|
77
|
-
if (parent.right !== currentNode) {
|
78
|
-
return parent;
|
79
|
-
}
|
80
|
-
break;
|
81
|
-
case utils_1.AST_NODE_TYPES.ArrowFunctionExpression:
|
82
|
-
case utils_1.AST_NODE_TYPES.FunctionExpression:
|
83
|
-
case utils_1.AST_NODE_TYPES.FunctionDeclaration:
|
84
|
-
// We don't need to check nested functions.
|
85
|
-
return null;
|
86
|
-
default:
|
87
|
-
break;
|
88
|
-
}
|
89
|
-
}
|
90
|
-
return null;
|
91
|
-
}
|
92
|
-
/**
|
93
|
-
* Gets the containing loop node of a given node.
|
94
|
-
* If the loop was nested, this returns the most outer loop.
|
95
|
-
* @param node A node to get. This is a loop node.
|
96
|
-
* @param excludedNode A node that the result node should not include.
|
97
|
-
* @returns The most outer loop node.
|
98
|
-
*/
|
99
|
-
function getTopLoopNode(node, excludedNode) {
|
100
|
-
const border = excludedNode ? excludedNode.range[1] : 0;
|
101
|
-
let retv = node;
|
102
|
-
let containingLoopNode = node;
|
103
|
-
while (containingLoopNode && containingLoopNode.range[0] >= border) {
|
104
|
-
retv = containingLoopNode;
|
105
|
-
containingLoopNode = getContainingLoopNode(containingLoopNode);
|
106
|
-
}
|
107
|
-
return retv;
|
108
|
-
}
|
109
|
-
/**
|
110
|
-
* Checks whether a given reference which refers to an upper scope's variable is
|
111
|
-
* safe or not.
|
112
|
-
* @param loopNode A containing loop node.
|
113
|
-
* @param reference A reference to check.
|
114
|
-
* @returns `true` if the reference is safe or not.
|
115
|
-
*/
|
116
|
-
function isSafe(loopNode, reference) {
|
117
|
-
const variable = reference.resolved;
|
118
|
-
const definition = variable?.defs[0];
|
119
|
-
const declaration = definition?.parent;
|
120
|
-
const kind = declaration?.type === utils_1.AST_NODE_TYPES.VariableDeclaration
|
121
|
-
? declaration.kind
|
122
|
-
: '';
|
123
|
-
// type references are all safe
|
124
|
-
// this only really matters for global types that haven't been configured
|
125
|
-
if (reference.isTypeReference) {
|
126
|
-
return true;
|
127
|
-
}
|
128
|
-
// Variables which are declared by `const` is safe.
|
129
|
-
if (kind === 'const') {
|
130
|
-
return true;
|
131
|
-
}
|
132
|
-
/*
|
133
|
-
* Variables which are declared by `let` in the loop is safe.
|
134
|
-
* It's a different instance from the next loop step's.
|
135
|
-
*/
|
136
|
-
if (kind === 'let' &&
|
137
|
-
declaration &&
|
138
|
-
declaration.range[0] > loopNode.range[0] &&
|
139
|
-
declaration.range[1] < loopNode.range[1]) {
|
140
|
-
return true;
|
141
|
-
}
|
142
|
-
/*
|
143
|
-
* WriteReferences which exist after this border are unsafe because those
|
144
|
-
* can modify the variable.
|
145
|
-
*/
|
146
|
-
const border = getTopLoopNode(loopNode, kind === 'let' ? declaration : null)
|
147
|
-
.range[0];
|
148
|
-
/**
|
149
|
-
* Checks whether a given reference is safe or not.
|
150
|
-
* The reference is every reference of the upper scope's variable we are
|
151
|
-
* looking now.
|
152
|
-
*
|
153
|
-
* It's safe if the reference matches one of the following condition.
|
154
|
-
* - is readonly.
|
155
|
-
* - doesn't exist inside a local function and after the border.
|
156
|
-
*
|
157
|
-
* @param upperRef A reference to check.
|
158
|
-
* @returns `true` if the reference is safe.
|
159
|
-
*/
|
160
|
-
function isSafeReference(upperRef) {
|
161
|
-
const id = upperRef.identifier;
|
162
|
-
return (!upperRef.isWrite() ||
|
163
|
-
(variable?.scope.variableScope === upperRef.from.variableScope &&
|
164
|
-
id.range[0] < border));
|
165
|
-
}
|
166
|
-
return variable?.references.every(isSafeReference) ?? false;
|
183
|
+
function isIIFE(node) {
|
184
|
+
return (node.parent.type === utils_1.AST_NODE_TYPES.CallExpression &&
|
185
|
+
node.parent.callee === node);
|
167
186
|
}
|
168
187
|
//# sourceMappingURL=no-loop-func.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"no-loop-func.js","sourceRoot":"","sources":["../../src/rules/no-loop-func.ts"],"names":[],"mappings":";;AAEA,oDAA0D;AAO1D,kCAAqC;AACrC,iEAA8D;AAE9D,MAAM,QAAQ,GAAG,IAAA,qCAAiB,EAAC,cAAc,CAAC,CAAC;AAKnD,kBAAe,IAAA,iBAAU,EAAsB;IAC7C,IAAI,EAAE,cAAc;IACpB,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EACT,sFAAsF;YACxF,eAAe,EAAE,IAAI;SACtB;QACD,cAAc,EAAE,QAAQ,CAAC,IAAI,CAAC,cAAc;QAC5C,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,QAAQ;QAChC,MAAM,EAAE,EAAE;KACX;IACD,cAAc,EAAE,EAAE;IACl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tC,mBAAmB,EAAE,aAAa;YAClC,kBAAkB,EAAE,aAAa;SAClC,CAAC;IACJ,CAAC;CACF,CAAC,CAAC;AAEH,SAAS,MAAM,CACb,IAG+B;IAE/B,OAAO,CACL,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,sBAAc,CAAC,cAAc;QAClD,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,IAAI,CAC5B,CAAC;AACJ,CAAC"}
|
@@ -78,7 +78,7 @@ Note that whereas [no-unsafe-function-type](./no-unsafe-function-type.mdx) helps
|
|
78
78
|
See, for example, the following code:
|
79
79
|
|
80
80
|
```ts
|
81
|
-
function
|
81
|
+
function callUnsafe(maybeFunction: unknown): string {
|
82
82
|
if (typeof maybeFunction === 'function') {
|
83
83
|
// TypeScript allows this, but it's completely unsound.
|
84
84
|
return maybeFunction('call', 'with', 'any', 'args');
|
@@ -87,6 +87,23 @@ function unsafe(maybeFunction: unknown): string {
|
|
87
87
|
}
|
88
88
|
```
|
89
89
|
|
90
|
+
In this sort of situation, beware that there is no way to guarantee with runtime checks that a value is safe to call.
|
91
|
+
If you _really_ want to call a value whose type you don't know, your best best is to use a `try`/`catch` and suppress any TypeScript or linter errors that get in your way.
|
92
|
+
|
93
|
+
```ts
|
94
|
+
function callSafe(maybeFunction: unknown): void {
|
95
|
+
try {
|
96
|
+
// intentionally unsound type assertion
|
97
|
+
(maybeFunction as () => unknown)();
|
98
|
+
} catch (e) {
|
99
|
+
console.error(
|
100
|
+
'Function either could not be called or threw an error when called: ',
|
101
|
+
e,
|
102
|
+
);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
90
107
|
## When Not To Use It
|
91
108
|
|
92
109
|
If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.8.2-alpha.
|
3
|
+
"version": "8.8.2-alpha.5",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -60,10 +60,10 @@
|
|
60
60
|
},
|
61
61
|
"dependencies": {
|
62
62
|
"@eslint-community/regexpp": "^4.10.0",
|
63
|
-
"@typescript-eslint/scope-manager": "8.8.2-alpha.
|
64
|
-
"@typescript-eslint/type-utils": "8.8.2-alpha.
|
65
|
-
"@typescript-eslint/utils": "8.8.2-alpha.
|
66
|
-
"@typescript-eslint/visitor-keys": "8.8.2-alpha.
|
63
|
+
"@typescript-eslint/scope-manager": "8.8.2-alpha.5",
|
64
|
+
"@typescript-eslint/type-utils": "8.8.2-alpha.5",
|
65
|
+
"@typescript-eslint/utils": "8.8.2-alpha.5",
|
66
|
+
"@typescript-eslint/visitor-keys": "8.8.2-alpha.5",
|
67
67
|
"graphemer": "^1.4.0",
|
68
68
|
"ignore": "^5.3.1",
|
69
69
|
"natural-compare": "^1.4.0",
|
@@ -74,8 +74,8 @@
|
|
74
74
|
"@types/marked": "^5.0.2",
|
75
75
|
"@types/mdast": "^4.0.3",
|
76
76
|
"@types/natural-compare": "*",
|
77
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.8.2-alpha.
|
78
|
-
"@typescript-eslint/rule-tester": "8.8.2-alpha.
|
77
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.8.2-alpha.5",
|
78
|
+
"@typescript-eslint/rule-tester": "8.8.2-alpha.5",
|
79
79
|
"ajv": "^6.12.6",
|
80
80
|
"cross-env": "^7.0.3",
|
81
81
|
"cross-fetch": "*",
|