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.
Files changed (278) hide show
  1. package/README.md +175 -115
  2. package/dist/bin/loadwise.js +132 -67
  3. package/dist/lib/accessibility/analyze.js +5 -3
  4. package/dist/lib/accessibility/angular-inline.js +9 -11
  5. package/dist/lib/accessibility/angular-literal.js +39 -0
  6. package/dist/lib/accessibility/form-context.js +36 -0
  7. package/dist/lib/accessibility/frame-head.js +92 -0
  8. package/dist/lib/accessibility/frame-viewport.js +149 -0
  9. package/dist/lib/accessibility/frontend-detect.js +6 -5
  10. package/dist/lib/accessibility/jsx-elements.js +13 -3
  11. package/dist/lib/accessibility/markup-files.js +7 -2
  12. package/dist/lib/accessibility/rule-ids.js +3 -0
  13. package/dist/lib/accessibility/rules.js +128 -41
  14. package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
  15. package/dist/lib/accessibility/template-attributes.js +5 -3
  16. package/dist/lib/accessibility/template-elements.js +25 -6
  17. package/dist/lib/accessibility/template-file.js +40 -25
  18. package/dist/lib/accessibility/usage.js +10 -2
  19. package/dist/lib/editor/accessibility-examples.js +6 -1
  20. package/dist/lib/editor/actions.js +46 -7
  21. package/dist/lib/editor/apply-fix.js +69 -4
  22. package/dist/lib/editor/configured-setup.js +63 -0
  23. package/dist/lib/editor/diagnostics.js +206 -17
  24. package/dist/lib/editor/dismissals.js +85 -26
  25. package/dist/lib/editor/editor-document.js +5 -2
  26. package/dist/lib/editor/editor-launcher.js +170 -0
  27. package/dist/lib/editor/fix-edits.js +19 -0
  28. package/dist/lib/editor/jetbrains-setup.js +53 -0
  29. package/dist/lib/editor/language-server.js +80 -24
  30. package/dist/lib/editor/managed-config.js +37 -0
  31. package/dist/lib/editor/native-setup.js +86 -0
  32. package/dist/lib/editor/perf-watch.js +8 -6
  33. package/dist/lib/editor/release-vsix.js +61 -0
  34. package/dist/lib/editor/reliability-examples.js +15 -0
  35. package/dist/lib/editor/scan-session.js +29 -8
  36. package/dist/lib/editor/server-commands.js +38 -9
  37. package/dist/lib/editor/setup-server.js +87 -0
  38. package/dist/lib/editor/setup-vim.js +63 -0
  39. package/dist/lib/editor/setup.js +131 -0
  40. package/dist/lib/editor/stdio-setup.js +6 -0
  41. package/dist/lib/editor/workspace-files.js +61 -7
  42. package/dist/lib/editor/workspace-paths.js +41 -1
  43. package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
  44. package/dist/lib/maintainability/architecture/architecture.js +4 -4
  45. package/dist/lib/maintainability/cli/changed-lines.js +20 -2
  46. package/dist/lib/maintainability/cli/inputs.js +12 -3
  47. package/dist/lib/maintainability/cli.js +18 -5
  48. package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
  49. package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
  50. package/dist/lib/maintainability/examples.js +4 -0
  51. package/dist/lib/maintainability/quality/functions.js +9 -6
  52. package/dist/lib/maintainability/quality/limits.js +19 -0
  53. package/dist/lib/maintainability/quality/size-limits.js +8 -2
  54. package/dist/lib/maintainability/quality/stray-console.js +62 -1
  55. package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
  56. package/dist/lib/maintainability/rule-ids.js +3 -0
  57. package/dist/lib/maintainability/scan-changed.js +4 -1
  58. package/dist/lib/maintainability/scan-worker.js +6 -2
  59. package/dist/lib/maintainability/scan.js +21 -8
  60. package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
  61. package/dist/lib/maintainability/unused/module-usage.js +43 -21
  62. package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
  63. package/dist/lib/maintainability/unused/unused-modules.js +5 -10
  64. package/dist/lib/performance/analysis/source-inputs.js +7 -2
  65. package/dist/lib/performance/analysis/source-options.js +7 -6
  66. package/dist/lib/performance/analyze.js +16 -3
  67. package/dist/lib/performance/cli/changed-lines.js +8 -1
  68. package/dist/lib/performance/cli/cli-arguments.js +19 -8
  69. package/dist/lib/performance/cli/cli-output.js +4 -0
  70. package/dist/lib/performance/cli/cli-scan.js +21 -2
  71. package/dist/lib/performance/cli.js +16 -7
  72. package/dist/lib/performance/collections/memory-growth.js +55 -12
  73. package/dist/lib/performance/contracts/openapi.js +13 -4
  74. package/dist/lib/performance/datastores/pg-integrity.js +68 -22
  75. package/dist/lib/performance/datastores/pg-session-release.js +114 -0
  76. package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
  77. package/dist/lib/performance/datastores/pooling.js +62 -3
  78. package/dist/lib/performance/datastores/redis.js +94 -18
  79. package/dist/lib/performance/datastores/session-store.js +2 -1
  80. package/dist/lib/performance/db/checks.js +19 -8
  81. package/dist/lib/performance/db/gateway.js +3 -1
  82. package/dist/lib/performance/db/plan-estimates.js +4 -4
  83. package/dist/lib/performance/engine/native-syntax.js +3 -0
  84. package/dist/lib/performance/engine/native.js +11 -7
  85. package/dist/lib/performance/evidence/plans.js +7 -5
  86. package/dist/lib/performance/evidence/traces.js +21 -1
  87. package/dist/lib/performance/frontend/angular-teardown.js +96 -0
  88. package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
  89. package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
  90. package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
  91. package/dist/lib/performance/frontend/frontend-perf.js +149 -74
  92. package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
  93. package/dist/lib/performance/migrations/extract-code.js +13 -5
  94. package/dist/lib/performance/migrations/extract-sql.js +82 -38
  95. package/dist/lib/performance/migrations/operations.js +56 -13
  96. package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
  97. package/dist/lib/performance/migrations/postgres-registry.js +127 -26
  98. package/dist/lib/performance/migrations/postgres-settings.js +78 -0
  99. package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
  100. package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
  101. package/dist/lib/performance/migrations/rules-postgres.js +88 -39
  102. package/dist/lib/performance/orm/offset-pagination.js +6 -4
  103. package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
  104. package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
  105. package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
  106. package/dist/lib/performance/polyglot.js +111 -14
  107. package/dist/lib/performance/projects.js +3 -3
  108. package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
  109. package/dist/lib/performance/request/backend-mastery.js +151 -42
  110. package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
  111. package/dist/lib/performance/request/fastify-receivers.js +93 -0
  112. package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
  113. package/dist/lib/performance/request/mongoose-lean.js +298 -46
  114. package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
  115. package/dist/lib/performance/request/phase5-ast.js +6 -7
  116. package/dist/lib/performance/request/phase5-rules.js +204 -23
  117. package/dist/lib/performance/request/request-handlers.js +120 -23
  118. package/dist/lib/performance/request/sql-select-star.js +150 -0
  119. package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
  120. package/dist/lib/performance/request/upload-limits.js +5 -1
  121. package/dist/lib/performance/site-fix.js +4 -4
  122. package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
  123. package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
  124. package/dist/lib/performance/site-fixes/collections.js +1 -2
  125. package/dist/lib/performance/site-fixes/frontend-fixes.js +358 -14
  126. package/dist/lib/performance/site-fixes/helpers.js +3 -4
  127. package/dist/lib/performance/site-fixes/integrity.js +2 -4
  128. package/dist/lib/performance/site-fixes/migration-context.js +8 -1
  129. package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
  130. package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
  131. package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
  132. package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
  133. package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
  134. package/dist/lib/performance/sql/mysql-tokens.js +38 -12
  135. package/dist/lib/reliability/boundary-access.js +114 -57
  136. package/dist/lib/reliability/react-bugs.js +29 -7
  137. package/dist/lib/reliability/resource-leak-intervals.js +258 -0
  138. package/dist/lib/reliability/resource-leaks.js +209 -195
  139. package/dist/lib/reliability/rule-ids.js +10 -1
  140. package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
  141. package/dist/lib/reliability/swallowed-errors.js +107 -21
  142. package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
  143. package/dist/lib/reliability/usage.js +16 -5
  144. package/dist/lib/reliability/walk.js +3 -2
  145. package/dist/lib/security/auth-crypto/hashing.js +24 -8
  146. package/dist/lib/security/auth-crypto/jwt.js +21 -6
  147. package/dist/lib/security/auth-crypto/secrets.js +44 -1
  148. package/dist/lib/security/auth-crypto/shared.js +3 -1
  149. package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
  150. package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
  151. package/dist/lib/security/cli.js +21 -6
  152. package/dist/lib/security/cookie-credentials.js +115 -0
  153. package/dist/lib/security/cookie-csrf.js +241 -0
  154. package/dist/lib/security/deps/advisory.js +50 -109
  155. package/dist/lib/security/deps/cli.js +1 -1
  156. package/dist/lib/security/deps/cvss-score.js +14 -0
  157. package/dist/lib/security/deps/fixed-version.js +97 -0
  158. package/dist/lib/security/deps/lockfiles.js +135 -74
  159. package/dist/lib/security/deps/osv.js +11 -3
  160. package/dist/lib/security/deps/scan.js +47 -20
  161. package/dist/lib/security/deps/yarn-lock.js +158 -0
  162. package/dist/lib/security/examples/injection.cjs +4 -0
  163. package/dist/lib/security/exposure/cookies.js +17 -2
  164. package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
  165. package/dist/lib/security/exposure/frontend-security.js +21 -14
  166. package/dist/lib/security/exposure/jsx-url.js +18 -33
  167. package/dist/lib/security/exposure/logging.js +49 -10
  168. package/dist/lib/security/exposure/prototype-pollution.js +126 -56
  169. package/dist/lib/security/exposure/react-html.js +26 -0
  170. package/dist/lib/security/exposure/reflected-xss.js +34 -5
  171. package/dist/lib/security/files/compose.js +126 -1
  172. package/dist/lib/security/files/dockerfile.js +62 -4
  173. package/dist/lib/security/files/index.js +1 -1
  174. package/dist/lib/security/injection/command.js +46 -18
  175. package/dist/lib/security/injection/index.js +2 -0
  176. package/dist/lib/security/injection/ldap.js +86 -0
  177. package/dist/lib/security/injection/nosql.js +28 -4
  178. package/dist/lib/security/injection/redos.js +1 -1
  179. package/dist/lib/security/injection/rule-ids.js +1 -0
  180. package/dist/lib/security/injection/shared.js +5 -1
  181. package/dist/lib/security/injection/sql.js +28 -13
  182. package/dist/lib/security/lexical-declaration.js +113 -0
  183. package/dist/lib/security/pg-dynamic-query.js +28 -1
  184. package/dist/lib/security/pg-identifier-guards.js +107 -3
  185. package/dist/lib/security/phase5-rules.js +26 -135
  186. package/dist/lib/security/polyglot.js +2 -2
  187. package/dist/lib/security/redirect-settings.js +98 -0
  188. package/dist/lib/security/request-forgery/open-redirect.js +18 -5
  189. package/dist/lib/security/request-forgery/path-traversal.js +84 -6
  190. package/dist/lib/security/request-forgery/shared.js +1 -7
  191. package/dist/lib/security/request-forgery/ssrf.js +176 -14
  192. package/dist/lib/security/rule-descriptions.js +5 -1
  193. package/dist/lib/security/rule-ids.js +7 -0
  194. package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
  195. package/dist/lib/security/site-fixes/exposure.js +21 -3
  196. package/dist/lib/security/site-fixes/files.js +11 -13
  197. package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
  198. package/dist/lib/security/site-fixes/injection.js +17 -4
  199. package/dist/lib/security/site-fixes/jwt.js +1 -20
  200. package/dist/lib/security/site-fixes/request-forgery.js +37 -29
  201. package/dist/lib/security/taint/binding-inputs.js +236 -0
  202. package/dist/lib/security/taint/filename-guard.js +45 -45
  203. package/dist/lib/security/taint/guards.js +14 -8
  204. package/dist/lib/security/taint/index.js +126 -154
  205. package/dist/lib/security/taint/propagate.js +43 -9
  206. package/dist/lib/security/taint/record-writes.js +161 -0
  207. package/dist/lib/security/taint/resolution-cache.js +13 -7
  208. package/dist/lib/security/taint/sanitizers.js +89 -5
  209. package/dist/lib/security/taint/trace-nodes.js +61 -0
  210. package/dist/lib/security/taint/validators.js +89 -9
  211. package/dist/lib/shared/agent/agent-init.js +28 -5
  212. package/dist/lib/shared/agent/agent-prompt.js +74 -0
  213. package/dist/lib/shared/agent/agent-session.js +6 -1
  214. package/dist/lib/shared/agent/agent-verify.js +58 -11
  215. package/dist/lib/shared/agent/git-hook.js +17 -6
  216. package/dist/lib/shared/agent/review-delivery.js +1 -1
  217. package/dist/lib/shared/agent/review-mcp.js +8 -2
  218. package/dist/lib/shared/analysis/ast-semantics.js +2 -2
  219. package/dist/lib/shared/analysis/scan-cache.js +99 -14
  220. package/dist/lib/shared/check.js +172 -20
  221. package/dist/lib/shared/cli-dispatch.js +17 -0
  222. package/dist/lib/shared/config.js +27 -10
  223. package/dist/lib/shared/explain.js +1 -1
  224. package/dist/lib/shared/fix/fix-convergence.js +19 -31
  225. package/dist/lib/shared/fix/fix-handoff.js +16 -11
  226. package/dist/lib/shared/fix/fix-imports.js +4 -162
  227. package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
  228. package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
  229. package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
  230. package/dist/lib/shared/fix/fix-presentation.js +9 -6
  231. package/dist/lib/shared/fix/fix-safety.js +32 -1
  232. package/dist/lib/shared/fix/fix-scope.js +82 -0
  233. package/dist/lib/shared/fix/fix-terminal.js +28 -1
  234. package/dist/lib/shared/fix/fix-typecheck.js +8 -4
  235. package/dist/lib/shared/fix/import-text-edits.js +148 -0
  236. package/dist/lib/shared/fix/project-file-access.js +17 -26
  237. package/dist/lib/shared/fix/read-fix-text.js +20 -0
  238. package/dist/lib/shared/fix.js +241 -104
  239. package/dist/lib/shared/formats/finding-location.js +4 -1
  240. package/dist/lib/shared/formats/github.js +3 -3
  241. package/dist/lib/shared/formats/sarif.js +9 -3
  242. package/dist/lib/shared/report-fp.js +97 -0
  243. package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
  244. package/dist/lib/shared/reporting/baseline.js +7 -5
  245. package/dist/lib/shared/reporting/check-text.js +50 -0
  246. package/dist/lib/shared/reporting/pillars.js +54 -19
  247. package/dist/lib/shared/reporting/source-report.js +3 -0
  248. package/dist/lib/shared/system/atomic-write.js +5 -2
  249. package/dist/lib/shared/system/glob.js +34 -8
  250. package/dist/lib/shared/system/sentry.js +222 -0
  251. package/dist/lib/shared/system/shell-argument.js +5 -1
  252. package/dist/lib/shared/system/terminal-safe.js +3 -2
  253. package/dist/lib/shared/system/terminal-style.js +131 -0
  254. package/dist/lib/shared/workspaces.js +15 -3
  255. package/docs/assets/cli-workflow.gif +0 -0
  256. package/integrations/sublime/plugin.py +22 -0
  257. package/integrations/zed/Cargo.lock +845 -0
  258. package/integrations/zed/Cargo.toml +11 -0
  259. package/integrations/zed/extension.toml +11 -0
  260. package/integrations/zed/src/lib.rs +25 -0
  261. package/package.json +15 -3
  262. package/plugin/.claude-plugin/plugin.json +1 -1
  263. package/plugin/commands/loadwise.md +11 -6
  264. package/plugin/hooks/check-edited-file.mjs +27 -5
  265. package/polyglot/performance/rules/csharp.yml +34 -5
  266. package/polyglot/performance/rules/java.yml +24 -1
  267. package/polyglot/performance/rules/python.yml +74 -2
  268. package/polyglot/performance/sgconfig.yml +1 -0
  269. package/polyglot/reliability/rules/csharp.yml +12 -6
  270. package/polyglot/reliability/rules/go.yml +86 -6
  271. package/polyglot/reliability/rules/java.yml +22 -1
  272. package/polyglot/reliability/sgconfig.yml +1 -0
  273. package/polyglot/security/rules/go.yml +100 -22
  274. package/polyglot/security/rules/python.yml +74 -6
  275. package/polyglot/security/rules/rust.yml +65 -0
  276. package/polyglot/security/sgconfig.yml +1 -0
  277. package/schema/loadwise.config.schema.json +556 -76
  278. /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
@@ -11,11 +11,13 @@
11
11
  // frontend-render-perf.ts adds react-component-defined-in-component and angular-ngfor-missing-trackby.
12
12
  // Every rule rests on where a name comes from (its import), never on the name alone.
13
13
  import { ts } from '../engine/compiler.js';
14
+ import { effectCleanup, effectDisposes, releases } from './effect-cleanup.js';
15
+ import { angularTeardown, subscriptionField, thisField } from './angular-teardown.js';
14
16
  import { isTestSource } from '../analysis/review-context.js';
15
17
  import { declaredInPackage, initializerOf, packageOf, propertyName } from '../schema/ts-helpers.js';
16
18
  import { checkComponentInComponent, checkNgForTrackBy, isFunctionNode } from './frontend-render-perf.js';
17
19
  import { checkAddEventListener, checkHeavyBarrelImport, checkLayoutThrashing, isLoop } from './frontend-dom-perf.js';
18
- import { ANGULAR_MODULES, REACT_MODULES, VUE_MODULES, calleeName, containsReturnValue, effectCallback, importedName, isBrowserGlobal, isGlobalEventTarget, isVueImport, walkAll, walkSkippingFunctions, } from './frontend-helpers.js';
20
+ import { ANGULAR_MODULES, REACT_MODULES, VUE_MODULES, calleeName, effectCallback, importedName, isBrowserGlobal, isGlobalEventTarget, isVueImport, walkAll, walkSkippingFunctions, } from './frontend-helpers.js';
19
21
  const RXJS_OPERATOR_MODULES = ['rxjs', 'rxjs/operators', '@angular/core/rxjs-interop'];
20
22
  const DEFINITE_LEAK_CONSTRUCTORS = new Set(['WebSocket', 'EventSource']);
21
23
  const OBSERVER_CONSTRUCTORS = new Set(['ResizeObserver', 'IntersectionObserver', 'MutationObserver']);
@@ -25,12 +27,24 @@ const COMPLETING_OPERATORS = new Set([
25
27
  'takeUntilDestroyed',
26
28
  'take',
27
29
  'takeWhile',
28
- 'takeLast',
29
30
  'first',
30
- 'last',
31
31
  'single',
32
32
  'elementAt',
33
33
  ]);
34
+ const FLATTENING_OPERATORS = new Set([
35
+ 'concatAll',
36
+ 'concatMap',
37
+ 'concatMapTo',
38
+ 'exhaust',
39
+ 'exhaustAll',
40
+ 'exhaustMap',
41
+ 'mergeAll',
42
+ 'mergeMap',
43
+ 'mergeMapTo',
44
+ 'switchAll',
45
+ 'switchMap',
46
+ 'switchMapTo',
47
+ ]);
34
48
  const ENDLESS_PACKAGES = ['@ngrx/store', '@angular/forms'];
35
49
  const COMPLETING_PACKAGES = ['@angular/common'];
36
50
  const SUBSCRIBE_MAY_COMPLETE_REASON = 'The source may complete on its own (route params, single-emission or finite observables); dismiss if it does.';
@@ -41,30 +55,70 @@ const OBSERVER_REASON = 'An observer attached to an element the component owns i
41
55
  const ON_PUSH_REASON = 'OnPush skips checks for @Input objects that are mutated in place, so confirm the inputs are immutable or signal-driven first; zoneless apps do not need it.';
42
56
  // --- RxJS subscriptions
43
57
  // -------------------------------------------------------------------------------------
44
- // `source.pipe(a(), b()).asObservable()` -> the source expression plus the imported operator names
45
- // applied to it.
46
- function unwrapPipes(expression, checker) {
47
- const operatorNames = [];
58
+ // Keep the lifetime effects in pipeline order, including across separate pipe() calls.
59
+ function unwrapPipes(expression, checker, teardown) {
60
+ const effects = [];
48
61
  let source = ts.skipOuterExpressions(expression);
49
62
  while (ts.isCallExpression(source) && ts.isPropertyAccessExpression(source.expression)) {
50
63
  const method = source.expression.name.text;
51
64
  if (method === 'pipe')
52
- operatorNames.push(...importedOperatorNames(source.arguments, checker));
65
+ effects.unshift(...operatorEffects(source.arguments, checker, teardown));
53
66
  else if (method !== 'asObservable')
54
67
  break;
55
68
  source = ts.skipOuterExpressions(source.expression.expression);
56
69
  }
57
- return { source, operatorNames };
70
+ return { source, effects };
58
71
  }
59
- function importedOperatorNames(pipeArguments, checker) {
60
- const names = [];
72
+ function operatorEffects(pipeArguments, checker, teardown) {
73
+ const effects = [];
61
74
  for (const argument of pipeArguments) {
62
75
  const operator = ts.skipOuterExpressions(argument);
63
- const name = ts.isCallExpression(operator) && importedName(operator.expression, checker, RXJS_OPERATOR_MODULES);
64
- if (name)
65
- names.push(name);
76
+ if (!ts.isCallExpression(operator))
77
+ continue;
78
+ const name = importedName(operator.expression, checker, RXJS_OPERATOR_MODULES);
79
+ if (!name)
80
+ continue;
81
+ if (name === 'takeUntil' && teardown) {
82
+ const notifier = thisField(operator.arguments[0]);
83
+ if (notifier && teardown.ownedNotifiers.has(notifier) && !teardown.notifiers.has(notifier))
84
+ continue;
85
+ }
86
+ // RxJS implements a nonpositive takeLast count as an immediately completing stream.
87
+ if (COMPLETING_OPERATORS.has(name) || (name === 'takeLast' && isNonpositiveCount(operator.arguments[0]))) {
88
+ effects.push('complete');
89
+ }
90
+ else if (FLATTENING_OPERATORS.has(name) && !hasFiniteInnerStream(name, operator, checker)) {
91
+ effects.push('flatten');
92
+ }
93
+ }
94
+ return effects;
95
+ }
96
+ function hasFiniteInnerStream(name, operator, checker) {
97
+ const argument = operator.arguments[0] && ts.skipOuterExpressions(operator.arguments[0]);
98
+ if (!argument)
99
+ return false;
100
+ if (name.endsWith('MapTo'))
101
+ return isFiniteRxjsSource(argument, checker);
102
+ if (!name.endsWith('Map') || (!ts.isArrowFunction(argument) && !ts.isFunctionExpression(argument))) {
103
+ return false;
66
104
  }
67
- return names;
105
+ const returned = returnedExpressions(argument);
106
+ return returned.length > 0 && returned.every((value) => isFiniteRxjsSource(value, checker));
107
+ }
108
+ function isFiniteRxjsSource(value, checker) {
109
+ if (ts.isCallExpression(value))
110
+ return importedName(value.expression, checker, ['rxjs']) === 'of';
111
+ return importedName(value, checker, ['rxjs']) === 'EMPTY';
112
+ }
113
+ function isNonpositiveCount(argument) {
114
+ if (!argument)
115
+ return false;
116
+ const count = ts.skipOuterExpressions(argument);
117
+ if (ts.isNumericLiteral(count))
118
+ return Number(count.text) === 0;
119
+ return (ts.isPrefixUnaryExpression(count) &&
120
+ count.operator === ts.SyntaxKind.MinusToken &&
121
+ ts.isNumericLiteral(count.operand));
68
122
  }
69
123
  function isRxjsSubject(source, checker) {
70
124
  const symbol = checker.getTypeAtLocation(source).getSymbol();
@@ -94,27 +148,25 @@ function classifySource(source, checker) {
94
148
  }
95
149
  // undefined: not an RxJS subscription, or one that ends on its own. Otherwise 'endless' or
96
150
  // 'unknown'.
97
- function unmanagedSubscription(call, checker) {
151
+ function unmanagedSubscription(call, checker, teardown) {
98
152
  const callee = ts.skipOuterExpressions(call.expression);
99
153
  if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== 'subscribe')
100
154
  return;
101
155
  if (!declaredInPackage(callee, checker, ['rxjs']))
102
156
  return;
103
- const { source, operatorNames } = unwrapPipes(callee.expression, checker);
104
- if (operatorNames.some((name) => COMPLETING_OPERATORS.has(name)))
105
- return;
106
- const kind = classifySource(source, checker);
157
+ const { source, effects } = unwrapPipes(callee.expression, checker, teardown);
158
+ let kind = classifySource(source, checker);
159
+ for (const effect of effects) {
160
+ if (effect === 'complete')
161
+ kind = 'completing';
162
+ // Completing the outer stream can leave an active inner subscription.
163
+ else if (kind === 'completing')
164
+ kind = 'unknown';
165
+ }
107
166
  return kind === 'completing' ? undefined : kind;
108
167
  }
109
168
  // --- React
110
169
  // ---------------------------------------------------------------------------------------------------
111
- function leakedEffectResource(body, checker) {
112
- let leak;
113
- walkSkippingFunctions(body, (node) => {
114
- leak ??= leakOfNode(node, checker);
115
- });
116
- return leak;
117
- }
118
170
  function leakOfNode(node, checker) {
119
171
  if (ts.isNewExpression(node))
120
172
  return leakOfConstructor(node, checker);
@@ -122,39 +174,53 @@ function leakOfNode(node, checker) {
122
174
  return;
123
175
  const name = calleeName(node);
124
176
  if (name === 'setInterval' && isBrowserGlobal(node.expression, checker))
125
- return { label: 'setInterval()' };
177
+ return { label: 'setInterval()', release: 'clearInterval' };
126
178
  const callee = ts.skipOuterExpressions(node.expression);
127
179
  if (name === 'addEventListener' &&
128
180
  ts.isPropertyAccessExpression(callee) &&
129
181
  isGlobalEventTarget(callee.expression, checker)) {
130
- return { label: 'addEventListener()' };
182
+ return { label: 'addEventListener()', release: 'removeEventListener' };
131
183
  }
132
184
  const kind = unmanagedSubscription(node, checker);
133
185
  if (kind)
134
- return { label: 'subscribe()', reason: kind === 'unknown' ? SUBSCRIBE_MAY_COMPLETE_REASON : undefined };
186
+ return {
187
+ label: 'subscribe()',
188
+ release: 'unsubscribe',
189
+ reason: kind === 'unknown' ? SUBSCRIBE_MAY_COMPLETE_REASON : undefined,
190
+ };
135
191
  }
136
192
  function leakOfConstructor(node, checker) {
137
193
  if (!ts.isIdentifier(node.expression) || !isBrowserGlobal(node.expression, checker))
138
194
  return;
139
195
  const name = node.expression.text;
140
196
  if (DEFINITE_LEAK_CONSTRUCTORS.has(name))
141
- return { label: `new ${name}()` };
197
+ return { label: `new ${name}()`, release: 'close' };
142
198
  if (OBSERVER_CONSTRUCTORS.has(name))
143
- return { label: `new ${name}()`, reason: OBSERVER_REASON };
144
- }
145
- function findFetchWithoutAbort(body, checker) {
146
- let fetchCall;
147
- let hasAbort = false;
148
- walkAll(body, (node) => {
149
- if (ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'AbortController') {
150
- hasAbort = true;
151
- }
152
- else if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'fetch') {
153
- if (isBrowserGlobal(node.expression, checker) && !passesSignal(node))
154
- fetchCall ??= node;
155
- }
199
+ return { label: `new ${name}()`, release: 'disconnect', reason: OBSERVER_REASON };
200
+ }
201
+ function cleanupInvalidatesFetch(cleanup, call, checker) {
202
+ // The stale-response flag belongs to this request only if its promise chain reads that flag.
203
+ let chain = call;
204
+ while (ts.isPropertyAccessExpression(chain.parent) &&
205
+ chain.parent.expression === chain &&
206
+ ts.isCallExpression(chain.parent.parent))
207
+ chain = chain.parent.parent;
208
+ return effectDisposes(cleanup, call, checker, (expression) => {
209
+ if (!ts.isBinaryExpression(expression) ||
210
+ expression.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
211
+ !ts.isIdentifier(expression.left) ||
212
+ ![ts.SyntaxKind.FalseKeyword, ts.SyntaxKind.TrueKeyword].includes(expression.right.kind))
213
+ return false;
214
+ const symbol = checker.getSymbolAtLocation(expression.left);
215
+ if (!symbol)
216
+ return false;
217
+ let read = false;
218
+ walkAll(chain, (node) => {
219
+ if (ts.isIdentifier(node) && checker.getSymbolAtLocation(node) === symbol)
220
+ read = true;
221
+ });
222
+ return read;
156
223
  });
157
- return hasAbort ? undefined : fetchCall;
158
224
  }
159
225
  function passesSignal(fetchCall) {
160
226
  const options = fetchCall.arguments[1] && ts.skipOuterExpressions(fetchCall.arguments[1]);
@@ -189,21 +255,34 @@ function isFireAndForgetMutation(fetchCall) {
189
255
  }
190
256
  function checkReactEffect(call, context) {
191
257
  const effect = effectCallback(call, context.checker);
192
- if (!effect || containsReturnValue(effect.body))
193
- return;
194
- const leak = leakedEffectResource(effect.body, context.checker);
195
- if (leak) {
196
- context.report(call, 'react-missing-cleanup', `\`${effect.hook}\` allocates \`${leak.label}\` without returning a cleanup function, leaking listeners, timers, or subscriptions across remounts. Return a cleanup callback that disposes it.`, leak.reason);
197
- }
198
- const fetchCall = findFetchWithoutAbort(effect.body, context.checker);
199
- if (!fetchCall)
258
+ if (!effect)
200
259
  return;
260
+ const cleanup = effectCleanup(effect.body);
261
+ walkSkippingFunctions(effect.body, (node) => {
262
+ const leak = leakOfNode(node, context.checker);
263
+ if (!leak ||
264
+ effectDisposes(cleanup, node, context.checker, (release) => releases(node, leak.release, release, context.checker)))
265
+ return;
266
+ context.report(node, 'react-missing-cleanup', `\`${effect.hook}\` allocates \`${leak.label}\` without matching teardown. Return a cleanup callback that disposes this resource.`, leak.reason);
267
+ });
268
+ walkAll(effect.body, (node) => {
269
+ if (!ts.isCallExpression(node) ||
270
+ !ts.isIdentifier(node.expression) ||
271
+ node.expression.text !== 'fetch' ||
272
+ !isBrowserGlobal(node.expression, context.checker) ||
273
+ passesSignal(node) ||
274
+ cleanupInvalidatesFetch(cleanup, node, context.checker))
275
+ return;
276
+ reportFetch(node, call, effect.hook, context);
277
+ });
278
+ }
279
+ function reportFetch(fetchCall, call, hook, context) {
201
280
  const reason = isFireAndForgetMutation(fetchCall)
202
281
  ? 'Fire-and-forget mutating request whose response is unused.'
203
282
  : hasNonEmptyDependencies(call)
204
283
  ? undefined
205
284
  : FETCH_STATIC_DEPS_REASON;
206
- context.report(fetchCall, 'react-fetch-in-effect-no-abort', `\`fetch()\` inside \`${effect.hook}\` has no \`AbortController\` cleanup, so fast navigation or dependency changes can trigger race conditions and stale state updates. Pass \`signal: controller.signal\` and call \`controller.abort()\` in the cleanup function.`, reason);
285
+ context.report(fetchCall, 'react-fetch-in-effect-no-abort', `\`fetch()\` inside \`${hook}\` has no \`AbortController\` cleanup, so fast navigation or dependency changes can trigger race conditions and stale state updates. Pass \`signal: controller.signal\` and call \`controller.abort()\` in the cleanup function.`, reason);
207
286
  }
208
287
  function isArrayExpression(expression, checker) {
209
288
  const type = checker.getNonNullableType(checker.getTypeAtLocation(expression));
@@ -330,20 +409,6 @@ function checkAngularChangeDetection(classNode, context) {
330
409
  return;
331
410
  context.report(component.decorator, 'angular-default-change-detection', 'This Angular `@Component` uses the default change detection strategy, re-checking the entire component subtree on every browser event. Set `changeDetection: ChangeDetectionStrategy.OnPush` where inputs are immutable or driven by observables/signals.', ON_PUSH_REASON);
332
411
  }
333
- function destroyHookReleases(classNode) {
334
- const hook = classNode.members.find((member) => ts.isMethodDeclaration(member) && ts.isIdentifier(member.name) && member.name.text === 'ngOnDestroy');
335
- if (!hook?.body)
336
- return false;
337
- let releases = false;
338
- walkAll(hook.body, (node) => {
339
- if (ts.isCallExpression(node)) {
340
- const name = calleeName(node);
341
- if (name !== undefined && ['unsubscribe', 'next', 'complete'].includes(name))
342
- releases = true;
343
- }
344
- });
345
- return releases;
346
- }
347
412
  function checkAngularClass(classNode, context) {
348
413
  if (!ts.canHaveDecorators(classNode) || !ts.getDecorators(classNode)?.length)
349
414
  return;
@@ -353,15 +418,17 @@ function checkAngularClass(classNode, context) {
353
418
  checkNgForTrackBy(component.metadata, context);
354
419
  if (!isAngularComponentOrDirective(classNode, context.checker))
355
420
  return;
356
- if (destroyHookReleases(classNode))
357
- return;
421
+ const teardown = angularTeardown(classNode, context.checker);
358
422
  walkAll(classNode, (node) => {
359
423
  if (!ts.isCallExpression(node))
360
424
  return;
361
- const kind = unmanagedSubscription(node, context.checker);
425
+ const field = subscriptionField(node, teardown);
426
+ if (field && teardown.subscriptions.has(field))
427
+ return;
428
+ const kind = unmanagedSubscription(node, context.checker, teardown);
362
429
  if (!kind)
363
430
  return;
364
- context.report(node, 'angular-subscribe-without-cleanup', 'This RxJS `.subscribe()` inside an Angular component/directive has no `takeUntil` / `takeUntilDestroyed` operator and no `ngOnDestroy` unsubscribe, leaking memory and running callbacks after the component is destroyed. Prefer the `async` pipe, `toSignal()`, or `takeUntilDestroyed()`.', kind === 'unknown' ? SUBSCRIBE_MAY_COMPLETE_REASON : undefined);
431
+ context.report(node, 'angular-subscribe-without-cleanup', 'This RxJS `.subscribe()` inside an Angular component/directive can outlive the component. Prefer the `async` pipe, `toSignal()`, or place `takeUntilDestroyed()` after operators that create inner subscriptions.', kind === 'unknown' ? SUBSCRIBE_MAY_COMPLETE_REASON : undefined);
365
432
  });
366
433
  }
367
434
  // --- Vue
@@ -380,7 +447,15 @@ function scopeRegistersUnmount(scope, checker) {
380
447
  if (!ts.isCallExpression(node))
381
448
  return;
382
449
  const name = importedName(node.expression, checker, VUE_MODULES);
383
- if (name === 'onUnmounted' || name === 'onBeforeUnmount' || name === 'onScopeDispose')
450
+ if (name !== 'onUnmounted' && name !== 'onBeforeUnmount' && name !== 'onScopeDispose')
451
+ return;
452
+ const cleanup = node.arguments[0] && ts.skipOuterExpressions(node.arguments[0]);
453
+ if (cleanup &&
454
+ (ts.isArrowFunction(cleanup) || ts.isFunctionExpression(cleanup)) &&
455
+ ts.isBlock(cleanup.body) &&
456
+ cleanup.body.statements.every(ts.isEmptyStatement))
457
+ return;
458
+ if (cleanup)
384
459
  registers = true;
385
460
  });
386
461
  return registers;
@@ -403,7 +478,7 @@ function checkVueMounted(call, context) {
403
478
  const leak = vueLeakInCallback(callback, context.checker);
404
479
  if (!leak)
405
480
  return;
406
- context.report(call, 'vue-watch-missing-cleanup', `\`onMounted\` registers \`${leak.label}\` without a matching \`onUnmounted\` / \`onBeforeUnmount\` cleanup, leaking resources when the Vue component unmounts.`, leak.reason);
481
+ context.report(call, 'vue-watch-missing-cleanup', `\`onMounted\` registers \`${leak.label}\` without an apparent unmount cleanup, leaking resources when the Vue component unmounts. Release it with \`onUnmounted\` or \`onScopeDispose\`.`, leak.reason);
407
482
  }
408
483
  function hasKeyProperty(propsArgument) {
409
484
  const props = ts.skipOuterExpressions(propsArgument);
@@ -138,15 +138,27 @@ export function tableArgument(node) {
138
138
  return stringArgument(objectProperty(value.arguments?.[0], 'name')?.initializer);
139
139
  return stringArgument(value);
140
140
  }
141
- export function isInsideTransactionCallback(node, upFunction) {
141
+ export function transactionCallbackScope(node, upFunction, framework) {
142
+ let transactionScope;
143
+ let transactionConnectionScope;
144
+ let nestedReceiver;
142
145
  for (let current = node.parent; current && current !== upFunction; current = current.parent) {
143
146
  if (!isFunctionLike(current) || !ts.isCallExpression(current.parent))
144
147
  continue;
145
148
  const parts = calleeParts(current.parent);
146
- if (parts?.method === 'transaction')
147
- return true;
149
+ if (parts?.method !== 'transaction')
150
+ continue;
151
+ const parameter = current.parameters[0]?.name;
152
+ if (transactionScope !== undefined &&
153
+ (!parameter || !ts.isIdentifier(parameter) || parameter.text !== nestedReceiver))
154
+ break;
155
+ transactionScope ??= current.getStart();
156
+ transactionConnectionScope = current.getStart();
157
+ if (framework !== 'knex' || !ts.isIdentifier(parts.receiver))
158
+ break;
159
+ nestedReceiver = parts.receiver.text;
148
160
  }
149
- return false;
161
+ return transactionScope === undefined ? undefined : { transactionScope, transactionConnectionScope };
150
162
  }
151
163
  // Parses a raw SQL default (TypeORM `default`, `Sequelize.literal`, `knex.raw`) into an expression
152
164
  // node.
@@ -5,7 +5,7 @@ import ts from 'typescript';
5
5
  import { createOperation } from './operations.js';
6
6
  import { builderOperations, isRawSqlCall, rawSqlOperations } from './extract-code-builders.js';
7
7
  import { defaultTransaction, detectFramework, findDeclaration, findUpFunctions, isFunctionLike, propertyName, scriptKindFor, unwrapExpression, } from './extract-code-discovery.js';
8
- import { EMPTY_BINDINGS, calleeParts, isInsideTransactionCallback, positionOf, receiverName, } from './extract-code-sql.js';
8
+ import { EMPTY_BINDINGS, calleeParts, transactionCallbackScope, positionOf, receiverName } from './extract-code-sql.js';
9
9
  const MAX_HELPER_DEPTH = 3;
10
10
  // `this.helper(...)` resolves to a method of the migration class; `helper(...)` to a function
11
11
  // declared in the file.
@@ -66,6 +66,10 @@ function functionOperations(fn, context, scope) {
66
66
  return;
67
67
  }
68
68
  const parts = calleeParts(node);
69
+ const transaction = transactionCallbackScope(node, fn, context.framework) ?? {
70
+ transactionScope: scope.transactionScope,
71
+ transactionConnectionScope: scope.transactionConnectionScope,
72
+ };
69
73
  if (parts) {
70
74
  const receiverText = receiverName(parts.receiver, context.sourceFile);
71
75
  const found = isRawSqlCall(parts.method, receiverText, context)
@@ -75,16 +79,20 @@ function functionOperations(fn, context, scope) {
75
79
  // The receiver of a chained builder call (`schema.createTable(...).alterTable(...)`) runs
76
80
  // first. Arguments, including Knex alterTable callbacks, were already read with their call.
77
81
  visit(parts.receiver);
78
- const explicitTransaction = scope.explicitTransaction || isInsideTransactionCallback(node, fn);
79
82
  for (const fields of found) {
80
- operations.push(createOperation({ file: context.file, ...fields, explicitTransaction }));
83
+ operations.push(createOperation({
84
+ file: context.file,
85
+ ...fields,
86
+ explicitTransaction: transaction.transactionScope !== undefined,
87
+ ...transaction,
88
+ }));
81
89
  }
82
90
  return;
83
91
  }
84
92
  }
85
93
  const helperOperations = helperCallOperations(node, context, {
86
94
  ...scope,
87
- explicitTransaction: scope.explicitTransaction || isInsideTransactionCallback(node, fn),
95
+ ...transaction,
88
96
  });
89
97
  if (helperOperations.length > 0) {
90
98
  ts.forEachChild(node, visit);
@@ -102,7 +110,7 @@ function upFunctionOperations(upFunction, owner, context) {
102
110
  return [];
103
111
  const firstParameter = upFunction.parameters[0]?.name.getText(context.sourceFile);
104
112
  const unitContext = { ...context, firstParameter, bindings: EMPTY_BINDINGS };
105
- return functionOperations(upFunction, unitContext, { owner, helpers: [upFunction], explicitTransaction: false });
113
+ return functionOperations(upFunction, unitContext, { owner, helpers: [upFunction] });
106
114
  }
107
115
  function extractCodeFile(text, { file, dialect }) {
108
116
  const sourceFile = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindFor(file));