loadwise 0.5.1 → 0.6.0
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.
- package/README.md +542 -627
- package/bin/loadwise.js +4 -0
- package/lib/accessibility/frontend-detect.js +2 -1
- package/lib/accessibility/rule-ids.js +12 -9
- package/{editor/accessibility-examples.cjs → lib/editor/accessibility-examples.js} +20 -21
- package/lib/editor/actions.js +117 -0
- package/{editor/apply-fix.cjs → lib/editor/apply-fix.js} +1 -6
- package/{editor/control-flow-comparison.cjs → lib/editor/control-flow-comparison.js} +1 -3
- package/lib/editor/diagnostics.js +365 -0
- package/lib/editor/dismissals.js +111 -0
- package/lib/editor/editor-document.js +99 -0
- package/{editor/finding-comparison.cjs → lib/editor/finding-comparison.js} +4 -7
- package/lib/editor/fix-edits.js +200 -0
- package/lib/editor/language-server.js +437 -0
- package/{editor/maintainability-examples.cjs → lib/editor/maintainability-examples.js} +1 -3
- package/{editor/perf-site-comparison.cjs → lib/editor/perf-site-comparison.js} +1 -3
- package/lib/editor/perf-watch.js +78 -0
- package/{editor/performance-examples.cjs → lib/editor/performance-examples.js} +129 -128
- package/{editor/reliability-examples.cjs → lib/editor/reliability-examples.js} +100 -41
- package/lib/editor/scan-session.js +334 -0
- package/{editor/security-examples.cjs → lib/editor/security-examples.js} +28 -32
- package/lib/editor/server-commands.js +232 -0
- package/{editor/source-example.cjs → lib/editor/source-example.js} +3 -5
- package/{editor/standalone-performance-examples.cjs → lib/editor/standalone-performance-examples.js} +349 -333
- package/lib/editor/types/dismissal.js +1 -0
- package/lib/editor/types/fix-example.js +1 -0
- package/lib/editor/types/source-document.js +1 -0
- package/lib/editor/workspace-files.js +78 -0
- package/lib/editor/workspace-paths.js +22 -0
- package/lib/maintainability/analyze-source.js +2 -0
- package/lib/maintainability/apply-fix.js +1 -1
- package/lib/maintainability/cli.js +1 -1
- package/lib/maintainability/examples.js +58 -41
- package/lib/maintainability/rule-ids.js +29 -14
- package/lib/maintainability/scan-changed.js +3 -0
- package/lib/maintainability/scan.js +13 -9
- package/lib/maintainability/tsconfig-strict.js +69 -0
- package/lib/maintainability/type-safety-hygiene.js +204 -0
- package/lib/performance/analyze.js +2 -0
- package/lib/performance/cli-arguments.js +0 -2
- package/lib/performance/cli-policy.js +17 -9
- package/lib/performance/cli-scan.js +10 -8
- package/lib/performance/cli.js +4 -23
- package/lib/performance/editor-signal.js +1 -1
- package/lib/performance/finding-text.js +3 -3
- package/lib/performance/fix-cli.js +1 -1
- package/lib/performance/frontend-perf.js +7 -0
- package/lib/performance/frontend-render-perf.js +200 -0
- package/lib/performance/llm-max-tokens.js +56 -0
- package/lib/performance/native.js +46 -26
- package/lib/performance/polyglot.js +22 -1
- package/lib/performance/pooling.js +38 -23
- package/lib/performance/projects.js +42 -21
- package/lib/performance/rule-ids.js +236 -222
- package/lib/performance/site-fixes/database.js +3 -1
- package/lib/performance/site-fixes/integrity.js +15 -1
- package/lib/performance/source-inputs.js +18 -6
- package/lib/performance/unscanned-javascript.js +66 -19
- package/lib/reliability/analyze.js +12 -0
- package/lib/reliability/child-process.js +106 -0
- package/lib/reliability/error-values.js +177 -0
- package/lib/reliability/pool-timeout.js +79 -0
- package/lib/reliability/react-bugs.js +602 -0
- package/lib/reliability/resource-leaks.js +3 -6
- package/lib/reliability/rule-ids.js +169 -127
- package/lib/reliability/switch-exhaustive.js +284 -0
- package/lib/reliability/uncaught-exception.js +223 -0
- package/lib/security/auth-crypto/rule-ids.js +3 -2
- package/lib/security/cli.js +6 -5
- package/lib/security/deps/rule-ids.js +1 -0
- package/lib/security/examples/auth-crypto.cjs +5 -4
- package/lib/security/examples/deps.cjs +1 -0
- package/lib/security/examples/exposure.cjs +55 -42
- package/lib/security/examples/files.cjs +1 -0
- package/lib/security/examples/injection.cjs +1 -0
- package/lib/security/examples/moved.cjs +5 -4
- package/lib/security/examples/request-forgery.cjs +1 -0
- package/lib/security/exposure/client-env.js +113 -0
- package/lib/security/exposure/frontend-security.js +12 -10
- package/lib/security/exposure/index.js +14 -3
- package/lib/security/exposure/jsx-url.js +267 -0
- package/lib/security/exposure/next-image-config.js +34 -0
- package/lib/security/exposure/package-manifest.js +45 -0
- package/lib/security/exposure/react-version.js +45 -0
- package/lib/security/exposure/rule-ids.js +16 -12
- package/lib/security/files/index.js +1 -1
- package/lib/security/files/rule-ids.js +1 -0
- package/lib/security/injection/code.js +24 -10
- package/lib/security/injection/command.js +81 -28
- package/lib/security/injection/nosql.js +109 -22
- package/lib/security/injection/redos.js +152 -308
- package/lib/security/injection/regex-pattern.js +290 -0
- package/lib/security/injection/regex.js +30 -11
- package/lib/security/injection/rule-ids.js +2 -1
- package/lib/security/injection/shared.js +19 -1
- package/lib/security/injection/sql.js +83 -2
- package/lib/security/request-forgery/rule-ids.js +1 -0
- package/lib/security/rule-descriptions.js +36 -32
- package/lib/security/rule-ids.js +74 -67
- package/lib/security/taint/filename-guard.js +3 -1
- package/lib/security/taint/guards.js +84 -10
- package/lib/security/taint/index.js +124 -13
- package/lib/security/taint/propagate.js +10 -7
- package/lib/security/taint/sanitizers.js +7 -3
- package/lib/security/taint/sources.js +6 -3
- package/lib/security/taint/summaries.js +13 -1
- package/lib/security/taint/url-parts.js +14 -5
- package/lib/shared/agent-session.js +2 -2
- package/lib/shared/check.js +9 -6
- package/lib/shared/explain.js +5 -5
- package/lib/shared/fix-convergence.js +1 -1
- package/lib/shared/formats/finding-location.js +6 -2
- package/lib/shared/formats/sarif.js +3 -3
- package/lib/shared/heap-limit.js +6 -0
- package/lib/shared/pillars.js +7 -7
- package/lib/shared/rule-docs.js +12 -0
- package/lib/shared/scan-cache.js +0 -1
- package/lib/shared/workspaces.js +31 -9
- package/package.json +9 -16
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/schema/loadwise.config.schema.json +44 -0
- package/editor/README.md +0 -47
- package/examples/redis-keys.otlp.json +0 -22
- package/lib/maintainability/notify-editor.js +0 -1
- package/lib/performance/editor-report.js +0 -191
- package/lib/performance/editor-setup.js +0 -189
- package/lib/performance/editor-watch-supervisor.js +0 -104
- package/lib/performance/editor-watch.js +0 -193
- package/lib/performance/notify-editor.js +0 -1
- package/lib/performance/recursive-watch.js +0 -181
- /package/lib/{performance/types/editor-report.js → editor/types/comparison.js} +0 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
// React and Next.js bugs that break an app at runtime without a build error: an effect that re-renders forever, an
|
|
2
|
+
// async effect callback, state mutated in place, list items without a key, and server-only environment variables read
|
|
3
|
+
// in a client component. Hooks are recognised by their import from react, never by name alone.
|
|
4
|
+
import { ts } from '../performance/compiler.js';
|
|
5
|
+
import { REACT_EFFECTS, REACT_MODULES, importedName, walkSkippingFunctions } from '../performance/frontend-helpers.js';
|
|
6
|
+
import { walkNodes } from './walk.js';
|
|
7
|
+
import { isAccess, isAssignmentOperator, isIgnoredSource, withoutParentheses } from './source-helpers.js';
|
|
8
|
+
// Array methods that change the array they are called on.
|
|
9
|
+
const mutatingArrayMethods = new Set([
|
|
10
|
+
'push',
|
|
11
|
+
'pop',
|
|
12
|
+
'shift',
|
|
13
|
+
'unshift',
|
|
14
|
+
'splice',
|
|
15
|
+
'sort',
|
|
16
|
+
'reverse',
|
|
17
|
+
'fill',
|
|
18
|
+
'copyWithin',
|
|
19
|
+
]);
|
|
20
|
+
// Map and Set methods that change the collection they are called on.
|
|
21
|
+
const mutatingCollectionMethods = new Set(['set', 'add', 'delete', 'clear']);
|
|
22
|
+
const collectionTypeNames = new Set(['Map', 'Set', 'WeakMap', 'WeakSet']);
|
|
23
|
+
const jsxImportSourcePragma = /@jsxImportSource\s+(\S+)/;
|
|
24
|
+
// Calls that always hand back a new array, so a setter given one gets a new value on every render.
|
|
25
|
+
const freshArrayMethods = new Set(['map', 'filter', 'slice', 'concat', 'flat', 'flatMap', 'toSorted', 'toReversed']);
|
|
26
|
+
// JSX runtimes where list items are not reconciled by `key`.
|
|
27
|
+
const keylessJsxModules = /^(?:solid-js|@solidjs\/|vue|@vue\/|@builder\.io\/qwik|@stencil\/core)/;
|
|
28
|
+
const clientDirective = 'use client';
|
|
29
|
+
// Next.js inlines these into the browser bundle.
|
|
30
|
+
const browserSafeEnvironmentName = /^(?:NEXT_PUBLIC_|NODE_ENV$|NEXT_RUNTIME$)/;
|
|
31
|
+
const serverGlobals = new Set(['window', 'document']);
|
|
32
|
+
function findingAt(sourceFile, file, node, ruleId, message) {
|
|
33
|
+
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
34
|
+
return { file, line: position.line + 1, column: position.character + 1, ruleId, message };
|
|
35
|
+
}
|
|
36
|
+
function hasBlockBody(declaration) {
|
|
37
|
+
return declaration.body !== undefined;
|
|
38
|
+
}
|
|
39
|
+
// An inline function, or a function declared elsewhere in the file and passed by name.
|
|
40
|
+
function functionBodyOf(node, checker) {
|
|
41
|
+
const target = node && withoutParentheses(node);
|
|
42
|
+
if (!target)
|
|
43
|
+
return;
|
|
44
|
+
if (ts.isArrowFunction(target) || ts.isFunctionExpression(target))
|
|
45
|
+
return target;
|
|
46
|
+
if (!ts.isIdentifier(target))
|
|
47
|
+
return;
|
|
48
|
+
const declarations = checker.getSymbolAtLocation(target)?.declarations;
|
|
49
|
+
// An overloaded function has body-less signatures before its implementation.
|
|
50
|
+
const implementation = declarations?.find((candidate) => ts.isFunctionDeclaration(candidate) && hasBlockBody(candidate));
|
|
51
|
+
if (implementation && ts.isFunctionDeclaration(implementation) && hasBlockBody(implementation))
|
|
52
|
+
return implementation;
|
|
53
|
+
const declaration = declarations?.[0];
|
|
54
|
+
if (declaration && ts.isFunctionDeclaration(declaration))
|
|
55
|
+
return;
|
|
56
|
+
const initializer = declaration && ts.isVariableDeclaration(declaration) && declaration.initializer
|
|
57
|
+
? withoutParentheses(declaration.initializer)
|
|
58
|
+
: undefined;
|
|
59
|
+
return initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))
|
|
60
|
+
? initializer
|
|
61
|
+
: undefined;
|
|
62
|
+
}
|
|
63
|
+
function reactEffectCall(node, checker) {
|
|
64
|
+
if (!ts.isCallExpression(node))
|
|
65
|
+
return;
|
|
66
|
+
const hook = importedName(node.expression, checker, REACT_MODULES);
|
|
67
|
+
return hook && REACT_EFFECTS.has(hook) ? node : undefined;
|
|
68
|
+
}
|
|
69
|
+
// --- state setters ---
|
|
70
|
+
// `const [items, setItems] = useState(...)` -> the symbols of `items` and `setItems`.
|
|
71
|
+
function stateSetterOf(declaration, checker) {
|
|
72
|
+
if (!ts.isVariableDeclaration(declaration) || !ts.isArrayBindingPattern(declaration.name))
|
|
73
|
+
return;
|
|
74
|
+
const initializer = declaration.initializer && withoutParentheses(declaration.initializer);
|
|
75
|
+
if (!initializer || !ts.isCallExpression(initializer))
|
|
76
|
+
return;
|
|
77
|
+
if (importedName(initializer.expression, checker, REACT_MODULES) !== 'useState')
|
|
78
|
+
return;
|
|
79
|
+
const [stateElement, setterElement] = declaration.name.elements;
|
|
80
|
+
if (!setterElement || !ts.isBindingElement(setterElement) || !ts.isIdentifier(setterElement.name))
|
|
81
|
+
return;
|
|
82
|
+
const setterSymbol = checker.getSymbolAtLocation(setterElement.name);
|
|
83
|
+
if (!setterSymbol)
|
|
84
|
+
return;
|
|
85
|
+
const stateName = stateElement && ts.isBindingElement(stateElement) ? stateElement.name : undefined;
|
|
86
|
+
const stateSymbol = stateName && ts.isIdentifier(stateName) ? checker.getSymbolAtLocation(stateName) : undefined;
|
|
87
|
+
return { setterSymbol, stateSymbol };
|
|
88
|
+
}
|
|
89
|
+
function stateSetterCalledBy(call, checker) {
|
|
90
|
+
const callee = withoutParentheses(call.expression);
|
|
91
|
+
if (!ts.isIdentifier(callee))
|
|
92
|
+
return;
|
|
93
|
+
const symbol = checker.getSymbolAtLocation(callee);
|
|
94
|
+
// The symbol's declaration is the binding element; the useState call hangs off the declaration two levels up.
|
|
95
|
+
const declaration = symbol?.declarations?.[0]?.parent?.parent;
|
|
96
|
+
const setter = declaration && stateSetterOf(declaration, checker);
|
|
97
|
+
return setter && setter.setterSymbol === symbol ? setter : undefined;
|
|
98
|
+
}
|
|
99
|
+
// --- react-async-effect-callback ---
|
|
100
|
+
function isAsync(callback) {
|
|
101
|
+
return Boolean(callback.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword));
|
|
102
|
+
}
|
|
103
|
+
function checkAsyncEffects(effects, sourceFile, file, checker, findings) {
|
|
104
|
+
for (const effect of effects) {
|
|
105
|
+
const callback = functionBodyOf(effect.arguments[0], checker);
|
|
106
|
+
if (!callback || !isAsync(callback))
|
|
107
|
+
continue;
|
|
108
|
+
findings.push(findingAt(sourceFile, file,
|
|
109
|
+
// A callback declared in another file has offsets that mean nothing here, so point at the effect call.
|
|
110
|
+
callback.getSourceFile() === sourceFile ? callback : effect, 'react-async-effect-callback', 'An async effect callback returns a promise, which React treats as the cleanup function: React warns, the effect never cleans up, and a rejection goes unhandled. Declare an async function inside the effect and call it.'));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// --- react-effect-sets-state-every-render ---
|
|
114
|
+
// A bare `return` hands back undefined, which is `undefined` here so it counts as a result that never changes the value.
|
|
115
|
+
function updaterResults(updater) {
|
|
116
|
+
if (!ts.isBlock(updater.body))
|
|
117
|
+
return [withoutParentheses(updater.body)];
|
|
118
|
+
const results = [];
|
|
119
|
+
walkSkippingFunctions(updater.body, (node) => {
|
|
120
|
+
if (ts.isReturnStatement(node))
|
|
121
|
+
results.push(node.expression && withoutParentheses(node.expression));
|
|
122
|
+
});
|
|
123
|
+
// Running off the end returns undefined too.
|
|
124
|
+
if (!alwaysExits(updater.body))
|
|
125
|
+
results.push(undefined);
|
|
126
|
+
return results;
|
|
127
|
+
}
|
|
128
|
+
function alwaysExits(statement) {
|
|
129
|
+
if (ts.isReturnStatement(statement) || ts.isThrowStatement(statement))
|
|
130
|
+
return true;
|
|
131
|
+
if (ts.isBlock(statement))
|
|
132
|
+
return statement.statements.some(alwaysExits);
|
|
133
|
+
return (ts.isIfStatement(statement) &&
|
|
134
|
+
statement.elseStatement !== undefined &&
|
|
135
|
+
alwaysExits(statement.thenStatement) &&
|
|
136
|
+
alwaysExits(statement.elseStatement));
|
|
137
|
+
}
|
|
138
|
+
function isZeroLiteral(expression) {
|
|
139
|
+
const value = withoutParentheses(expression);
|
|
140
|
+
return ts.isNumericLiteral(value) && Number(value.text) === 0;
|
|
141
|
+
}
|
|
142
|
+
// `prev => prev`, `prev || true` and `prev + 0` settle on a value React bails out on; `prev + 1` and `[...prev, x]` never do.
|
|
143
|
+
function changesValue(result) {
|
|
144
|
+
if (ts.isArrayLiteralExpression(result) || ts.isObjectLiteralExpression(result))
|
|
145
|
+
return true;
|
|
146
|
+
if (!ts.isBinaryExpression(result))
|
|
147
|
+
return false;
|
|
148
|
+
const kind = result.operatorToken.kind;
|
|
149
|
+
const isArithmetic = kind === ts.SyntaxKind.PlusToken || kind === ts.SyntaxKind.MinusToken;
|
|
150
|
+
return isArithmetic && !isZeroLiteral(result.left) && !isZeroLiteral(result.right);
|
|
151
|
+
}
|
|
152
|
+
function returnsOrThrows(statement) {
|
|
153
|
+
let exits = false;
|
|
154
|
+
walkSkippingFunctions(statement, (node) => {
|
|
155
|
+
if (ts.isReturnStatement(node) || ts.isThrowStatement(node))
|
|
156
|
+
exits = true;
|
|
157
|
+
});
|
|
158
|
+
return exits;
|
|
159
|
+
}
|
|
160
|
+
// A setter given the value it already holds bails out of the re-render, so only arguments that are a new object,
|
|
161
|
+
// array or computed number each time keep the loop going.
|
|
162
|
+
function isFreshValue(argument, checker) {
|
|
163
|
+
const value = withoutParentheses(argument);
|
|
164
|
+
if (ts.isObjectLiteralExpression(value) || ts.isArrayLiteralExpression(value) || ts.isNewExpression(value))
|
|
165
|
+
return true;
|
|
166
|
+
if (ts.isCallExpression(value)) {
|
|
167
|
+
// `Date.now()` can repeat within one millisecond, so the clock is not a guaranteed change.
|
|
168
|
+
const callee = withoutParentheses(value.expression);
|
|
169
|
+
if (!ts.isPropertyAccessExpression(callee))
|
|
170
|
+
return false;
|
|
171
|
+
return freshArrayMethods.has(callee.name.text);
|
|
172
|
+
}
|
|
173
|
+
const updater = functionBodyOf(value, checker);
|
|
174
|
+
if (!updater || updater.parameters.length === 0)
|
|
175
|
+
return false;
|
|
176
|
+
const results = updaterResults(updater);
|
|
177
|
+
return results.length > 0 && results.every((result) => result !== undefined && changesValue(result));
|
|
178
|
+
}
|
|
179
|
+
function freshSetterCall(expression, checker) {
|
|
180
|
+
const call = withoutParentheses(expression);
|
|
181
|
+
if (!ts.isCallExpression(call) || !stateSetterCalledBy(call, checker))
|
|
182
|
+
return;
|
|
183
|
+
return call.arguments[0] && isFreshValue(call.arguments[0], checker) ? call : undefined;
|
|
184
|
+
}
|
|
185
|
+
function unconditionalSetterCall(body, checker) {
|
|
186
|
+
if (!ts.isBlock(body))
|
|
187
|
+
return ts.isExpression(body) ? freshSetterCall(body, checker) : undefined;
|
|
188
|
+
for (const statement of body.statements) {
|
|
189
|
+
// `return setItems([])` still runs the setter before it leaves.
|
|
190
|
+
if (ts.isReturnStatement(statement))
|
|
191
|
+
return statement.expression && freshSetterCall(statement.expression, checker);
|
|
192
|
+
// A guard before the call (an early return, a thrown error) makes the update conditional.
|
|
193
|
+
if (returnsOrThrows(statement) && !ts.isExpressionStatement(statement))
|
|
194
|
+
return;
|
|
195
|
+
if (!ts.isExpressionStatement(statement))
|
|
196
|
+
continue;
|
|
197
|
+
const call = freshSetterCall(statement.expression, checker);
|
|
198
|
+
if (call)
|
|
199
|
+
return call;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// React treats an explicit `undefined` like an omitted dependency array.
|
|
203
|
+
function hasNoDependencies(effect, checker) {
|
|
204
|
+
if (effect.arguments.length === 1)
|
|
205
|
+
return true;
|
|
206
|
+
const dependencies = effect.arguments[1] && withoutParentheses(effect.arguments[1]);
|
|
207
|
+
return (effect.arguments.length === 2 &&
|
|
208
|
+
dependencies !== undefined &&
|
|
209
|
+
ts.isIdentifier(dependencies) &&
|
|
210
|
+
dependencies.text === 'undefined' &&
|
|
211
|
+
// A local `const undefined = []` is an ordinary binding, not the global value.
|
|
212
|
+
!checker.getSymbolAtLocation(dependencies)?.declarations?.length);
|
|
213
|
+
}
|
|
214
|
+
function checkEffectLoops(effects, sourceFile, file, checker, findings) {
|
|
215
|
+
for (const effect of effects) {
|
|
216
|
+
if (!hasNoDependencies(effect, checker))
|
|
217
|
+
continue;
|
|
218
|
+
const callback = functionBodyOf(effect.arguments[0], checker);
|
|
219
|
+
const setterCall = callback && unconditionalSetterCall(callback.body, checker);
|
|
220
|
+
if (!setterCall)
|
|
221
|
+
continue;
|
|
222
|
+
findings.push(findingAt(sourceFile, file, setterCall, 'react-effect-sets-state-every-render', 'This effect has no dependency array and sets state to a new value on every run, so each render schedules another render without end. Add a dependency array or move the update into an event handler.'));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// --- react-state-mutated-in-place ---
|
|
226
|
+
// An alias is followed back to the state it points at, so `const alias = items; alias.push(x)` changes `items`.
|
|
227
|
+
function stateSymbolOfRoot(expression, checker, aliases) {
|
|
228
|
+
let current = withoutParentheses(expression);
|
|
229
|
+
while (isAccess(current))
|
|
230
|
+
current = withoutParentheses(current.expression);
|
|
231
|
+
const symbol = ts.isIdentifier(current) ? checker.getSymbolAtLocation(current) : undefined;
|
|
232
|
+
return symbol && (aliases.get(symbol) ?? symbol);
|
|
233
|
+
}
|
|
234
|
+
function isCollection(receiver, checker) {
|
|
235
|
+
const type = checker.getNonNullableType(checker.getTypeAtLocation(receiver));
|
|
236
|
+
const name = type.getSymbol()?.name;
|
|
237
|
+
return name !== undefined && collectionTypeNames.has(name);
|
|
238
|
+
}
|
|
239
|
+
function isMutatingCall(callee, checker) {
|
|
240
|
+
if (!ts.isPropertyAccessExpression(callee))
|
|
241
|
+
return false;
|
|
242
|
+
if (mutatingArrayMethods.has(callee.name.text))
|
|
243
|
+
return true;
|
|
244
|
+
return mutatingCollectionMethods.has(callee.name.text) && isCollection(callee.expression, checker);
|
|
245
|
+
}
|
|
246
|
+
function mutatedSymbolOf(node, checker, aliases) {
|
|
247
|
+
if (ts.isCallExpression(node)) {
|
|
248
|
+
const callee = withoutParentheses(node.expression);
|
|
249
|
+
// A call result in the chain (`items.map(...).sort()`) is a copy, and the root lookup finds no state behind it.
|
|
250
|
+
return isMutatingCall(callee, checker) && ts.isPropertyAccessExpression(callee)
|
|
251
|
+
? stateSymbolOfRoot(callee.expression, checker, aliases)
|
|
252
|
+
: undefined;
|
|
253
|
+
}
|
|
254
|
+
if (ts.isBinaryExpression(node) && isAssignmentOperator(node.operatorToken.kind)) {
|
|
255
|
+
const target = withoutParentheses(node.left);
|
|
256
|
+
return isAccess(target) ? stateSymbolOfRoot(target, checker, aliases) : undefined;
|
|
257
|
+
}
|
|
258
|
+
if (ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) {
|
|
259
|
+
const isStep = node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken;
|
|
260
|
+
const target = withoutParentheses(node.operand);
|
|
261
|
+
return isStep && isAccess(target) ? stateSymbolOfRoot(target, checker, aliases) : undefined;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// `const alias = items` makes `alias` another name for the same state value.
|
|
265
|
+
function recordAlias(node, checker, aliases) {
|
|
266
|
+
if (!ts.isVariableDeclaration(node) || !ts.isIdentifier(node.name) || !node.initializer)
|
|
267
|
+
return;
|
|
268
|
+
const source = stateSymbolOfRoot(node.initializer, checker, aliases);
|
|
269
|
+
const initializer = withoutParentheses(node.initializer);
|
|
270
|
+
const aliasSymbol = checker.getSymbolAtLocation(node.name);
|
|
271
|
+
if (source && aliasSymbol && ts.isIdentifier(initializer))
|
|
272
|
+
aliases.set(aliasSymbol, source);
|
|
273
|
+
}
|
|
274
|
+
function isCallbackArgument(node) {
|
|
275
|
+
return ((ts.isArrowFunction(node) || ts.isFunctionExpression(node)) &&
|
|
276
|
+
ts.isCallExpression(node.parent) &&
|
|
277
|
+
node.parent.arguments.some((argument) => argument === node));
|
|
278
|
+
}
|
|
279
|
+
// Callbacks passed straight to a call (`items.forEach(...)`) run as part of the function; a nested helper that is
|
|
280
|
+
// only declared does not, and is checked as a function of its own.
|
|
281
|
+
// Two branches of one `if` or `?:` never both run, so each is walked with the facts known before the branch.
|
|
282
|
+
function walkRunningCode(root, visit, branches) {
|
|
283
|
+
(function walk(node) {
|
|
284
|
+
if (node !== root && ts.isFunctionLike(node) && !isCallbackArgument(node))
|
|
285
|
+
return;
|
|
286
|
+
const exclusive = exclusiveBranches(node);
|
|
287
|
+
if (exclusive) {
|
|
288
|
+
walk(exclusive.condition);
|
|
289
|
+
branches.begin();
|
|
290
|
+
walk(exclusive.first);
|
|
291
|
+
branches.next(leavesFunction(exclusive.first));
|
|
292
|
+
if (exclusive.second)
|
|
293
|
+
walk(exclusive.second);
|
|
294
|
+
branches.end(exclusive.second !== undefined && leavesFunction(exclusive.second));
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
ts.forEachChild(node, walk);
|
|
298
|
+
}
|
|
299
|
+
// A node takes effect after its operands run: `items.push((setItems(items), 1))` calls the setter first.
|
|
300
|
+
visit(node);
|
|
301
|
+
})(root);
|
|
302
|
+
}
|
|
303
|
+
function exclusiveBranches(node) {
|
|
304
|
+
if (ts.isIfStatement(node))
|
|
305
|
+
return { condition: node.expression, first: node.thenStatement, second: node.elseStatement };
|
|
306
|
+
if (ts.isConditionalExpression(node))
|
|
307
|
+
return { condition: node.condition, first: node.whenTrue, second: node.whenFalse };
|
|
308
|
+
}
|
|
309
|
+
// A branch that returns or throws never reaches the code after the `if`, so its facts must not flow there.
|
|
310
|
+
function leavesFunction(branch) {
|
|
311
|
+
const statements = ts.isBlock(branch) ? branch.statements : [branch];
|
|
312
|
+
return statements.some((statement) => ts.isReturnStatement(statement) || ts.isThrowStatement(statement));
|
|
313
|
+
}
|
|
314
|
+
// Mutations made before a branch stay visible inside it; after it, a mutation from either branch counts.
|
|
315
|
+
function branchTrackerFor(mutated) {
|
|
316
|
+
const open = [];
|
|
317
|
+
const closeBranch = (frame, leaves) => {
|
|
318
|
+
if (leaves)
|
|
319
|
+
return;
|
|
320
|
+
for (const symbol of mutated)
|
|
321
|
+
frame.fromBranches.add(symbol);
|
|
322
|
+
};
|
|
323
|
+
return {
|
|
324
|
+
begin: () => open.push({ before: new Set(mutated), fromBranches: new Set() }),
|
|
325
|
+
next(firstLeaves) {
|
|
326
|
+
const frame = open[open.length - 1];
|
|
327
|
+
if (!frame)
|
|
328
|
+
return;
|
|
329
|
+
closeBranch(frame, firstLeaves);
|
|
330
|
+
mutated.clear();
|
|
331
|
+
for (const symbol of frame.before)
|
|
332
|
+
mutated.add(symbol);
|
|
333
|
+
},
|
|
334
|
+
end(secondLeaves) {
|
|
335
|
+
const frame = open.pop();
|
|
336
|
+
if (!frame)
|
|
337
|
+
return;
|
|
338
|
+
closeBranch(frame, secondLeaves);
|
|
339
|
+
mutated.clear();
|
|
340
|
+
for (const symbol of frame.fromBranches)
|
|
341
|
+
mutated.add(symbol);
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function checkStateMutations(sourceFile, file, checker, findings) {
|
|
346
|
+
// A call sits inside every function that encloses it, so each is reported once.
|
|
347
|
+
const reported = new Set();
|
|
348
|
+
walkNodes(sourceFile, (node) => {
|
|
349
|
+
if (!ts.isFunctionLike(node) || !('body' in node) || !node.body)
|
|
350
|
+
return;
|
|
351
|
+
const mutated = new Set();
|
|
352
|
+
const aliases = new Map();
|
|
353
|
+
const setterCalls = [];
|
|
354
|
+
// Source order puts the mutation before the setter call it feeds; a mutation after the call is another update.
|
|
355
|
+
walkRunningCode(node.body, (inner) => {
|
|
356
|
+
recordAlias(inner, checker, aliases);
|
|
357
|
+
const symbol = mutatedSymbolOf(inner, checker, aliases);
|
|
358
|
+
if (symbol)
|
|
359
|
+
mutated.add(symbol);
|
|
360
|
+
if (!ts.isCallExpression(inner))
|
|
361
|
+
return;
|
|
362
|
+
const setter = stateSetterCalledBy(inner, checker);
|
|
363
|
+
const argument = inner.arguments[0] && withoutParentheses(inner.arguments[0]);
|
|
364
|
+
if (!setter?.stateSymbol || !argument || !ts.isIdentifier(argument))
|
|
365
|
+
return;
|
|
366
|
+
if (checker.getSymbolAtLocation(argument) === setter.stateSymbol && mutated.has(setter.stateSymbol))
|
|
367
|
+
setterCalls.push({ call: inner, setter });
|
|
368
|
+
}, branchTrackerFor(mutated));
|
|
369
|
+
for (const { call } of setterCalls) {
|
|
370
|
+
if (reported.has(call))
|
|
371
|
+
continue;
|
|
372
|
+
reported.add(call);
|
|
373
|
+
findings.push(findingAt(sourceFile, file, call, 'react-state-mutated-in-place', 'This state value is changed in place and then passed back to its own setter. React compares by reference, sees the same object and skips the re-render. Set a copy instead, such as `setItems([...items, item])`.'));
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
// --- react-list-missing-key ---
|
|
378
|
+
function jsxAttributes(element) {
|
|
379
|
+
if (ts.isJsxSelfClosingElement(element))
|
|
380
|
+
return element.attributes.properties;
|
|
381
|
+
return ts.isJsxElement(element) ? element.openingElement.attributes.properties : undefined;
|
|
382
|
+
}
|
|
383
|
+
function hasKeyOrSpread(element) {
|
|
384
|
+
const attributes = jsxAttributes(element);
|
|
385
|
+
return Boolean(attributes?.some((attribute) => ts.isJsxSpreadAttribute(attribute) || (ts.isIdentifier(attribute.name) && attribute.name.text === 'key')));
|
|
386
|
+
}
|
|
387
|
+
function renderedRoots(expression) {
|
|
388
|
+
const value = withoutParentheses(expression);
|
|
389
|
+
if (ts.isConditionalExpression(value))
|
|
390
|
+
return [...renderedRoots(value.whenTrue), ...renderedRoots(value.whenFalse)];
|
|
391
|
+
if (ts.isBinaryExpression(value) && value.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken)
|
|
392
|
+
return renderedRoots(value.right);
|
|
393
|
+
return [value];
|
|
394
|
+
}
|
|
395
|
+
function returnedFrom(callback) {
|
|
396
|
+
if (!ts.isBlock(callback.body))
|
|
397
|
+
return renderedRoots(callback.body);
|
|
398
|
+
const returned = [];
|
|
399
|
+
walkSkippingFunctions(callback.body, (node) => {
|
|
400
|
+
if (ts.isReturnStatement(node) && node.expression)
|
|
401
|
+
returned.push(...renderedRoots(node.expression));
|
|
402
|
+
});
|
|
403
|
+
return returned;
|
|
404
|
+
}
|
|
405
|
+
function isJsxItem(expression) {
|
|
406
|
+
return ts.isJsxElement(expression) || ts.isJsxSelfClosingElement(expression) || ts.isJsxFragment(expression);
|
|
407
|
+
}
|
|
408
|
+
function isReactChildrenHelper(receiver) {
|
|
409
|
+
const target = withoutParentheses(receiver);
|
|
410
|
+
const name = ts.isPropertyAccessExpression(target) ? target.name : target;
|
|
411
|
+
return ts.isIdentifier(name) && name.text === 'Children';
|
|
412
|
+
}
|
|
413
|
+
// Known non-array receivers (immutable collections, observables) are left alone; an unresolved type is trusted.
|
|
414
|
+
function mayBeArray(receiver, checker) {
|
|
415
|
+
const type = checker.getNonNullableType(checker.getTypeAtLocation(receiver));
|
|
416
|
+
if (type.flags & ts.TypeFlags.Any)
|
|
417
|
+
return true;
|
|
418
|
+
const members = type.isUnion() && type.types ? type.types : [type];
|
|
419
|
+
return members.every((member) => checker.isArrayType(member));
|
|
420
|
+
}
|
|
421
|
+
function checkListKeys(sourceFile, file, checker, findings) {
|
|
422
|
+
walkNodes(sourceFile, (node) => {
|
|
423
|
+
if (!ts.isCallExpression(node))
|
|
424
|
+
return;
|
|
425
|
+
const callee = withoutParentheses(node.expression);
|
|
426
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== 'map')
|
|
427
|
+
return;
|
|
428
|
+
if (isReactChildrenHelper(callee.expression) || !mayBeArray(callee.expression, checker))
|
|
429
|
+
return;
|
|
430
|
+
const callback = functionBodyOf(node.arguments[0], checker);
|
|
431
|
+
if (!callback)
|
|
432
|
+
return;
|
|
433
|
+
for (const item of returnedFrom(callback)) {
|
|
434
|
+
if (!isJsxItem(item) || hasKeyOrSpread(item))
|
|
435
|
+
continue;
|
|
436
|
+
findings.push(findingAt(sourceFile, file,
|
|
437
|
+
// A callback declared in another file has offsets that mean nothing here, so point at the `.map` call.
|
|
438
|
+
item.getSourceFile() === sourceFile ? item : node, 'react-list-missing-key', ts.isJsxFragment(item)
|
|
439
|
+
? 'This list item is a `<>...</>` fragment, which cannot take a key, so React cannot tell the items apart when the list changes. Use `<Fragment key={...}>` instead.'
|
|
440
|
+
: 'This list item has no `key` prop, so React cannot tell the items apart and can reuse the wrong state or DOM when the list changes. Add a stable `key` from the item.'));
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
// A `@jsxImportSource` pragma wins over the project setting, and either can name a runtime without keys.
|
|
445
|
+
function usesKeylessJsx(sourceFile, projectJsxImportSource) {
|
|
446
|
+
const runtime = jsxImportSourcePragma.exec(sourceFile.text)?.[1] ?? projectJsxImportSource;
|
|
447
|
+
return Boolean(runtime && keylessJsxModules.test(runtime)) || importsKeylessJsx(sourceFile);
|
|
448
|
+
}
|
|
449
|
+
function importsKeylessJsx(sourceFile) {
|
|
450
|
+
return sourceFile.statements.some((statement) => ts.isImportDeclaration(statement) &&
|
|
451
|
+
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
452
|
+
keylessJsxModules.test(statement.moduleSpecifier.text));
|
|
453
|
+
}
|
|
454
|
+
// --- next-server-env-in-client ---
|
|
455
|
+
function isClientComponent(sourceFile) {
|
|
456
|
+
for (const statement of sourceFile.statements) {
|
|
457
|
+
if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression))
|
|
458
|
+
return false;
|
|
459
|
+
if (statement.expression.text === clientDirective)
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
// A parameter or variable named `process` is not Node's global; the global is declared only in declaration files.
|
|
465
|
+
function isLocalBinding(identifier, checker) {
|
|
466
|
+
const declarations = checker.getSymbolAtLocation(identifier)?.declarations;
|
|
467
|
+
return Boolean(declarations?.some((declaration) => !declaration.getSourceFile().isDeclarationFile));
|
|
468
|
+
}
|
|
469
|
+
function isProcessEnv(expression, checker) {
|
|
470
|
+
const target = withoutParentheses(expression);
|
|
471
|
+
if (!ts.isPropertyAccessExpression(target) || target.name.text !== 'env')
|
|
472
|
+
return false;
|
|
473
|
+
const owner = withoutParentheses(target.expression);
|
|
474
|
+
return ts.isIdentifier(owner) && owner.text === 'process' && !isLocalBinding(owner, checker);
|
|
475
|
+
}
|
|
476
|
+
function environmentReadsIn(node, checker) {
|
|
477
|
+
if (ts.isPropertyAccessExpression(node) && isProcessEnv(node.expression, checker))
|
|
478
|
+
return [{ name: node.name.text, at: node }];
|
|
479
|
+
if (ts.isElementAccessExpression(node) && isProcessEnv(node.expression, checker)) {
|
|
480
|
+
const key = withoutParentheses(node.argumentExpression);
|
|
481
|
+
return [{ name: ts.isStringLiteralLike(key) ? key.text : undefined, at: node }];
|
|
482
|
+
}
|
|
483
|
+
// `const { API_SECRET } = process.env` reads each listed name.
|
|
484
|
+
if (ts.isVariableDeclaration(node) && ts.isObjectBindingPattern(node.name) && node.initializer) {
|
|
485
|
+
if (!isProcessEnv(node.initializer, checker))
|
|
486
|
+
return [];
|
|
487
|
+
return node.name.elements
|
|
488
|
+
.filter((element) => !element.dotDotDotToken)
|
|
489
|
+
.map((element) => {
|
|
490
|
+
const key = element.propertyName ?? element.name;
|
|
491
|
+
const isNamed = ts.isIdentifier(key) || ts.isStringLiteralLike(key);
|
|
492
|
+
return { name: isNamed ? key.text : undefined, at: element };
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
return [];
|
|
496
|
+
}
|
|
497
|
+
function isServerTypeofCheck(operand, checker) {
|
|
498
|
+
const target = withoutParentheses(operand);
|
|
499
|
+
const subject = ts.isTypeOfExpression(target) ? withoutParentheses(target.expression) : undefined;
|
|
500
|
+
// A parameter or variable named `window` is not the browser global, so a typeof check on it says nothing.
|
|
501
|
+
return (subject !== undefined &&
|
|
502
|
+
ts.isIdentifier(subject) &&
|
|
503
|
+
serverGlobals.has(subject.text) &&
|
|
504
|
+
!isLocalBinding(subject, checker));
|
|
505
|
+
}
|
|
506
|
+
function isUndefinedLiteral(operand) {
|
|
507
|
+
const target = withoutParentheses(operand);
|
|
508
|
+
return ts.isStringLiteralLike(target) && target.text === 'undefined';
|
|
509
|
+
}
|
|
510
|
+
function flipped(branch) {
|
|
511
|
+
if (!branch)
|
|
512
|
+
return;
|
|
513
|
+
return branch === 'whenTrue' ? 'whenFalse' : 'whenTrue';
|
|
514
|
+
}
|
|
515
|
+
// Which branch of `condition` only runs on the server (`typeof window === 'undefined'` is true there, `!==` is false).
|
|
516
|
+
function serverBranchOf(condition, checker) {
|
|
517
|
+
const target = withoutParentheses(condition);
|
|
518
|
+
if (ts.isPrefixUnaryExpression(target) && target.operator === ts.SyntaxKind.ExclamationToken)
|
|
519
|
+
return flipped(serverBranchOf(target.operand, checker));
|
|
520
|
+
if (ts.isBinaryExpression(target)) {
|
|
521
|
+
const kind = target.operatorToken.kind;
|
|
522
|
+
const isEquals = kind === ts.SyntaxKind.EqualsEqualsEqualsToken || kind === ts.SyntaxKind.EqualsEqualsToken;
|
|
523
|
+
const isNotEquals = kind === ts.SyntaxKind.ExclamationEqualsEqualsToken || kind === ts.SyntaxKind.ExclamationEqualsToken;
|
|
524
|
+
const comparesTypeof = (isServerTypeofCheck(target.left, checker) && isUndefinedLiteral(target.right)) ||
|
|
525
|
+
(isServerTypeofCheck(target.right, checker) && isUndefinedLiteral(target.left));
|
|
526
|
+
if (!comparesTypeof)
|
|
527
|
+
return;
|
|
528
|
+
if (isEquals)
|
|
529
|
+
return 'whenTrue';
|
|
530
|
+
return isNotEquals ? 'whenFalse' : undefined;
|
|
531
|
+
}
|
|
532
|
+
const flag = ts.isPropertyAccessExpression(target) ? target.name : target;
|
|
533
|
+
const isServerFlag = ts.isIdentifier(flag) && flag.text === 'isServer';
|
|
534
|
+
return isServerFlag || target.getText() === 'import.meta.env.SSR' ? 'whenTrue' : undefined;
|
|
535
|
+
}
|
|
536
|
+
function branchTaken(ancestor, child) {
|
|
537
|
+
if (ts.isIfStatement(ancestor)) {
|
|
538
|
+
if (child === ancestor.thenStatement)
|
|
539
|
+
return { condition: ancestor.expression, branch: 'whenTrue' };
|
|
540
|
+
if (child === ancestor.elseStatement)
|
|
541
|
+
return { condition: ancestor.expression, branch: 'whenFalse' };
|
|
542
|
+
}
|
|
543
|
+
if (ts.isConditionalExpression(ancestor)) {
|
|
544
|
+
if (child === ancestor.whenTrue)
|
|
545
|
+
return { condition: ancestor.condition, branch: 'whenTrue' };
|
|
546
|
+
if (child === ancestor.whenFalse)
|
|
547
|
+
return { condition: ancestor.condition, branch: 'whenFalse' };
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
// Server-only branches legitimately read secrets; the other branch of the same guard runs in the browser.
|
|
551
|
+
function isGuardedForServer(node, checker) {
|
|
552
|
+
let child = node;
|
|
553
|
+
for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
|
|
554
|
+
const taken = branchTaken(ancestor, child);
|
|
555
|
+
if (taken && serverBranchOf(taken.condition, checker) === taken.branch)
|
|
556
|
+
return true;
|
|
557
|
+
child = ancestor;
|
|
558
|
+
}
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
function checkClientEnvironment(sourceFile, file, checker, findings) {
|
|
562
|
+
if (!isClientComponent(sourceFile) || !sourceFile.text.includes('process.env'))
|
|
563
|
+
return;
|
|
564
|
+
walkNodes(sourceFile, (node) => {
|
|
565
|
+
for (const read of environmentReadsIn(node, checker)) {
|
|
566
|
+
if (read.name !== undefined && browserSafeEnvironmentName.test(read.name))
|
|
567
|
+
continue;
|
|
568
|
+
if (isGuardedForServer(read.at, checker))
|
|
569
|
+
continue;
|
|
570
|
+
findings.push(findingAt(sourceFile, file, read.at, 'next-server-env-in-client', read.name === undefined
|
|
571
|
+
? "`process.env` is read with a computed key in a 'use client' file, and Next.js inlines only literal NEXT_PUBLIC_ names into the browser bundle, so the value is unavailable there by default. Read it on the server and pass only a non-sensitive derived value to the Client Component, never the raw value."
|
|
572
|
+
: `\`process.env.${read.name}\` is read in a 'use client' file, but Next.js only exposes variables prefixed with NEXT_PUBLIC_ to the browser by default, so it is unavailable there unless the env option in next.config.js publishes it. Read it on the server and pass only a non-sensitive derived value to the Client Component, never the raw value. Rename it with the NEXT_PUBLIC_ prefix only if it is safe to publish.`));
|
|
573
|
+
}
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
function checkReactBugs(sourceFile, file, checker, projectJsxImportSource) {
|
|
577
|
+
if (isIgnoredSource(sourceFile, file))
|
|
578
|
+
return [];
|
|
579
|
+
const findings = [];
|
|
580
|
+
checkClientEnvironment(sourceFile, file, checker, findings);
|
|
581
|
+
const isScript = /\.[cm]?js$/.test(file) && sourceFile.text.includes('.map(');
|
|
582
|
+
const mentionsReact = sourceFile.text.includes('react') || /\.[jt]sx$/.test(file) || isScript;
|
|
583
|
+
if (!mentionsReact)
|
|
584
|
+
return findings;
|
|
585
|
+
const effects = [];
|
|
586
|
+
const isReactSource = sourceFile.text.includes('useEffect') || sourceFile.text.includes('useLayoutEffect');
|
|
587
|
+
if (isReactSource)
|
|
588
|
+
walkNodes(sourceFile, (node) => {
|
|
589
|
+
const effect = reactEffectCall(node, checker);
|
|
590
|
+
if (effect)
|
|
591
|
+
effects.push(effect);
|
|
592
|
+
});
|
|
593
|
+
checkAsyncEffects(effects, sourceFile, file, checker, findings);
|
|
594
|
+
checkEffectLoops(effects, sourceFile, file, checker, findings);
|
|
595
|
+
if (sourceFile.text.includes('useState'))
|
|
596
|
+
checkStateMutations(sourceFile, file, checker, findings);
|
|
597
|
+
// Plain .js files are parsed with JSX too, so components in them need the same check.
|
|
598
|
+
if (/\.(?:[jt]sx|[cm]?js)$/.test(file) && !usesKeylessJsx(sourceFile, projectJsxImportSource))
|
|
599
|
+
checkListKeys(sourceFile, file, checker, findings);
|
|
600
|
+
return findings;
|
|
601
|
+
}
|
|
602
|
+
export { checkReactBugs };
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// intervals. Each rule only fires for a resource held in a local (or a field, for intervals), so a
|
|
3
3
|
// resource that is returned, stored or passed on is never reported: its new owner decides when it ends.
|
|
4
4
|
import { ts } from '../performance/compiler.js';
|
|
5
|
+
import { calleeName } from '../performance/call-syntax.js';
|
|
5
6
|
import { walkNodes } from './walk.js';
|
|
6
7
|
import { isIgnoredSource } from './source-helpers.js';
|
|
7
8
|
const typeormPackage = /[\\/]node_modules[\\/]typeorm[\\/]/;
|
|
@@ -348,12 +349,6 @@ function findingAt(sourceFile, file, node, ruleId, message) {
|
|
|
348
349
|
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
349
350
|
return { file, line: position.line + 1, column: position.character + 1, ruleId, message };
|
|
350
351
|
}
|
|
351
|
-
function calleeName(call) {
|
|
352
|
-
const callee = ts.skipOuterExpressions(call.expression);
|
|
353
|
-
if (ts.isIdentifier(callee))
|
|
354
|
-
return callee.text;
|
|
355
|
-
return ts.isPropertyAccessExpression(callee) ? callee.name.text : undefined;
|
|
356
|
-
}
|
|
357
352
|
function functionNameOf(functionNode) {
|
|
358
353
|
if ((ts.isMethodDeclaration(functionNode) ||
|
|
359
354
|
ts.isFunctionDeclaration(functionNode) ||
|
|
@@ -524,3 +519,5 @@ function checkResourceLeaks(sourceFile, file, checker) {
|
|
|
524
519
|
return findings;
|
|
525
520
|
}
|
|
526
521
|
export { checkResourceLeaks };
|
|
522
|
+
// Shared with the child-process rule, which resolves imports and finds uses of a local the same way.
|
|
523
|
+
export { findingAt, importedReference, insideNestedFunction, outermostWrapper, referencesTo };
|