loadwise 0.7.0 → 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 (228) hide show
  1. package/README.md +23 -15
  2. package/dist/bin/loadwise.js +12 -24
  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/jsx-elements.js +13 -3
  10. package/dist/lib/accessibility/markup-files.js +7 -2
  11. package/dist/lib/accessibility/rule-ids.js +3 -0
  12. package/dist/lib/accessibility/rules.js +128 -41
  13. package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
  14. package/dist/lib/accessibility/template-attributes.js +5 -3
  15. package/dist/lib/accessibility/template-elements.js +25 -6
  16. package/dist/lib/accessibility/template-file.js +40 -25
  17. package/dist/lib/editor/accessibility-examples.js +6 -1
  18. package/dist/lib/editor/actions.js +46 -7
  19. package/dist/lib/editor/apply-fix.js +69 -4
  20. package/dist/lib/editor/configured-setup.js +5 -3
  21. package/dist/lib/editor/diagnostics.js +206 -17
  22. package/dist/lib/editor/dismissals.js +85 -26
  23. package/dist/lib/editor/editor-document.js +5 -2
  24. package/dist/lib/editor/editor-launcher.js +7 -2
  25. package/dist/lib/editor/language-server.js +64 -25
  26. package/dist/lib/editor/native-setup.js +3 -2
  27. package/dist/lib/editor/perf-watch.js +8 -6
  28. package/dist/lib/editor/reliability-examples.js +15 -0
  29. package/dist/lib/editor/scan-session.js +29 -8
  30. package/dist/lib/editor/server-commands.js +36 -9
  31. package/dist/lib/editor/setup-server.js +1 -1
  32. package/dist/lib/editor/setup-vim.js +6 -4
  33. package/dist/lib/editor/workspace-files.js +61 -7
  34. package/dist/lib/editor/workspace-paths.js +41 -1
  35. package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
  36. package/dist/lib/maintainability/architecture/architecture.js +4 -4
  37. package/dist/lib/maintainability/cli/changed-lines.js +20 -2
  38. package/dist/lib/maintainability/cli/inputs.js +9 -1
  39. package/dist/lib/maintainability/cli.js +4 -0
  40. package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
  41. package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
  42. package/dist/lib/maintainability/quality/functions.js +9 -6
  43. package/dist/lib/maintainability/quality/limits.js +19 -0
  44. package/dist/lib/maintainability/quality/size-limits.js +8 -2
  45. package/dist/lib/maintainability/quality/stray-console.js +62 -1
  46. package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
  47. package/dist/lib/maintainability/scan-changed.js +4 -1
  48. package/dist/lib/maintainability/scan-worker.js +6 -2
  49. package/dist/lib/maintainability/scan.js +21 -8
  50. package/dist/lib/maintainability/unused/module-usage.js +43 -21
  51. package/dist/lib/maintainability/unused/unused-modules.js +5 -10
  52. package/dist/lib/performance/analysis/source-inputs.js +7 -2
  53. package/dist/lib/performance/analysis/source-options.js +7 -6
  54. package/dist/lib/performance/analyze.js +16 -3
  55. package/dist/lib/performance/cli/cli-output.js +1 -0
  56. package/dist/lib/performance/cli/cli-scan.js +21 -2
  57. package/dist/lib/performance/cli.js +8 -3
  58. package/dist/lib/performance/collections/memory-growth.js +55 -12
  59. package/dist/lib/performance/contracts/openapi.js +13 -4
  60. package/dist/lib/performance/datastores/pg-integrity.js +61 -22
  61. package/dist/lib/performance/datastores/pg-session-release.js +114 -0
  62. package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
  63. package/dist/lib/performance/datastores/pooling.js +56 -2
  64. package/dist/lib/performance/datastores/redis.js +94 -18
  65. package/dist/lib/performance/datastores/session-store.js +2 -1
  66. package/dist/lib/performance/db/checks.js +19 -8
  67. package/dist/lib/performance/db/gateway.js +3 -1
  68. package/dist/lib/performance/db/plan-estimates.js +4 -4
  69. package/dist/lib/performance/engine/native.js +11 -7
  70. package/dist/lib/performance/evidence/plans.js +7 -5
  71. package/dist/lib/performance/evidence/traces.js +21 -1
  72. package/dist/lib/performance/frontend/angular-teardown.js +96 -0
  73. package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
  74. package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
  75. package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
  76. package/dist/lib/performance/frontend/frontend-perf.js +72 -57
  77. package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
  78. package/dist/lib/performance/migrations/extract-code.js +13 -5
  79. package/dist/lib/performance/migrations/extract-sql.js +82 -38
  80. package/dist/lib/performance/migrations/operations.js +56 -13
  81. package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
  82. package/dist/lib/performance/migrations/postgres-registry.js +127 -26
  83. package/dist/lib/performance/migrations/postgres-settings.js +78 -0
  84. package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
  85. package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
  86. package/dist/lib/performance/migrations/rules-postgres.js +88 -39
  87. package/dist/lib/performance/orm/offset-pagination.js +6 -4
  88. package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
  89. package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
  90. package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
  91. package/dist/lib/performance/polyglot.js +111 -14
  92. package/dist/lib/performance/projects.js +3 -3
  93. package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
  94. package/dist/lib/performance/request/backend-mastery.js +151 -42
  95. package/dist/lib/performance/request/mongoose-lean.js +175 -10
  96. package/dist/lib/performance/request/phase5-rules.js +74 -13
  97. package/dist/lib/performance/request/sql-select-star.js +150 -0
  98. package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
  99. package/dist/lib/performance/request/upload-limits.js +5 -1
  100. package/dist/lib/performance/site-fix.js +4 -4
  101. package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
  102. package/dist/lib/performance/site-fixes/frontend-fixes.js +350 -10
  103. package/dist/lib/performance/site-fixes/migration-context.js +8 -1
  104. package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
  105. package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
  106. package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
  107. package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
  108. package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
  109. package/dist/lib/performance/sql/mysql-tokens.js +38 -12
  110. package/dist/lib/reliability/boundary-access.js +114 -57
  111. package/dist/lib/reliability/react-bugs.js +18 -4
  112. package/dist/lib/reliability/resource-leak-intervals.js +258 -0
  113. package/dist/lib/reliability/resource-leaks.js +209 -195
  114. package/dist/lib/reliability/rule-ids.js +10 -1
  115. package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
  116. package/dist/lib/reliability/swallowed-errors.js +107 -21
  117. package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
  118. package/dist/lib/reliability/walk.js +3 -2
  119. package/dist/lib/security/auth-crypto/hashing.js +24 -8
  120. package/dist/lib/security/auth-crypto/jwt.js +21 -6
  121. package/dist/lib/security/auth-crypto/secrets.js +44 -1
  122. package/dist/lib/security/auth-crypto/shared.js +3 -1
  123. package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
  124. package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
  125. package/dist/lib/security/cli.js +6 -2
  126. package/dist/lib/security/cookie-credentials.js +115 -0
  127. package/dist/lib/security/cookie-csrf.js +241 -0
  128. package/dist/lib/security/deps/advisory.js +50 -109
  129. package/dist/lib/security/deps/cli.js +1 -1
  130. package/dist/lib/security/deps/cvss-score.js +14 -0
  131. package/dist/lib/security/deps/fixed-version.js +97 -0
  132. package/dist/lib/security/deps/lockfiles.js +135 -74
  133. package/dist/lib/security/deps/osv.js +11 -3
  134. package/dist/lib/security/deps/scan.js +47 -20
  135. package/dist/lib/security/deps/yarn-lock.js +158 -0
  136. package/dist/lib/security/examples/injection.cjs +4 -0
  137. package/dist/lib/security/exposure/cookies.js +17 -2
  138. package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
  139. package/dist/lib/security/exposure/frontend-security.js +21 -14
  140. package/dist/lib/security/exposure/jsx-url.js +18 -33
  141. package/dist/lib/security/exposure/logging.js +49 -10
  142. package/dist/lib/security/exposure/prototype-pollution.js +126 -56
  143. package/dist/lib/security/exposure/react-html.js +26 -0
  144. package/dist/lib/security/exposure/reflected-xss.js +34 -5
  145. package/dist/lib/security/files/compose.js +126 -1
  146. package/dist/lib/security/files/dockerfile.js +62 -4
  147. package/dist/lib/security/files/index.js +1 -1
  148. package/dist/lib/security/injection/command.js +46 -18
  149. package/dist/lib/security/injection/index.js +2 -0
  150. package/dist/lib/security/injection/ldap.js +86 -0
  151. package/dist/lib/security/injection/nosql.js +28 -4
  152. package/dist/lib/security/injection/redos.js +1 -1
  153. package/dist/lib/security/injection/rule-ids.js +1 -0
  154. package/dist/lib/security/injection/shared.js +5 -1
  155. package/dist/lib/security/injection/sql.js +28 -13
  156. package/dist/lib/security/lexical-declaration.js +113 -0
  157. package/dist/lib/security/pg-dynamic-query.js +28 -1
  158. package/dist/lib/security/pg-identifier-guards.js +107 -3
  159. package/dist/lib/security/phase5-rules.js +26 -135
  160. package/dist/lib/security/polyglot.js +2 -2
  161. package/dist/lib/security/redirect-settings.js +98 -0
  162. package/dist/lib/security/request-forgery/open-redirect.js +18 -5
  163. package/dist/lib/security/request-forgery/path-traversal.js +84 -6
  164. package/dist/lib/security/request-forgery/shared.js +1 -7
  165. package/dist/lib/security/request-forgery/ssrf.js +176 -14
  166. package/dist/lib/security/rule-descriptions.js +5 -1
  167. package/dist/lib/security/rule-ids.js +7 -0
  168. package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
  169. package/dist/lib/security/site-fixes/exposure.js +21 -3
  170. package/dist/lib/security/site-fixes/files.js +11 -13
  171. package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
  172. package/dist/lib/security/site-fixes/injection.js +17 -4
  173. package/dist/lib/security/site-fixes/jwt.js +1 -20
  174. package/dist/lib/security/site-fixes/request-forgery.js +37 -29
  175. package/dist/lib/security/taint/binding-inputs.js +236 -0
  176. package/dist/lib/security/taint/filename-guard.js +45 -45
  177. package/dist/lib/security/taint/guards.js +14 -8
  178. package/dist/lib/security/taint/index.js +126 -154
  179. package/dist/lib/security/taint/propagate.js +43 -9
  180. package/dist/lib/security/taint/record-writes.js +161 -0
  181. package/dist/lib/security/taint/resolution-cache.js +13 -7
  182. package/dist/lib/security/taint/sanitizers.js +89 -5
  183. package/dist/lib/security/taint/trace-nodes.js +61 -0
  184. package/dist/lib/security/taint/validators.js +89 -9
  185. package/dist/lib/shared/agent/agent-init.js +2 -1
  186. package/dist/lib/shared/agent/agent-verify.js +4 -1
  187. package/dist/lib/shared/agent/git-hook.js +17 -6
  188. package/dist/lib/shared/agent/review-mcp.js +8 -2
  189. package/dist/lib/shared/analysis/ast-semantics.js +2 -2
  190. package/dist/lib/shared/analysis/scan-cache.js +96 -12
  191. package/dist/lib/shared/cli-dispatch.js +17 -0
  192. package/dist/lib/shared/config.js +27 -10
  193. package/dist/lib/shared/explain.js +1 -1
  194. package/dist/lib/shared/fix/fix-convergence.js +5 -3
  195. package/dist/lib/shared/fix/fix-imports.js +4 -171
  196. package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
  197. package/dist/lib/shared/fix/fix-presentation.js +5 -3
  198. package/dist/lib/shared/fix/fix-safety.js +7 -1
  199. package/dist/lib/shared/fix/fix-typecheck.js +8 -4
  200. package/dist/lib/shared/fix/import-text-edits.js +148 -0
  201. package/dist/lib/shared/fix.js +31 -7
  202. package/dist/lib/shared/formats/finding-location.js +4 -1
  203. package/dist/lib/shared/formats/github.js +3 -3
  204. package/dist/lib/shared/formats/sarif.js +9 -3
  205. package/dist/lib/shared/report-fp.js +6 -2
  206. package/dist/lib/shared/reporting/baseline.js +7 -5
  207. package/dist/lib/shared/reporting/pillars.js +9 -3
  208. package/dist/lib/shared/reporting/source-report.js +1 -0
  209. package/dist/lib/shared/system/glob.js +34 -8
  210. package/dist/lib/shared/system/sentry.js +26 -7
  211. package/dist/lib/shared/workspaces.js +15 -3
  212. package/package.json +5 -1
  213. package/plugin/.claude-plugin/plugin.json +1 -1
  214. package/plugin/hooks/check-edited-file.mjs +27 -5
  215. package/polyglot/performance/rules/csharp.yml +34 -5
  216. package/polyglot/performance/rules/java.yml +24 -1
  217. package/polyglot/performance/rules/python.yml +74 -2
  218. package/polyglot/performance/sgconfig.yml +1 -0
  219. package/polyglot/reliability/rules/csharp.yml +12 -6
  220. package/polyglot/reliability/rules/go.yml +86 -6
  221. package/polyglot/reliability/rules/java.yml +22 -1
  222. package/polyglot/reliability/sgconfig.yml +1 -0
  223. package/polyglot/security/rules/go.yml +100 -22
  224. package/polyglot/security/rules/python.yml +74 -6
  225. package/polyglot/security/rules/rust.yml +65 -0
  226. package/polyglot/security/sgconfig.yml +1 -0
  227. package/schema/loadwise.config.schema.json +554 -76
  228. /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
@@ -1,33 +1,37 @@
1
1
  // Rule id -> build({ finding, node, sourceFile, text, load }) => { edits, note } | undefined; see
2
2
  // lib/performance/site-fix.js.
3
3
  import { ts, ancestor, calleeCall, replaceNode, requireReview } from '../../performance/site-fixes/helpers.js';
4
- function hasPathBinding(sourceFile) {
5
- for (const stmt of sourceFile.statements) {
6
- if (ts.isImportDeclaration(stmt) && stmt.importClause) {
7
- if (stmt.importClause.name?.text === 'path')
8
- return true;
9
- const bindings = stmt.importClause.namedBindings;
10
- if (bindings && ts.isNamespaceImport(bindings) && bindings.name.text === 'path')
11
- return true;
12
- if (bindings && ts.isNamedImports(bindings) && bindings.elements.some((el) => el.name.text === 'path')) {
13
- return true;
14
- }
15
- }
16
- else if (ts.isVariableStatement(stmt)) {
17
- if (stmt.declarationList.declarations.some((decl) => ts.isIdentifier(decl.name) && decl.name.text === 'path')) {
18
- return true;
19
- }
20
- }
21
- else if ((ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) && stmt.name?.text === 'path') {
22
- return true;
23
- }
24
- }
25
- return false;
26
- }
27
- function pathImportEdit(input) {
28
- const { sourceFile } = input;
29
- if (!sourceFile || hasPathBinding(sourceFile))
30
- return;
4
+ function pathImportEdit(sourceFile) {
5
+ let bindingCount = 0;
6
+ let commonJs = /\.c[tj]s$/i.test(sourceFile.fileName);
7
+ (function visit(node) {
8
+ if (ts.isIdentifier(node) && node.text === 'path' && ts.isDeclarationName(node))
9
+ bindingCount++;
10
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'require')
11
+ commonJs = true;
12
+ if (ts.isPropertyAccessExpression(node) &&
13
+ ts.isIdentifier(node.expression) &&
14
+ (node.expression.text === 'exports' || (node.expression.text === 'module' && node.name.text === 'exports')))
15
+ commonJs = true;
16
+ ts.forEachChild(node, visit);
17
+ })(sourceFile);
18
+ if (commonJs)
19
+ return false;
20
+ const existing = sourceFile.statements.some((statement) => {
21
+ if (!ts.isImportDeclaration(statement) ||
22
+ !statement.importClause ||
23
+ statement.importClause.isTypeOnly ||
24
+ !ts.isStringLiteral(statement.moduleSpecifier) ||
25
+ !['node:path', 'path'].includes(statement.moduleSpecifier.text))
26
+ return false;
27
+ const clause = statement.importClause;
28
+ return (clause.name?.text === 'path' ||
29
+ (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings) && clause.namedBindings.name.text === 'path'));
30
+ });
31
+ if (existing)
32
+ return bindingCount === 1 ? undefined : false;
33
+ if (bindingCount || (/\.jsx?$/i.test(sourceFile.fileName) && !ts.isExternalModule(sourceFile)))
34
+ return false;
31
35
  const imports = sourceFile.statements.filter(ts.isImportDeclaration);
32
36
  if (imports.length) {
33
37
  const last = imports[imports.length - 1];
@@ -55,7 +59,9 @@ function uploadClientFilenameFix(input) {
55
59
  const access = ancestor(node, ts.isPropertyAccessExpression);
56
60
  if (!access || (access.name.text !== 'originalname' && access.name.text !== 'originalFilename'))
57
61
  return;
58
- const importEdit = pathImportEdit(input);
62
+ const importEdit = pathImportEdit(sourceFile);
63
+ if (importEdit === false)
64
+ return;
59
65
  const edits = [replaceNode(access, `path.basename(${access.getText(sourceFile)})`)];
60
66
  if (importEdit)
61
67
  edits.push(importEdit);
@@ -100,7 +106,9 @@ function pathTraversalFix(input) {
100
106
  const lastArg = arg.arguments.at(-1);
101
107
  if (!lastArg || ts.isStringLiteralLike(lastArg))
102
108
  return;
103
- const importEdit = pathImportEdit(input);
109
+ const importEdit = pathImportEdit(sourceFile);
110
+ if (importEdit === false)
111
+ return;
104
112
  const edits = [replaceNode(lastArg, `path.basename(${lastArg.getText(sourceFile)})`)];
105
113
  if (importEdit)
106
114
  edits.push(importEdit);
@@ -0,0 +1,236 @@
1
+ import { effectiveProperty, isUnshadowedGlobal, staticPropertyName } from '../../shared/analysis/ast-semantics.js';
2
+ import { assignmentsTo } from './guards.js';
3
+ import { ts } from '../../performance/engine/compiler.js';
4
+ function usesUndefined(value, checker) {
5
+ const node = ts.skipOuterExpressions(value);
6
+ return ts.isVoidExpression(node) || isUnshadowedGlobal(node, 'undefined', checker);
7
+ }
8
+ function parameterInput(parameter, argument, call, checker) {
9
+ const omitted = !argument || usesUndefined(argument, checker);
10
+ return omitted && !call.arguments.some(ts.isSpreadElement) ? parameter.initializer : argument;
11
+ }
12
+ // Only select the default when the literal itself establishes that the field is absent or undefined.
13
+ function selectedBindingDefault(element, checker) {
14
+ if (!element.initializer || !ts.isObjectBindingPattern(element.parent))
15
+ return;
16
+ const declaration = element.parent.parent;
17
+ if (!ts.isVariableDeclaration(declaration) || !declaration.initializer)
18
+ return;
19
+ const object = ts.skipOuterExpressions(declaration.initializer);
20
+ const key = element.propertyName ?? element.name;
21
+ if (ts.isObjectBindingPattern(key) || ts.isArrayBindingPattern(key) || !ts.isObjectLiteralExpression(object))
22
+ return;
23
+ const name = staticPropertyName(key);
24
+ if (!name)
25
+ return;
26
+ const property = effectiveProperty(object, name, { guardSpreadGetters: true });
27
+ if (property === 'unknown')
28
+ return;
29
+ if (property)
30
+ return ts.isPropertyAssignment(property) && usesUndefined(property.initializer, checker)
31
+ ? element.initializer
32
+ : undefined;
33
+ if (name in Object.prototype ||
34
+ object.properties.some((entry) => ts.isPropertyAssignment(entry) && staticPropertyName(entry.name) === '__proto__'))
35
+ return;
36
+ return element.initializer;
37
+ }
38
+ function* assignmentTargets(assignment, checker) {
39
+ if (ts.isIdentifier(assignment.left)) {
40
+ const declaration = checker.getSymbolAtLocation(assignment.left)?.valueDeclaration;
41
+ if (declaration)
42
+ yield [declaration];
43
+ return;
44
+ }
45
+ if (!ts.isObjectLiteralExpression(assignment.left))
46
+ return;
47
+ for (const property of assignment.left.properties) {
48
+ const field = staticPropertyName(property.name);
49
+ const symbol = ts.isShorthandPropertyAssignment(property)
50
+ ? checker.getShorthandAssignmentValueSymbol(property)
51
+ : ts.isPropertyAssignment(property) && ts.isIdentifier(property.initializer)
52
+ ? checker.getSymbolAtLocation(property.initializer)
53
+ : undefined;
54
+ if (symbol?.valueDeclaration && field !== undefined)
55
+ yield [symbol.valueDeclaration, field];
56
+ }
57
+ }
58
+ const writesByChecker = new WeakMap();
59
+ function jumpBefore(body, initializer) {
60
+ function scan(node) {
61
+ if (node.pos >= initializer.pos || ts.isFunctionLike(node))
62
+ return;
63
+ if (ts.isBreakStatement(node) || ts.isContinueStatement(node))
64
+ return true;
65
+ return ts.forEachChild(node, scan);
66
+ }
67
+ return !!scan(body);
68
+ }
69
+ function branchExecution(parent, child, initializer) {
70
+ if (ts.isForStatement(parent) && parent.initializer === child)
71
+ return true;
72
+ // A prior break/continue can skip even the first iteration's initializer.
73
+ if (ts.isDoStatement(parent))
74
+ return jumpBefore(child, initializer) ? undefined : true;
75
+ if (ts.isWhileStatement(parent) || ts.isForStatement(parent)) {
76
+ const test = ts.isWhileStatement(parent) ? parent.expression : parent.condition;
77
+ const kind = test && ts.skipOuterExpressions(test).kind;
78
+ if (kind === ts.SyntaxKind.FalseKeyword)
79
+ return false;
80
+ if (!test || kind === ts.SyntaxKind.TrueKeyword)
81
+ return jumpBefore(child, initializer) ? undefined : true;
82
+ return;
83
+ }
84
+ if (!ts.isIfStatement(parent))
85
+ return;
86
+ const condition = ts.skipOuterExpressions(parent.expression).kind;
87
+ if (condition === ts.SyntaxKind.TrueKeyword)
88
+ return parent.thenStatement === child;
89
+ if (condition === ts.SyntaxKind.FalseKeyword)
90
+ return parent.elseStatement === child;
91
+ }
92
+ // Whether a hoisted declaration's initializer actually supersedes a write in this block.
93
+ function bindingInitialization(declaration, use, block) {
94
+ let binding = declaration;
95
+ while (ts.isBindingElement(binding))
96
+ binding = binding.parent.parent;
97
+ if (!ts.isVariableDeclaration(binding) || binding.end > use.pos)
98
+ return;
99
+ const container = binding.parent.parent;
100
+ const loop = ts.isForOfStatement(container) || ts.isForInStatement(container) ? container : undefined;
101
+ // The iterated expression is read before the loop binds anything.
102
+ if (loop ? use.pos < loop.statement.pos : !binding.initializer)
103
+ return;
104
+ const statement = loop || ts.isVariableStatement(container) ? container : binding.parent;
105
+ // A loop that iterates nothing leaves the earlier value for a use after it.
106
+ let possible = !!loop && use.pos >= loop.end;
107
+ for (let child = statement; child.parent !== block; child = child.parent) {
108
+ const parent = child.parent;
109
+ if (!parent || ts.isFunctionLike(parent))
110
+ return;
111
+ if (ts.isBlock(parent))
112
+ continue;
113
+ const executes = branchExecution(parent, child, statement);
114
+ if (executes === false)
115
+ return;
116
+ if (executes)
117
+ continue;
118
+ possible = true;
119
+ }
120
+ return { end: binding.end, possible };
121
+ }
122
+ // Index direct object destructuring writes in this block once. A sibling branch, callback or
123
+ // later statement cannot establish the value used at this site.
124
+ function bindingWritesBefore(declaration, use, checker) {
125
+ const block = ts.findAncestor(use, (node) => ts.isBlock(node) || ts.isSourceFile(node));
126
+ if (!block)
127
+ return;
128
+ let scopes = writesByChecker.get(checker);
129
+ if (!scopes)
130
+ writesByChecker.set(checker, (scopes = new WeakMap()));
131
+ let writes = scopes.get(block);
132
+ if (!writes) {
133
+ writes = new Map();
134
+ for (const statement of block.statements) {
135
+ if (!ts.isExpressionStatement(statement))
136
+ continue;
137
+ const assignment = ts.skipOuterExpressions(statement.expression);
138
+ if (!ts.isBinaryExpression(assignment) || assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken)
139
+ continue;
140
+ for (const [target, field] of assignmentTargets(assignment, checker)) {
141
+ const entries = writes.get(target) ?? [];
142
+ entries.push({ assignment, field });
143
+ writes.set(target, entries);
144
+ }
145
+ }
146
+ scopes.set(block, writes);
147
+ }
148
+ const initialization = bindingInitialization(declaration, use, block);
149
+ const start = initialization && !initialization.possible ? initialization.end : -1;
150
+ const preceding = (writes.get(declaration) ?? []).filter(({ assignment }) => assignment.pos >= start && assignment.end <= use.pos);
151
+ // Plain variable writes outside a destructuring chain use the engine's existing variable analysis.
152
+ const last = preceding.at(-1);
153
+ if (!last || !(ts.isBindingElement(declaration) || preceding.some((write) => write.field !== undefined)))
154
+ return;
155
+ // A conditional or compound write after the last definite value can still supply input.
156
+ const owner = ts.findAncestor(use, ts.isFunctionLike);
157
+ const possible = assignmentsTo(declaration, block, checker).filter((assignment) => assignment.pos >= last.assignment.end &&
158
+ assignment.end <= use.pos &&
159
+ ts.findAncestor(assignment, ts.isFunctionLike) === owner);
160
+ const inputs = [
161
+ last,
162
+ ...possible.map((assignment) => ({ assignment })),
163
+ ];
164
+ if (initialization?.possible &&
165
+ last.assignment.end < initialization.end &&
166
+ (ts.isBindingElement(declaration) || ts.isVariableDeclaration(declaration)))
167
+ inputs.push(declaration);
168
+ return { inputs, last: last.assignment };
169
+ }
170
+ // Whether a field write changes the value bound by `last`. A function nested beside the use may run
171
+ // between them; a write in a function around the use is ordered by `last` itself.
172
+ function writesAfter(assignment, last, use) {
173
+ const writer = ts.findAncestor(assignment, ts.isFunctionLike);
174
+ if (writer === ts.findAncestor(use, ts.isFunctionLike))
175
+ return assignment.pos >= last.end && assignment.end <= use.pos;
176
+ return !!writer && !ts.findAncestor(use, (node) => node === writer);
177
+ }
178
+ function bindingKey(element) {
179
+ const property = element.propertyName ?? element.name;
180
+ return ts.isObjectBindingPattern(property) || ts.isArrayBindingPattern(property)
181
+ ? undefined
182
+ : staticPropertyName(property);
183
+ }
184
+ function bindingPath(element) {
185
+ const names = [];
186
+ let current = element;
187
+ while (ts.isBindingElement(current)) {
188
+ const pattern = current.parent;
189
+ if (ts.isArrayBindingPattern(pattern)) {
190
+ const index = pattern.elements.indexOf(current);
191
+ names.unshift(current.dotDotDotToken ? index : String(index));
192
+ }
193
+ else {
194
+ const excluded = current.dotDotDotToken && pattern.elements.filter((item) => !item.dotDotDotToken).map(bindingKey);
195
+ names.unshift(excluded ? new Set(excluded.filter((name) => name !== undefined)) : bindingKey(current));
196
+ }
197
+ current = pattern.parent;
198
+ }
199
+ return { names, current };
200
+ }
201
+ function restProjection(result, selector) {
202
+ if (!result.objectFields)
203
+ return result;
204
+ const fields = new Map();
205
+ for (const [name, field] of result.objectFields) {
206
+ if (typeof selector === 'number') {
207
+ if (/^\d+$/.test(name) && Number(name) >= selector)
208
+ fields.set(String(Number(name) - selector), field);
209
+ }
210
+ else if (!selector.has(name))
211
+ fields.set(name, field);
212
+ }
213
+ const first = fields.values().next().value;
214
+ return (first && {
215
+ ...first,
216
+ steps: [...first.steps, ...result.steps.slice(result.objectSteps)],
217
+ objectFields: fields,
218
+ objectSteps: first.steps.length,
219
+ });
220
+ }
221
+ function argumentsRecord(fields) {
222
+ const first = fields.values().next().value;
223
+ return first && { ...first, objectFields: fields, objectSteps: first.steps.length };
224
+ }
225
+ function collectRestArguments(call, index, fields, trace) {
226
+ if (call.arguments.slice(0, index).some(ts.isSpreadElement))
227
+ return;
228
+ for (const [offset, item] of call.arguments.slice(index).entries()) {
229
+ if (ts.isSpreadElement(item))
230
+ break;
231
+ const field = trace(item);
232
+ if (field)
233
+ fields.set(String(offset), field);
234
+ }
235
+ }
236
+ export { argumentsRecord, bindingPath, bindingWritesBefore, collectRestArguments, restProjection, selectedBindingDefault, parameterInput, writesAfter, };
@@ -1,8 +1,6 @@
1
1
  import { ts } from '../../performance/engine/compiler.js';
2
2
  // Each accepted range stays within ASCII digits or one case of letters. A hyphen at the edge is
3
3
  // literal.
4
- const PATH_SEPARATOR = '/';
5
- const BLOCKED_SEPARATORS = new Set(['/', '\\', '..']);
6
4
  const safeClass = /\[-?(?:[a-z]-[a-z]|[A-Z]-[A-Z]|\d-\d|\\[dw.-]|[A-Za-z0-9_.])+-?\]/g;
7
5
  const extension = /\\\.(?:[A-Za-z0-9]+|\([A-Za-z0-9]+(?:\|[A-Za-z0-9]+)+\))$/;
8
6
  function isFilenamePattern(expression) {
@@ -13,6 +11,9 @@ function isFilenamePattern(expression) {
13
11
  return false;
14
12
  // A stem can contain only filename characters, captures and quantifiers. The optional extension
15
13
  // is fixed, or a group of fixed alternatives. No application regex is evaluated by the scanner.
14
+ // Without a fixed extension, an allowed dot could form an entire special directory component.
15
+ if (anchored[1].includes('.') && !extension.test(anchored[1]))
16
+ return false;
16
17
  const stem = anchored[1]
17
18
  .replace(extension, '')
18
19
  .replaceAll(safeClass, 'x')
@@ -62,51 +63,50 @@ function filenameValidation(condition, isSubject, checker) {
62
63
  const pattern = constantOf(receiver, checker);
63
64
  return (!!pattern?.initializer && isFilenamePattern(ts.skipOuterExpressions(pattern.initializer)) && onlyRegexCalls(pattern));
64
65
  }
65
- // A negated class (`[^a-z/]`) matches everything except its members, so a separator inside it is
66
- // evidence of nothing.
67
- const separatorRegexPattern = /^\/(?:\[(?!\^)[^\]]*[/\\][^\]]*\]|\\\.\.)\/[gi]*$/;
68
- const foundIndexComparisons = new Set(['!== -1', '!= -1', '>= 0', '> -1']);
69
- const missingIndexComparisons = new Set(['=== -1', '== -1', '< 0']);
70
- function isSeparatorStringCall(expression, methodName, expectedMethod, args, isSubject) {
71
- if (methodName !== expectedMethod || args.length !== 1)
66
+ // A portable component cannot introduce a directory, drive prefix, or dot-only parent.
67
+ const excludedFilenameParts = ['/', '\\', ':', '.', '..'];
68
+ function excludesFilenamePart(condition, truth, part, isSubject) {
69
+ function searches(expression, methodName) {
70
+ const call = ts.skipOuterExpressions(expression);
71
+ if (!ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression))
72
+ return false;
73
+ const method = call.expression;
74
+ const [argument] = call.arguments;
75
+ return (method.name.text === methodName &&
76
+ isSubject(method.expression) &&
77
+ call.arguments.length === 1 &&
78
+ ts.isStringLiteralLike(argument) &&
79
+ (argument.text === part || (part === '..' && argument.text === '.')));
80
+ }
81
+ if (!truth && searches(condition, 'includes'))
82
+ return true;
83
+ if (ts.isBinaryExpression(condition) && searches(condition.left, 'indexOf')) {
84
+ const index = ts.skipOuterExpressions(condition.right);
85
+ const zero = ts.isNumericLiteral(index) && Number(index.text) === 0;
86
+ const operand = ts.isPrefixUnaryExpression(index) && index.operator === ts.SyntaxKind.MinusToken
87
+ ? ts.skipOuterExpressions(index.operand)
88
+ : undefined;
89
+ const minusOne = operand && ts.isNumericLiteral(operand) && Number(operand.text) === 1;
90
+ const operator = condition.operatorToken.kind;
91
+ if (zero)
92
+ return truth ? operator === ts.SyntaxKind.LessThanToken : operator === ts.SyntaxKind.GreaterThanEqualsToken;
93
+ if (minusOne)
94
+ return truth
95
+ ? operator === ts.SyntaxKind.EqualsEqualsEqualsToken || operator === ts.SyntaxKind.EqualsEqualsToken
96
+ : operator === ts.SyntaxKind.ExclamationEqualsEqualsToken ||
97
+ operator === ts.SyntaxKind.ExclamationEqualsToken ||
98
+ operator === ts.SyntaxKind.GreaterThanToken;
99
+ }
100
+ if (part !== '.' && part !== '..')
72
101
  return false;
73
- const [separator] = args;
74
- return (ts.isStringLiteralLike(separator) &&
75
- (separator.text === PATH_SEPARATOR || BLOCKED_SEPARATORS.has(separator.text)) &&
76
- isSubject(expression));
77
- }
78
- function matchesIndexOfSeparator(condition, comparisons, isSubject) {
79
102
  if (!ts.isBinaryExpression(condition))
80
103
  return false;
81
- const left = ts.skipOuterExpressions(condition.left);
82
- const comparison = `${condition.operatorToken.getText()} ${ts.skipOuterExpressions(condition.right).getText()}`;
83
- if (!ts.isCallExpression(left) || !ts.isPropertyAccessExpression(left.expression) || !comparisons.has(comparison)) {
104
+ const expected = truth ? ts.SyntaxKind.ExclamationEqualsEqualsToken : ts.SyntaxKind.EqualsEqualsEqualsToken;
105
+ if (condition.operatorToken.kind !== expected)
84
106
  return false;
85
- }
86
- return isSeparatorStringCall(left.expression.expression, left.expression.name.text, 'indexOf', left.arguments, isSubject);
87
- }
88
- // `value.indexOf('/') === -1`: confirms the filename carries no path separator.
89
- function lacksSeparator(condition, isSubject) {
90
- return matchesIndexOfSeparator(condition, missingIndexComparisons, isSubject);
91
- }
92
- // `value.includes('/')`, `value.indexOf('/') !== -1`, `value.match(/[/\\]/)` or `/[/\\]/.test(value)`:
93
- // a name holding a separator is turned away.
94
- function containsSeparator(condition, isSubject) {
95
- if (matchesIndexOfSeparator(condition, foundIndexComparisons, isSubject))
96
- return true;
97
- if (!ts.isCallExpression(condition) || !ts.isPropertyAccessExpression(condition.expression))
98
- return false;
99
- const { expression, name } = condition.expression;
100
- if (isSeparatorStringCall(expression, name.text, 'includes', condition.arguments, isSubject))
101
- return true;
102
- if (name.text === 'test' && condition.arguments.length === 1 && isSubject(condition.arguments[0])) {
103
- const pattern = ts.skipOuterExpressions(expression);
104
- return ts.isRegularExpressionLiteral(pattern) && separatorRegexPattern.test(pattern.text);
105
- }
106
- if (name.text === 'match' && condition.arguments.length === 1 && isSubject(expression)) {
107
- const pattern = ts.skipOuterExpressions(condition.arguments[0]);
108
- return ts.isRegularExpressionLiteral(pattern) && separatorRegexPattern.test(pattern.text);
109
- }
110
- return false;
107
+ return [
108
+ [condition.left, condition.right],
109
+ [condition.right, condition.left],
110
+ ].some(([key, literal]) => isSubject(key) && ts.isStringLiteralLike(literal) && literal.text === part);
111
111
  }
112
- export { filenameValidation, containsSeparator, lacksSeparator };
112
+ export { filenameValidation, excludedFilenameParts, excludesFilenamePart };
@@ -5,7 +5,7 @@
5
5
  import { ts } from '../../performance/engine/compiler.js';
6
6
  import { unwrapExpression } from '../../shared/analysis/ast-semantics.js';
7
7
  import { isValidation } from '../pg-identifier-guards.js';
8
- import { filenameValidation, containsSeparator, lacksSeparator } from './filename-guard.js';
8
+ import { filenameValidation, excludedFilenameParts, excludesFilenamePart } from './filename-guard.js';
9
9
  const scalarTypeofResults = new Set(['number', 'boolean', 'bigint']);
10
10
  const numberChecks = new Set(['isInteger', 'isSafeInteger', 'isFinite']);
11
11
  function isLiteral(node) {
@@ -85,8 +85,7 @@ function confirms(node, guard) {
85
85
  return confirms(condition.left, guard) || confirms(condition.right, guard);
86
86
  if (operator === ts.SyntaxKind.BarBarToken)
87
87
  return confirms(condition.left, guard) && confirms(condition.right, guard);
88
- return (pinningComparison(condition, guard.isSubject) === 'equal' ||
89
- (Boolean(guard.filename) && lacksSeparator(condition, guard.isSubject)));
88
+ return pinningComparison(condition, guard.isSubject) === 'equal';
90
89
  }
91
90
  if (ts.isPrefixUnaryExpression(condition) && condition.operator === ts.SyntaxKind.ExclamationToken)
92
91
  return rejects(condition.operand, guard);
@@ -107,12 +106,11 @@ function rejects(node, guard) {
107
106
  return rejects(condition.left, guard) || rejects(condition.right, guard);
108
107
  if (operator === ts.SyntaxKind.AmpersandAmpersandToken)
109
108
  return rejects(condition.left, guard) && rejects(condition.right, guard);
110
- return (pinningComparison(condition, guard.isSubject) === 'unequal' ||
111
- (Boolean(guard.filename) && containsSeparator(condition, guard.isSubject)));
109
+ return pinningComparison(condition, guard.isSubject) === 'unequal';
112
110
  }
113
111
  if (ts.isPrefixUnaryExpression(condition) && condition.operator === ts.SyntaxKind.ExclamationToken)
114
112
  return confirms(condition.operand, guard);
115
- return Boolean(guard.filename) && containsSeparator(condition, guard.isSubject);
113
+ return false;
116
114
  }
117
115
  function exits(statement) {
118
116
  if (ts.isReturnStatement(statement) ||
@@ -261,12 +259,17 @@ function fieldWritesTo(declaration, scope, checker) {
261
259
  if (ts.isBinaryExpression(node) &&
262
260
  valueAssignmentOperators.has(node.operatorToken.kind) &&
263
261
  (ts.isPropertyAccessExpression(node.left) || ts.isElementAccessExpression(node.left))) {
264
- const object = ts.skipOuterExpressions(node.left.expression);
262
+ const path = [writtenFieldName(node.left)];
263
+ let object = ts.skipOuterExpressions(node.left.expression);
264
+ while (ts.isPropertyAccessExpression(object) || ts.isElementAccessExpression(object)) {
265
+ path.unshift(writtenFieldName(object));
266
+ object = ts.skipOuterExpressions(object.expression);
267
+ }
265
268
  const written = ts.isIdentifier(object) ? checker.getSymbolAtLocation(object)?.valueDeclaration : undefined;
266
269
  if (written && !writes.has(written))
267
270
  writes.set(written, []);
268
271
  if (written)
269
- writes.get(written)?.push({ name: writtenFieldName(node.left), assignment: node });
272
+ writes.get(written)?.push({ path, assignment: node });
270
273
  }
271
274
  ts.forEachChild(node, visit);
272
275
  })(scope);
@@ -317,6 +320,9 @@ function isGuardedUse(identifier, use, checker, filename) {
317
320
  guardedBy(child, parent, (node) => confirms(node, guard), (node) => rejects(node, guard)))
318
321
  return !isReassigned(rewriteScope(declaration, body), symbol, checker);
319
322
  }
323
+ if (filename &&
324
+ excludedFilenameParts.every((part) => isGuardedByCondition(use, (condition, truth) => excludesFilenamePart(condition, truth, part, guard.isSubject))))
325
+ return !isReassigned(rewriteScope(declaration, body), symbol, checker);
320
326
  return false;
321
327
  }
322
328
  function impliesEvidence(node, truth, evidence) {