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,151 +1,6 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return;
|
|
5
|
-
const moduleSpecifier = statement.moduleSpecifier.text;
|
|
6
|
-
const clause = statement.importClause;
|
|
7
|
-
if (!clause)
|
|
8
|
-
return { moduleSpecifier, named: [], isTypeOnly: false };
|
|
9
|
-
const isTypeOnly = Boolean(clause.isTypeOnly);
|
|
10
|
-
const defaultName = clause.name?.text;
|
|
11
|
-
const named = [];
|
|
12
|
-
let namespaceName;
|
|
13
|
-
if (clause.namedBindings) {
|
|
14
|
-
if (ts.isNamespaceImport(clause.namedBindings)) {
|
|
15
|
-
namespaceName = clause.namedBindings.name.text;
|
|
16
|
-
}
|
|
17
|
-
else if (ts.isNamedImports(clause.namedBindings)) {
|
|
18
|
-
for (const element of clause.namedBindings.elements) {
|
|
19
|
-
const imported = element.propertyName
|
|
20
|
-
? `${element.propertyName.text} as ${element.name.text}`
|
|
21
|
-
: element.name.text;
|
|
22
|
-
named.push(imported);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return { moduleSpecifier, named, defaultName, namespaceName, isTypeOnly };
|
|
27
|
-
}
|
|
28
|
-
function formatImport(entry) {
|
|
29
|
-
const prefix = entry.isTypeOnly ? 'import type ' : 'import ';
|
|
30
|
-
const parts = [];
|
|
31
|
-
if (entry.defaultName)
|
|
32
|
-
parts.push(entry.defaultName);
|
|
33
|
-
if (entry.namespaceName)
|
|
34
|
-
parts.push(`* as ${entry.namespaceName}`);
|
|
35
|
-
if (entry.named.length)
|
|
36
|
-
parts.push(`{ ${[...new Set(entry.named)].join(', ')} }`);
|
|
37
|
-
if (!parts.length)
|
|
38
|
-
return `${prefix}'${entry.moduleSpecifier}';`;
|
|
39
|
-
return `${prefix}${parts.join(', ')} from '${entry.moduleSpecifier}';`;
|
|
40
|
-
}
|
|
41
|
-
function mergeEditIntoParsed(target, edit) {
|
|
42
|
-
if (edit.defaultName && !target.defaultName)
|
|
43
|
-
target.defaultName = edit.defaultName;
|
|
44
|
-
if (edit.namespaceName && !target.namespaceName)
|
|
45
|
-
target.namespaceName = edit.namespaceName;
|
|
46
|
-
if (edit.named?.length)
|
|
47
|
-
target.named.push(...edit.named);
|
|
48
|
-
}
|
|
49
|
-
function newImportInsertion(text, source, existingImports, lines) {
|
|
50
|
-
const joined = lines.join('\n');
|
|
51
|
-
if (existingImports.length) {
|
|
52
|
-
const last = existingImports[existingImports.length - 1];
|
|
53
|
-
return { start: last.getEnd(), text: `\n${joined}` };
|
|
54
|
-
}
|
|
55
|
-
let lastDirective;
|
|
56
|
-
for (const stmt of source.statements) {
|
|
57
|
-
if (!ts.isExpressionStatement(stmt) || !ts.isStringLiteralLike(stmt.expression))
|
|
58
|
-
break;
|
|
59
|
-
lastDirective = stmt;
|
|
60
|
-
}
|
|
61
|
-
if (lastDirective) {
|
|
62
|
-
return { start: lastDirective.getEnd(), text: `\n${joined}` };
|
|
63
|
-
}
|
|
64
|
-
const shebang = /^#![^\n]*\r?\n/.exec(text);
|
|
65
|
-
if (shebang) {
|
|
66
|
-
return { start: shebang[0].length, text: `${joined}\n` };
|
|
67
|
-
}
|
|
68
|
-
return { start: 0, text: `${joined}\n` };
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Merges structured `ImportEdit`s into existing import declarations in `text` (or inserts a single
|
|
72
|
-
* import per module specifier after any leading shebang, directive prologues, or existing imports)
|
|
73
|
-
* without creating duplicate imports.
|
|
74
|
-
*/
|
|
75
|
-
export function applyImportEdits(text, edits, fileName = 'file.ts') {
|
|
76
|
-
if (!edits.length)
|
|
77
|
-
return text;
|
|
78
|
-
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true);
|
|
79
|
-
const existingImports = source.statements.filter(ts.isImportDeclaration);
|
|
80
|
-
const matchedReplacements = new Map();
|
|
81
|
-
const mergedNew = new Map();
|
|
82
|
-
for (const edit of edits) {
|
|
83
|
-
const wantsNamed = Boolean(edit.named?.length);
|
|
84
|
-
const wantsNamespace = Boolean(edit.namespaceName);
|
|
85
|
-
const match = existingImports.find((decl) => {
|
|
86
|
-
if (!ts.isStringLiteral(decl.moduleSpecifier) ||
|
|
87
|
-
decl.moduleSpecifier.text !== edit.moduleSpecifier ||
|
|
88
|
-
Boolean(decl.importClause?.isTypeOnly) !== Boolean(edit.isTypeOnly)) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
const bindings = decl.importClause?.namedBindings;
|
|
92
|
-
const hasNamespace = Boolean(bindings && ts.isNamespaceImport(bindings));
|
|
93
|
-
const hasNamed = Boolean(bindings && ts.isNamedImports(bindings));
|
|
94
|
-
// JS/TS forbids combining `* as ns` and `{ named }` in one import declaration.
|
|
95
|
-
if (wantsNamed && hasNamespace)
|
|
96
|
-
return false;
|
|
97
|
-
if (wantsNamespace && hasNamed)
|
|
98
|
-
return false;
|
|
99
|
-
return true;
|
|
100
|
-
});
|
|
101
|
-
const parsed = match ? (matchedReplacements.get(match) ?? parseImportStatement(match)) : undefined;
|
|
102
|
-
if (match && parsed) {
|
|
103
|
-
mergeEditIntoParsed(parsed, edit);
|
|
104
|
-
matchedReplacements.set(match, parsed);
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
const key = `${edit.isTypeOnly ? 'type:' : ''}${wantsNamespace ? 'ns:' : ''}${edit.moduleSpecifier}`;
|
|
108
|
-
const current = mergedNew.get(key) ?? {
|
|
109
|
-
moduleSpecifier: edit.moduleSpecifier,
|
|
110
|
-
named: [],
|
|
111
|
-
isTypeOnly: Boolean(edit.isTypeOnly),
|
|
112
|
-
};
|
|
113
|
-
mergeEditIntoParsed(current, edit);
|
|
114
|
-
mergedNew.set(key, current);
|
|
115
|
-
}
|
|
116
|
-
const textEdits = [...matchedReplacements.entries()].map(([decl, parsed]) => ({
|
|
117
|
-
start: decl.getStart(source),
|
|
118
|
-
end: decl.getEnd(),
|
|
119
|
-
text: formatImport(parsed),
|
|
120
|
-
}));
|
|
121
|
-
if (mergedNew.size) {
|
|
122
|
-
const insertion = newImportInsertion(text, source, existingImports, [...mergedNew.values()].map(formatImport));
|
|
123
|
-
textEdits.push({ start: insertion.start, end: insertion.start, text: insertion.text });
|
|
124
|
-
}
|
|
125
|
-
let result = text;
|
|
126
|
-
for (const rep of textEdits.sort((a, b) => b.start - a.start)) {
|
|
127
|
-
result = result.slice(0, rep.start) + rep.text + result.slice(rep.end);
|
|
128
|
-
}
|
|
129
|
-
return result;
|
|
130
|
-
}
|
|
131
|
-
function parsePureImportBlock(text) {
|
|
132
|
-
const trimmed = text.trim();
|
|
133
|
-
if (!trimmed)
|
|
134
|
-
return undefined;
|
|
135
|
-
const source = ts.createSourceFile('import-edit.ts', trimmed, ts.ScriptTarget.Latest, true);
|
|
136
|
-
if (source.parseDiagnostics?.length || !source.statements.length)
|
|
137
|
-
return undefined;
|
|
138
|
-
const edits = [];
|
|
139
|
-
for (const statement of source.statements) {
|
|
140
|
-
if (!ts.isImportDeclaration(statement))
|
|
141
|
-
return undefined;
|
|
142
|
-
const parsed = parseImportStatement(statement);
|
|
143
|
-
if (!parsed)
|
|
144
|
-
return undefined;
|
|
145
|
-
edits.push(parsed);
|
|
146
|
-
}
|
|
147
|
-
return edits;
|
|
148
|
-
}
|
|
2
|
+
import { hunksFor, lineStartsOf } from './fix-hunks.js';
|
|
3
|
+
import { importTextEdits, parsePureImportBlock } from './import-text-edits.js';
|
|
149
4
|
function importBindingKeys(edits) {
|
|
150
5
|
const keys = new Set();
|
|
151
6
|
for (const edit of edits) {
|
|
@@ -212,21 +67,8 @@ export function coalesceImportChanges(changes, readText) {
|
|
|
212
67
|
continue;
|
|
213
68
|
}
|
|
214
69
|
}
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
continue;
|
|
218
|
-
const existingLines = existingText.replaceAll('\r\n', '\n').split('\n');
|
|
219
|
-
const updatedLines = updated.replaceAll('\r\n', '\n').split('\n');
|
|
220
|
-
// Compute minimal line-1 prefix/suffix diff for applyFix.
|
|
221
|
-
let suffix = 0;
|
|
222
|
-
while (suffix < existingLines.length &&
|
|
223
|
-
suffix < updatedLines.length &&
|
|
224
|
-
existingLines[existingLines.length - 1 - suffix] === updatedLines[updatedLines.length - 1 - suffix]) {
|
|
225
|
-
suffix++;
|
|
226
|
-
}
|
|
227
|
-
const beforeSlice = existingLines.slice(0, existingLines.length - suffix).join('\n');
|
|
228
|
-
const afterSlice = updatedLines.slice(0, updatedLines.length - suffix).join('\n');
|
|
229
|
-
coalesced.push({ file, line: 1, before: beforeSlice, after: afterSlice });
|
|
70
|
+
const textEdits = importTextEdits(existingText, edits, file);
|
|
71
|
+
coalesced.push(...hunksFor({ filename: file, text: existingText, lineStarts: lineStartsOf(existingText) }, textEdits, 0));
|
|
230
72
|
}
|
|
231
73
|
return coalesced;
|
|
232
74
|
}
|
|
@@ -183,7 +183,9 @@ function createWhyCommentTools({ checkerFor, loadTs, }) {
|
|
|
183
183
|
return { edits };
|
|
184
184
|
}
|
|
185
185
|
function rememberCommentRow(fix, file, row) {
|
|
186
|
-
commentAnchors.
|
|
186
|
+
const anchors = commentAnchors.get(fix) ?? [];
|
|
187
|
+
anchors.push({ file, row });
|
|
188
|
+
commentAnchors.set(fix, anchors);
|
|
187
189
|
}
|
|
188
190
|
function withoutWhyComment(fix, ruleId) {
|
|
189
191
|
const comment = fix?.comment ?? whyComment(ruleId);
|
|
@@ -225,21 +227,33 @@ function createWhyCommentTools({ checkerFor, loadTs, }) {
|
|
|
225
227
|
const commentedChanges = new Map();
|
|
226
228
|
for (const finding of findings) {
|
|
227
229
|
const { fix } = finding;
|
|
228
|
-
const
|
|
229
|
-
if (!fix || !
|
|
230
|
+
const anchors = fix && commentAnchors.get(fix);
|
|
231
|
+
if (!fix || !anchors?.length)
|
|
230
232
|
continue;
|
|
231
233
|
const comment = fix.comment ?? whyComment(finding.ruleId);
|
|
232
234
|
const withComment = fix.changes
|
|
233
235
|
.filter((change) => withoutComment(change.after, comment) !== change.after)
|
|
234
236
|
.map((change) => JSON.stringify(change));
|
|
235
237
|
const own = new Set(withoutWhyComment(fix, finding.ruleId).changes.map((change) => JSON.stringify(change)));
|
|
236
|
-
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
238
|
+
let conflict = false;
|
|
239
|
+
for (const anchor of anchors) {
|
|
240
|
+
const overlapsOther = (changedRows.get(anchor.file) ?? []).some((range) => range.finding !== finding && !own.has(range.key) && range.start <= anchor.row && anchor.row < range.end);
|
|
241
|
+
const anchorKey = anchor.file + '\0' + anchor.row;
|
|
242
|
+
const earlier = commentedChanges.get(anchorKey);
|
|
243
|
+
if (overlapsOther || (earlier && !withComment.every((change) => earlier.has(change)))) {
|
|
244
|
+
conflict = true;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (conflict) {
|
|
240
249
|
finding.fix = withoutWhyComment(fix, finding.ruleId);
|
|
241
|
-
|
|
242
|
-
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
for (const anchor of anchors) {
|
|
253
|
+
const anchorKey = anchor.file + '\0' + anchor.row;
|
|
254
|
+
if (!commentedChanges.has(anchorKey))
|
|
255
|
+
commentedChanges.set(anchorKey, new Set(withComment));
|
|
256
|
+
}
|
|
243
257
|
}
|
|
244
258
|
}
|
|
245
259
|
return {
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import {
|
|
3
|
+
import { lineHunks, splitRows } from './fix-presentation-rows.js';
|
|
4
4
|
const missingPrettierNote = 'Install prettier in the project to format applied fixes.';
|
|
5
5
|
const scriptFile = /\.[cm]?[jt]sx?$/i;
|
|
6
6
|
// Generous: a run formats every changed range of every touched file in one process.
|
|
7
7
|
const formatTimeoutMs = 120_000;
|
|
8
8
|
const formatOutputLimit = 1 << 30;
|
|
9
|
-
// Comparing larger reformatted regions line by line costs more than formatting is worth.
|
|
10
|
-
const maxDiffCells = 4_000_000;
|
|
11
9
|
// Formats row ranges `[start, end)` of each file with the Prettier its folder resolves. Returns a
|
|
12
10
|
// map from file to the text after each range, bottom range first, or undefined when no file could
|
|
13
11
|
// load Prettier.
|
|
@@ -43,49 +41,6 @@ function rangeOffsets(text, [startRow, endRow]) {
|
|
|
43
41
|
const rangeEnd = endRow < starts.length ? starts[endRow] - 1 : text.length;
|
|
44
42
|
return { rangeStart: starts[Math.min(startRow, starts.length - 1)], rangeEnd };
|
|
45
43
|
}
|
|
46
|
-
// Line hunks `{ start, end, rows }` that turn `before` rows into `after` rows, or undefined when
|
|
47
|
-
// the changed region is too large to compare.
|
|
48
|
-
function lineHunks(before, after) {
|
|
49
|
-
const { first, beforeEnd, afterEnd } = changedRows(before, after);
|
|
50
|
-
const left = before.slice(first, beforeEnd);
|
|
51
|
-
const right = after.slice(first, afterEnd);
|
|
52
|
-
if ((left.length + 1) * (right.length + 1) > maxDiffCells)
|
|
53
|
-
return;
|
|
54
|
-
// Longest common subsequence lengths of the suffixes left[i..] and right[j..].
|
|
55
|
-
const width = right.length + 1;
|
|
56
|
-
const common = new Uint32Array((left.length + 1) * width);
|
|
57
|
-
for (let i = left.length - 1; i >= 0; i--) {
|
|
58
|
-
for (let j = right.length - 1; j >= 0; j--) {
|
|
59
|
-
common[i * width + j] =
|
|
60
|
-
left[i] === right[j]
|
|
61
|
-
? common[(i + 1) * width + j + 1] + 1
|
|
62
|
-
: Math.max(common[(i + 1) * width + j], common[i * width + j + 1]);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
const hunks = [];
|
|
66
|
-
let open;
|
|
67
|
-
let i = 0;
|
|
68
|
-
let j = 0;
|
|
69
|
-
while (i < left.length || j < right.length) {
|
|
70
|
-
if (i < left.length && j < right.length && left[i] === right[j]) {
|
|
71
|
-
if (open)
|
|
72
|
-
hunks.push(open);
|
|
73
|
-
open = undefined;
|
|
74
|
-
i++;
|
|
75
|
-
j++;
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
open ??= { start: first + i, end: first + i, rows: [] };
|
|
79
|
-
if (j < right.length && (i === left.length || common[i * width + j + 1] >= common[(i + 1) * width + j])) {
|
|
80
|
-
open.rows.push(right[j++]);
|
|
81
|
-
}
|
|
82
|
-
else
|
|
83
|
-
open.end = first + ++i;
|
|
84
|
-
}
|
|
85
|
-
if (open)
|
|
86
|
-
hunks.push(open);
|
|
87
|
-
return hunks;
|
|
88
|
-
}
|
|
89
44
|
function parseErrors(loadTs, file, source) {
|
|
90
45
|
const ts = loadTs();
|
|
91
46
|
return ts.createSourceFile(file, source, ts.ScriptTarget.Latest).parseDiagnostics?.length ?? 0;
|
|
@@ -30,6 +30,50 @@ function changedRows(beforeRows, afterRows) {
|
|
|
30
30
|
same++;
|
|
31
31
|
return { first, beforeEnd: beforeRows.length - same, afterEnd: afterRows.length - same };
|
|
32
32
|
}
|
|
33
|
+
const maxDiffCells = 4_000_000;
|
|
34
|
+
// Line hunks `{ start, end, rows }` that turn `before` rows into `after` rows, or undefined when
|
|
35
|
+
// the changed region is too large to compare.
|
|
36
|
+
function lineHunks(before, after) {
|
|
37
|
+
const { first, beforeEnd, afterEnd } = changedRows(before, after);
|
|
38
|
+
const left = before.slice(first, beforeEnd);
|
|
39
|
+
const right = after.slice(first, afterEnd);
|
|
40
|
+
if ((left.length + 1) * (right.length + 1) > maxDiffCells)
|
|
41
|
+
return;
|
|
42
|
+
// Longest common subsequence lengths of the suffixes left[i..] and right[j..].
|
|
43
|
+
const width = right.length + 1;
|
|
44
|
+
const common = new Uint32Array((left.length + 1) * width);
|
|
45
|
+
for (let i = left.length - 1; i >= 0; i--) {
|
|
46
|
+
for (let j = right.length - 1; j >= 0; j--) {
|
|
47
|
+
common[i * width + j] =
|
|
48
|
+
left[i] === right[j]
|
|
49
|
+
? common[(i + 1) * width + j + 1] + 1
|
|
50
|
+
: Math.max(common[(i + 1) * width + j], common[i * width + j + 1]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const hunks = [];
|
|
54
|
+
let open;
|
|
55
|
+
let i = 0;
|
|
56
|
+
let j = 0;
|
|
57
|
+
while (i < left.length || j < right.length) {
|
|
58
|
+
if (i < left.length && j < right.length && left[i] === right[j]) {
|
|
59
|
+
if (open)
|
|
60
|
+
hunks.push(open);
|
|
61
|
+
open = undefined;
|
|
62
|
+
i++;
|
|
63
|
+
j++;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
open ??= { start: first + i, end: first + i, rows: [] };
|
|
67
|
+
if (j < right.length && (i === left.length || common[i * width + j + 1] >= common[(i + 1) * width + j])) {
|
|
68
|
+
open.rows.push(right[j++]);
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
open.end = first + ++i;
|
|
72
|
+
}
|
|
73
|
+
if (open)
|
|
74
|
+
hunks.push(open);
|
|
75
|
+
return hunks;
|
|
76
|
+
}
|
|
33
77
|
// Tracks which rows of a file each applied change owns while Prettier rewrites them. A segment is a
|
|
34
78
|
// run of rows: unchanged original rows, or rows written by changes (`owners`) that replaced
|
|
35
79
|
// `originalLength` original rows.
|
|
@@ -154,4 +198,4 @@ function styleRegions(segments, beforeRowsOf) {
|
|
|
154
198
|
}
|
|
155
199
|
return regions;
|
|
156
200
|
}
|
|
157
|
-
export { bottomFirst, changedRows, followSteps, ownedHunks, rangesOf, replaceRows, segmentsOf, splitRows, styleRegions, };
|
|
201
|
+
export { lineHunks, bottomFirst, changedRows, followSteps, ownedHunks, rangesOf, replaceRows, segmentsOf, splitRows, styleRegions, };
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
// How an applied fix reads in the project: a one-line comment above the fixed statement says why it changed, and the
|
|
2
2
|
// project's own Prettier reformats only the changed lines. Prettier's API is asynchronous while the fix paths are
|
|
3
3
|
// not, so formatting runs once per fix run in a child process started from this file.
|
|
4
|
-
import
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
5
5
|
import { createRequire } from 'node:module';
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import * as ts from 'typescript';
|
|
8
|
+
import { packagePath } from '../package-root.js';
|
|
8
9
|
import { pillarOfRule, rulesByPillar } from '../rule-registry.js';
|
|
10
|
+
import { readFixText } from './read-fix-text.js';
|
|
9
11
|
import { writeFileAtomic } from '../system/atomic-write.js';
|
|
10
12
|
import { createWhyCommentTools, formatComment, withoutComment } from './fix-presentation-comments.js';
|
|
11
13
|
import { formatInChild, formatRanges, missingPrettierNote, scriptFile } from './fix-presentation-prettier.js';
|
|
12
14
|
import { bottomFirst, followSteps, ownedHunks, rangesOf, replaceRows, segmentsOf, splitRows, styleRegions, } from './fix-presentation-rows.js';
|
|
13
15
|
import { matchFileStyle } from './fix-presentation-style.js';
|
|
16
|
+
const presentationScript = packagePath('dist/lib/shared/fix/fix-presentation.js');
|
|
14
17
|
function checkerFor(ruleId) {
|
|
15
18
|
const pillar = pillarOfRule(ruleId);
|
|
16
19
|
return pillar && rulesByPillar[pillar];
|
|
@@ -64,7 +67,7 @@ function formatUpdates(root, updates) {
|
|
|
64
67
|
ranges: ranges.map(({ start, end }) => [start, end]),
|
|
65
68
|
});
|
|
66
69
|
}
|
|
67
|
-
const stepsByFile = formatRanges(
|
|
70
|
+
const stepsByFile = formatRanges(presentationScript, root, files);
|
|
68
71
|
const texts = new Map();
|
|
69
72
|
const hunks = new Map();
|
|
70
73
|
for (const update of updates) {
|
|
@@ -147,10 +150,10 @@ function createWriteTracker() {
|
|
|
147
150
|
const files = [...segmentsByFile].map(([file, segments]) => ({
|
|
148
151
|
file,
|
|
149
152
|
segments,
|
|
150
|
-
text:
|
|
153
|
+
text: readFixText(file),
|
|
151
154
|
ranges: bottomFirst(rangesOf(segments)).map(({ start, end }) => [start, end]),
|
|
152
155
|
}));
|
|
153
|
-
const stepsByFile = formatRanges(
|
|
156
|
+
const stepsByFile = formatRanges(presentationScript, root, files.map(({ file, text, ranges }) => ({ file, text, ranges })));
|
|
154
157
|
for (const { file, segments, text } of files) {
|
|
155
158
|
const steps = stepsByFile?.get(file);
|
|
156
159
|
const beforeRowsOf = (segment) => [...(segment.owners ?? [])].flatMap((owner) => beforeRowsByOwner.get(owner) ?? []);
|
|
@@ -159,7 +162,7 @@ function createWriteTracker() {
|
|
|
159
162
|
: matchFileStyle(loadTs, file, text, styleRegions(segments, beforeRowsOf));
|
|
160
163
|
if (formattedText === text)
|
|
161
164
|
continue;
|
|
162
|
-
if (
|
|
165
|
+
if (readFixText(file) !== text)
|
|
163
166
|
continue;
|
|
164
167
|
writeFileAtomic(file, formattedText);
|
|
165
168
|
const formatted = ownedHunks(steps?.length ? followSteps(segments, text, steps) : segments, [], splitRows(formattedText));
|
|
@@ -170,7 +173,7 @@ function createWriteTracker() {
|
|
|
170
173
|
},
|
|
171
174
|
};
|
|
172
175
|
}
|
|
173
|
-
if (process.argv[1] && path.resolve(process.argv[1]) ===
|
|
176
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === presentationScript) {
|
|
174
177
|
const input = JSON.parse(readFileSync(0, 'utf8'));
|
|
175
178
|
formatInChild(input, { projectPrettier, loadTs }).then((output) => process.stdout.write(JSON.stringify(output)));
|
|
176
179
|
}
|
|
@@ -31,9 +31,40 @@ function introducedSyntaxError(file, before, after) {
|
|
|
31
31
|
remaining.set(key, (remaining.get(key) ?? 0) - 1);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
+
function validateSource(file, text) {
|
|
35
|
+
if (!javaScriptFile.test(file))
|
|
36
|
+
return;
|
|
37
|
+
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true);
|
|
38
|
+
ts.bindSourceFile(source, {});
|
|
39
|
+
const [diagnostic] = source.bindDiagnostics ?? [];
|
|
40
|
+
if (diagnostic)
|
|
41
|
+
throw new Error(`${file}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, ' ')} No fixes applied.`);
|
|
42
|
+
const hasBody = (declaration) => (ts.isFunctionDeclaration(declaration) ||
|
|
43
|
+
ts.isMethodDeclaration(declaration) ||
|
|
44
|
+
ts.isConstructorDeclaration(declaration)) &&
|
|
45
|
+
Boolean(declaration.body);
|
|
46
|
+
function duplicateImplementation(node) {
|
|
47
|
+
if ((ts.isFunctionDeclaration(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node)) &&
|
|
48
|
+
node.body &&
|
|
49
|
+
(node.symbol?.declarations?.filter((declaration) => declaration.kind === node.kind && hasBody(declaration))
|
|
50
|
+
.length ?? 0) > 1) {
|
|
51
|
+
return node.name?.getText(source) ?? 'constructor';
|
|
52
|
+
}
|
|
53
|
+
return ts.forEachChild(node, duplicateImplementation);
|
|
54
|
+
}
|
|
55
|
+
const duplicate = duplicateImplementation(source);
|
|
56
|
+
if (duplicate)
|
|
57
|
+
throw new Error(`${file}: duplicate implementation of ${duplicate}. No fixes applied.`);
|
|
58
|
+
}
|
|
34
59
|
function refusalReason(file, syntaxError) {
|
|
35
60
|
return `${file}: the fix would introduce a syntax error (${syntaxError}), so the file was not changed`;
|
|
36
61
|
}
|
|
62
|
+
function explainPassLimit(deferred, passLimit) {
|
|
63
|
+
return deferred.map((refused) => ({
|
|
64
|
+
...refused,
|
|
65
|
+
reason: `${refused.reason} (stopped after reaching the ${passLimit}-pass fix convergence limit; rerun fix to continue)`,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
37
68
|
// Runs `applyFix` but holds its writes back until every file has passed the syntax check, so a
|
|
38
69
|
// refused fix writes nothing. `readFile`/`writeFile` are the accessors the checker's applyFix
|
|
39
70
|
// takes.
|
|
@@ -61,4 +92,4 @@ function applyFixSafely(applyFix, fix, { readFile, writeFile }, ...options) {
|
|
|
61
92
|
writeFile(file, text);
|
|
62
93
|
return result;
|
|
63
94
|
}
|
|
64
|
-
export { applyFixSafely, introducedSyntaxError, refusalReason };
|
|
95
|
+
export { applyFixSafely, explainPassLimit, introducedSyntaxError, refusalReason, validateSource };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { collectChangedLines, fileKey } from '../../performance/cli/changed-lines.js';
|
|
4
|
+
import { discoverWorkspaces, isWithin, selectWorkspaces } from '../workspaces.js';
|
|
5
|
+
import { realTarget } from '../system/real-target.js';
|
|
6
|
+
import { hunkRange } from './fix-convergence.js';
|
|
7
|
+
function scopedWorkspaceRoots({ directory, workspace }) {
|
|
8
|
+
if (!workspace.length)
|
|
9
|
+
return;
|
|
10
|
+
const { packages, withoutConfig } = discoverWorkspaces(directory);
|
|
11
|
+
return selectWorkspaces([...packages, ...withoutConfig], directory, workspace).map((entry) => realTarget(entry.directory));
|
|
12
|
+
}
|
|
13
|
+
// Every contiguous block of untouched original lines must survive in order. Anchor the first
|
|
14
|
+
// and last blocks to their file boundaries. This also handles edits far apart in a large file.
|
|
15
|
+
function preservesOutsideRanges(before, after, ranges) {
|
|
16
|
+
const rows = before.split('\n');
|
|
17
|
+
const output = '\n' + after + '\n';
|
|
18
|
+
let originalRow = 0;
|
|
19
|
+
let outputOffset = 0;
|
|
20
|
+
for (const [start, end] of ranges) {
|
|
21
|
+
const nextRow = Math.min(start - 1, rows.length);
|
|
22
|
+
if (originalRow < nextRow) {
|
|
23
|
+
const block = '\n' + rows.slice(originalRow, nextRow).join('\n') + '\n';
|
|
24
|
+
const found = output.indexOf(block, outputOffset);
|
|
25
|
+
if (found < 0 || (originalRow === 0 && found !== 0))
|
|
26
|
+
return false;
|
|
27
|
+
outputOffset = found + block.length - 1;
|
|
28
|
+
}
|
|
29
|
+
originalRow = Math.max(originalRow, end);
|
|
30
|
+
}
|
|
31
|
+
if (originalRow >= rows.length)
|
|
32
|
+
return true;
|
|
33
|
+
const suffix = '\n' + rows.slice(originalRow).join('\n') + '\n';
|
|
34
|
+
return output.endsWith(suffix) && output.length - suffix.length >= outputOffset;
|
|
35
|
+
}
|
|
36
|
+
export function fixScopeGuard(scope) {
|
|
37
|
+
const roots = scopedWorkspaceRoots(scope);
|
|
38
|
+
if (!roots && !scope.staged && scope.ref === undefined)
|
|
39
|
+
return;
|
|
40
|
+
// Git reports paths relative to its working directory; fix targets use physical paths.
|
|
41
|
+
const directory = realTarget(scope.directory);
|
|
42
|
+
function currentRanges() {
|
|
43
|
+
if (!scope.staged && scope.ref === undefined)
|
|
44
|
+
return;
|
|
45
|
+
const changed = collectChangedLines({ staged: scope.staged, ref: scope.ref, allFiles: true }, directory);
|
|
46
|
+
if (scope.staged) {
|
|
47
|
+
// Staged ranges use index coordinates; a partially staged working file can have different lines there.
|
|
48
|
+
const unstaged = execFileSync('git', ['diff', '--relative', '--name-only', '--no-renames', '-z', '--', '.'], {
|
|
49
|
+
cwd: directory,
|
|
50
|
+
encoding: 'utf8',
|
|
51
|
+
});
|
|
52
|
+
for (const name of unstaged.split('\0').filter(Boolean))
|
|
53
|
+
changed.delete(fileKey(path.resolve(directory, name)));
|
|
54
|
+
}
|
|
55
|
+
return changed;
|
|
56
|
+
}
|
|
57
|
+
const ranges = currentRanges();
|
|
58
|
+
function inWorkspace(file) {
|
|
59
|
+
return !roots || roots.some((root) => isWithin(root, file));
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
allowsChange(change) {
|
|
63
|
+
const file = realTarget(path.resolve(directory, change.file));
|
|
64
|
+
if (!inWorkspace(file))
|
|
65
|
+
return false;
|
|
66
|
+
if (!ranges)
|
|
67
|
+
return true;
|
|
68
|
+
const [first, last] = hunkRange(change);
|
|
69
|
+
return (!change.create && Boolean(ranges.get(fileKey(file))?.some(([start, end]) => start <= first && last <= end)));
|
|
70
|
+
},
|
|
71
|
+
validateUpdates(updates) {
|
|
72
|
+
if (!updates.length || !ranges)
|
|
73
|
+
return;
|
|
74
|
+
const latest = currentRanges();
|
|
75
|
+
for (const { file, before, after } of updates) {
|
|
76
|
+
const permitted = latest?.get(fileKey(realTarget(file)));
|
|
77
|
+
if (!permitted?.length || before === undefined || !preservesOutsideRanges(before, after, permitted))
|
|
78
|
+
throw new Error(`Final edits to ${file} extend outside the verified changed lines. No fixes applied.`);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { changedRows, lineHunks } from './fix-presentation-rows.js';
|
|
1
2
|
import * as readline from 'node:readline/promises';
|
|
2
3
|
async function askChoice(prompt) {
|
|
3
4
|
const reader = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
@@ -62,4 +63,30 @@ function labelledLines(label, text, indentation = ' ') {
|
|
|
62
63
|
const hanging = ' '.repeat(head.length);
|
|
63
64
|
return wrapWords(text, terminalNoteWidth - head.length).map((line, index) => (index ? hanging : head) + line);
|
|
64
65
|
}
|
|
65
|
-
|
|
66
|
+
// Keep line terminators while comparing so the preview also shows a missing final newline.
|
|
67
|
+
function* previewDiff(before, after) {
|
|
68
|
+
const lines = (text) => text.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
69
|
+
const removed = lines(before);
|
|
70
|
+
const added = lines(after);
|
|
71
|
+
let hunks = lineHunks(removed, added);
|
|
72
|
+
if (!hunks) {
|
|
73
|
+
const { first, beforeEnd, afterEnd } = changedRows(removed, added);
|
|
74
|
+
hunks = [{ start: first, end: beforeEnd, rows: added.slice(first, afterEnd) }];
|
|
75
|
+
}
|
|
76
|
+
let offset = 0;
|
|
77
|
+
for (const { start, end, rows } of hunks) {
|
|
78
|
+
const count = end - start;
|
|
79
|
+
yield `@@ -${start + Number(count > 0)},${count} +${start + offset + Number(rows.length > 0)},${rows.length} @@`;
|
|
80
|
+
yield* patchLines('-', removed.slice(start, end));
|
|
81
|
+
yield* patchLines('+', rows);
|
|
82
|
+
offset += rows.length - count;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function* patchLines(marker, lines) {
|
|
86
|
+
for (const line of lines) {
|
|
87
|
+
yield marker + line.replace(/\r?\n$/, '');
|
|
88
|
+
if (!line.endsWith('\n'))
|
|
89
|
+
yield '\';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export { previewDiff, askChoice, diffLines, labelledLines, wrapWords };
|
|
@@ -8,13 +8,16 @@ function findTsConfig(directory) {
|
|
|
8
8
|
let current = path.resolve(directory);
|
|
9
9
|
for (;;) {
|
|
10
10
|
if (isTempRoot(current))
|
|
11
|
-
return
|
|
11
|
+
return { hitGitRoot: false };
|
|
12
12
|
const candidate = path.join(current, 'tsconfig.json');
|
|
13
13
|
if (safeExists(candidate))
|
|
14
|
-
return candidate;
|
|
14
|
+
return { path: candidate, hitGitRoot: false };
|
|
15
|
+
// Stop at the enclosing Git repository root so an unrelated parent tsconfig never claims an independent repo.
|
|
16
|
+
if (safeExists(path.join(current, '.git')))
|
|
17
|
+
return { hitGitRoot: true };
|
|
15
18
|
const parent = path.dirname(current);
|
|
16
19
|
if (parent === current)
|
|
17
|
-
return
|
|
20
|
+
return { hitGitRoot: false };
|
|
18
21
|
current = parent;
|
|
19
22
|
}
|
|
20
23
|
}
|
|
@@ -160,7 +163,8 @@ export function introducedTypeErrors(directory, originals, updates, describeRefu
|
|
|
160
163
|
return refusals;
|
|
161
164
|
const byTsConfig = new Map();
|
|
162
165
|
for (const file of tsFiles) {
|
|
163
|
-
const
|
|
166
|
+
const fileConfig = findTsConfig(path.dirname(file));
|
|
167
|
+
const tsconfigPath = fileConfig.path ?? (fileConfig.hitGitRoot ? undefined : findTsConfig(directory).path);
|
|
164
168
|
if (!tsconfigPath)
|
|
165
169
|
continue;
|
|
166
170
|
const list = byTsConfig.get(tsconfigPath) ?? [];
|