loadwise 0.6.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -115
- package/dist/bin/loadwise.js +132 -67
- package/dist/lib/accessibility/analyze.js +5 -3
- package/dist/lib/accessibility/angular-inline.js +9 -11
- package/dist/lib/accessibility/angular-literal.js +39 -0
- package/dist/lib/accessibility/form-context.js +36 -0
- package/dist/lib/accessibility/frame-head.js +92 -0
- package/dist/lib/accessibility/frame-viewport.js +149 -0
- package/dist/lib/accessibility/frontend-detect.js +6 -5
- package/dist/lib/accessibility/jsx-elements.js +13 -3
- package/dist/lib/accessibility/markup-files.js +7 -2
- package/dist/lib/accessibility/rule-ids.js +3 -0
- package/dist/lib/accessibility/rules.js +128 -41
- package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
- package/dist/lib/accessibility/template-attributes.js +5 -3
- package/dist/lib/accessibility/template-elements.js +25 -6
- package/dist/lib/accessibility/template-file.js +40 -25
- package/dist/lib/accessibility/usage.js +10 -2
- package/dist/lib/editor/accessibility-examples.js +6 -1
- package/dist/lib/editor/actions.js +46 -7
- package/dist/lib/editor/apply-fix.js +69 -4
- package/dist/lib/editor/configured-setup.js +63 -0
- package/dist/lib/editor/diagnostics.js +206 -17
- package/dist/lib/editor/dismissals.js +85 -26
- package/dist/lib/editor/editor-document.js +5 -2
- package/dist/lib/editor/editor-launcher.js +170 -0
- package/dist/lib/editor/fix-edits.js +19 -0
- package/dist/lib/editor/jetbrains-setup.js +53 -0
- package/dist/lib/editor/language-server.js +80 -24
- package/dist/lib/editor/managed-config.js +37 -0
- package/dist/lib/editor/native-setup.js +86 -0
- package/dist/lib/editor/perf-watch.js +8 -6
- package/dist/lib/editor/release-vsix.js +61 -0
- package/dist/lib/editor/reliability-examples.js +15 -0
- package/dist/lib/editor/scan-session.js +29 -8
- package/dist/lib/editor/server-commands.js +38 -9
- package/dist/lib/editor/setup-server.js +87 -0
- package/dist/lib/editor/setup-vim.js +63 -0
- package/dist/lib/editor/setup.js +131 -0
- package/dist/lib/editor/stdio-setup.js +6 -0
- package/dist/lib/editor/workspace-files.js +61 -7
- package/dist/lib/editor/workspace-paths.js +41 -1
- package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
- package/dist/lib/maintainability/architecture/architecture.js +4 -4
- package/dist/lib/maintainability/cli/changed-lines.js +20 -2
- package/dist/lib/maintainability/cli/inputs.js +12 -3
- package/dist/lib/maintainability/cli.js +18 -5
- package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
- package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
- package/dist/lib/maintainability/examples.js +4 -0
- package/dist/lib/maintainability/quality/functions.js +9 -6
- package/dist/lib/maintainability/quality/limits.js +19 -0
- package/dist/lib/maintainability/quality/size-limits.js +8 -2
- package/dist/lib/maintainability/quality/stray-console.js +62 -1
- package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
- package/dist/lib/maintainability/rule-ids.js +3 -0
- package/dist/lib/maintainability/scan-changed.js +4 -1
- package/dist/lib/maintainability/scan-worker.js +6 -2
- package/dist/lib/maintainability/scan.js +21 -8
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
- package/dist/lib/maintainability/unused/module-usage.js +43 -21
- package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
- package/dist/lib/maintainability/unused/unused-modules.js +5 -10
- package/dist/lib/performance/analysis/source-inputs.js +7 -2
- package/dist/lib/performance/analysis/source-options.js +7 -6
- package/dist/lib/performance/analyze.js +16 -3
- package/dist/lib/performance/cli/changed-lines.js +8 -1
- package/dist/lib/performance/cli/cli-arguments.js +19 -8
- package/dist/lib/performance/cli/cli-output.js +4 -0
- package/dist/lib/performance/cli/cli-scan.js +21 -2
- package/dist/lib/performance/cli.js +16 -7
- package/dist/lib/performance/collections/memory-growth.js +55 -12
- package/dist/lib/performance/contracts/openapi.js +13 -4
- package/dist/lib/performance/datastores/pg-integrity.js +68 -22
- package/dist/lib/performance/datastores/pg-session-release.js +114 -0
- package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
- package/dist/lib/performance/datastores/pooling.js +62 -3
- package/dist/lib/performance/datastores/redis.js +94 -18
- package/dist/lib/performance/datastores/session-store.js +2 -1
- package/dist/lib/performance/db/checks.js +19 -8
- package/dist/lib/performance/db/gateway.js +3 -1
- package/dist/lib/performance/db/plan-estimates.js +4 -4
- package/dist/lib/performance/engine/native-syntax.js +3 -0
- package/dist/lib/performance/engine/native.js +11 -7
- package/dist/lib/performance/evidence/plans.js +7 -5
- package/dist/lib/performance/evidence/traces.js +21 -1
- package/dist/lib/performance/frontend/angular-teardown.js +96 -0
- package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
- package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
- package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
- package/dist/lib/performance/frontend/frontend-perf.js +149 -74
- package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
- package/dist/lib/performance/migrations/extract-code.js +13 -5
- package/dist/lib/performance/migrations/extract-sql.js +82 -38
- package/dist/lib/performance/migrations/operations.js +56 -13
- package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
- package/dist/lib/performance/migrations/postgres-registry.js +127 -26
- package/dist/lib/performance/migrations/postgres-settings.js +78 -0
- package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
- package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
- package/dist/lib/performance/migrations/rules-postgres.js +88 -39
- package/dist/lib/performance/orm/offset-pagination.js +6 -4
- package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
- package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
- package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
- package/dist/lib/performance/polyglot.js +111 -14
- package/dist/lib/performance/projects.js +3 -3
- package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
- package/dist/lib/performance/request/backend-mastery.js +151 -42
- package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
- package/dist/lib/performance/request/fastify-receivers.js +93 -0
- package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
- package/dist/lib/performance/request/mongoose-lean.js +298 -46
- package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
- package/dist/lib/performance/request/phase5-ast.js +6 -7
- package/dist/lib/performance/request/phase5-rules.js +204 -23
- package/dist/lib/performance/request/request-handlers.js +120 -23
- package/dist/lib/performance/request/sql-select-star.js +150 -0
- package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
- package/dist/lib/performance/request/upload-limits.js +5 -1
- package/dist/lib/performance/site-fix.js +4 -4
- package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
- package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
- package/dist/lib/performance/site-fixes/collections.js +1 -2
- package/dist/lib/performance/site-fixes/frontend-fixes.js +358 -14
- package/dist/lib/performance/site-fixes/helpers.js +3 -4
- package/dist/lib/performance/site-fixes/integrity.js +2 -4
- package/dist/lib/performance/site-fixes/migration-context.js +8 -1
- package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
- package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
- package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
- package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
- package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
- package/dist/lib/performance/sql/mysql-tokens.js +38 -12
- package/dist/lib/reliability/boundary-access.js +114 -57
- package/dist/lib/reliability/react-bugs.js +29 -7
- package/dist/lib/reliability/resource-leak-intervals.js +258 -0
- package/dist/lib/reliability/resource-leaks.js +209 -195
- package/dist/lib/reliability/rule-ids.js +10 -1
- package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
- package/dist/lib/reliability/swallowed-errors.js +107 -21
- package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
- package/dist/lib/reliability/usage.js +16 -5
- package/dist/lib/reliability/walk.js +3 -2
- package/dist/lib/security/auth-crypto/hashing.js +24 -8
- package/dist/lib/security/auth-crypto/jwt.js +21 -6
- package/dist/lib/security/auth-crypto/secrets.js +44 -1
- package/dist/lib/security/auth-crypto/shared.js +3 -1
- package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
- package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
- package/dist/lib/security/cli.js +21 -6
- package/dist/lib/security/cookie-credentials.js +115 -0
- package/dist/lib/security/cookie-csrf.js +241 -0
- package/dist/lib/security/deps/advisory.js +50 -109
- package/dist/lib/security/deps/cli.js +1 -1
- package/dist/lib/security/deps/cvss-score.js +14 -0
- package/dist/lib/security/deps/fixed-version.js +97 -0
- package/dist/lib/security/deps/lockfiles.js +135 -74
- package/dist/lib/security/deps/osv.js +11 -3
- package/dist/lib/security/deps/scan.js +47 -20
- package/dist/lib/security/deps/yarn-lock.js +158 -0
- package/dist/lib/security/examples/injection.cjs +4 -0
- package/dist/lib/security/exposure/cookies.js +17 -2
- package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
- package/dist/lib/security/exposure/frontend-security.js +21 -14
- package/dist/lib/security/exposure/jsx-url.js +18 -33
- package/dist/lib/security/exposure/logging.js +49 -10
- package/dist/lib/security/exposure/prototype-pollution.js +126 -56
- package/dist/lib/security/exposure/react-html.js +26 -0
- package/dist/lib/security/exposure/reflected-xss.js +34 -5
- package/dist/lib/security/files/compose.js +126 -1
- package/dist/lib/security/files/dockerfile.js +62 -4
- package/dist/lib/security/files/index.js +1 -1
- package/dist/lib/security/injection/command.js +46 -18
- package/dist/lib/security/injection/index.js +2 -0
- package/dist/lib/security/injection/ldap.js +86 -0
- package/dist/lib/security/injection/nosql.js +28 -4
- package/dist/lib/security/injection/redos.js +1 -1
- package/dist/lib/security/injection/rule-ids.js +1 -0
- package/dist/lib/security/injection/shared.js +5 -1
- package/dist/lib/security/injection/sql.js +28 -13
- package/dist/lib/security/lexical-declaration.js +113 -0
- package/dist/lib/security/pg-dynamic-query.js +28 -1
- package/dist/lib/security/pg-identifier-guards.js +107 -3
- package/dist/lib/security/phase5-rules.js +26 -135
- package/dist/lib/security/polyglot.js +2 -2
- package/dist/lib/security/redirect-settings.js +98 -0
- package/dist/lib/security/request-forgery/open-redirect.js +18 -5
- package/dist/lib/security/request-forgery/path-traversal.js +84 -6
- package/dist/lib/security/request-forgery/shared.js +1 -7
- package/dist/lib/security/request-forgery/ssrf.js +176 -14
- package/dist/lib/security/rule-descriptions.js +5 -1
- package/dist/lib/security/rule-ids.js +7 -0
- package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
- package/dist/lib/security/site-fixes/exposure.js +21 -3
- package/dist/lib/security/site-fixes/files.js +11 -13
- package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
- package/dist/lib/security/site-fixes/injection.js +17 -4
- package/dist/lib/security/site-fixes/jwt.js +1 -20
- package/dist/lib/security/site-fixes/request-forgery.js +37 -29
- package/dist/lib/security/taint/binding-inputs.js +236 -0
- package/dist/lib/security/taint/filename-guard.js +45 -45
- package/dist/lib/security/taint/guards.js +14 -8
- package/dist/lib/security/taint/index.js +126 -154
- package/dist/lib/security/taint/propagate.js +43 -9
- package/dist/lib/security/taint/record-writes.js +161 -0
- package/dist/lib/security/taint/resolution-cache.js +13 -7
- package/dist/lib/security/taint/sanitizers.js +89 -5
- package/dist/lib/security/taint/trace-nodes.js +61 -0
- package/dist/lib/security/taint/validators.js +89 -9
- package/dist/lib/shared/agent/agent-init.js +28 -5
- package/dist/lib/shared/agent/agent-prompt.js +74 -0
- package/dist/lib/shared/agent/agent-session.js +6 -1
- package/dist/lib/shared/agent/agent-verify.js +58 -11
- package/dist/lib/shared/agent/git-hook.js +17 -6
- package/dist/lib/shared/agent/review-delivery.js +1 -1
- package/dist/lib/shared/agent/review-mcp.js +8 -2
- package/dist/lib/shared/analysis/ast-semantics.js +2 -2
- package/dist/lib/shared/analysis/scan-cache.js +99 -14
- package/dist/lib/shared/check.js +172 -20
- package/dist/lib/shared/cli-dispatch.js +17 -0
- package/dist/lib/shared/config.js +27 -10
- package/dist/lib/shared/explain.js +1 -1
- package/dist/lib/shared/fix/fix-convergence.js +19 -31
- package/dist/lib/shared/fix/fix-handoff.js +16 -11
- package/dist/lib/shared/fix/fix-imports.js +4 -162
- package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
- package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
- package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
- package/dist/lib/shared/fix/fix-presentation.js +9 -6
- package/dist/lib/shared/fix/fix-safety.js +32 -1
- package/dist/lib/shared/fix/fix-scope.js +82 -0
- package/dist/lib/shared/fix/fix-terminal.js +28 -1
- package/dist/lib/shared/fix/fix-typecheck.js +8 -4
- package/dist/lib/shared/fix/import-text-edits.js +148 -0
- package/dist/lib/shared/fix/project-file-access.js +17 -26
- package/dist/lib/shared/fix/read-fix-text.js +20 -0
- package/dist/lib/shared/fix.js +241 -104
- package/dist/lib/shared/formats/finding-location.js +4 -1
- package/dist/lib/shared/formats/github.js +3 -3
- package/dist/lib/shared/formats/sarif.js +9 -3
- package/dist/lib/shared/report-fp.js +97 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
- package/dist/lib/shared/reporting/baseline.js +7 -5
- package/dist/lib/shared/reporting/check-text.js +50 -0
- package/dist/lib/shared/reporting/pillars.js +54 -19
- package/dist/lib/shared/reporting/source-report.js +3 -0
- package/dist/lib/shared/system/atomic-write.js +5 -2
- package/dist/lib/shared/system/glob.js +34 -8
- package/dist/lib/shared/system/sentry.js +222 -0
- package/dist/lib/shared/system/shell-argument.js +5 -1
- package/dist/lib/shared/system/terminal-safe.js +3 -2
- package/dist/lib/shared/system/terminal-style.js +131 -0
- package/dist/lib/shared/workspaces.js +15 -3
- package/docs/assets/cli-workflow.gif +0 -0
- package/integrations/sublime/plugin.py +22 -0
- package/integrations/zed/Cargo.lock +845 -0
- package/integrations/zed/Cargo.toml +11 -0
- package/integrations/zed/extension.toml +11 -0
- package/integrations/zed/src/lib.rs +25 -0
- package/package.json +15 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/commands/loadwise.md +11 -6
- package/plugin/hooks/check-edited-file.mjs +27 -5
- package/polyglot/performance/rules/csharp.yml +34 -5
- package/polyglot/performance/rules/java.yml +24 -1
- package/polyglot/performance/rules/python.yml +74 -2
- package/polyglot/performance/sgconfig.yml +1 -0
- package/polyglot/reliability/rules/csharp.yml +12 -6
- package/polyglot/reliability/rules/go.yml +86 -6
- package/polyglot/reliability/rules/java.yml +22 -1
- package/polyglot/reliability/sgconfig.yml +1 -0
- package/polyglot/security/rules/go.yml +100 -22
- package/polyglot/security/rules/python.yml +74 -6
- package/polyglot/security/rules/rust.yml +65 -0
- package/polyglot/security/sgconfig.yml +1 -0
- package/schema/loadwise.config.schema.json +556 -76
- /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Auto-fix builders for frontend performance rules.
|
|
2
|
-
import { requireReview, ts, ancestor, calleeCall, replaceNode, addOptionsArgument, addProperty } from './helpers.js';
|
|
2
|
+
import { requireReview, ts, ancestor, calleeCall, freshName, replaceNode, addOptionsArgument, addProperty, } from './helpers.js';
|
|
3
|
+
import { listenerCapture } from '../frontend/effect-cleanup.js';
|
|
3
4
|
const PASSIVE_CAPABLE_EVENTS = new Set(['wheel', 'touchmove', 'touchstart']);
|
|
4
5
|
// True when the handler can call preventDefault(), which a passive listener would silently ignore.
|
|
5
6
|
function mayPreventDefault(handler) {
|
|
@@ -82,6 +83,159 @@ function angularOnPushFix({ node }) {
|
|
|
82
83
|
note: 'Enabled `ChangeDetectionStrategy.OnPush`.',
|
|
83
84
|
};
|
|
84
85
|
}
|
|
86
|
+
// `.aborted` is deliberately not recognised: the fixer adds a fresh controller, so an existing
|
|
87
|
+
// `signal.aborted` check never reflects the abort that cleanup performs.
|
|
88
|
+
function isCancellationAwareText(text) {
|
|
89
|
+
return /\bAbortError\b/.test(text);
|
|
90
|
+
}
|
|
91
|
+
function containsThrow(node) {
|
|
92
|
+
let found = false;
|
|
93
|
+
function visit(current) {
|
|
94
|
+
if (found)
|
|
95
|
+
return;
|
|
96
|
+
if (ts.isFunctionLike(current))
|
|
97
|
+
return;
|
|
98
|
+
if (ts.isThrowStatement(current)) {
|
|
99
|
+
found = true;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
ts.forEachChild(current, visit);
|
|
103
|
+
}
|
|
104
|
+
if (ts.isFunctionLike(node) && 'body' in node && node.body)
|
|
105
|
+
visit(node.body);
|
|
106
|
+
else
|
|
107
|
+
visit(node);
|
|
108
|
+
return found;
|
|
109
|
+
}
|
|
110
|
+
function isNegatedCancellationCondition(expression) {
|
|
111
|
+
const unwrapped = ts.skipOuterExpressions(expression);
|
|
112
|
+
if (ts.isPrefixUnaryExpression(unwrapped) && unwrapped.operator === ts.SyntaxKind.ExclamationToken)
|
|
113
|
+
return true;
|
|
114
|
+
if (!ts.isBinaryExpression(unwrapped))
|
|
115
|
+
return false;
|
|
116
|
+
const op = unwrapped.operatorToken.kind;
|
|
117
|
+
return op === ts.SyntaxKind.ExclamationEqualsToken || op === ts.SyntaxKind.ExclamationEqualsEqualsToken;
|
|
118
|
+
}
|
|
119
|
+
function isCancellationSafeBody(node, sourceFile) {
|
|
120
|
+
const body = ts.isArrowFunction(node) || ts.isFunctionExpression(node) ? node.body : ts.isCatchClause(node) ? node.block : node;
|
|
121
|
+
if (!body)
|
|
122
|
+
return false;
|
|
123
|
+
if (!isCancellationAwareText(body.getText(sourceFile)))
|
|
124
|
+
return false;
|
|
125
|
+
if (!containsThrow(body))
|
|
126
|
+
return true;
|
|
127
|
+
if (!ts.isBlock(body))
|
|
128
|
+
return false;
|
|
129
|
+
for (let index = 0; index < body.statements.length; index++) {
|
|
130
|
+
const statement = body.statements[index];
|
|
131
|
+
if (ts.isIfStatement(statement) && isCancellationAwareText(statement.expression.getText(sourceFile))) {
|
|
132
|
+
const negated = isNegatedCancellationCondition(statement.expression);
|
|
133
|
+
const cancelBranch = negated ? statement.elseStatement : statement.thenStatement;
|
|
134
|
+
if (cancelBranch && containsThrow(cancelBranch))
|
|
135
|
+
return false;
|
|
136
|
+
const tailHasThrow = body.statements.slice(index + 1).some(containsThrow);
|
|
137
|
+
if (!negated && tailHasThrow) {
|
|
138
|
+
const returnsOrExits = ts.isReturnStatement(statement.thenStatement) ||
|
|
139
|
+
(ts.isBlock(statement.thenStatement) &&
|
|
140
|
+
statement.thenStatement.statements.some((inner) => ts.isReturnStatement(inner)));
|
|
141
|
+
return returnsOrExits;
|
|
142
|
+
}
|
|
143
|
+
if (negated && tailHasThrow)
|
|
144
|
+
return false;
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
if (containsThrow(statement))
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
// A safe catch only covers the chain when nothing chained after it can reject on the fulfilled
|
|
153
|
+
// `undefined` it produces.
|
|
154
|
+
function isTerminalChainCall(chainCall) {
|
|
155
|
+
let current = chainCall;
|
|
156
|
+
while (current.parent && ts.isParenthesizedExpression(current.parent))
|
|
157
|
+
current = current.parent;
|
|
158
|
+
return !(ts.isPropertyAccessExpression(current.parent) && current.parent.expression === current);
|
|
159
|
+
}
|
|
160
|
+
function isNullishArgument(argument) {
|
|
161
|
+
if (!argument)
|
|
162
|
+
return false;
|
|
163
|
+
const unwrapped = ts.skipOuterExpressions(argument);
|
|
164
|
+
return unwrapped.kind === ts.SyntaxKind.NullKeyword || (ts.isIdentifier(unwrapped) && unwrapped.text === 'undefined');
|
|
165
|
+
}
|
|
166
|
+
function hasCancellationAwareHandler(fetchCall, effectCallback, sourceFile) {
|
|
167
|
+
let current = fetchCall;
|
|
168
|
+
while (current.parent && current.parent !== effectCallback) {
|
|
169
|
+
const parent = current.parent;
|
|
170
|
+
if (ts.isParenthesizedExpression(parent)) {
|
|
171
|
+
current = parent;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (ts.isPropertyAccessExpression(parent) &&
|
|
175
|
+
parent.expression === current &&
|
|
176
|
+
ts.isCallExpression(parent.parent) &&
|
|
177
|
+
parent.parent.expression === parent) {
|
|
178
|
+
const chainCall = parent.parent;
|
|
179
|
+
const method = parent.name.text;
|
|
180
|
+
if (method === 'catch' &&
|
|
181
|
+
chainCall.arguments[0] &&
|
|
182
|
+
isCancellationSafeBody(ts.skipOuterExpressions(chainCall.arguments[0]), sourceFile) &&
|
|
183
|
+
isTerminalChainCall(chainCall))
|
|
184
|
+
return true;
|
|
185
|
+
// The rejection callback of `.then(onFulfilled, onRejected)` does not cover a rejection from
|
|
186
|
+
// `onFulfilled` (e.g. an aborted body read), so only `.then(undefined, onRejected)` is safe.
|
|
187
|
+
if (method === 'then' &&
|
|
188
|
+
isNullishArgument(chainCall.arguments[0]) &&
|
|
189
|
+
chainCall.arguments[1] &&
|
|
190
|
+
isCancellationSafeBody(ts.skipOuterExpressions(chainCall.arguments[1]), sourceFile) &&
|
|
191
|
+
isTerminalChainCall(chainCall))
|
|
192
|
+
return true;
|
|
193
|
+
current = chainCall;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (ts.isAwaitExpression(parent))
|
|
197
|
+
return isAwaitCaughtByCancellationSafeCatch(parent, effectCallback, sourceFile);
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
function isAwaitCaughtByCancellationSafeCatch(awaitNode, effectCallback, sourceFile) {
|
|
203
|
+
let child = awaitNode;
|
|
204
|
+
for (let ancestorNode = awaitNode.parent; ancestorNode && ancestorNode !== effectCallback; child = ancestorNode, ancestorNode = ancestorNode.parent) {
|
|
205
|
+
if (ts.isFunctionLike(ancestorNode))
|
|
206
|
+
return false;
|
|
207
|
+
if (ts.isTryStatement(ancestorNode) && ancestorNode.tryBlock === child && ancestorNode.catchClause) {
|
|
208
|
+
// An intervening catch may rewrap the error, so the outer catch no longer sees AbortError.
|
|
209
|
+
return isCancellationSafeBody(ancestorNode.catchClause, sourceFile);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
// A return inside a try or catch block still runs the `finally` block, which can start the target.
|
|
215
|
+
function returnEntersFinallyAfter(node, root, targetStart) {
|
|
216
|
+
for (let child = node, parent = node.parent; child !== root && parent; child = parent, parent = parent.parent) {
|
|
217
|
+
if (ts.isTryStatement(parent) &&
|
|
218
|
+
child !== parent.finallyBlock &&
|
|
219
|
+
parent.finallyBlock &&
|
|
220
|
+
parent.finallyBlock.end >= targetStart)
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
function hasEffectExitSince(root, targetStart) {
|
|
226
|
+
let exits = false;
|
|
227
|
+
function visit(node) {
|
|
228
|
+
if (exits || (node !== root && ts.isFunctionLike(node)))
|
|
229
|
+
return;
|
|
230
|
+
if (ts.isReturnStatement(node) && (node.end >= targetStart || returnEntersFinallyAfter(node, root, targetStart))) {
|
|
231
|
+
exits = true;
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
ts.forEachChild(node, visit);
|
|
235
|
+
}
|
|
236
|
+
visit(root);
|
|
237
|
+
return exits;
|
|
238
|
+
}
|
|
85
239
|
function reactFetchAbortFix({ node, sourceFile, text }) {
|
|
86
240
|
if (!node || !sourceFile || text === undefined)
|
|
87
241
|
return;
|
|
@@ -94,28 +248,43 @@ function reactFetchAbortFix({ node, sourceFile, text }) {
|
|
|
94
248
|
const callback = effectCall?.arguments[0];
|
|
95
249
|
if (!callback || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) || !ts.isBlock(callback.body))
|
|
96
250
|
return;
|
|
97
|
-
|
|
251
|
+
// A fetch inside a nested function may run at any point, so any effect-level return can precede it.
|
|
252
|
+
const fetchInNestedFunction = ts.findAncestor(fetchCall, ts.isFunctionLike) !== callback;
|
|
253
|
+
if (hasEffectExitSince(callback.body, fetchInNestedFunction ? 0 : fetchCall.getStart(sourceFile)) ||
|
|
98
254
|
/\bAbortController\b/.test(callback.body.getText(sourceFile)))
|
|
99
255
|
return;
|
|
256
|
+
const controller = freshName(sourceFile, 'controller');
|
|
257
|
+
if (!controller)
|
|
258
|
+
return;
|
|
259
|
+
const signal = `signal: ${controller}.signal`;
|
|
100
260
|
const edits = [];
|
|
101
261
|
const openBrace = callback.body.getStart(sourceFile) + 1;
|
|
102
|
-
edits.push({ start: openBrace, end: openBrace, text:
|
|
262
|
+
edits.push({ start: openBrace, end: openBrace, text: `\n const ${controller} = new AbortController();` });
|
|
103
263
|
if (fetchCall.arguments.length === 1) {
|
|
104
|
-
edits.push(addOptionsArgument(fetchCall,
|
|
264
|
+
edits.push(addOptionsArgument(fetchCall, signal, text));
|
|
105
265
|
}
|
|
106
266
|
else {
|
|
107
267
|
const opts = ts.skipOuterExpressions(fetchCall.arguments[1]);
|
|
108
268
|
if (!ts.isObjectLiteralExpression(opts))
|
|
109
269
|
return;
|
|
110
|
-
edits.push(addProperty(opts,
|
|
270
|
+
edits.push(addProperty(opts, signal, text));
|
|
111
271
|
}
|
|
112
272
|
const closeBrace = callback.body.end - 1;
|
|
113
|
-
edits.push({ start: closeBrace, end: closeBrace, text:
|
|
273
|
+
edits.push({ start: closeBrace, end: closeBrace, text: ` return () => ${controller}.abort();\n` });
|
|
274
|
+
const cancellationAware = hasCancellationAwareHandler(fetchCall, callback, sourceFile);
|
|
114
275
|
return {
|
|
115
276
|
edits,
|
|
116
|
-
|
|
277
|
+
...(cancellationAware ? {} : { requiresReview: true }),
|
|
278
|
+
note: cancellationAware
|
|
279
|
+
? 'Creates an `AbortController`, passes `controller.signal` to `fetch()`, and aborts on unmount.'
|
|
280
|
+
: 'Creates an `AbortController`, passes `controller.signal` to `fetch()`, and aborts on unmount. Aborting rejects with `AbortError`; verify rejection handling so unmount does not trigger unhandled rejections.',
|
|
117
281
|
};
|
|
118
282
|
}
|
|
283
|
+
function containsReturn(node) {
|
|
284
|
+
if (ts.isFunctionLike(node))
|
|
285
|
+
return false;
|
|
286
|
+
return ts.isReturnStatement(node) || (ts.forEachChild(node, (child) => containsReturn(child) || undefined) ?? false);
|
|
287
|
+
}
|
|
119
288
|
function domListenerCleanupFix({ node, sourceFile }) {
|
|
120
289
|
if (!node || !sourceFile)
|
|
121
290
|
return;
|
|
@@ -123,7 +292,16 @@ function domListenerCleanupFix({ node, sourceFile }) {
|
|
|
123
292
|
if (!call || !ts.isPropertyAccessExpression(call.expression) || call.expression.name.text !== 'addEventListener')
|
|
124
293
|
return;
|
|
125
294
|
const [eventArg, handlerArg] = call.arguments;
|
|
126
|
-
|
|
295
|
+
const target = call.expression.expression;
|
|
296
|
+
if (!eventArg ||
|
|
297
|
+
!ts.isStringLiteralLike(eventArg) ||
|
|
298
|
+
!handlerArg ||
|
|
299
|
+
!ts.isIdentifier(handlerArg) ||
|
|
300
|
+
!ts.isIdentifier(target) ||
|
|
301
|
+
!['window', 'document', 'globalThis'].includes(target.text))
|
|
302
|
+
return;
|
|
303
|
+
const capture = listenerCapture(call.arguments[2]);
|
|
304
|
+
if (capture === undefined)
|
|
127
305
|
return;
|
|
128
306
|
const effectCall = ts.findAncestor(call, (candidate) => ts.isCallExpression(candidate) &&
|
|
129
307
|
ts.isIdentifier(candidate.expression) &&
|
|
@@ -131,7 +309,9 @@ function domListenerCleanupFix({ node, sourceFile }) {
|
|
|
131
309
|
const callback = effectCall?.arguments[0];
|
|
132
310
|
if (!callback || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) || !ts.isBlock(callback.body))
|
|
133
311
|
return;
|
|
134
|
-
if (
|
|
312
|
+
if (!ts.isExpressionStatement(call.parent) ||
|
|
313
|
+
call.parent.parent !== callback.body ||
|
|
314
|
+
callback.body.statements.some(containsReturn))
|
|
135
315
|
return;
|
|
136
316
|
const receiver = call.expression.expression.getText(sourceFile);
|
|
137
317
|
const closeBrace = callback.body.end - 1;
|
|
@@ -140,22 +320,184 @@ function domListenerCleanupFix({ node, sourceFile }) {
|
|
|
140
320
|
{
|
|
141
321
|
start: closeBrace,
|
|
142
322
|
end: closeBrace,
|
|
143
|
-
text: ` return () => ${receiver}.removeEventListener(${eventArg.getText(sourceFile)}, ${handlerArg.text});\n`,
|
|
323
|
+
text: ` return () => ${receiver}.removeEventListener(${eventArg.getText(sourceFile)}, ${handlerArg.text}${capture ? ', true' : ''});\n`,
|
|
144
324
|
},
|
|
145
325
|
],
|
|
146
326
|
note: 'Adds a cleanup function that calls `removeEventListener` on unmount.',
|
|
147
327
|
};
|
|
148
328
|
}
|
|
149
|
-
function
|
|
150
|
-
|
|
329
|
+
function takeUntilDestroyedLocalName(sourceFile) {
|
|
330
|
+
for (const statement of sourceFile.statements) {
|
|
331
|
+
if (!ts.isImportDeclaration(statement) || statement.importClause?.isTypeOnly)
|
|
332
|
+
continue;
|
|
333
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier))
|
|
334
|
+
continue;
|
|
335
|
+
if (statement.moduleSpecifier.text !== '@angular/core/rxjs-interop')
|
|
336
|
+
continue;
|
|
337
|
+
const named = statement.importClause?.namedBindings;
|
|
338
|
+
if (!named || !ts.isNamedImports(named))
|
|
339
|
+
continue;
|
|
340
|
+
for (const spec of named.elements) {
|
|
341
|
+
if (spec.isTypeOnly)
|
|
342
|
+
continue;
|
|
343
|
+
if ((spec.propertyName ?? spec.name).text === 'takeUntilDestroyed')
|
|
344
|
+
return spec.name.text;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
function destroyRefLocalName(sourceFile) {
|
|
350
|
+
for (const statement of sourceFile.statements) {
|
|
351
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
352
|
+
continue;
|
|
353
|
+
if (statement.moduleSpecifier.text !== '@angular/core')
|
|
354
|
+
continue;
|
|
355
|
+
const named = statement.importClause?.namedBindings;
|
|
356
|
+
if (!named || !ts.isNamedImports(named))
|
|
357
|
+
continue;
|
|
358
|
+
for (const spec of named.elements) {
|
|
359
|
+
if ((spec.propertyName ?? spec.name).text === 'DestroyRef')
|
|
360
|
+
return spec.name.text;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
function isDestroyRefType(typeNode, destroyRefName) {
|
|
366
|
+
return Boolean(typeNode &&
|
|
367
|
+
ts.isTypeReferenceNode(typeNode) &&
|
|
368
|
+
ts.isIdentifier(typeNode.typeName) &&
|
|
369
|
+
typeNode.typeName.text === destroyRefName);
|
|
370
|
+
}
|
|
371
|
+
function isInjectDestroyRefCall(expr, destroyRefName) {
|
|
372
|
+
if (!expr || !ts.isCallExpression(expr))
|
|
373
|
+
return false;
|
|
374
|
+
if (!ts.isIdentifier(expr.expression) || expr.expression.text !== 'inject')
|
|
375
|
+
return false;
|
|
376
|
+
return expr.arguments.length === 1 && ts.isIdentifier(expr.arguments[0]) && expr.arguments[0].text === destroyRefName;
|
|
377
|
+
}
|
|
378
|
+
function findDestroyRefParameterProperty(constructor, destroyRefName) {
|
|
379
|
+
for (const param of constructor.parameters) {
|
|
380
|
+
if (!ts.isParameterPropertyDeclaration(param, constructor))
|
|
381
|
+
continue;
|
|
382
|
+
if (!ts.isIdentifier(param.name) || !isDestroyRefType(param.type, destroyRefName))
|
|
383
|
+
continue;
|
|
384
|
+
return param.name.text;
|
|
385
|
+
}
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
function findDestroyRefMember(call, destroyRefName) {
|
|
389
|
+
const cls = ancestor(call, ts.isClassLike);
|
|
390
|
+
if (!cls)
|
|
391
|
+
return undefined;
|
|
392
|
+
for (const member of cls.members) {
|
|
393
|
+
if (ts.isPropertyDeclaration(member) &&
|
|
394
|
+
ts.isIdentifier(member.name) &&
|
|
395
|
+
!member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword) &&
|
|
396
|
+
(isInjectDestroyRefCall(member.initializer, destroyRefName) ||
|
|
397
|
+
(isDestroyRefType(member.type, destroyRefName) && !member.questionToken && member.initializer !== undefined))) {
|
|
398
|
+
return member.name.text;
|
|
399
|
+
}
|
|
400
|
+
if (!ts.isConstructorDeclaration(member))
|
|
401
|
+
continue;
|
|
402
|
+
const parameterName = findDestroyRefParameterProperty(member, destroyRefName);
|
|
403
|
+
if (parameterName)
|
|
404
|
+
return parameterName;
|
|
405
|
+
}
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
function isStaticClassMember(node) {
|
|
409
|
+
let current = node.parent;
|
|
410
|
+
while (current) {
|
|
411
|
+
if (ts.isPropertyDeclaration(current) || ts.isMethodDeclaration(current) || ts.isAccessor(current)) {
|
|
412
|
+
return Boolean(current.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword));
|
|
413
|
+
}
|
|
414
|
+
if (ts.isClassLike(current))
|
|
415
|
+
return false;
|
|
416
|
+
current = current.parent;
|
|
417
|
+
}
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
function hasLexicalClassReceiver(node) {
|
|
421
|
+
let current = node.parent;
|
|
422
|
+
while (current) {
|
|
423
|
+
if (ts.isFunctionDeclaration(current) || ts.isFunctionExpression(current))
|
|
424
|
+
return false;
|
|
425
|
+
if (ts.isMethodDeclaration(current) ||
|
|
426
|
+
ts.isConstructorDeclaration(current) ||
|
|
427
|
+
ts.isPropertyDeclaration(current) ||
|
|
428
|
+
ts.isAccessor(current)) {
|
|
429
|
+
return !current.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword);
|
|
430
|
+
}
|
|
431
|
+
if (ts.isClassLike(current))
|
|
432
|
+
return false;
|
|
433
|
+
current = current.parent;
|
|
434
|
+
}
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
function isInAngularInjectionContext(call) {
|
|
438
|
+
let current = call.parent;
|
|
439
|
+
while (current) {
|
|
440
|
+
if (ts.isConstructorDeclaration(current))
|
|
441
|
+
return true;
|
|
442
|
+
if (ts.isPropertyDeclaration(current)) {
|
|
443
|
+
return !current.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword);
|
|
444
|
+
}
|
|
445
|
+
if (ts.isMethodDeclaration(current) ||
|
|
446
|
+
ts.isFunctionDeclaration(current) ||
|
|
447
|
+
ts.isArrowFunction(current) ||
|
|
448
|
+
ts.isFunctionExpression(current) ||
|
|
449
|
+
ts.isGetAccessorDeclaration(current) ||
|
|
450
|
+
ts.isSetAccessorDeclaration(current)) {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
current = current.parent;
|
|
454
|
+
}
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
const MAX_TYPED_PIPE_OPERATORS = 9;
|
|
458
|
+
function angularSubscribeCleanupFix({ node, sourceFile }) {
|
|
459
|
+
if (!node || !sourceFile)
|
|
460
|
+
return;
|
|
461
|
+
const localTakeUntilDestroyed = takeUntilDestroyedLocalName(sourceFile);
|
|
462
|
+
if (!localTakeUntilDestroyed)
|
|
151
463
|
return;
|
|
152
464
|
const call = calleeCall(node);
|
|
153
465
|
if (!call || !ts.isPropertyAccessExpression(call.expression) || call.expression.name.text !== 'subscribe')
|
|
154
466
|
return;
|
|
467
|
+
if (isStaticClassMember(call))
|
|
468
|
+
return;
|
|
469
|
+
let destroyRefArg = '';
|
|
470
|
+
if (!isInAngularInjectionContext(call)) {
|
|
471
|
+
if (!hasLexicalClassReceiver(call))
|
|
472
|
+
return;
|
|
473
|
+
const destroyRefName = destroyRefLocalName(sourceFile);
|
|
474
|
+
const memberName = destroyRefName ? findDestroyRefMember(call, destroyRefName) : undefined;
|
|
475
|
+
if (!memberName)
|
|
476
|
+
return;
|
|
477
|
+
destroyRefArg = `this.${memberName}`;
|
|
478
|
+
}
|
|
479
|
+
const operatorCall = `${localTakeUntilDestroyed}(${destroyRefArg})`;
|
|
155
480
|
const receiver = call.expression.expression;
|
|
481
|
+
if (ts.isCallExpression(receiver) &&
|
|
482
|
+
ts.isPropertyAccessExpression(receiver.expression) &&
|
|
483
|
+
receiver.expression.name.text === 'pipe') {
|
|
484
|
+
// RxJS types pipe() with 9 operators precisely; a 10th falls back to Observable<unknown>.
|
|
485
|
+
if (receiver.arguments.length >= MAX_TYPED_PIPE_OPERATORS) {
|
|
486
|
+
return {
|
|
487
|
+
edits: [{ start: receiver.end, end: receiver.end, text: `.pipe(${operatorCall})` }],
|
|
488
|
+
note: `Pipes through \`${operatorCall}\` so the subscription completes when the component is destroyed.`,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const insertPos = receiver.arguments.length > 0 ? receiver.arguments[receiver.arguments.length - 1].end : receiver.end - 1;
|
|
492
|
+
const prefix = receiver.arguments.length > 0 ? ', ' : '';
|
|
493
|
+
return {
|
|
494
|
+
edits: [{ start: insertPos, end: insertPos, text: `${prefix}${operatorCall}` }],
|
|
495
|
+
note: `Pipes through \`${operatorCall}\` so the subscription completes when the component is destroyed.`,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
156
498
|
return {
|
|
157
|
-
edits: [replaceNode(receiver, `${receiver.getText(sourceFile)}.pipe(
|
|
158
|
-
note:
|
|
499
|
+
edits: [replaceNode(receiver, `${receiver.getText(sourceFile)}.pipe(${operatorCall})`)],
|
|
500
|
+
note: `Pipes through \`${operatorCall}\` so the subscription completes when the component is destroyed.`,
|
|
159
501
|
};
|
|
160
502
|
}
|
|
161
503
|
const SUBPATH_BARREL_PACKAGES = new Set(['lodash', 'lodash-es', 'date-fns']);
|
|
@@ -172,6 +514,8 @@ function barrelHeavyImportFix({ node, sourceFile }) {
|
|
|
172
514
|
if (!named || !ts.isNamedImports(named) || named.elements.length !== 1 || decl.importClause?.name)
|
|
173
515
|
return;
|
|
174
516
|
const [spec] = named.elements;
|
|
517
|
+
if (decl.importClause?.isTypeOnly || spec.isTypeOnly)
|
|
518
|
+
return;
|
|
175
519
|
const imported = (spec.propertyName ?? spec.name).text;
|
|
176
520
|
const local = spec.name.text;
|
|
177
521
|
const dateFnsSpecifier = imported === local ? local : `${imported} as ${local}`;
|
|
@@ -129,7 +129,7 @@ function addProperty(literal, property, text) {
|
|
|
129
129
|
return { start: literal.getStart(), end: literal.end, text: `{ ${property} }` };
|
|
130
130
|
const trailingComma = literal.properties.hasTrailingComma;
|
|
131
131
|
const multiline = spansLines(text, literal.getStart(), last.getStart());
|
|
132
|
-
const commaEnd =
|
|
132
|
+
const commaEnd = literal.properties.end;
|
|
133
133
|
if (multiline) {
|
|
134
134
|
const indentation = indentationAt(text, last.getStart());
|
|
135
135
|
const lineText = `\n${indentation}${property}`;
|
|
@@ -142,12 +142,11 @@ function addOptionsArgument(call, property, text) {
|
|
|
142
142
|
const options = `{ ${property} }`;
|
|
143
143
|
const [last] = call.arguments.slice(-1);
|
|
144
144
|
if (!last) {
|
|
145
|
-
const
|
|
146
|
-
const insertAt = open === -1 ? call.end - 1 : open + 1;
|
|
145
|
+
const insertAt = call.arguments.pos;
|
|
147
146
|
return { start: insertAt, end: insertAt, text: options };
|
|
148
147
|
}
|
|
149
148
|
const trailingComma = call.arguments.hasTrailingComma;
|
|
150
|
-
const commaEnd =
|
|
149
|
+
const commaEnd = call.arguments.end;
|
|
151
150
|
if (spansLines(text, call.expression.end, last.getStart())) {
|
|
152
151
|
const lineText = `\n${indentationAt(text, last.getStart())}${options}`;
|
|
153
152
|
return { start: commaEnd, end: commaEnd, text: trailingComma ? `${lineText},` : `,${lineText}` };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { hasMultilineTemplate, ts, textOf, ancestor, calleeCall, indentationAt, lineStart, freshName, isPlainReference, mentionsIdentifier, requireReview, } from './helpers.js';
|
|
2
2
|
import { drizzleScopeUnverified, drizzleUnfilteredWrite } from './integrity-drizzle.js';
|
|
3
3
|
import { eolOf, formatterNote, unitFor, wrapStatements } from './integrity-wrap.js';
|
|
4
|
+
import { isSessionIndependentCheckoutQuery } from '../datastores/pg-integrity.js';
|
|
4
5
|
const transactionStart = /^\s*(?:begin|start\s+transaction)\b/i;
|
|
5
6
|
const transactionEnd = /^\s*(?:commit|rollback|end|abort)\b/i;
|
|
6
|
-
const transactionControl = /^\s*(?:begin|start|commit|rollback|end|abort|savepoint|release|set\s+transaction)\b/i;
|
|
7
7
|
// ---- pg-unreleased-checkout ----
|
|
8
8
|
function unreleasedCheckout({ node }) {
|
|
9
9
|
if (!node)
|
|
@@ -25,9 +25,7 @@ function unreleasedCheckout({ node }) {
|
|
|
25
25
|
const query = access.parent;
|
|
26
26
|
if (!ts.isCallExpression(query) || query.expression !== access)
|
|
27
27
|
return;
|
|
28
|
-
|
|
29
|
-
const [statement] = query.arguments;
|
|
30
|
-
if (statement && ts.isStringLiteralLike(statement) && transactionControl.test(statement.text))
|
|
28
|
+
if (!isSessionIndependentCheckoutQuery(query))
|
|
31
29
|
return;
|
|
32
30
|
return {
|
|
33
31
|
edits: [{ start: receiver.getStart(), end: receiver.end, text: textOf(pool) }],
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// helpers shared across migration site-fix rules.
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { detectFramework } from '../migrations/extract-code.js';
|
|
5
|
+
import { splitStatementRanges as splitMysqlStatements } from '../sql/mysql-tokens.js';
|
|
5
6
|
import { ts, ancestor } from './helpers.js';
|
|
6
7
|
export const IDENT = String.raw `(?:"[^"]*"|\x60[^\x60]*\x60|\$\{[^}]*\}|[A-Za-z_][\w$]*)`;
|
|
7
8
|
export const QUALIFIED = String.raw `${IDENT}(?:\s*\.\s*${IDENT})*`;
|
|
@@ -237,7 +238,13 @@ export function migrationContext({ finding, node, sourceFile, text, load, }) {
|
|
|
237
238
|
return;
|
|
238
239
|
}
|
|
239
240
|
const options = { escapes: Boolean(context.quote), holes: context.quote === '`' };
|
|
240
|
-
|
|
241
|
+
const isMysqlOnlineChange = finding.ruleId === 'migration-mysql-table-copy' || finding.ruleId === 'migration-mysql-lock-unspecified';
|
|
242
|
+
if (isMysqlOnlineChange && sourceFile && text.slice(context.regionStart, context.regionEnd).includes('#'))
|
|
243
|
+
return;
|
|
244
|
+
const ranges = isMysqlOnlineChange && !sourceFile
|
|
245
|
+
? splitMysqlStatements(text).map((range) => ({ ...range, semicolon: text[range.end] === ';' ? range.end : null }))
|
|
246
|
+
: splitStatements(text, context.regionStart, context.regionEnd, options);
|
|
247
|
+
context.statements = ranges.map((statement) => {
|
|
241
248
|
const sql = decodeRaw(text.slice(statement.start, statement.end), context.quote);
|
|
242
249
|
return { ...statement, sql, masked: mask(sql, options.holes) };
|
|
243
250
|
});
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// Migration site-fix rules for indexes, constraints, transactions, lock timeouts and online
|
|
2
2
|
// table rewrites.
|
|
3
3
|
import { quoteIdentifier } from '../migrations/operations.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { mysqlOptionTokens, parseMysqlStatements, tokenizeRange } from '../sql/mysql-tokens.js';
|
|
5
|
+
import { ts, ancestor, enclosingStatement, insertLinesBefore, lineStart } from './helpers.js';
|
|
6
|
+
import { IDENT, alterParts, groupText, hasIndices, lastNamePart, sliceOf, splice, subcommandAt, unquote, } from './migration-context.js';
|
|
6
7
|
import { insideSequelizeTransaction, joinNotes, place } from './migration-placement.js';
|
|
7
8
|
const LOCK_TIMEOUT_SQL = "SET lock_timeout = '5s'";
|
|
8
9
|
const INVALID_INDEX_NOTE = 'If a concurrent build fails it leaves an INVALID index behind: drop it with `DROP INDEX CONCURRENTLY` and rerun.';
|
|
@@ -131,27 +132,37 @@ function upFunction(node) {
|
|
|
131
132
|
}
|
|
132
133
|
function lockTimeoutInCode(context) {
|
|
133
134
|
const receiver = LOCK_TIMEOUT_RECEIVERS[context.framework];
|
|
134
|
-
|
|
135
|
-
if (!receiver || !fn)
|
|
135
|
+
if (!receiver || !context.node)
|
|
136
136
|
return;
|
|
137
|
-
const
|
|
137
|
+
const fn = upFunction(context.node);
|
|
138
|
+
if (!fn)
|
|
139
|
+
return;
|
|
140
|
+
const target = enclosingStatement(context.node);
|
|
138
141
|
const parameter = fn.parameters[0]?.name;
|
|
139
142
|
const isAsync = (ts.getModifiers(fn) ?? []).some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword);
|
|
140
|
-
if (!
|
|
141
|
-
|
|
143
|
+
if (!target ||
|
|
144
|
+
target.parent !== fn.body ||
|
|
145
|
+
!ts.isExpressionStatement(target) ||
|
|
146
|
+
!parameter ||
|
|
147
|
+
!ts.isIdentifier(parameter) ||
|
|
148
|
+
!isAsync)
|
|
149
|
+
return;
|
|
150
|
+
const statement = `await ${parameter.text}${receiver}("${LOCK_TIMEOUT_SQL}");`;
|
|
151
|
+
const start = target.getStart();
|
|
152
|
+
const inline = Boolean(context.text.slice(lineStart(context.text, start), start).trim());
|
|
142
153
|
return {
|
|
143
|
-
edits: [
|
|
144
|
-
note: '
|
|
154
|
+
edits: [inline ? { start, end: start, text: statement + ' ' } : insertLinesBefore(target, [statement])],
|
|
155
|
+
note: 'Verify the effective `lock_timeout` on the DDL connection. Retry the migration when it times out.',
|
|
145
156
|
};
|
|
146
157
|
}
|
|
147
158
|
export function missingLockTimeout(context) {
|
|
148
|
-
if (context.sourceFile)
|
|
159
|
+
if (context.sourceFile && context.statements.length <= 1)
|
|
149
160
|
return lockTimeoutInCode(context);
|
|
150
161
|
if (!context.statement)
|
|
151
162
|
return;
|
|
152
163
|
return place(context, {
|
|
153
164
|
sqls: [LOCK_TIMEOUT_SQL, context.statement.sql],
|
|
154
|
-
note: '
|
|
165
|
+
note: 'Verify the effective `lock_timeout` on the DDL connection. Retry the migration when it times out.',
|
|
155
166
|
});
|
|
156
167
|
}
|
|
157
168
|
export function tableRewriteCommand(context) {
|
|
@@ -170,31 +181,56 @@ export function tableRewriteCommand(context) {
|
|
|
170
181
|
});
|
|
171
182
|
}
|
|
172
183
|
// States ALGORITHM=INPLACE and LOCK=NONE, replacing whatever the statement asked for.
|
|
173
|
-
function
|
|
174
|
-
const
|
|
175
|
-
|
|
184
|
+
function rewriteMysqlOptions(sql, separator) {
|
|
185
|
+
const tokens = tokenizeRange(sql, 0, sql.length);
|
|
186
|
+
const lastToken = tokens.at(-1);
|
|
187
|
+
if (!lastToken)
|
|
176
188
|
return;
|
|
177
|
-
|
|
178
|
-
if (!createIndex && !/^ALTER\s+TABLE\b/i.test(statement.masked))
|
|
179
|
-
return;
|
|
180
|
-
let { sql } = statement;
|
|
189
|
+
let rewritten = sql;
|
|
181
190
|
const missing = [];
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
191
|
+
const replacements = [];
|
|
192
|
+
const options = mysqlOptionTokens(tokens);
|
|
193
|
+
for (const [name, value, allowedValues] of ONLINE_MYSQL_OPTIONS) {
|
|
194
|
+
const option = options.findIndex((token) => token.type === 'word' && token.upper === name);
|
|
195
|
+
const current = options[option + (options[option + 1]?.value === '=' ? 2 : 1)];
|
|
196
|
+
if (option === -1)
|
|
197
|
+
missing.push(`${name}=${value}`);
|
|
198
|
+
else if (current?.type === 'word' && allowedValues.has(current.upper))
|
|
199
|
+
replacements.push({ ...current, value });
|
|
190
200
|
else
|
|
191
|
-
|
|
201
|
+
return;
|
|
192
202
|
}
|
|
193
203
|
if (missing.length)
|
|
194
|
-
|
|
195
|
-
|
|
204
|
+
rewritten = splice(rewritten, lastToken.end, lastToken.end, separator + missing.join(separator));
|
|
205
|
+
for (const { start, end, value } of replacements.sort((left, right) => right.start - left.start)) {
|
|
206
|
+
rewritten = splice(rewritten, start, end, value);
|
|
207
|
+
}
|
|
208
|
+
return rewritten;
|
|
209
|
+
}
|
|
210
|
+
function onlineMysqlStatement(context, note) {
|
|
211
|
+
const { literal, statement } = context;
|
|
212
|
+
if (literal && ts.isTemplateExpression(literal))
|
|
196
213
|
return;
|
|
197
|
-
|
|
214
|
+
if (!statement || /\/\*(?:!|M!)/i.test(statement.sql))
|
|
215
|
+
return;
|
|
216
|
+
const createIndex = /^CREATE\s+(?:UNIQUE\s+|FULLTEXT\s+|SPATIAL\s+)?INDEX\b/i.test(statement.masked);
|
|
217
|
+
if (!createIndex && !/^ALTER\s+TABLE\b/i.test(statement.masked))
|
|
218
|
+
return;
|
|
219
|
+
const rewritten = rewriteMysqlOptions(statement.sql, createIndex ? ' ' : ', ');
|
|
220
|
+
if (!rewritten || rewritten === statement.sql || !hasOnlineMysqlOptions(rewritten))
|
|
221
|
+
return;
|
|
222
|
+
return place(context, { sqls: [rewritten], note });
|
|
223
|
+
}
|
|
224
|
+
const ONLINE_MYSQL_OPTIONS = [
|
|
225
|
+
['ALGORITHM', 'INPLACE', new Set(['DEFAULT', 'INPLACE', 'COPY', 'INSTANT', 'NOCOPY'])],
|
|
226
|
+
['LOCK', 'NONE', new Set(['DEFAULT', 'NONE', 'SHARED', 'EXCLUSIVE'])],
|
|
227
|
+
];
|
|
228
|
+
function hasOnlineMysqlOptions(sql) {
|
|
229
|
+
const [statement, more] = parseMysqlStatements(sql);
|
|
230
|
+
return (!more &&
|
|
231
|
+
(statement?.kind === 'alter-table' || statement?.kind === 'create-index') &&
|
|
232
|
+
statement.algorithm === 'INPLACE' &&
|
|
233
|
+
statement.lock === 'NONE');
|
|
198
234
|
}
|
|
199
235
|
export function mysqlTableCopy(context) {
|
|
200
236
|
return onlineMysqlStatement(context, 'MySQL now refuses the statement with an error instead of copying the table and blocking writes. If it refuses, run the change with `gh-ost` or `pt-online-schema-change`, or add a new column, backfill in batches and swap.');
|