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
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
function parseImportStatement(statement) {
|
|
3
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier))
|
|
4
|
+
return;
|
|
5
|
+
const moduleSpecifier = statement.moduleSpecifier.text;
|
|
6
|
+
const clause = statement.importClause;
|
|
7
|
+
// Leave declarations with attributes, comments, or a nonstandard phase intact. Only declarations represented losslessly
|
|
8
|
+
// below may be formatted again.
|
|
9
|
+
if (!clause || statement.attributes || statement.modifiers?.length || /\/[/*]/.test(statement.getText()))
|
|
10
|
+
return;
|
|
11
|
+
if (clause.phaseModifier !== undefined && clause.phaseModifier !== ts.SyntaxKind.TypeKeyword)
|
|
12
|
+
return;
|
|
13
|
+
const isTypeOnly = Boolean(clause.isTypeOnly);
|
|
14
|
+
const defaultName = clause.name?.text;
|
|
15
|
+
const named = [];
|
|
16
|
+
let namespaceName;
|
|
17
|
+
if (clause.namedBindings) {
|
|
18
|
+
if (ts.isNamespaceImport(clause.namedBindings)) {
|
|
19
|
+
namespaceName = clause.namedBindings.name.text;
|
|
20
|
+
}
|
|
21
|
+
else if (ts.isNamedImports(clause.namedBindings)) {
|
|
22
|
+
for (const element of clause.namedBindings.elements) {
|
|
23
|
+
const imported = element.propertyName
|
|
24
|
+
? `${element.propertyName.getText()} as ${element.name.text}`
|
|
25
|
+
: element.name.text;
|
|
26
|
+
named.push((element.isTypeOnly ? 'type ' : '') + imported);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { moduleSpecifier, named, defaultName, namespaceName, isTypeOnly };
|
|
31
|
+
}
|
|
32
|
+
function formatImport(entry) {
|
|
33
|
+
const prefix = entry.isTypeOnly ? 'import type ' : 'import ';
|
|
34
|
+
const literal = /^[\w@./#:-]+$/.test(entry.moduleSpecifier)
|
|
35
|
+
? `'${entry.moduleSpecifier}'`
|
|
36
|
+
: JSON.stringify(entry.moduleSpecifier);
|
|
37
|
+
const parts = [];
|
|
38
|
+
if (entry.defaultName)
|
|
39
|
+
parts.push(entry.defaultName);
|
|
40
|
+
if (entry.namespaceName)
|
|
41
|
+
parts.push(`* as ${entry.namespaceName}`);
|
|
42
|
+
if (entry.named.length)
|
|
43
|
+
parts.push(`{ ${[...new Set(entry.named)].join(', ')} }`);
|
|
44
|
+
if (!parts.length)
|
|
45
|
+
return `${prefix}${literal};`;
|
|
46
|
+
return `${prefix}${parts.join(', ')} from ${literal};`;
|
|
47
|
+
}
|
|
48
|
+
function acceptsImportEdit(target, edit) {
|
|
49
|
+
if (target.moduleSpecifier !== edit.moduleSpecifier || target.isTypeOnly !== Boolean(edit.isTypeOnly))
|
|
50
|
+
return false;
|
|
51
|
+
if (target.defaultName && edit.defaultName && target.defaultName !== edit.defaultName)
|
|
52
|
+
return false;
|
|
53
|
+
if (target.namespaceName && edit.namespaceName && target.namespaceName !== edit.namespaceName)
|
|
54
|
+
return false;
|
|
55
|
+
const hasNamed = target.named.length || edit.named?.length;
|
|
56
|
+
const hasNamespace = target.namespaceName || edit.namespaceName;
|
|
57
|
+
if (hasNamed && hasNamespace)
|
|
58
|
+
return false;
|
|
59
|
+
return !(target.isTypeOnly && (target.defaultName || edit.defaultName) && (hasNamed || hasNamespace));
|
|
60
|
+
}
|
|
61
|
+
function mergeEditIntoParsed(target, edit) {
|
|
62
|
+
if (edit.defaultName && !target.defaultName)
|
|
63
|
+
target.defaultName = edit.defaultName;
|
|
64
|
+
if (edit.namespaceName && !target.namespaceName)
|
|
65
|
+
target.namespaceName = edit.namespaceName;
|
|
66
|
+
if (edit.named?.length)
|
|
67
|
+
target.named.push(...edit.named);
|
|
68
|
+
}
|
|
69
|
+
function newImportInsertion(text, source, existingImports, lines) {
|
|
70
|
+
const joined = lines.join('\n');
|
|
71
|
+
if (existingImports.length) {
|
|
72
|
+
const last = existingImports[existingImports.length - 1];
|
|
73
|
+
return { start: last.getEnd(), text: `\n${joined}` };
|
|
74
|
+
}
|
|
75
|
+
let lastDirective;
|
|
76
|
+
for (const stmt of source.statements) {
|
|
77
|
+
if (!ts.isExpressionStatement(stmt) || !ts.isStringLiteralLike(stmt.expression))
|
|
78
|
+
break;
|
|
79
|
+
lastDirective = stmt;
|
|
80
|
+
}
|
|
81
|
+
if (lastDirective) {
|
|
82
|
+
return { start: lastDirective.getEnd(), text: `\n${joined}` };
|
|
83
|
+
}
|
|
84
|
+
const shebang = /^#![^\n]*\r?\n/.exec(text);
|
|
85
|
+
if (shebang) {
|
|
86
|
+
return { start: shebang[0].length, text: `${joined}\n` };
|
|
87
|
+
}
|
|
88
|
+
return { start: 0, text: `${joined}\n` };
|
|
89
|
+
}
|
|
90
|
+
export function importTextEdits(text, edits, fileName) {
|
|
91
|
+
if (!edits.length)
|
|
92
|
+
return [];
|
|
93
|
+
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true);
|
|
94
|
+
const existingImports = source.statements.filter(ts.isImportDeclaration);
|
|
95
|
+
const matchedReplacements = new Map();
|
|
96
|
+
const parsedImports = existingImports.map((declaration) => ({
|
|
97
|
+
declaration,
|
|
98
|
+
parsed: parseImportStatement(declaration),
|
|
99
|
+
}));
|
|
100
|
+
const newImports = [];
|
|
101
|
+
for (const edit of edits) {
|
|
102
|
+
const match = parsedImports.find(({ parsed }) => parsed && acceptsImportEdit(parsed, edit));
|
|
103
|
+
if (match?.parsed) {
|
|
104
|
+
mergeEditIntoParsed(match.parsed, edit);
|
|
105
|
+
matchedReplacements.set(match.declaration, match.parsed);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
// Import attributes affect module loading. A plain requested import cannot safely inherit or omit them.
|
|
109
|
+
if (existingImports.some((declaration) => declaration.attributes &&
|
|
110
|
+
ts.isStringLiteral(declaration.moduleSpecifier) &&
|
|
111
|
+
declaration.moduleSpecifier.text === edit.moduleSpecifier))
|
|
112
|
+
throw new Error(`Cannot combine a generated import with import attributes for ${edit.moduleSpecifier}. No fixes applied.`);
|
|
113
|
+
let current = newImports.find((entry) => acceptsImportEdit(entry, edit));
|
|
114
|
+
if (!current) {
|
|
115
|
+
current = { moduleSpecifier: edit.moduleSpecifier, named: [], isTypeOnly: Boolean(edit.isTypeOnly) };
|
|
116
|
+
newImports.push(current);
|
|
117
|
+
}
|
|
118
|
+
mergeEditIntoParsed(current, edit);
|
|
119
|
+
}
|
|
120
|
+
const textEdits = [...matchedReplacements.entries()].map(([decl, parsed]) => ({
|
|
121
|
+
start: decl.getStart(source),
|
|
122
|
+
end: decl.getEnd(),
|
|
123
|
+
text: formatImport(parsed),
|
|
124
|
+
}));
|
|
125
|
+
if (newImports.length) {
|
|
126
|
+
const insertion = newImportInsertion(text, source, existingImports, newImports.map(formatImport));
|
|
127
|
+
textEdits.push({ start: insertion.start, end: insertion.start, text: insertion.text });
|
|
128
|
+
}
|
|
129
|
+
return textEdits.filter((edit) => text.slice(edit.start, edit.end) !== edit.text);
|
|
130
|
+
}
|
|
131
|
+
export 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
|
+
}
|
|
@@ -6,6 +6,7 @@ import fs from 'node:fs';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import { renameWithRetry, unlinkWithRetry, writeResolvedFileAtomic } from '../system/atomic-write.js';
|
|
8
8
|
import { errorCode, errorMessage } from '../system/error-code.js';
|
|
9
|
+
import { currentText, readFixText } from './read-fix-text.js';
|
|
9
10
|
import { realTarget } from '../system/real-target.js';
|
|
10
11
|
const byteOrderMark = '\uFEFF';
|
|
11
12
|
// What `link` reports when a filesystem or a system policy does not allow hard links. Windows reports a volume
|
|
@@ -44,7 +45,7 @@ function projectFileAccess(roots, base = roots[0]) {
|
|
|
44
45
|
assertCanFix: resolveSafe,
|
|
45
46
|
readFile(file) {
|
|
46
47
|
const { absolute, real } = resolveSafe(file);
|
|
47
|
-
const text =
|
|
48
|
+
const text = readFixText(real);
|
|
48
49
|
if (!text.startsWith(byteOrderMark))
|
|
49
50
|
return text;
|
|
50
51
|
marked.add(absolute);
|
|
@@ -52,22 +53,13 @@ function projectFileAccess(roots, base = roots[0]) {
|
|
|
52
53
|
},
|
|
53
54
|
writeFile(file, text) {
|
|
54
55
|
const { absolute, real } = resolveSafe(file);
|
|
56
|
+
currentText(real); // Recheck the target before replacing it, including files created since the plan.
|
|
55
57
|
// A fix can create a file, such as a migration, in a folder that does not exist yet.
|
|
56
58
|
fs.mkdirSync(path.dirname(real), { recursive: true });
|
|
57
59
|
writeResolvedFileAtomic(real, (marked.has(absolute) ? byteOrderMark : '') + text);
|
|
58
60
|
},
|
|
59
61
|
};
|
|
60
62
|
}
|
|
61
|
-
function currentText(filename) {
|
|
62
|
-
try {
|
|
63
|
-
return fs.readFileSync(filename, 'utf8');
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
if (errorCode(error) === 'ENOENT')
|
|
67
|
-
return undefined;
|
|
68
|
-
throw error;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
63
|
// A hard link makes the complete staged file appear at once and never replaces an existing one. The exclusive copy
|
|
72
64
|
// used where links are unavailable does not replace one either. Returns whether the file was linked.
|
|
73
65
|
function publishNewFile(temporary, file) {
|
|
@@ -82,16 +74,20 @@ function publishNewFile(temporary, file) {
|
|
|
82
74
|
fs.copyFileSync(temporary, file, fs.constants.COPYFILE_EXCL);
|
|
83
75
|
return false;
|
|
84
76
|
}
|
|
77
|
+
function assertCurrentTarget(file, expected) {
|
|
78
|
+
if (realTarget(file) !== file || currentText(file) !== expected)
|
|
79
|
+
throw new Error('File changed while applying fixes: ' + file);
|
|
80
|
+
}
|
|
85
81
|
function commitStagedUpdates(staged, written) {
|
|
86
82
|
for (const update of staged) {
|
|
87
83
|
const { file, before, temporary } = update;
|
|
88
|
-
if (currentText(file) !== before || realTarget(file) !== file)
|
|
89
|
-
throw new Error('File changed while applying fixes: ' + file);
|
|
90
84
|
let linked = false;
|
|
91
|
-
if (before === undefined)
|
|
85
|
+
if (before === undefined) {
|
|
86
|
+
assertCurrentTarget(file, before);
|
|
92
87
|
linked = publishNewFile(temporary, file);
|
|
88
|
+
}
|
|
93
89
|
else
|
|
94
|
-
renameWithRetry(temporary, file);
|
|
90
|
+
renameWithRetry(temporary, file, () => assertCurrentTarget(file, before));
|
|
95
91
|
written.push(update);
|
|
96
92
|
// Drop the staged name at once: later fixes refuse a file with two names. A failure here rolls the run back.
|
|
97
93
|
if (linked)
|
|
@@ -105,8 +101,7 @@ function writeFixUpdates(updates) {
|
|
|
105
101
|
try {
|
|
106
102
|
for (const update of updates) {
|
|
107
103
|
const { file, before } = update;
|
|
108
|
-
|
|
109
|
-
throw new Error('File changed while applying fixes: ' + file);
|
|
104
|
+
assertCurrentTarget(file, before);
|
|
110
105
|
if (before === undefined)
|
|
111
106
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
112
107
|
else
|
|
@@ -128,15 +123,11 @@ function writeFixUpdates(updates) {
|
|
|
128
123
|
catch (error) {
|
|
129
124
|
const unrestored = [];
|
|
130
125
|
for (const { file, before, after, temporary } of written.reverse()) {
|
|
131
|
-
const
|
|
126
|
+
const assertStillApplied = () => assertCurrentTarget(file, after);
|
|
132
127
|
try {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
unlinkWithRetry(file, () => {
|
|
137
|
-
if (hasChanged())
|
|
138
|
-
throw new Error('File changed during rollback: ' + file);
|
|
139
|
-
});
|
|
128
|
+
assertStillApplied();
|
|
129
|
+
if (before === undefined)
|
|
130
|
+
unlinkWithRetry(file, assertStillApplied);
|
|
140
131
|
else {
|
|
141
132
|
const mode = fs.statSync(file).mode;
|
|
142
133
|
const descriptor = fs.openSync(temporary, 'wx', mode);
|
|
@@ -148,7 +139,7 @@ function writeFixUpdates(updates) {
|
|
|
148
139
|
finally {
|
|
149
140
|
fs.closeSync(descriptor);
|
|
150
141
|
}
|
|
151
|
-
renameWithRetry(temporary, file);
|
|
142
|
+
renameWithRetry(temporary, file, assertStillApplied);
|
|
152
143
|
}
|
|
153
144
|
}
|
|
154
145
|
catch {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { errorCode } from '../system/error-code.js';
|
|
4
|
+
// Validate bytes before decoding: replacement characters inserted by Node cannot be recovered on write.
|
|
5
|
+
export function readFixText(file) {
|
|
6
|
+
const bytes = fs.readFileSync(file);
|
|
7
|
+
if (!isUtf8(bytes))
|
|
8
|
+
throw new Error('Invalid UTF-8 in fix target: ' + file + '. Save the file as UTF-8 before applying fixes.');
|
|
9
|
+
return bytes.toString('utf8');
|
|
10
|
+
}
|
|
11
|
+
export function currentText(file) {
|
|
12
|
+
try {
|
|
13
|
+
return readFixText(file);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (errorCode(error) === 'ENOENT')
|
|
17
|
+
return undefined;
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|