loadwise 0.6.2 → 0.8.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 +175 -115
- package/dist/bin/loadwise.js +132 -67
- package/dist/lib/accessibility/analyze.js +5 -3
- package/dist/lib/accessibility/angular-inline.js +9 -11
- package/dist/lib/accessibility/angular-literal.js +39 -0
- package/dist/lib/accessibility/form-context.js +36 -0
- package/dist/lib/accessibility/frame-head.js +92 -0
- package/dist/lib/accessibility/frame-viewport.js +149 -0
- package/dist/lib/accessibility/frontend-detect.js +6 -5
- package/dist/lib/accessibility/jsx-elements.js +13 -3
- package/dist/lib/accessibility/markup-files.js +7 -2
- package/dist/lib/accessibility/rule-ids.js +3 -0
- package/dist/lib/accessibility/rules.js +128 -41
- package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
- package/dist/lib/accessibility/template-attributes.js +5 -3
- package/dist/lib/accessibility/template-elements.js +25 -6
- package/dist/lib/accessibility/template-file.js +40 -25
- package/dist/lib/accessibility/usage.js +10 -2
- package/dist/lib/editor/accessibility-examples.js +6 -1
- package/dist/lib/editor/actions.js +46 -7
- package/dist/lib/editor/apply-fix.js +69 -4
- package/dist/lib/editor/configured-setup.js +63 -0
- package/dist/lib/editor/diagnostics.js +206 -17
- package/dist/lib/editor/dismissals.js +85 -26
- package/dist/lib/editor/editor-document.js +5 -2
- package/dist/lib/editor/editor-launcher.js +170 -0
- package/dist/lib/editor/fix-edits.js +19 -0
- package/dist/lib/editor/jetbrains-setup.js +53 -0
- package/dist/lib/editor/language-server.js +80 -24
- package/dist/lib/editor/managed-config.js +37 -0
- package/dist/lib/editor/native-setup.js +86 -0
- package/dist/lib/editor/perf-watch.js +8 -6
- package/dist/lib/editor/release-vsix.js +61 -0
- package/dist/lib/editor/reliability-examples.js +15 -0
- package/dist/lib/editor/scan-session.js +29 -8
- package/dist/lib/editor/server-commands.js +38 -9
- package/dist/lib/editor/setup-server.js +87 -0
- package/dist/lib/editor/setup-vim.js +63 -0
- package/dist/lib/editor/setup.js +131 -0
- package/dist/lib/editor/stdio-setup.js +6 -0
- package/dist/lib/editor/workspace-files.js +61 -7
- package/dist/lib/editor/workspace-paths.js +41 -1
- package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
- package/dist/lib/maintainability/architecture/architecture.js +4 -4
- package/dist/lib/maintainability/cli/changed-lines.js +20 -2
- package/dist/lib/maintainability/cli/inputs.js +12 -3
- package/dist/lib/maintainability/cli.js +18 -5
- package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
- package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
- package/dist/lib/maintainability/examples.js +4 -0
- package/dist/lib/maintainability/quality/functions.js +9 -6
- package/dist/lib/maintainability/quality/limits.js +19 -0
- package/dist/lib/maintainability/quality/size-limits.js +8 -2
- package/dist/lib/maintainability/quality/stray-console.js +62 -1
- package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
- package/dist/lib/maintainability/rule-ids.js +3 -0
- package/dist/lib/maintainability/scan-changed.js +4 -1
- package/dist/lib/maintainability/scan-worker.js +6 -2
- package/dist/lib/maintainability/scan.js +21 -8
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
- package/dist/lib/maintainability/unused/module-usage.js +43 -21
- package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
- package/dist/lib/maintainability/unused/unused-modules.js +5 -10
- package/dist/lib/performance/analysis/source-inputs.js +7 -2
- package/dist/lib/performance/analysis/source-options.js +7 -6
- package/dist/lib/performance/analyze.js +16 -3
- package/dist/lib/performance/cli/changed-lines.js +8 -1
- package/dist/lib/performance/cli/cli-arguments.js +19 -8
- package/dist/lib/performance/cli/cli-output.js +4 -0
- package/dist/lib/performance/cli/cli-scan.js +21 -2
- package/dist/lib/performance/cli.js +16 -7
- package/dist/lib/performance/collections/memory-growth.js +55 -12
- package/dist/lib/performance/contracts/openapi.js +13 -4
- package/dist/lib/performance/datastores/pg-integrity.js +68 -22
- package/dist/lib/performance/datastores/pg-session-release.js +114 -0
- package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
- package/dist/lib/performance/datastores/pooling.js +62 -3
- package/dist/lib/performance/datastores/redis.js +94 -18
- package/dist/lib/performance/datastores/session-store.js +2 -1
- package/dist/lib/performance/db/checks.js +19 -8
- package/dist/lib/performance/db/gateway.js +3 -1
- package/dist/lib/performance/db/plan-estimates.js +4 -4
- package/dist/lib/performance/engine/native-syntax.js +3 -0
- package/dist/lib/performance/engine/native.js +11 -7
- package/dist/lib/performance/evidence/plans.js +7 -5
- package/dist/lib/performance/evidence/traces.js +21 -1
- package/dist/lib/performance/frontend/angular-teardown.js +96 -0
- package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
- package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
- package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
- package/dist/lib/performance/frontend/frontend-perf.js +149 -74
- package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
- package/dist/lib/performance/migrations/extract-code.js +13 -5
- package/dist/lib/performance/migrations/extract-sql.js +82 -38
- package/dist/lib/performance/migrations/operations.js +56 -13
- package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
- package/dist/lib/performance/migrations/postgres-registry.js +127 -26
- package/dist/lib/performance/migrations/postgres-settings.js +78 -0
- package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
- package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
- package/dist/lib/performance/migrations/rules-postgres.js +88 -39
- package/dist/lib/performance/orm/offset-pagination.js +6 -4
- package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
- package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
- package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
- package/dist/lib/performance/polyglot.js +111 -14
- package/dist/lib/performance/projects.js +3 -3
- package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
- package/dist/lib/performance/request/backend-mastery.js +151 -42
- package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
- package/dist/lib/performance/request/fastify-receivers.js +93 -0
- package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
- package/dist/lib/performance/request/mongoose-lean.js +298 -46
- package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
- package/dist/lib/performance/request/phase5-ast.js +6 -7
- package/dist/lib/performance/request/phase5-rules.js +204 -23
- package/dist/lib/performance/request/request-handlers.js +120 -23
- package/dist/lib/performance/request/sql-select-star.js +150 -0
- package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
- package/dist/lib/performance/request/upload-limits.js +5 -1
- package/dist/lib/performance/site-fix.js +4 -4
- package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
- package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
- package/dist/lib/performance/site-fixes/collections.js +1 -2
- package/dist/lib/performance/site-fixes/frontend-fixes.js +358 -14
- package/dist/lib/performance/site-fixes/helpers.js +3 -4
- package/dist/lib/performance/site-fixes/integrity.js +2 -4
- package/dist/lib/performance/site-fixes/migration-context.js +8 -1
- package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
- package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
- package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
- package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
- package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
- package/dist/lib/performance/sql/mysql-tokens.js +38 -12
- package/dist/lib/reliability/boundary-access.js +114 -57
- package/dist/lib/reliability/react-bugs.js +29 -7
- package/dist/lib/reliability/resource-leak-intervals.js +258 -0
- package/dist/lib/reliability/resource-leaks.js +209 -195
- package/dist/lib/reliability/rule-ids.js +10 -1
- package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
- package/dist/lib/reliability/swallowed-errors.js +107 -21
- package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
- package/dist/lib/reliability/usage.js +16 -5
- package/dist/lib/reliability/walk.js +3 -2
- package/dist/lib/security/auth-crypto/hashing.js +24 -8
- package/dist/lib/security/auth-crypto/jwt.js +21 -6
- package/dist/lib/security/auth-crypto/secrets.js +44 -1
- package/dist/lib/security/auth-crypto/shared.js +3 -1
- package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
- package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
- package/dist/lib/security/cli.js +21 -6
- package/dist/lib/security/cookie-credentials.js +115 -0
- package/dist/lib/security/cookie-csrf.js +241 -0
- package/dist/lib/security/deps/advisory.js +50 -109
- package/dist/lib/security/deps/cli.js +1 -1
- package/dist/lib/security/deps/cvss-score.js +14 -0
- package/dist/lib/security/deps/fixed-version.js +97 -0
- package/dist/lib/security/deps/lockfiles.js +135 -74
- package/dist/lib/security/deps/osv.js +11 -3
- package/dist/lib/security/deps/scan.js +47 -20
- package/dist/lib/security/deps/yarn-lock.js +158 -0
- package/dist/lib/security/examples/injection.cjs +4 -0
- package/dist/lib/security/exposure/cookies.js +17 -2
- package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
- package/dist/lib/security/exposure/frontend-security.js +21 -14
- package/dist/lib/security/exposure/jsx-url.js +18 -33
- package/dist/lib/security/exposure/logging.js +49 -10
- package/dist/lib/security/exposure/prototype-pollution.js +126 -56
- package/dist/lib/security/exposure/react-html.js +26 -0
- package/dist/lib/security/exposure/reflected-xss.js +34 -5
- package/dist/lib/security/files/compose.js +126 -1
- package/dist/lib/security/files/dockerfile.js +62 -4
- package/dist/lib/security/files/index.js +1 -1
- package/dist/lib/security/injection/command.js +46 -18
- package/dist/lib/security/injection/index.js +2 -0
- package/dist/lib/security/injection/ldap.js +86 -0
- package/dist/lib/security/injection/nosql.js +28 -4
- package/dist/lib/security/injection/redos.js +1 -1
- package/dist/lib/security/injection/rule-ids.js +1 -0
- package/dist/lib/security/injection/shared.js +5 -1
- package/dist/lib/security/injection/sql.js +28 -13
- package/dist/lib/security/lexical-declaration.js +113 -0
- package/dist/lib/security/pg-dynamic-query.js +28 -1
- package/dist/lib/security/pg-identifier-guards.js +107 -3
- package/dist/lib/security/phase5-rules.js +26 -135
- package/dist/lib/security/polyglot.js +2 -2
- package/dist/lib/security/redirect-settings.js +98 -0
- package/dist/lib/security/request-forgery/open-redirect.js +18 -5
- package/dist/lib/security/request-forgery/path-traversal.js +84 -6
- package/dist/lib/security/request-forgery/shared.js +1 -7
- package/dist/lib/security/request-forgery/ssrf.js +176 -14
- package/dist/lib/security/rule-descriptions.js +5 -1
- package/dist/lib/security/rule-ids.js +7 -0
- package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
- package/dist/lib/security/site-fixes/exposure.js +21 -3
- package/dist/lib/security/site-fixes/files.js +11 -13
- package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
- package/dist/lib/security/site-fixes/injection.js +17 -4
- package/dist/lib/security/site-fixes/jwt.js +1 -20
- package/dist/lib/security/site-fixes/request-forgery.js +37 -29
- package/dist/lib/security/taint/binding-inputs.js +236 -0
- package/dist/lib/security/taint/filename-guard.js +45 -45
- package/dist/lib/security/taint/guards.js +14 -8
- package/dist/lib/security/taint/index.js +126 -154
- package/dist/lib/security/taint/propagate.js +43 -9
- package/dist/lib/security/taint/record-writes.js +161 -0
- package/dist/lib/security/taint/resolution-cache.js +13 -7
- package/dist/lib/security/taint/sanitizers.js +89 -5
- package/dist/lib/security/taint/trace-nodes.js +61 -0
- package/dist/lib/security/taint/validators.js +89 -9
- package/dist/lib/shared/agent/agent-init.js +28 -5
- package/dist/lib/shared/agent/agent-prompt.js +74 -0
- package/dist/lib/shared/agent/agent-session.js +6 -1
- package/dist/lib/shared/agent/agent-verify.js +58 -11
- package/dist/lib/shared/agent/git-hook.js +17 -6
- package/dist/lib/shared/agent/review-delivery.js +1 -1
- package/dist/lib/shared/agent/review-mcp.js +8 -2
- package/dist/lib/shared/analysis/ast-semantics.js +2 -2
- package/dist/lib/shared/analysis/scan-cache.js +99 -14
- package/dist/lib/shared/check.js +172 -20
- package/dist/lib/shared/cli-dispatch.js +17 -0
- package/dist/lib/shared/config.js +27 -10
- package/dist/lib/shared/explain.js +1 -1
- package/dist/lib/shared/fix/fix-convergence.js +19 -31
- package/dist/lib/shared/fix/fix-handoff.js +16 -11
- package/dist/lib/shared/fix/fix-imports.js +4 -162
- package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
- package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
- package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
- package/dist/lib/shared/fix/fix-presentation.js +9 -6
- package/dist/lib/shared/fix/fix-safety.js +32 -1
- package/dist/lib/shared/fix/fix-scope.js +82 -0
- package/dist/lib/shared/fix/fix-terminal.js +28 -1
- package/dist/lib/shared/fix/fix-typecheck.js +8 -4
- package/dist/lib/shared/fix/import-text-edits.js +148 -0
- package/dist/lib/shared/fix/project-file-access.js +17 -26
- package/dist/lib/shared/fix/read-fix-text.js +20 -0
- package/dist/lib/shared/fix.js +241 -104
- package/dist/lib/shared/formats/finding-location.js +4 -1
- package/dist/lib/shared/formats/github.js +3 -3
- package/dist/lib/shared/formats/sarif.js +9 -3
- package/dist/lib/shared/report-fp.js +97 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
- package/dist/lib/shared/reporting/baseline.js +7 -5
- package/dist/lib/shared/reporting/check-text.js +50 -0
- package/dist/lib/shared/reporting/pillars.js +54 -19
- package/dist/lib/shared/reporting/source-report.js +3 -0
- package/dist/lib/shared/system/atomic-write.js +5 -2
- package/dist/lib/shared/system/glob.js +34 -8
- package/dist/lib/shared/system/sentry.js +222 -0
- package/dist/lib/shared/system/shell-argument.js +5 -1
- package/dist/lib/shared/system/terminal-safe.js +3 -2
- package/dist/lib/shared/system/terminal-style.js +131 -0
- package/dist/lib/shared/workspaces.js +15 -3
- package/docs/assets/cli-workflow.gif +0 -0
- package/integrations/sublime/plugin.py +22 -0
- package/integrations/zed/Cargo.lock +845 -0
- package/integrations/zed/Cargo.toml +11 -0
- package/integrations/zed/extension.toml +11 -0
- package/integrations/zed/src/lib.rs +25 -0
- package/package.json +15 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/commands/loadwise.md +11 -6
- package/plugin/hooks/check-edited-file.mjs +27 -5
- package/polyglot/performance/rules/csharp.yml +34 -5
- package/polyglot/performance/rules/java.yml +24 -1
- package/polyglot/performance/rules/python.yml +74 -2
- package/polyglot/performance/sgconfig.yml +1 -0
- package/polyglot/reliability/rules/csharp.yml +12 -6
- package/polyglot/reliability/rules/go.yml +86 -6
- package/polyglot/reliability/rules/java.yml +22 -1
- package/polyglot/reliability/sgconfig.yml +1 -0
- package/polyglot/security/rules/go.yml +100 -22
- package/polyglot/security/rules/python.yml +74 -6
- package/polyglot/security/rules/rust.yml +65 -0
- package/polyglot/security/sgconfig.yml +1 -0
- package/schema/loadwise.config.schema.json +556 -76
- /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { ts } from '../engine/compiler.js';
|
|
2
|
+
import { isBulkListenerRegistration } from './bulk-listener-registration.js';
|
|
3
|
+
import { isFreshLoopSocket } from './fresh-loop-socket.js';
|
|
2
4
|
import { enclosingRequestHandler } from './request-handlers.js';
|
|
3
5
|
import { propertyName } from '../schema/ts-helpers.js';
|
|
4
|
-
import { unwrapExpression } from '../../shared/analysis/ast-semantics.js';
|
|
5
|
-
import { ARRAY_LOOP_METHODS, containsMatchingNode, enclosingLoop, valueSymbol } from './phase5-ast.js';
|
|
6
|
+
import { resolveImportedBinding, unwrapExpression } from '../../shared/analysis/ast-semantics.js';
|
|
7
|
+
import { ARRAY_LOOP_METHODS, containsMatchingNode, enclosingLoop, isGlobalMethodCall, valueSymbol, } from './phase5-ast.js';
|
|
6
8
|
const EVENT_ADD_METHODS = new Set(['on', 'addListener', 'addEventListener']);
|
|
7
9
|
const EVENT_REMOVE_METHODS = new Set(['off', 'removeListener', 'removeEventListener']);
|
|
8
10
|
const LIFECYCLE_HOOKS = new Set(['useEffect', 'useLayoutEffect', 'onMounted', 'onBeforeMount']);
|
|
11
|
+
const UNMOUNT_HOOKS = new Set(['onBeforeUnmount', 'onUnmounted']);
|
|
9
12
|
const REQUEST_SCOPED_EMITTERS = new Set([
|
|
10
13
|
'req',
|
|
11
14
|
'_req',
|
|
@@ -49,10 +52,53 @@ function enclosingHookCallback(node) {
|
|
|
49
52
|
}
|
|
50
53
|
return undefined;
|
|
51
54
|
}
|
|
52
|
-
function hasListenerCleanup(scope) {
|
|
55
|
+
function hasListenerCleanup(scope, registration) {
|
|
53
56
|
return containsMatchingNode(scope, (node) => ts.isCallExpression(node) &&
|
|
54
57
|
ts.isPropertyAccessExpression(node.expression) &&
|
|
55
|
-
EVENT_REMOVE_METHODS.has(node.expression.name.text)
|
|
58
|
+
EVENT_REMOVE_METHODS.has(node.expression.name.text) &&
|
|
59
|
+
(!registration || matchesCleanupTarget(node, registration)), false);
|
|
60
|
+
}
|
|
61
|
+
function matchesCleanupTarget(cleanupCall, registration) {
|
|
62
|
+
const cleanCallee = ts.skipOuterExpressions(cleanupCall.expression);
|
|
63
|
+
const regCallee = ts.skipOuterExpressions(registration.expression);
|
|
64
|
+
if (!ts.isPropertyAccessExpression(cleanCallee) || !ts.isPropertyAccessExpression(regCallee))
|
|
65
|
+
return false;
|
|
66
|
+
if (!EVENT_REMOVE_METHODS.has(cleanCallee.name.text))
|
|
67
|
+
return false;
|
|
68
|
+
const cleanReceiver = ts.skipOuterExpressions(cleanCallee.expression).getText();
|
|
69
|
+
const regReceiver = ts.skipOuterExpressions(regCallee.expression).getText();
|
|
70
|
+
if (cleanReceiver !== regReceiver)
|
|
71
|
+
return false;
|
|
72
|
+
const [cleanEvent, cleanHandler] = cleanupCall.arguments;
|
|
73
|
+
const [regEvent, regHandler] = registration.arguments;
|
|
74
|
+
if (!cleanEvent || !regEvent)
|
|
75
|
+
return false;
|
|
76
|
+
const cleanupEvent = ts.skipOuterExpressions(cleanEvent);
|
|
77
|
+
const registrationEvent = ts.skipOuterExpressions(regEvent);
|
|
78
|
+
const sameEvent = ts.isStringLiteralLike(cleanupEvent) && ts.isStringLiteralLike(registrationEvent)
|
|
79
|
+
? cleanupEvent.text === registrationEvent.text
|
|
80
|
+
: cleanupEvent.getText() === registrationEvent.getText();
|
|
81
|
+
if (!sameEvent)
|
|
82
|
+
return false;
|
|
83
|
+
if (!cleanHandler || !regHandler)
|
|
84
|
+
return false;
|
|
85
|
+
const cleanupHandler = ts.skipOuterExpressions(cleanHandler);
|
|
86
|
+
const registrationHandler = ts.skipOuterExpressions(regHandler);
|
|
87
|
+
if (ts.isArrowFunction(registrationHandler) || ts.isFunctionExpression(registrationHandler))
|
|
88
|
+
return false;
|
|
89
|
+
return cleanupHandler.getText() === registrationHandler.getText();
|
|
90
|
+
}
|
|
91
|
+
function hasUnmountCleanup(enclosingFn, registration) {
|
|
92
|
+
return containsMatchingNode(enclosingFn, (node) => {
|
|
93
|
+
if (!ts.isCallExpression(node))
|
|
94
|
+
return false;
|
|
95
|
+
const callee = ts.skipOuterExpressions(node.expression);
|
|
96
|
+
if (!ts.isIdentifier(callee) || !UNMOUNT_HOOKS.has(callee.text))
|
|
97
|
+
return false;
|
|
98
|
+
const [callback] = node.arguments;
|
|
99
|
+
return (callback !== undefined &&
|
|
100
|
+
containsMatchingNode(callback, (inner) => ts.isCallExpression(inner) && matchesCleanupTarget(inner, registration), false));
|
|
101
|
+
}, false);
|
|
56
102
|
}
|
|
57
103
|
function isLiteralList(node) {
|
|
58
104
|
const value = unwrapExpression(node);
|
|
@@ -134,6 +180,41 @@ function repeatingLoop(node, checker) {
|
|
|
134
180
|
loop = enclosingLoop(loop);
|
|
135
181
|
return loop;
|
|
136
182
|
}
|
|
183
|
+
function isFreshReceiver(callee, call, hook, handler, checker) {
|
|
184
|
+
const receiver = callee.expression;
|
|
185
|
+
if (!ts.isIdentifier(receiver))
|
|
186
|
+
return false;
|
|
187
|
+
const declaration = valueSymbol(receiver, checker)?.valueDeclaration;
|
|
188
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !ts.isVariableStatement(declaration.parent.parent))
|
|
189
|
+
return false;
|
|
190
|
+
if ((declaration.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const)
|
|
191
|
+
return false;
|
|
192
|
+
const allocation = unwrapExpression(declaration.initializer);
|
|
193
|
+
if (!allocation || !ts.isCallExpression(allocation))
|
|
194
|
+
return false;
|
|
195
|
+
const loop = enclosingLoop(call);
|
|
196
|
+
if (callee.name.text === 'addEventListener') {
|
|
197
|
+
return (!hook &&
|
|
198
|
+
loop !== undefined &&
|
|
199
|
+
enclosingLoop(declaration) === loop &&
|
|
200
|
+
isGlobalMethodCall(allocation, 'document', 'createElement', checker));
|
|
201
|
+
}
|
|
202
|
+
const binding = resolveImportedBinding(unwrapExpression(allocation.expression), checker, {
|
|
203
|
+
includeNamespaceImports: false,
|
|
204
|
+
});
|
|
205
|
+
// Watch mode creates each watcher for the matching configuration's lifetime.
|
|
206
|
+
if (binding?.module === 'rollup' && binding.exported === 'watch') {
|
|
207
|
+
return !hook && !handler && loop !== undefined && enclosingLoop(declaration) === loop;
|
|
208
|
+
}
|
|
209
|
+
// A multipart parser allocated by the request callback handles a single request.
|
|
210
|
+
return (binding?.module === 'busboy' &&
|
|
211
|
+
(binding.exported === 'default' || binding.exported === '*') &&
|
|
212
|
+
!loop &&
|
|
213
|
+
!hook &&
|
|
214
|
+
handler !== undefined &&
|
|
215
|
+
ts.findAncestor(call, ts.isFunctionLike) === handler &&
|
|
216
|
+
ts.findAncestor(declaration, ts.isFunctionLike) === handler);
|
|
217
|
+
}
|
|
137
218
|
function checkNodeEventListenerLeak(call, context) {
|
|
138
219
|
const callee = ts.skipOuterExpressions(call.expression);
|
|
139
220
|
if (!ts.isPropertyAccessExpression(callee) || !EVENT_ADD_METHODS.has(callee.name.text))
|
|
@@ -145,6 +226,14 @@ function checkNodeEventListenerLeak(call, context) {
|
|
|
145
226
|
const handler = enclosingRequestHandler(call);
|
|
146
227
|
if (!hook && !loop && !handler)
|
|
147
228
|
return;
|
|
229
|
+
if (loop && !hook && !handler) {
|
|
230
|
+
if (isBulkListenerRegistration(call, loop, context.checker))
|
|
231
|
+
return;
|
|
232
|
+
if (callee.name.text !== 'addEventListener' && isFreshLoopSocket(callee.expression, call, loop, context.checker))
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (isFreshReceiver(callee, call, hook, handler, context.checker))
|
|
236
|
+
return;
|
|
148
237
|
if (handler &&
|
|
149
238
|
!loop &&
|
|
150
239
|
!hook &&
|
|
@@ -153,8 +242,13 @@ function checkNodeEventListenerLeak(call, context) {
|
|
|
153
242
|
return;
|
|
154
243
|
}
|
|
155
244
|
const cleanupScope = hook ?? ts.findAncestor(call, ts.isFunctionLike);
|
|
156
|
-
if (cleanupScope && hasListenerCleanup(cleanupScope))
|
|
245
|
+
if (cleanupScope && hasListenerCleanup(cleanupScope, hook ? call : undefined))
|
|
157
246
|
return;
|
|
247
|
+
if (hook) {
|
|
248
|
+
const enclosingFn = ts.findAncestor(hook.parent, ts.isFunctionLike);
|
|
249
|
+
if (enclosingFn && hasUnmountCleanup(enclosingFn, call))
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
158
252
|
context.report(call, 'node-event-listener-leak', 'Event listener registered inside a repeated or per-request context without matching cleanup leaks listeners over time.');
|
|
159
253
|
}
|
|
160
254
|
export { checkNodeEventListenerLeak };
|
|
@@ -70,11 +70,10 @@ function constSource(identifier, checker) {
|
|
|
70
70
|
const initializer = unwrapExpression(variable.initializer);
|
|
71
71
|
return initializer && !ts.isFunctionLike(initializer) ? initializer : undefined;
|
|
72
72
|
}
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const known = blockIdentifiers.get(block);
|
|
73
|
+
// Cache syntax only so a watch build that reuses the tree with a new checker sees current symbols.
|
|
74
|
+
const scopeIdentifiers = new WeakMap();
|
|
75
|
+
function identifiersByName(scope) {
|
|
76
|
+
const known = scopeIdentifiers.get(scope);
|
|
78
77
|
if (known)
|
|
79
78
|
return known;
|
|
80
79
|
const byName = new Map();
|
|
@@ -88,8 +87,8 @@ function identifiersByName(block) {
|
|
|
88
87
|
}
|
|
89
88
|
ts.forEachChild(node, visit);
|
|
90
89
|
}
|
|
91
|
-
visit(
|
|
92
|
-
|
|
90
|
+
visit(scope);
|
|
91
|
+
scopeIdentifiers.set(scope, byName);
|
|
93
92
|
return byName;
|
|
94
93
|
}
|
|
95
94
|
function valueSymbol(identifier, checker) {
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
// - node-event-listener-leak
|
|
7
7
|
// - json-stringify-in-loop
|
|
8
8
|
import { ts } from '../engine/compiler.js';
|
|
9
|
+
import { standardInterface } from '../collections/repeated.js';
|
|
10
|
+
import { isProjectFunction, signatureDeclaration } from '../query-loops/service-call-resolution.js';
|
|
9
11
|
import { checkMongooseMissingLean } from './mongoose-lean.js';
|
|
10
12
|
import { checkNodeEventListenerLeak } from './node-event-listener-leak.js';
|
|
11
13
|
import { isTestSource } from '../analysis/review-context.js';
|
|
@@ -18,10 +20,13 @@ const MAPPING_METHODS = new Set(['map', 'flatMap']);
|
|
|
18
20
|
const OUTPUT_METHODS = new Set(['push', 'write']);
|
|
19
21
|
const GQL_ROOT_TYPES = new Set(['Query', 'Mutation', 'Subscription']);
|
|
20
22
|
const GQL_DECORATORS = new Set(['ResolveField', 'ResolveProperty']);
|
|
23
|
+
const GQL_PARENT_DECORATORS = new Set(['Parent', 'Root']);
|
|
21
24
|
const DB_HTTP_METHODS = new Set([
|
|
22
25
|
'findMany',
|
|
23
26
|
'findUnique',
|
|
27
|
+
'findUniqueOrThrow',
|
|
24
28
|
'findFirst',
|
|
29
|
+
'findFirstOrThrow',
|
|
25
30
|
'findOne',
|
|
26
31
|
'findById',
|
|
27
32
|
'findByPk',
|
|
@@ -35,18 +40,20 @@ const DB_HTTP_METHODS = new Set([
|
|
|
35
40
|
'patch',
|
|
36
41
|
'delete',
|
|
37
42
|
]);
|
|
43
|
+
const IN_MEMORY_COLLECTIONS = new Set(['Map', 'ReadonlyMap', 'Set', 'ReadonlySet', 'WeakMap', 'WeakSet']);
|
|
38
44
|
const HTTP_RECEIVERS = new Set(['axios', 'got', 'ky', 'undici', 'http', 'https']);
|
|
45
|
+
const GET_CLIENT_PACKAGES = ['@nestjs/axios', 'better-sqlite3', '@types/better-sqlite3', 'sqlite3'];
|
|
39
46
|
// --- Rule 3: graphql-n-plus-one-resolver ---------------------------------------------------------
|
|
40
|
-
function
|
|
47
|
+
function hasGraphqlDecorator(node, names) {
|
|
41
48
|
if (!node.modifiers)
|
|
42
49
|
return false;
|
|
43
50
|
for (const mod of node.modifiers) {
|
|
44
51
|
if (!ts.isDecorator(mod))
|
|
45
52
|
continue;
|
|
46
53
|
const expr = ts.skipOuterExpressions(mod.expression);
|
|
47
|
-
if (ts.isIdentifier(expr) &&
|
|
54
|
+
if (ts.isIdentifier(expr) && names.has(expr.text))
|
|
48
55
|
return true;
|
|
49
|
-
if (ts.isCallExpression(expr) && ts.isIdentifier(expr.expression) &&
|
|
56
|
+
if (ts.isCallExpression(expr) && ts.isIdentifier(expr.expression) && names.has(expr.expression.text)) {
|
|
50
57
|
return true;
|
|
51
58
|
}
|
|
52
59
|
}
|
|
@@ -83,7 +90,7 @@ function isResolverMapField(fn) {
|
|
|
83
90
|
/^(?:parent|root)$/.test(fn.parameters[0].name.text));
|
|
84
91
|
}
|
|
85
92
|
function isGraphqlFieldResolver(node) {
|
|
86
|
-
if (ts.isMethodDeclaration(node) &&
|
|
93
|
+
if (ts.isMethodDeclaration(node) && hasGraphqlDecorator(node, GQL_DECORATORS))
|
|
87
94
|
return true;
|
|
88
95
|
if (ts.isMethodDeclaration(node) || ts.isFunctionExpression(node) || ts.isArrowFunction(node)) {
|
|
89
96
|
return isResolverMapField(node);
|
|
@@ -96,7 +103,7 @@ function usesDataLoader(fn) {
|
|
|
96
103
|
ts.isPropertyAccessExpression(node.expression) &&
|
|
97
104
|
(node.expression.name.text === 'load' || node.expression.name.text === 'loadMany')), false);
|
|
98
105
|
}
|
|
99
|
-
function isDbOrHttpCall(node) {
|
|
106
|
+
function isDbOrHttpCall(node, context) {
|
|
100
107
|
if (!ts.isCallExpression(node))
|
|
101
108
|
return false;
|
|
102
109
|
const callee = ts.skipOuterExpressions(node.expression);
|
|
@@ -111,45 +118,162 @@ function isDbOrHttpCall(node) {
|
|
|
111
118
|
const firstArg = node.arguments[0];
|
|
112
119
|
if (firstArg && (ts.isArrowFunction(firstArg) || ts.isFunctionExpression(firstArg)))
|
|
113
120
|
return false;
|
|
121
|
+
const receiverSource = ts.isIdentifier(callee.expression)
|
|
122
|
+
? constSource(callee.expression, context.checker)
|
|
123
|
+
: undefined;
|
|
124
|
+
const receiverType = context.checker.getNonNullableType(context.checker.getTypeAtLocation(callee.expression));
|
|
125
|
+
if (ts.isArrayLiteralExpression(receiverSource ?? callee.expression) ||
|
|
126
|
+
(receiverType.getProperty('length') !== undefined && receiverType.getProperty('findIndex') !== undefined)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
114
129
|
}
|
|
115
|
-
|
|
130
|
+
const owner = standardInterface(node, context.checker) ?? context.checker.getTypeAtLocation(callee.expression).getSymbol()?.name;
|
|
131
|
+
if (owner !== undefined && IN_MEMORY_COLLECTIONS.has(owner))
|
|
132
|
+
return false;
|
|
133
|
+
if (method !== 'get' && method !== 'delete')
|
|
116
134
|
return true;
|
|
117
|
-
|
|
135
|
+
if (method === 'delete')
|
|
136
|
+
return true;
|
|
137
|
+
// A generic get can be an in-memory read. Require independent client or return-type evidence.
|
|
138
|
+
const receiver = unwrapExpression(callee.expression);
|
|
139
|
+
const name = ts.isPropertyAccessExpression(receiver) ? receiver.name : receiver;
|
|
140
|
+
return ((ts.isIdentifier(name) && HTTP_RECEIVERS.has(name.text)) ||
|
|
141
|
+
context.isPackageType(receiver, GET_CLIENT_PACKAGES) ||
|
|
142
|
+
context.checker.getTypeAtLocation(node).getProperty('then') !== undefined);
|
|
143
|
+
}
|
|
144
|
+
function callsProjectQuery(call, context) {
|
|
145
|
+
if (!ts.isPropertyAccessExpression(unwrapExpression(call.expression)))
|
|
146
|
+
return false;
|
|
147
|
+
if (!context.checker.getTypeAtLocation(call).getProperty('then'))
|
|
148
|
+
return false;
|
|
149
|
+
const method = signatureDeclaration(call, context.checker);
|
|
150
|
+
if (!isProjectFunction(method) || !ts.isMethodDeclaration(method) || usesDataLoader(method.body))
|
|
151
|
+
return false;
|
|
152
|
+
// Follow one call edge and stop at functions that the service method may never invoke.
|
|
153
|
+
return containsMatchingNode(method.body, (node) => isDbOrHttpCall(node, context));
|
|
154
|
+
}
|
|
155
|
+
function delegatedResolverQuery(resolver, context) {
|
|
156
|
+
if (!resolver.body)
|
|
157
|
+
return;
|
|
158
|
+
const { checker } = context;
|
|
159
|
+
const parents = ts.isMethodDeclaration(resolver) && hasGraphqlDecorator(resolver, GQL_DECORATORS)
|
|
160
|
+
? resolver.parameters.filter((parameter) => hasGraphqlDecorator(parameter, GQL_PARENT_DECORATORS))
|
|
161
|
+
: resolver.parameters.slice(0, 1);
|
|
162
|
+
const symbols = new Set(parents.flatMap((parameter) => bindingIdentifiers(parameter.name, [])).map((name) => valueSymbol(name, checker)));
|
|
163
|
+
symbols.delete(undefined);
|
|
164
|
+
if (symbols.size === 0)
|
|
165
|
+
return;
|
|
166
|
+
function dependsOnParent(root, depth = 0) {
|
|
167
|
+
if (ts.isFunctionLike(root))
|
|
168
|
+
return false;
|
|
169
|
+
return containsMatchingNode(root, (candidate) => {
|
|
170
|
+
if (!ts.isIdentifier(candidate))
|
|
171
|
+
return false;
|
|
172
|
+
if (symbols.has(valueSymbol(candidate, checker)))
|
|
173
|
+
return true;
|
|
174
|
+
const source = depth < MAX_SOURCE_DEPTH ? constSource(candidate, checker) : undefined;
|
|
175
|
+
return source !== undefined && dependsOnParent(source, depth + 1);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
let query;
|
|
179
|
+
containsMatchingNode(resolver.body, (node) => {
|
|
180
|
+
if (!ts.isCallExpression(node) ||
|
|
181
|
+
!node.arguments.some((argument) => dependsOnParent(argument)) ||
|
|
182
|
+
!callsProjectQuery(node, context))
|
|
183
|
+
return false;
|
|
184
|
+
query = node;
|
|
185
|
+
return true;
|
|
186
|
+
});
|
|
187
|
+
return query;
|
|
118
188
|
}
|
|
119
189
|
function checkGraphqlNPlusOneResolver(node, context) {
|
|
120
190
|
if (!node.body || !isGraphqlFieldResolver(node))
|
|
121
191
|
return;
|
|
122
|
-
if (usesDataLoader(node.body))
|
|
123
|
-
return;
|
|
124
192
|
let queryCall;
|
|
125
193
|
function findQuery(current) {
|
|
126
194
|
if (queryCall)
|
|
127
195
|
return;
|
|
128
|
-
|
|
196
|
+
// A named batch callback is only deferred work (e.g. DataLoader's batch function), not a direct resolver query.
|
|
197
|
+
if (ts.isFunctionDeclaration(current))
|
|
198
|
+
return;
|
|
199
|
+
if (ts.isVariableDeclaration(current) &&
|
|
200
|
+
current.initializer &&
|
|
201
|
+
(ts.isArrowFunction(current.initializer) || ts.isFunctionExpression(current.initializer))) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (ts.isNewExpression(current) &&
|
|
205
|
+
(ts.isIdentifier(current.expression)
|
|
206
|
+
? /dataloader/i.test(current.expression.text)
|
|
207
|
+
: ts.isPropertyAccessExpression(current.expression) && /dataloader/i.test(current.expression.name.text))) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (isDbOrHttpCall(current, context)) {
|
|
129
211
|
queryCall = current;
|
|
130
212
|
return;
|
|
131
213
|
}
|
|
132
214
|
ts.forEachChild(current, findQuery);
|
|
133
215
|
}
|
|
134
216
|
findQuery(node.body);
|
|
217
|
+
queryCall ??= delegatedResolverQuery(node, context);
|
|
135
218
|
if (queryCall) {
|
|
136
219
|
context.report(queryCall, 'graphql-n-plus-one-resolver', 'GraphQL field resolver executes a database or HTTP query per parent item without DataLoader batching.');
|
|
137
220
|
}
|
|
138
221
|
}
|
|
139
222
|
// --- Rule 4: react-context-value-recreated -------------------------------------------------------
|
|
223
|
+
function isReactContextExpression(expr, context) {
|
|
224
|
+
const type = context.checker.getTypeAtLocation(expr);
|
|
225
|
+
const typeSymbol = type.getSymbol();
|
|
226
|
+
const rawSymbol = context.checker.getSymbolAtLocation(expr);
|
|
227
|
+
const symbol = rawSymbol && rawSymbol.flags & ts.SymbolFlags.Alias ? context.checker.getAliasedSymbol(rawSymbol) : rawSymbol;
|
|
228
|
+
const decl = symbol?.valueDeclaration;
|
|
229
|
+
const init = decl && ts.isVariableDeclaration(decl) && decl.initializer ? ts.skipOuterExpressions(decl.initializer) : undefined;
|
|
230
|
+
if (init && ts.isObjectLiteralExpression(init))
|
|
231
|
+
return false;
|
|
232
|
+
// An application type merely named `Context` is not React's; React's lives in a declaration file.
|
|
233
|
+
const isReactNamedContext = typeSymbol?.name === 'Context' &&
|
|
234
|
+
(typeSymbol.declarations ?? []).some((declaration) => declaration.getSourceFile().isDeclarationFile);
|
|
235
|
+
if (isReactNamedContext || (type.getProperty('Provider') && type.getProperty('Consumer'))) {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
if (init && ts.isCallExpression(init)) {
|
|
239
|
+
const callee = ts.skipOuterExpressions(init.expression);
|
|
240
|
+
const name = ts.isIdentifier(callee)
|
|
241
|
+
? callee.text
|
|
242
|
+
: ts.isPropertyAccessExpression(callee)
|
|
243
|
+
? callee.name.text
|
|
244
|
+
: undefined;
|
|
245
|
+
if (name !== 'createContext')
|
|
246
|
+
return false;
|
|
247
|
+
// A local function with a body is the application's own factory, not React's createContext.
|
|
248
|
+
const calleeSymbol = ts.isIdentifier(callee) ? context.checker.getSymbolAtLocation(callee) : undefined;
|
|
249
|
+
return !calleeSymbol?.declarations?.some((declaration) => ts.isFunctionDeclaration(declaration) && declaration.body);
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
140
253
|
function checkReactContextValueRecreated(node, context) {
|
|
141
|
-
|
|
254
|
+
const targetExpr = ts.isPropertyAccessExpression(node.tagName) && node.tagName.name.text === 'Provider'
|
|
255
|
+
? node.tagName.expression
|
|
256
|
+
: node.tagName;
|
|
257
|
+
if (!ts.isExpression(targetExpr))
|
|
142
258
|
return;
|
|
259
|
+
let valueAttr;
|
|
143
260
|
for (const attr of node.attributes.properties) {
|
|
144
261
|
if (!ts.isJsxAttribute(attr) || !ts.isIdentifier(attr.name) || attr.name.text !== 'value')
|
|
145
262
|
continue;
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const expr = ts.skipOuterExpressions(attr.initializer.expression);
|
|
149
|
-
if (ts.isObjectLiteralExpression(expr) || ts.isArrayLiteralExpression(expr)) {
|
|
150
|
-
context.report(attr, 'react-context-value-recreated', 'Inline object or array literal in Context.Provider value creates a new reference on every render; wrap it in useMemo.');
|
|
151
|
-
}
|
|
263
|
+
valueAttr = attr;
|
|
264
|
+
break;
|
|
152
265
|
}
|
|
266
|
+
if (!valueAttr?.initializer || !ts.isJsxExpression(valueAttr.initializer) || !valueAttr.initializer.expression)
|
|
267
|
+
return;
|
|
268
|
+
const expr = ts.skipOuterExpressions(valueAttr.initializer.expression);
|
|
269
|
+
if (!ts.isObjectLiteralExpression(expr) && !ts.isArrayLiteralExpression(expr))
|
|
270
|
+
return;
|
|
271
|
+
// Module-level elements are evaluated once, so their literal is not recreated per render.
|
|
272
|
+
if (!ts.findAncestor(node, ts.isFunctionLike))
|
|
273
|
+
return;
|
|
274
|
+
if (!isReactContextExpression(targetExpr, context))
|
|
275
|
+
return;
|
|
276
|
+
context.report(valueAttr, 'react-context-value-recreated', 'Inline object or array literal in Context.Provider value creates a new reference on every render; wrap it in useMemo.');
|
|
153
277
|
}
|
|
154
278
|
// --- Rule 6: json-stringify-in-loop --------------------------------------------------------------
|
|
155
279
|
function bindingIdentifiers(name, into) {
|
|
@@ -189,8 +313,13 @@ function dependsOnIteration(value, serialize, loop, checker) {
|
|
|
189
313
|
return false;
|
|
190
314
|
function resolvesToIteration(expression, depth = 0, allowObjectLiteral = false) {
|
|
191
315
|
const origin = unwrapExpression(expression);
|
|
192
|
-
if (ts.isPropertyAccessExpression(origin)
|
|
316
|
+
if (ts.isPropertyAccessExpression(origin))
|
|
193
317
|
return resolvesToIteration(origin.expression, depth);
|
|
318
|
+
if (ts.isElementAccessExpression(origin))
|
|
319
|
+
return resolvesToIteration(origin.expression, depth) || resolvesToIteration(origin.argumentExpression, depth);
|
|
320
|
+
if (ts.isCallExpression(origin))
|
|
321
|
+
return (resolvesToIteration(origin.expression, depth) ||
|
|
322
|
+
origin.arguments.some((argument) => resolvesToIteration(argument, depth)));
|
|
194
323
|
if (allowObjectLiteral && ts.isObjectLiteralExpression(origin)) {
|
|
195
324
|
const names = origin.properties.map((property) => staticPropertyName(property.name));
|
|
196
325
|
if (names.includes(undefined) || new Set(names).size !== names.length)
|
|
@@ -249,6 +378,26 @@ function isSavedForOutput(declaration, checker) {
|
|
|
249
378
|
const references = (identifiersByName(statement.parent).get(name.text) ?? []).filter((candidate) => candidate !== name && valueSymbol(candidate, checker) === symbol);
|
|
250
379
|
return references.length === 1 && isOutputArgument(enclosingText(references[0]));
|
|
251
380
|
}
|
|
381
|
+
// Unwraps returned object/array/conditional expressions inside a mapping callback so per-item
|
|
382
|
+
// field serialization like `logs.map(log => ({ body: JSON.stringify(log.body) }))` is recognized.
|
|
383
|
+
function enclosingMappedValue(node) {
|
|
384
|
+
let value = enclosingText(node);
|
|
385
|
+
for (let parent = value.parent; parent; value = parent, parent = parent.parent) {
|
|
386
|
+
if (ts.isParenthesizedExpression(parent) ||
|
|
387
|
+
ts.isPropertyAssignment(parent) ||
|
|
388
|
+
ts.isObjectLiteralExpression(parent) ||
|
|
389
|
+
ts.isArrayLiteralExpression(parent) ||
|
|
390
|
+
ts.isAsExpression(parent) ||
|
|
391
|
+
ts.isSatisfiesExpression(parent) ||
|
|
392
|
+
ts.isTypeAssertionExpression(parent) ||
|
|
393
|
+
ts.isNonNullExpression(parent) ||
|
|
394
|
+
(ts.isConditionalExpression(parent) && (parent.whenTrue === value || parent.whenFalse === value))) {
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
return value;
|
|
398
|
+
}
|
|
399
|
+
return value;
|
|
400
|
+
}
|
|
252
401
|
// The value a pass produces: what a `map` or `flatMap` callback returns, or what it pushes or writes.
|
|
253
402
|
// Serializing each item there cannot move out of the loop.
|
|
254
403
|
function isPerItemOutput(call, loop, checker) {
|
|
@@ -263,25 +412,57 @@ function isPerItemOutput(call, loop, checker) {
|
|
|
263
412
|
if (!MAPPING_METHODS.has(loop.expression.name.text))
|
|
264
413
|
return false;
|
|
265
414
|
const callback = loop.arguments[0];
|
|
266
|
-
|
|
415
|
+
const mapped = enclosingMappedValue(call);
|
|
416
|
+
if (ts.isArrowFunction(callback) &&
|
|
417
|
+
!ts.isBlock(callback.body) &&
|
|
418
|
+
ts.isExpression(mapped) &&
|
|
419
|
+
unwrapExpression(callback.body) === unwrapExpression(mapped)) {
|
|
267
420
|
return true;
|
|
268
|
-
|
|
421
|
+
}
|
|
422
|
+
return ts.isReturnStatement(mapped.parent) && ts.findAncestor(mapped.parent, ts.isFunctionLike) === callback;
|
|
269
423
|
}
|
|
270
424
|
// A `for` initializer and a `for...of` iterable are evaluated once, before the first pass.
|
|
271
425
|
function runsOncePerLoop(node, loop) {
|
|
272
426
|
const setup = ts.isForStatement(loop) ? loop.initializer : ts.isForOfStatement(loop) ? loop.expression : undefined;
|
|
273
427
|
return setup !== undefined && node.pos >= setup.pos && node.end <= setup.end;
|
|
274
428
|
}
|
|
429
|
+
function isEscapingThrow(call, loop) {
|
|
430
|
+
if (ts.isCallExpression(loop))
|
|
431
|
+
return false;
|
|
432
|
+
const statement = ts.findAncestor(call, (node) => ts.isThrowStatement(node) || ts.isFunctionLike(node));
|
|
433
|
+
if (!statement || !ts.isThrowStatement(statement))
|
|
434
|
+
return false;
|
|
435
|
+
for (let child = statement; child.parent && !ts.isFunctionLike(child); child = child.parent) {
|
|
436
|
+
const parent = child.parent;
|
|
437
|
+
if (!ts.isTryStatement(parent))
|
|
438
|
+
continue;
|
|
439
|
+
if ((parent.tryBlock === child && parent.catchClause) || (parent.finallyBlock && parent.finallyBlock !== child))
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
function isJsonEquality(call) {
|
|
445
|
+
const parent = enclosingText(call).parent;
|
|
446
|
+
return (ts.isBinaryExpression(parent) &&
|
|
447
|
+
[
|
|
448
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
449
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
450
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
451
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
452
|
+
].includes(parent.operatorToken.kind));
|
|
453
|
+
}
|
|
275
454
|
function checkJsonStringifyInLoop(call, context, cliReason) {
|
|
276
455
|
if (!isGlobalMethodCall(call, 'JSON', 'stringify', context.checker))
|
|
277
456
|
return;
|
|
278
457
|
let loop = enclosingLoop(call);
|
|
279
458
|
while (loop && runsOncePerLoop(call, loop))
|
|
280
459
|
loop = enclosingLoop(loop);
|
|
281
|
-
if (!loop)
|
|
460
|
+
if (!loop || isEscapingThrow(call, loop))
|
|
282
461
|
return;
|
|
283
462
|
const [value] = call.arguments;
|
|
284
|
-
if (value &&
|
|
463
|
+
if (value &&
|
|
464
|
+
(isPerItemOutput(call, loop, context.checker) || isJsonEquality(call)) &&
|
|
465
|
+
dependsOnIteration(value, call, loop, context.checker))
|
|
285
466
|
return;
|
|
286
467
|
context.report(call, 'json-stringify-in-loop', 'JSON.stringify runs inside a loop; serialize once outside the loop or stream the output.', cliReason);
|
|
287
468
|
}
|
|
@@ -292,7 +473,7 @@ function phase5CliReason(sourceFile, context) {
|
|
|
292
473
|
}
|
|
293
474
|
function checkPhase5PerfNode(node, sourceFile, context, cliReason) {
|
|
294
475
|
if (ts.isCallExpression(node)) {
|
|
295
|
-
checkUnboundedPromiseAll(node,
|
|
476
|
+
checkUnboundedPromiseAll(node, context, cliReason);
|
|
296
477
|
checkMongooseMissingLean(node, sourceFile, context);
|
|
297
478
|
checkNodeEventListenerLeak(node, context);
|
|
298
479
|
checkJsonStringifyInLoop(node, context, cliReason);
|