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
@@ -0,0 +1,93 @@
1
+ import { ts } from '../engine/compiler.js';
2
+ const bindingsByFile = new WeakMap();
3
+ // A name declared in more than one scope is ambiguous without a checker. Skip it rather than
4
+ // mistaking a shadowed parameter, import, or local for a Fastify binding.
5
+ function bindingsOf(file) {
6
+ const cached = bindingsByFile.get(file);
7
+ if (cached)
8
+ return cached;
9
+ const bindings = new Map();
10
+ function visit(node) {
11
+ if (ts.isIdentifier(node) && isDeclarationName(node)) {
12
+ bindings.set(node.text, bindings.has(node.text) ? undefined : node);
13
+ }
14
+ ts.forEachChild(node, visit);
15
+ }
16
+ visit(file);
17
+ bindingsByFile.set(file, bindings);
18
+ return bindings;
19
+ }
20
+ function isDeclarationName(node) {
21
+ const parent = node.parent;
22
+ return ('name' in parent &&
23
+ parent.name === node &&
24
+ (ts.isVariableDeclaration(parent) ||
25
+ ts.isBindingElement(parent) ||
26
+ ts.isParameter(parent) ||
27
+ ts.isImportClause(parent) ||
28
+ ts.isImportSpecifier(parent) ||
29
+ ts.isNamespaceImport(parent) ||
30
+ ts.isImportEqualsDeclaration(parent) ||
31
+ ts.isClassLike(parent) ||
32
+ ts.isFunctionLike(parent) ||
33
+ ts.isEnumDeclaration(parent) ||
34
+ ts.isModuleDeclaration(parent)));
35
+ }
36
+ function importedFastifyFactory(name) {
37
+ const binding = name.parent;
38
+ if (ts.isImportClause(binding)) {
39
+ const { moduleSpecifier } = binding.parent;
40
+ return !binding.isTypeOnly && ts.isStringLiteralLike(moduleSpecifier) && moduleSpecifier.text === 'fastify';
41
+ }
42
+ if (!ts.isImportSpecifier(binding) || binding.isTypeOnly)
43
+ return false;
44
+ const clause = binding.parent.parent;
45
+ const { moduleSpecifier } = clause.parent;
46
+ const imported = (binding.propertyName ?? binding.name).text;
47
+ return (!clause.isTypeOnly &&
48
+ (imported === 'default' || imported === 'fastify') &&
49
+ ts.isStringLiteralLike(moduleSpecifier) &&
50
+ moduleSpecifier.text === 'fastify');
51
+ }
52
+ function isFastifyRequire(expression, bindings) {
53
+ if (!ts.isCallExpression(expression))
54
+ return false;
55
+ const { expression: callee, arguments: args } = expression;
56
+ const [module] = args;
57
+ return (ts.isIdentifier(callee) &&
58
+ callee.text === 'require' &&
59
+ !bindings.has('require') &&
60
+ args.length === 1 &&
61
+ ts.isStringLiteralLike(module) &&
62
+ module.text === 'fastify');
63
+ }
64
+ function constantInitializer(identifier, bindings) {
65
+ const declaration = bindings.get(identifier.text)?.parent;
66
+ if (!declaration || !ts.isVariableDeclaration(declaration) || !declaration.initializer)
67
+ return;
68
+ if (!(declaration.parent.flags & ts.NodeFlags.Const))
69
+ return;
70
+ return ts.skipOuterExpressions(declaration.initializer);
71
+ }
72
+ function createsFastifyInstance(expression, bindings) {
73
+ if (!ts.isCallExpression(expression))
74
+ return false;
75
+ const factory = ts.skipOuterExpressions(expression.expression);
76
+ if (isFastifyRequire(factory, bindings))
77
+ return true;
78
+ if (!ts.isIdentifier(factory))
79
+ return false;
80
+ const binding = bindings.get(factory.text);
81
+ if (binding && importedFastifyFactory(binding))
82
+ return true;
83
+ const initializer = constantInitializer(factory, bindings);
84
+ return initializer !== undefined && isFastifyRequire(initializer, bindings);
85
+ }
86
+ export function isProvenFastifyReceiver(expression) {
87
+ const receiver = ts.skipOuterExpressions(expression);
88
+ if (!ts.isIdentifier(receiver) && !ts.isCallExpression(receiver))
89
+ return false;
90
+ const bindings = bindingsOf(receiver.getSourceFile());
91
+ const value = ts.isIdentifier(receiver) ? constantInitializer(receiver, bindings) : receiver;
92
+ return value !== undefined && createsFastifyInstance(value, bindings);
93
+ }
@@ -0,0 +1,103 @@
1
+ import { ts } from '../engine/compiler.js';
2
+ import { typeParts } from '../collections/collection-bindings.js';
3
+ import { resolveImportedBinding, unwrapExpression } from '../../shared/analysis/ast-semantics.js';
4
+ import { identifiersByName, valueSymbol } from './phase5-ast.js';
5
+ const guardOperators = new Set(['&&', '||', '===', '!==', '<', '>', '<=', '>=']);
6
+ function hasDeclaredAccessor(property, checker) {
7
+ return !!checker
8
+ .getSymbolAtLocation(property.name)
9
+ ?.declarations?.some((declaration) => ts.isGetAccessorDeclaration(declaration) || ts.isSetAccessorDeclaration(declaration));
10
+ }
11
+ function createsNetSocket(expression, checker) {
12
+ const allocation = unwrapExpression(expression);
13
+ if (!ts.isCallExpression(allocation) && !ts.isNewExpression(allocation))
14
+ return false;
15
+ const callee = unwrapExpression(allocation.expression);
16
+ if (ts.isPropertyAccessExpression(callee) && callee.name.text === 'Socket') {
17
+ const owner = resolveImportedBinding(unwrapExpression(callee.expression), checker);
18
+ return owner?.module === 'net' && (owner.exported === '*' || owner.exported === 'default');
19
+ }
20
+ const binding = resolveImportedBinding(callee, checker);
21
+ return binding?.module === 'net' && binding.exported === 'Socket';
22
+ }
23
+ function assignsFreshSocket(statement, receiver, checker) {
24
+ if (!ts.isExpressionStatement(statement))
25
+ return false;
26
+ const assignment = unwrapExpression(statement.expression);
27
+ if (!ts.isBinaryExpression(assignment) || assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken)
28
+ return false;
29
+ const target = unwrapExpression(assignment.left);
30
+ if (!ts.isPropertyAccessExpression(target) || target.name.text !== receiver.name.text)
31
+ return false;
32
+ if (!ts.isIdentifier(target.expression) || !ts.isIdentifier(receiver.expression))
33
+ return false;
34
+ const owner = valueSymbol(receiver.expression, checker);
35
+ const declaration = owner?.valueDeclaration;
36
+ if (!declaration || !ts.isVariableDeclaration(declaration) || !(declaration.parent.flags & ts.NodeFlags.Const))
37
+ return false;
38
+ if (valueSymbol(target.expression, checker) !== owner || hasDeclaredAccessor(receiver, checker))
39
+ return false;
40
+ const initializer = unwrapExpression(assignment.right);
41
+ if (createsNetSocket(initializer, checker))
42
+ return true;
43
+ if (!ts.isCallExpression(initializer) || !ts.isIdentifier(initializer.expression))
44
+ return false;
45
+ const factorySymbol = valueSymbol(initializer.expression, checker);
46
+ const factory = factorySymbol?.valueDeclaration;
47
+ if (!factory || !ts.isFunctionDeclaration(factory) || factory.body?.statements.length !== 1)
48
+ return false;
49
+ const [result] = factory.body.statements;
50
+ if (!ts.isReturnStatement(result) || !result.expression || !createsNetSocket(result.expression, checker))
51
+ return false;
52
+ const references = identifiersByName(factory.getSourceFile()).get(initializer.expression.text);
53
+ return !references?.some((reference) => ts.isAssignmentTarget(reference) && valueSymbol(reference, checker) === factorySymbol);
54
+ }
55
+ function simpleGuard(condition, checker) {
56
+ const value = unwrapExpression(condition);
57
+ if (ts.isIdentifier(value) || ts.isNumericLiteral(value) || value.kind === ts.SyntaxKind.ThisKeyword)
58
+ return true;
59
+ if (ts.isPropertyAccessExpression(value))
60
+ return !hasDeclaredAccessor(value, checker) && simpleGuard(value.expression, checker);
61
+ return (ts.isBinaryExpression(value) &&
62
+ guardOperators.has(value.operatorToken.getText()) &&
63
+ simpleGuard(value.left, checker) &&
64
+ simpleGuard(value.right, checker));
65
+ }
66
+ function updatesOtherCounter(statement, receiver, checker) {
67
+ if (!ts.isExpressionStatement(statement))
68
+ return false;
69
+ const update = statement.expression;
70
+ if (!ts.isPostfixUnaryExpression(update))
71
+ return false;
72
+ const counter = update.operand;
73
+ return (ts.isPropertyAccessExpression(counter) &&
74
+ counter.expression.kind === ts.SyntaxKind.ThisKeyword &&
75
+ counter.name.text !== receiver.name.text &&
76
+ !!checker.getSymbolAtLocation(counter.name)?.declarations?.length &&
77
+ !hasDeclaredAccessor(counter, checker) &&
78
+ typeParts(checker.getTypeAtLocation(counter)).every((member) => (member.flags & (ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike)) !== 0));
79
+ }
80
+ // Walk only the straight-line path in this iteration. A write in a sibling branch,
81
+ // an intervening call, or an allocation outside the innermost loop cannot establish freshness.
82
+ function isFreshLoopSocket(receiver, call, loop, checker) {
83
+ if (!ts.isPropertyAccessExpression(receiver) || !ts.isIdentifier(receiver.expression))
84
+ return false;
85
+ if (!ts.isExpressionStatement(call.parent))
86
+ return false;
87
+ let statement = call.parent;
88
+ while (ts.isBlock(statement.parent) && statement.parent !== loop) {
89
+ const block = statement.parent;
90
+ for (let index = block.statements.indexOf(statement) - 1; index >= 0; index--) {
91
+ const prior = block.statements[index];
92
+ if (updatesOtherCounter(prior, receiver, checker))
93
+ continue;
94
+ return assignsFreshSocket(prior, receiver, checker);
95
+ }
96
+ const guard = block.parent;
97
+ if (!ts.isIfStatement(guard) || !simpleGuard(guard.expression, checker))
98
+ return false;
99
+ statement = guard;
100
+ }
101
+ return false;
102
+ }
103
+ export { isFreshLoopSocket };
@@ -1,7 +1,12 @@
1
1
  import { ts } from '../engine/compiler.js';
2
- import { declaredInPackage } from '../schema/ts-helpers.js';
2
+ import { outsideWrappers } from '../query-loops/query-results.js';
3
+ import { declaredInPackage, symbolDeclarations } from '../schema/ts-helpers.js';
4
+ import { constInitializer, staticMemberName, staticPropertyName, unwrapExpression, } from '../../shared/analysis/ast-semantics.js';
5
+ import { bindingUses, mayWriteLimit } from './http-options-writes.js';
3
6
  const MONGOOSE_PACKAGES = ['mongoose'];
4
7
  const MONGOOSE_FIND_METHODS = new Set(['find', 'findOne', 'findById']);
8
+ const LEAN_OPTION = 'lean';
9
+ const MAX_OPTIONS_DEPTH = 8;
5
10
  const MONGOOSE_QUERY_MODIFIERS = new Set([
6
11
  'select',
7
12
  'sort',
@@ -17,8 +22,28 @@ const MONGOOSE_QUERY_MODIFIERS = new Set([
17
22
  'maxTimeMS',
18
23
  'read',
19
24
  'batchSize',
25
+ 'setOptions',
26
+ ]);
27
+ const SAFE_ARRAY_METHODS = new Set([
28
+ 'map',
29
+ 'filter',
30
+ 'slice',
31
+ 'forEach',
32
+ 'find',
33
+ 'some',
34
+ 'every',
35
+ 'reduce',
36
+ 'reduceRight',
37
+ 'flatMap',
38
+ 'concat',
39
+ 'includes',
40
+ 'indexOf',
41
+ 'at',
42
+ 'join',
43
+ 'entries',
44
+ 'keys',
45
+ 'values',
20
46
  ]);
21
- const MONGOOSE_MUTATION_METHODS = new Set(['save', 'markModified', 'remove', 'deleteOne']);
22
47
  const SERIALIZATION_METHODS = new Set(['json', 'send', 'jsonp']);
23
48
  const RESPONSE_RECEIVERS = /^(?:res|response|reply|ctx|c|NextResponse|Response)$/;
24
49
  function hasMongooseImport(sourceFile) {
@@ -26,21 +51,62 @@ function hasMongooseImport(sourceFile) {
26
51
  ts.isStringLiteral(stmt.moduleSpecifier) &&
27
52
  stmt.moduleSpecifier.text === 'mongoose');
28
53
  }
29
- function isMongooseReadCall(call, callee, sourceFile, context) {
54
+ function isModelRead(call, callee, sourceFile, checker, declarations) {
30
55
  if (!MONGOOSE_FIND_METHODS.has(callee.name.text))
31
56
  return false;
32
- if (declaredInPackage(callee, context.checker, MONGOOSE_PACKAGES))
57
+ if (declaredInPackage(callee, checker, MONGOOSE_PACKAGES))
33
58
  return true;
34
- if (!hasMongooseImport(sourceFile))
59
+ if ((declarations ?? symbolDeclarations(callee, checker)).length !== 0 || !hasMongooseImport(sourceFile))
35
60
  return false;
36
61
  const firstArg = call.arguments[0];
37
62
  if (firstArg && (ts.isArrowFunction(firstArg) || ts.isFunctionExpression(firstArg)))
38
63
  return false;
39
64
  return true;
40
65
  }
41
- function unwrapQueryChain(call) {
66
+ function leanEnabled(expression, checker, depth = 0) {
67
+ const value = unwrapExpression(expression);
68
+ if (depth < MAX_OPTIONS_DEPTH && ts.isIdentifier(value)) {
69
+ const initializer = constInitializer(value, checker);
70
+ return initializer !== undefined && leanEnabled(initializer, checker, depth + 1);
71
+ }
72
+ return value.kind === ts.SyntaxKind.TrueKeyword || ts.isObjectLiteralExpression(value);
73
+ }
74
+ // Undefined means no override. Unknown writes, spreads or values cannot establish lean mode.
75
+ function readLeanOption(expression, checker, depth = 0) {
76
+ if (!expression)
77
+ return;
78
+ if (depth >= MAX_OPTIONS_DEPTH)
79
+ return false;
80
+ let options = unwrapExpression(expression);
81
+ if (ts.isIdentifier(options)) {
82
+ const literal = constInitializer(options, checker);
83
+ if (!literal ||
84
+ !ts.isObjectLiteralExpression(literal) ||
85
+ mayWriteLimit(literal, options, [LEAN_OPTION], [LEAN_OPTION], checker)) {
86
+ return false;
87
+ }
88
+ options = literal;
89
+ }
90
+ if (!ts.isObjectLiteralExpression(options))
91
+ return false;
92
+ for (const property of options.properties.toReversed()) {
93
+ if (ts.isSpreadAssignment(property)) {
94
+ const spreadLean = readLeanOption(property.expression, checker, depth + 1);
95
+ if (spreadLean !== undefined)
96
+ return spreadLean;
97
+ continue;
98
+ }
99
+ const name = staticPropertyName(property.name);
100
+ if (name === undefined && property.name && ts.isComputedPropertyName(property.name))
101
+ return false;
102
+ if (name === LEAN_OPTION)
103
+ return ts.isPropertyAssignment(property) && leanEnabled(property.initializer, checker);
104
+ }
105
+ }
106
+ function unwrapQueryChain(call, checker, modelRead = true) {
42
107
  let current = call;
43
- let hasLean = false;
108
+ // The second model argument is projection, even for an object with a field named "lean".
109
+ let hasLean = modelRead && readLeanOption(call.arguments[2], checker) === true;
44
110
  while (current.parent) {
45
111
  if (!ts.isPropertyAccessExpression(current.parent) || current.parent.expression !== current)
46
112
  break;
@@ -48,8 +114,10 @@ function unwrapQueryChain(call) {
48
114
  if (!nextCall || !ts.isCallExpression(nextCall) || nextCall.expression !== current.parent)
49
115
  break;
50
116
  const method = current.parent.name.text;
51
- if (method === 'lean')
52
- hasLean = true;
117
+ if (method === LEAN_OPTION)
118
+ hasLean = !nextCall.arguments[0] || leanEnabled(nextCall.arguments[0], checker);
119
+ if (method === 'setOptions')
120
+ hasLean = readLeanOption(nextCall.arguments[0], checker) ?? hasLean;
53
121
  if (!MONGOOSE_QUERY_MODIFIERS.has(method))
54
122
  break;
55
123
  current = nextCall;
@@ -60,6 +128,59 @@ function unwrapQueryChain(call) {
60
128
  }
61
129
  return { outer, hasLean };
62
130
  }
131
+ function initializedVariable(initializer) {
132
+ const parent = initializer.parent;
133
+ return parent && ts.isVariableDeclaration(parent) && parent.initializer === initializer ? parent : undefined;
134
+ }
135
+ // A repository method is distinct from a model. Report a wrapper only when its implementation
136
+ // shows a hydrated model document that it immediately uses as a plain object.
137
+ function convertsReadDocument(outer, checker) {
138
+ const declaration = initializedVariable(outer);
139
+ if (!declaration)
140
+ return false;
141
+ const converted = (value) => {
142
+ const document = initializedVariable(value);
143
+ if (!document)
144
+ return false;
145
+ const uses = bindingUses(document, checker);
146
+ return (uses.length > 0 &&
147
+ uses.every((use) => {
148
+ const reference = outsideWrappers(use);
149
+ const member = reference.parent;
150
+ return ((ts.isPropertyAccessExpression(member) || ts.isElementAccessExpression(member)) &&
151
+ member.expression === reference &&
152
+ staticMemberName(member) === 'toObject' &&
153
+ ts.isCallExpression(member.parent) &&
154
+ member.parent.expression === member);
155
+ }));
156
+ };
157
+ if (ts.isAwaitExpression(unwrapExpression(outer)))
158
+ return converted(outer);
159
+ return bindingUses(declaration, checker).some((use) => {
160
+ const awaited = use.parent;
161
+ return ts.isAwaitExpression(awaited) && awaited.expression === use && converted(awaited);
162
+ });
163
+ }
164
+ function hasHydratingRepositoryRead(declarations, checker) {
165
+ const implementation = declarations.find((node) => ts.isMethodDeclaration(node) && node.body);
166
+ if (!implementation || !ts.isMethodDeclaration(implementation) || !implementation.body)
167
+ return false;
168
+ const sourceFile = implementation.getSourceFile();
169
+ function findRead(node) {
170
+ if (ts.isFunctionLike(node))
171
+ return;
172
+ if (ts.isCallExpression(node)) {
173
+ const callee = unwrapExpression(node.expression);
174
+ if (ts.isPropertyAccessExpression(callee) && isModelRead(node, callee, sourceFile, checker)) {
175
+ const { outer, hasLean } = unwrapQueryChain(node, checker);
176
+ if (!hasLean && convertsReadDocument(outer, checker))
177
+ return true;
178
+ }
179
+ }
180
+ return ts.forEachChild(node, findRead);
181
+ }
182
+ return ts.forEachChild(implementation.body, findRead) === true;
183
+ }
63
184
  function isJsonSerializationCall(node, argNode) {
64
185
  if (!ts.isCallExpression(node) || !node.arguments.some((arg) => arg === argNode))
65
186
  return false;
@@ -85,65 +206,196 @@ function isDirectReadReturnOrSerialize(outer) {
85
206
  return true;
86
207
  return false;
87
208
  }
88
- function isVariableReturnedOrSerialized(varName, scope) {
89
- let mutated = false;
90
- let returnedOrSerialized = false;
209
+ function hasSchemaVirtuals(sourceFile) {
210
+ let found = false;
91
211
  function visit(node) {
92
- if (mutated)
212
+ if (found)
93
213
  return;
94
214
  if (ts.isCallExpression(node) &&
95
215
  ts.isPropertyAccessExpression(node.expression) &&
96
- ts.isIdentifier(node.expression.expression) &&
97
- node.expression.expression.text === varName &&
98
- MONGOOSE_MUTATION_METHODS.has(node.expression.name.text)) {
99
- mutated = true;
216
+ node.expression.name.text === 'virtual') {
217
+ found = true;
100
218
  return;
101
219
  }
102
- if (ts.isBinaryExpression(node) &&
103
- node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
104
- ts.isPropertyAccessExpression(node.left) &&
105
- ts.isIdentifier(node.left.expression) &&
106
- node.left.expression.text === varName) {
107
- mutated = true;
220
+ if (ts.isPropertyAssignment(node) &&
221
+ staticPropertyName(node.name) === 'virtuals' &&
222
+ unwrapExpression(node.initializer).kind === ts.SyntaxKind.TrueKeyword) {
223
+ found = true;
108
224
  return;
109
225
  }
110
- if (ts.isReturnStatement(node) &&
111
- node.expression &&
112
- ts.isIdentifier(node.expression) &&
113
- node.expression.text === varName) {
114
- returnedOrSerialized = true;
226
+ ts.forEachChild(node, visit);
227
+ }
228
+ visit(sourceFile);
229
+ return found;
230
+ }
231
+ function modelUsesVirtuals(callee, sourceFile, checker) {
232
+ if (hasSchemaVirtuals(sourceFile))
233
+ return true;
234
+ const checkedFiles = new Set([sourceFile]);
235
+ const declarations = symbolDeclarations(callee.expression, checker);
236
+ const queue = [...declarations];
237
+ while (queue.length > 0) {
238
+ const declaration = queue.pop();
239
+ if (!declaration)
240
+ continue;
241
+ const file = declaration.getSourceFile();
242
+ if (file.isDeclarationFile)
243
+ continue;
244
+ if (!checkedFiles.has(file)) {
245
+ checkedFiles.add(file);
246
+ if (hasSchemaVirtuals(file))
247
+ return true;
115
248
  }
116
- if (ts.isCallExpression(node)) {
117
- for (const arg of node.arguments) {
118
- if (ts.isIdentifier(arg) && arg.text === varName && isJsonSerializationCall(node, arg)) {
119
- returnedOrSerialized = true;
249
+ if (ts.isVariableDeclaration(declaration) && declaration.initializer) {
250
+ (function collectIdentifiers(node) {
251
+ if (ts.isIdentifier(node)) {
252
+ for (const target of symbolDeclarations(node, checker)) {
253
+ const targetFile = target.getSourceFile();
254
+ if (!targetFile.isDeclarationFile && !checkedFiles.has(targetFile)) {
255
+ queue.push(target);
256
+ }
257
+ }
258
+ return;
120
259
  }
260
+ ts.forEachChild(node, collectIdentifiers);
261
+ })(declaration.initializer);
262
+ }
263
+ }
264
+ return false;
265
+ }
266
+ function isSafeTruthinessCheck(result, parent) {
267
+ if (!parent)
268
+ return false;
269
+ if (ts.isIfStatement(parent) && parent.expression === result)
270
+ return true;
271
+ if (ts.isConditionalExpression(parent) && parent.condition === result)
272
+ return true;
273
+ if (ts.isPrefixUnaryExpression(parent) && parent.operator === ts.SyntaxKind.ExclamationToken)
274
+ return true;
275
+ if (ts.isBinaryExpression(parent)) {
276
+ const op = parent.operatorToken.kind;
277
+ if (op === ts.SyntaxKind.AmpersandAmpersandToken && parent.left === result)
278
+ return true;
279
+ if (op === ts.SyntaxKind.EqualsEqualsToken ||
280
+ op === ts.SyntaxKind.ExclamationEqualsToken ||
281
+ op === ts.SyntaxKind.EqualsEqualsEqualsToken ||
282
+ op === ts.SyntaxKind.ExclamationEqualsEqualsToken) {
283
+ const other = parent.left === result ? parent.right : parent.left;
284
+ const unwrapped = ts.skipOuterExpressions(other);
285
+ return (unwrapped.kind === ts.SyntaxKind.NullKeyword || (ts.isIdentifier(unwrapped) && unwrapped.text === 'undefined'));
286
+ }
287
+ }
288
+ return false;
289
+ }
290
+ function callbackUsesSafeElementReads(callback, checker) {
291
+ const fn = ts.skipOuterExpressions(callback);
292
+ if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn))
293
+ return false;
294
+ const firstParam = fn.parameters[0];
295
+ if (!firstParam || !ts.isIdentifier(firstParam.name))
296
+ return false;
297
+ for (const use of bindingUses(firstParam, checker)) {
298
+ const result = outsideWrappers(use);
299
+ const parent = result.parent;
300
+ if (ts.isAssignmentTarget(result))
301
+ return false;
302
+ if ((ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) &&
303
+ parent.expression === result) {
304
+ const member = outsideWrappers(parent);
305
+ const consumer = member.parent;
306
+ if (ts.isAssignmentTarget(member) ||
307
+ ts.isDeleteExpression(consumer) ||
308
+ (ts.isCallExpression(consumer) && consumer.expression === member)) {
309
+ return false;
121
310
  }
311
+ continue;
122
312
  }
123
- ts.forEachChild(node, visit);
313
+ if (!isSafeTruthinessCheck(result, parent))
314
+ return false;
315
+ }
316
+ return true;
317
+ }
318
+ function isUnsafeArrayCallResult(call) {
319
+ const callResult = outsideWrappers(call);
320
+ const callParent = callResult.parent;
321
+ const isMemberChain = (ts.isPropertyAccessExpression(callParent) || ts.isElementAccessExpression(callParent)) &&
322
+ callParent.expression === callResult;
323
+ if (!isMemberChain) {
324
+ return (ts.isVariableDeclaration(callParent) ||
325
+ ts.isAssignmentTarget(callResult) ||
326
+ (ts.isCallExpression(callParent) && !isJsonSerializationCall(callParent, callResult)));
124
327
  }
125
- visit(scope);
126
- return !mutated && returnedOrSerialized;
328
+ const nextMember = outsideWrappers(callParent);
329
+ const nextConsumer = nextMember.parent;
330
+ return (ts.isAssignmentTarget(nextMember) ||
331
+ ts.isDeleteExpression(nextConsumer) ||
332
+ (ts.isCallExpression(nextConsumer) && nextConsumer.expression === nextMember));
333
+ }
334
+ function isMemberAccessOn(node, expression) {
335
+ return (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) && node.expression === expression;
336
+ }
337
+ function isSafeMemberUse(memberAccess, isArrayRead, checker) {
338
+ const member = outsideWrappers(memberAccess);
339
+ const consumer = member.parent;
340
+ const method = staticMemberName(memberAccess);
341
+ if (ts.isAssignmentTarget(member) || ts.isDeleteExpression(consumer))
342
+ return false;
343
+ if (isArrayRead && ts.isElementAccessExpression(memberAccess) && isMemberAccessOn(consumer, member)) {
344
+ // An indexed element is a Document, so follow its member uses (e.g. users[0]?.validate()).
345
+ return isSafeMemberUse(consumer, false, checker);
346
+ }
347
+ if (!ts.isCallExpression(consumer) || consumer.expression !== member)
348
+ return true;
349
+ if (!isArrayRead || method === undefined || !SAFE_ARRAY_METHODS.has(method))
350
+ return false;
351
+ const [callback] = consumer.arguments;
352
+ if (callback && !callbackUsesSafeElementReads(callback, checker))
353
+ return false;
354
+ return !isUnsafeArrayCallResult(consumer);
355
+ }
356
+ function isVariableReturnedOrSerialized(declaration, checker, isArrayRead) {
357
+ let returnedOrSerialized = false;
358
+ for (const use of bindingUses(declaration, checker)) {
359
+ const result = outsideWrappers(use);
360
+ const parent = result.parent;
361
+ if (ts.isAssignmentTarget(result))
362
+ return false;
363
+ if ((ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) &&
364
+ parent.expression === result) {
365
+ if (!isSafeMemberUse(parent, isArrayRead, checker))
366
+ return false;
367
+ continue;
368
+ }
369
+ if ((ts.isReturnStatement(parent) && parent.expression === result) || isJsonSerializationCall(parent, result)) {
370
+ returnedOrSerialized = true;
371
+ continue;
372
+ }
373
+ if (!isSafeTruthinessCheck(result, parent))
374
+ return false;
375
+ }
376
+ return returnedOrSerialized;
127
377
  }
128
378
  function checkMongooseMissingLean(call, sourceFile, context) {
129
379
  const callee = ts.skipOuterExpressions(call.expression);
130
380
  if (!ts.isPropertyAccessExpression(callee))
131
381
  return;
132
- if (!isMongooseReadCall(call, callee, sourceFile, context))
382
+ if (!MONGOOSE_FIND_METHODS.has(callee.name.text))
383
+ return;
384
+ const { checker } = context;
385
+ const declarations = symbolDeclarations(callee, checker);
386
+ const modelRead = isModelRead(call, callee, sourceFile, checker, declarations);
387
+ if (!modelRead && (!hasMongooseImport(sourceFile) || !hasHydratingRepositoryRead(declarations, checker)))
133
388
  return;
134
- const { outer, hasLean } = unwrapQueryChain(call);
389
+ if (modelUsesVirtuals(callee, sourceFile, checker))
390
+ return;
391
+ const { outer, hasLean } = unwrapQueryChain(call, checker, modelRead);
135
392
  if (hasLean)
136
393
  return;
137
- if (isDirectReadReturnOrSerialize(outer)) {
394
+ const isArrayRead = callee.name.text === 'find';
395
+ const declaration = initializedVariable(outer);
396
+ if (isDirectReadReturnOrSerialize(outer) ||
397
+ (declaration && isVariableReturnedOrSerialized(declaration, checker, isArrayRead))) {
138
398
  context.report(call, 'mongoose-missing-lean', 'Mongoose read query is returned or serialized without .lean(), paying full Document hydration overhead.');
139
- return;
140
- }
141
- const decl = outer.parent;
142
- if (decl && ts.isVariableDeclaration(decl) && decl.initializer === outer && ts.isIdentifier(decl.name)) {
143
- const scope = ts.findAncestor(decl, ts.isFunctionLike) ?? sourceFile;
144
- if (isVariableReturnedOrSerialized(decl.name.text, scope)) {
145
- context.report(call, 'mongoose-missing-lean', 'Mongoose read query is returned or serialized without .lean(), paying full Document hydration overhead.');
146
- }
147
399
  }
148
400
  }
149
401
  export { checkMongooseMissingLean };