loadwise 0.5.0 → 0.6.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 +543 -628
- package/bin/loadwise.js +5 -1
- package/lib/accessibility/frontend-detect.js +2 -1
- package/lib/accessibility/rule-ids.js +12 -9
- package/lib/accessibility/template-attributes.js +4 -2
- package/{editor/accessibility-examples.cjs → lib/editor/accessibility-examples.js} +20 -21
- package/lib/editor/actions.js +117 -0
- package/{editor/apply-fix.cjs → lib/editor/apply-fix.js} +1 -6
- package/{editor/control-flow-comparison.cjs → lib/editor/control-flow-comparison.js} +1 -3
- package/lib/editor/diagnostics.js +365 -0
- package/lib/editor/dismissals.js +111 -0
- package/lib/editor/editor-document.js +99 -0
- package/{editor/finding-comparison.cjs → lib/editor/finding-comparison.js} +4 -7
- package/lib/editor/fix-edits.js +200 -0
- package/lib/editor/language-server.js +437 -0
- package/{editor/maintainability-examples.cjs → lib/editor/maintainability-examples.js} +1 -3
- package/{editor/perf-site-comparison.cjs → lib/editor/perf-site-comparison.js} +1 -3
- package/lib/editor/perf-watch.js +78 -0
- package/{editor/performance-examples.cjs → lib/editor/performance-examples.js} +129 -128
- package/{editor/reliability-examples.cjs → lib/editor/reliability-examples.js} +100 -41
- package/lib/editor/scan-session.js +334 -0
- package/{editor/security-examples.cjs → lib/editor/security-examples.js} +28 -32
- package/lib/editor/server-commands.js +232 -0
- package/{editor/source-example.cjs → lib/editor/source-example.js} +3 -5
- package/{editor/standalone-performance-examples.cjs → lib/editor/standalone-performance-examples.js} +349 -333
- package/lib/editor/types/dismissal.js +1 -0
- package/lib/editor/types/fix-example.js +1 -0
- package/lib/editor/types/source-document.js +1 -0
- package/lib/editor/workspace-files.js +78 -0
- package/lib/editor/workspace-paths.js +22 -0
- package/lib/maintainability/analyze-source.js +2 -0
- package/lib/maintainability/apply-fix.js +1 -1
- package/lib/maintainability/cli.js +1 -1
- package/lib/maintainability/examples.js +58 -41
- package/lib/maintainability/rule-ids.js +29 -14
- package/lib/maintainability/scan-changed.js +3 -0
- package/lib/maintainability/scan.js +13 -9
- package/lib/maintainability/tsconfig-strict.js +69 -0
- package/lib/maintainability/type-safety-hygiene.js +204 -0
- package/lib/performance/analyze.js +2 -0
- package/lib/performance/cli-arguments.js +0 -2
- package/lib/performance/cli-policy.js +17 -9
- package/lib/performance/cli-scan.js +10 -8
- package/lib/performance/cli.js +4 -23
- package/lib/performance/editor-signal.js +1 -1
- package/lib/performance/finding-text.js +3 -3
- package/lib/performance/fix-cli.js +1 -1
- package/lib/performance/frontend-perf.js +7 -0
- package/lib/performance/frontend-render-perf.js +200 -0
- package/lib/performance/llm-max-tokens.js +56 -0
- package/lib/performance/native.js +46 -26
- package/lib/performance/polyglot.js +22 -1
- package/lib/performance/pooling.js +38 -23
- package/lib/performance/projects.js +42 -21
- package/lib/performance/rule-ids.js +236 -222
- package/lib/performance/site-fixes/database.js +3 -1
- package/lib/performance/site-fixes/integrity.js +15 -1
- package/lib/performance/source-inputs.js +18 -6
- package/lib/performance/unscanned-javascript.js +66 -19
- package/lib/reliability/analyze.js +12 -0
- package/lib/reliability/child-process.js +106 -0
- package/lib/reliability/error-values.js +177 -0
- package/lib/reliability/pool-timeout.js +79 -0
- package/lib/reliability/react-bugs.js +602 -0
- package/lib/reliability/resource-leaks.js +3 -6
- package/lib/reliability/rule-ids.js +169 -127
- package/lib/reliability/switch-exhaustive.js +284 -0
- package/lib/reliability/uncaught-exception.js +223 -0
- package/lib/security/auth-crypto/rule-ids.js +3 -2
- package/lib/security/cli.js +6 -5
- package/lib/security/deps/rule-ids.js +1 -0
- package/lib/security/examples/auth-crypto.cjs +5 -4
- package/lib/security/examples/deps.cjs +1 -0
- package/lib/security/examples/exposure.cjs +55 -42
- package/lib/security/examples/files.cjs +1 -0
- package/lib/security/examples/injection.cjs +1 -0
- package/lib/security/examples/moved.cjs +5 -4
- package/lib/security/examples/request-forgery.cjs +1 -0
- package/lib/security/exposure/client-env.js +113 -0
- package/lib/security/exposure/frontend-security.js +12 -10
- package/lib/security/exposure/index.js +14 -3
- package/lib/security/exposure/jsx-url.js +267 -0
- package/lib/security/exposure/next-image-config.js +34 -0
- package/lib/security/exposure/package-manifest.js +45 -0
- package/lib/security/exposure/react-version.js +45 -0
- package/lib/security/exposure/rule-ids.js +16 -12
- package/lib/security/files/index.js +1 -1
- package/lib/security/files/rule-ids.js +1 -0
- package/lib/security/injection/code.js +24 -10
- package/lib/security/injection/command.js +81 -28
- package/lib/security/injection/nosql.js +109 -22
- package/lib/security/injection/redos.js +152 -308
- package/lib/security/injection/regex-pattern.js +290 -0
- package/lib/security/injection/regex.js +30 -11
- package/lib/security/injection/rule-ids.js +2 -1
- package/lib/security/injection/shared.js +19 -1
- package/lib/security/injection/sql.js +83 -2
- package/lib/security/request-forgery/rule-ids.js +1 -0
- package/lib/security/rule-descriptions.js +36 -32
- package/lib/security/rule-ids.js +74 -67
- package/lib/security/taint/filename-guard.js +3 -1
- package/lib/security/taint/guards.js +84 -10
- package/lib/security/taint/index.js +124 -13
- package/lib/security/taint/propagate.js +10 -7
- package/lib/security/taint/sanitizers.js +7 -3
- package/lib/security/taint/sources.js +6 -3
- package/lib/security/taint/summaries.js +13 -1
- package/lib/security/taint/url-parts.js +14 -5
- package/lib/shared/agent-session.js +2 -2
- package/lib/shared/check.js +16 -6
- package/lib/shared/explain.js +5 -5
- package/lib/shared/fix-convergence.js +1 -1
- package/lib/shared/formats/finding-location.js +6 -2
- package/lib/shared/formats/sarif.js +3 -3
- package/lib/shared/heap-limit.js +20 -3
- package/lib/shared/pillars.js +7 -7
- package/lib/shared/rule-docs.js +12 -0
- package/lib/shared/scan-cache.js +0 -1
- package/lib/shared/workspaces.js +31 -9
- package/package.json +12 -19
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/schema/loadwise.config.schema.json +44 -0
- package/editor/README.md +0 -47
- package/examples/redis-keys.otlp.json +0 -22
- package/lib/maintainability/notify-editor.js +0 -1
- package/lib/performance/editor-report.js +0 -191
- package/lib/performance/editor-setup.js +0 -189
- package/lib/performance/editor-watch-supervisor.js +0 -104
- package/lib/performance/editor-watch.js +0 -193
- package/lib/performance/notify-editor.js +0 -1
- package/lib/performance/recursive-watch.js +0 -181
- /package/lib/{performance/types/editor-report.js → editor/types/comparison.js} +0 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// Builds the workspace edits that apply scanner fixes. A fix stores the exact original lines, so it
|
|
2
|
+
// applies only where they still exist in the editor's buffer (or the saved file when it is not
|
|
3
|
+
// open); code changed since the scan refuses the fix instead of guessing. The edit goes through the
|
|
4
|
+
// editor, so undo restores the original code.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { isChangeApplied, locateChange } from './apply-fix.js';
|
|
8
|
+
const staleFixMessage = 'The code changed since the last scan. Save the file to rescan, then try again.';
|
|
9
|
+
const noCreateMessage = 'This editor cannot create files through the language server, so the fix was not applied.';
|
|
10
|
+
const overlappingChangesMessage = 'This fix changes the same lines twice, so it was not applied.';
|
|
11
|
+
function normalizeNewlines(text) {
|
|
12
|
+
return text.replaceAll('\r\n', '\n');
|
|
13
|
+
}
|
|
14
|
+
function planCreate(change, workspace) {
|
|
15
|
+
const existing = workspace.documentFor(change.filename);
|
|
16
|
+
// Never overwrite: the file may hold work written since the scan. The same content means it is
|
|
17
|
+
// already applied.
|
|
18
|
+
if (existing === undefined) {
|
|
19
|
+
if (!workspace.canCreateFiles)
|
|
20
|
+
return { error: noCreateMessage };
|
|
21
|
+
return {
|
|
22
|
+
operations: [
|
|
23
|
+
{ filename: change.filename, key: workspace.keyOf(change.filename), create: true, text: change.after },
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (normalizeNewlines(existing.getText()) !== normalizeNewlines(change.after))
|
|
28
|
+
return { error: change.file + ' already exists, so the fix was not applied.' };
|
|
29
|
+
return 'applied';
|
|
30
|
+
}
|
|
31
|
+
function planReplace(change, workspace) {
|
|
32
|
+
const document = workspace.documentFor(change.filename);
|
|
33
|
+
if (!document)
|
|
34
|
+
return { error: staleFixMessage };
|
|
35
|
+
const lines = document.getText().split(/\r?\n/);
|
|
36
|
+
const row = locateChange(lines, change);
|
|
37
|
+
// The code moved or changed since the scan; applying the stored rewrite could clobber the new
|
|
38
|
+
// code.
|
|
39
|
+
if (row === -1)
|
|
40
|
+
return { error: staleFixMessage };
|
|
41
|
+
if (isChangeApplied(lines, row, change))
|
|
42
|
+
return 'applied';
|
|
43
|
+
const lastRow = row + change.before.split(/\r?\n/).length - 1;
|
|
44
|
+
const range = { start: { line: row, character: 0 }, end: { line: lastRow, character: lines[lastRow].length } };
|
|
45
|
+
const operation = {
|
|
46
|
+
filename: change.filename,
|
|
47
|
+
key: workspace.keyOf(change.filename),
|
|
48
|
+
version: document.version,
|
|
49
|
+
firstRow: row,
|
|
50
|
+
lastRow,
|
|
51
|
+
range,
|
|
52
|
+
text: change.after.split(/\r?\n/).join(document.eol),
|
|
53
|
+
};
|
|
54
|
+
return { operations: [operation] };
|
|
55
|
+
}
|
|
56
|
+
// Resolves every change of one fix against the current files. Any stale change, existing file to
|
|
57
|
+
// create or pair of changes to the same lines rejects the whole fix, so a multi-file fix applies
|
|
58
|
+
// all-or-nothing.
|
|
59
|
+
function planFix(fix, workspace) {
|
|
60
|
+
const operations = [];
|
|
61
|
+
const claimed = new Map();
|
|
62
|
+
let alreadyApplied = 0;
|
|
63
|
+
for (const change of fix.changes) {
|
|
64
|
+
const plan = change.create ? planCreate(change, workspace) : planReplace(change, workspace);
|
|
65
|
+
if (plan === 'applied') {
|
|
66
|
+
alreadyApplied++;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (plan.operations === undefined)
|
|
70
|
+
return plan;
|
|
71
|
+
// Each change is planned against the unchanged file, so two of them on one line would both apply.
|
|
72
|
+
if (plan.operations.some((operation) => overlaps(claimed, operation)))
|
|
73
|
+
return { error: overlappingChangesMessage };
|
|
74
|
+
for (const operation of plan.operations)
|
|
75
|
+
claim(claimed, operation);
|
|
76
|
+
operations.push(...plan.operations);
|
|
77
|
+
}
|
|
78
|
+
if (!operations.length && alreadyApplied)
|
|
79
|
+
return { error: 'This fix is already applied.' };
|
|
80
|
+
return { operations };
|
|
81
|
+
}
|
|
82
|
+
// New files come first. An editor that aborts on a failed operation keeps the ones before it, and a
|
|
83
|
+
// created file is easier to spot and delete than a half-rewritten existing one.
|
|
84
|
+
function documentChanges(operations, workspace) {
|
|
85
|
+
const changes = [];
|
|
86
|
+
const editsByFile = new Map();
|
|
87
|
+
const ordered = [...operations.filter(({ create }) => create), ...operations.filter(({ create }) => !create)];
|
|
88
|
+
for (const operation of ordered) {
|
|
89
|
+
const uri = workspace.uriFor(operation.filename);
|
|
90
|
+
if (operation.create) {
|
|
91
|
+
changes.push({ kind: 'create', uri, options: { overwrite: false, ignoreIfExists: false } });
|
|
92
|
+
const start = { line: 0, character: 0 };
|
|
93
|
+
changes.push({
|
|
94
|
+
textDocument: { uri, version: null },
|
|
95
|
+
edits: [{ range: { start, end: start }, newText: operation.text }],
|
|
96
|
+
});
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const edit = { range: operation.range, newText: operation.text };
|
|
100
|
+
const edits = editsByFile.get(operation.key);
|
|
101
|
+
if (edits) {
|
|
102
|
+
edits.push(edit);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const fileEdits = [edit];
|
|
106
|
+
editsByFile.set(operation.key, fileEdits);
|
|
107
|
+
changes.push({ textDocument: { uri, version: operation.version }, edits: fileEdits });
|
|
108
|
+
}
|
|
109
|
+
return changes;
|
|
110
|
+
}
|
|
111
|
+
function plainChanges(operations, workspace) {
|
|
112
|
+
const changes = {};
|
|
113
|
+
for (const operation of operations) {
|
|
114
|
+
if (operation.create)
|
|
115
|
+
continue;
|
|
116
|
+
const uri = workspace.uriFor(operation.filename);
|
|
117
|
+
(changes[uri] ??= []).push({ range: operation.range, newText: operation.text });
|
|
118
|
+
}
|
|
119
|
+
return changes;
|
|
120
|
+
}
|
|
121
|
+
function workspaceEdit(operations, workspace) {
|
|
122
|
+
const createdFiles = operations.flatMap((operation) => (operation.create ? [operation.filename] : []));
|
|
123
|
+
if (workspace.supportsDocumentChanges) {
|
|
124
|
+
return { edit: { documentChanges: documentChanges(operations, workspace) }, createdFiles };
|
|
125
|
+
}
|
|
126
|
+
return { edit: { changes: plainChanges(operations, workspace) }, createdFiles };
|
|
127
|
+
}
|
|
128
|
+
/** The edit applying one fix, or why it cannot apply. */
|
|
129
|
+
function fixEdit(fix, workspace) {
|
|
130
|
+
const plan = planFix(fix, workspace);
|
|
131
|
+
if (plan.operations === undefined)
|
|
132
|
+
return plan;
|
|
133
|
+
return workspaceEdit(plan.operations, workspace);
|
|
134
|
+
}
|
|
135
|
+
function overlaps(claimed, operation) {
|
|
136
|
+
const taken = claimed.get(operation.key);
|
|
137
|
+
if (!taken)
|
|
138
|
+
return false;
|
|
139
|
+
if (operation.create || taken.created)
|
|
140
|
+
return true;
|
|
141
|
+
return taken.rows.some(([first, last]) => operation.firstRow <= last && first <= operation.lastRow);
|
|
142
|
+
}
|
|
143
|
+
function claim(claimed, operation) {
|
|
144
|
+
let taken = claimed.get(operation.key);
|
|
145
|
+
if (!taken) {
|
|
146
|
+
taken = { created: false, rows: [] };
|
|
147
|
+
claimed.set(operation.key, taken);
|
|
148
|
+
}
|
|
149
|
+
if (operation.create)
|
|
150
|
+
taken.created = true;
|
|
151
|
+
else
|
|
152
|
+
taken.rows.push([operation.firstRow, operation.lastRow]);
|
|
153
|
+
}
|
|
154
|
+
// Identical rewrites can come from several findings. Any request for review applies to the shared
|
|
155
|
+
// rewrite.
|
|
156
|
+
function bulkFixesFor(entries) {
|
|
157
|
+
const unique = new Map();
|
|
158
|
+
for (const { anchor } of entries) {
|
|
159
|
+
const fix = anchor.fix;
|
|
160
|
+
if (!fix)
|
|
161
|
+
continue;
|
|
162
|
+
const key = JSON.stringify(fix.changes);
|
|
163
|
+
if (!unique.has(key) || fix.requiresReview)
|
|
164
|
+
unique.set(key, fix);
|
|
165
|
+
}
|
|
166
|
+
const eligible = [...unique.values()].filter((fix) => !fix.requiresReview);
|
|
167
|
+
return { eligible, reviewCount: unique.size - eligible.length };
|
|
168
|
+
}
|
|
169
|
+
// Every applicable fix in one edit, so one undo reverts them all. Stale fixes, fixes that would
|
|
170
|
+
// create an existing file, and fixes overlapping an earlier one are skipped whole.
|
|
171
|
+
function fixAllEdit(fixes, workspace) {
|
|
172
|
+
const claimed = new Map();
|
|
173
|
+
const operations = [];
|
|
174
|
+
let applied = 0;
|
|
175
|
+
for (const fix of fixes) {
|
|
176
|
+
const plan = planFix(fix, workspace);
|
|
177
|
+
if (plan.operations === undefined || plan.operations.some((operation) => overlaps(claimed, operation)))
|
|
178
|
+
continue;
|
|
179
|
+
for (const operation of plan.operations)
|
|
180
|
+
claim(claimed, operation);
|
|
181
|
+
operations.push(...plan.operations);
|
|
182
|
+
applied++;
|
|
183
|
+
}
|
|
184
|
+
return { ...workspaceEdit(operations, workspace), applied, skipped: fixes.length - applied };
|
|
185
|
+
}
|
|
186
|
+
// A workspace edit can create a file but not a folder, and editors differ on whether they make the
|
|
187
|
+
// missing ones, so the server makes them before it sends such an edit.
|
|
188
|
+
/** The folders of the new files that do not exist yet, outermost first. */
|
|
189
|
+
function missingFolders(createdFiles) {
|
|
190
|
+
const missing = new Set();
|
|
191
|
+
for (const filename of createdFiles) {
|
|
192
|
+
for (let folder = path.dirname(filename); !fs.existsSync(folder); folder = path.dirname(folder)) {
|
|
193
|
+
if (path.dirname(folder) === folder)
|
|
194
|
+
break;
|
|
195
|
+
missing.add(folder);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return [...missing].sort((left, right) => left.length - right.length);
|
|
199
|
+
}
|
|
200
|
+
export { bulkFixesFor, fixAllEdit, fixEdit, missingFolders };
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
// `loadwise lsp`: the language server every editor uses. It runs the checkers on each workspace
|
|
2
|
+
// folder, publishes their findings as diagnostics, and offers hovers, fixes, dismissals and code
|
|
3
|
+
// lenses over the Language Server Protocol on stdin and stdout.
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
|
+
import packageManifest from '../../package.json' with { type: 'json' };
|
|
7
|
+
import { CodeActionKind, CodeLensRefreshRequest, DidChangeConfigurationNotification, DidChangeWatchedFilesNotification, FileChangeType, TextDocumentSyncKind, TextDocuments, createConnection, } from 'vscode-languageserver/node';
|
|
8
|
+
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
9
|
+
import { codeActions, codeLenses, commands, fixAllKind } from './actions.js';
|
|
10
|
+
import { buildDiagnostics, hoverFor } from './diagnostics.js';
|
|
11
|
+
import { createDismissals } from './dismissals.js';
|
|
12
|
+
import { createEditorDocument, readDiskDocument } from './editor-document.js';
|
|
13
|
+
import { createScanSession } from './scan-session.js';
|
|
14
|
+
import { classifyChange, watchedGlobs } from './workspace-files.js';
|
|
15
|
+
import { runCommand } from './server-commands.js';
|
|
16
|
+
import { isInside } from './workspace-paths.js';
|
|
17
|
+
import { isRecord } from '../shared/is-record.js';
|
|
18
|
+
const usage = 'Usage: loadwise lsp [--stdio]';
|
|
19
|
+
const defaultCli = path.resolve(import.meta.dirname, '../../bin/loadwise.js');
|
|
20
|
+
const severities = new Set(['default', 'error', 'warning', 'information', 'hint']);
|
|
21
|
+
const defaultSettings = { enabled: true, project: '', cli: '', severity: 'default', codeLens: true };
|
|
22
|
+
// Only these change what the checkers report; severity and code lenses only change how it is shown.
|
|
23
|
+
const scanSettingNames = ['enabled', 'project', 'cli'];
|
|
24
|
+
function settingsFrom(value) {
|
|
25
|
+
const section = isRecord(value) && isRecord(value.loadwise) ? value.loadwise : value;
|
|
26
|
+
if (!isRecord(section))
|
|
27
|
+
return {};
|
|
28
|
+
const settings = {};
|
|
29
|
+
if (typeof section.enabled === 'boolean')
|
|
30
|
+
settings.enabled = section.enabled;
|
|
31
|
+
if (typeof section.project === 'string')
|
|
32
|
+
settings.project = section.project;
|
|
33
|
+
if (typeof section.cli === 'string')
|
|
34
|
+
settings.cli = section.cli;
|
|
35
|
+
if (typeof section.severity === 'string' && severities.has(section.severity))
|
|
36
|
+
settings.severity = section.severity;
|
|
37
|
+
if (typeof section.codeLens === 'boolean')
|
|
38
|
+
settings.codeLens = section.codeLens;
|
|
39
|
+
return settings;
|
|
40
|
+
}
|
|
41
|
+
// Windows paths compare case-insensitively, and editors spell drive letters differently.
|
|
42
|
+
function fileKey(filename) {
|
|
43
|
+
const resolved = path.resolve(filename);
|
|
44
|
+
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
45
|
+
}
|
|
46
|
+
function filenameOf(uri) {
|
|
47
|
+
if (!uri.startsWith('file:'))
|
|
48
|
+
return;
|
|
49
|
+
try {
|
|
50
|
+
return fileURLToPath(uri);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function scanSettingsKey(settings) {
|
|
57
|
+
return JSON.stringify(scanSettingNames.map((name) => settings[name]));
|
|
58
|
+
}
|
|
59
|
+
// A parent folder and a folder nested in it both report the nested files. One diagnostic is shown
|
|
60
|
+
// for each, taken from the deepest folder, whose settings and dismissals own the file.
|
|
61
|
+
function findingKey({ uri, diagnostic }) {
|
|
62
|
+
return JSON.stringify([uri, diagnostic.source, diagnostic.code, diagnostic.range, diagnostic.message]);
|
|
63
|
+
}
|
|
64
|
+
function uniqueFindings(entries) {
|
|
65
|
+
const unique = new Map();
|
|
66
|
+
for (const entry of entries) {
|
|
67
|
+
const key = findingKey(entry);
|
|
68
|
+
if (!unique.has(key))
|
|
69
|
+
unique.set(key, entry);
|
|
70
|
+
}
|
|
71
|
+
return [...unique.values()];
|
|
72
|
+
}
|
|
73
|
+
function parseArguments(args) {
|
|
74
|
+
// Editors pass the transport flag; the parent process id is read by the protocol library itself.
|
|
75
|
+
const unknown = args.filter((arg) => arg !== '--stdio' && !arg.startsWith('--clientProcessId'));
|
|
76
|
+
if (unknown.length)
|
|
77
|
+
throw new Error(usage);
|
|
78
|
+
}
|
|
79
|
+
function serverCapabilities() {
|
|
80
|
+
return {
|
|
81
|
+
textDocumentSync: { openClose: true, change: TextDocumentSyncKind.Incremental, save: { includeText: false } },
|
|
82
|
+
hoverProvider: true,
|
|
83
|
+
codeActionProvider: { codeActionKinds: [CodeActionKind.QuickFix, fixAllKind] },
|
|
84
|
+
codeLensProvider: { resolveProvider: false },
|
|
85
|
+
executeCommandProvider: { commands: Object.values(commands) },
|
|
86
|
+
workspace: { workspaceFolders: { supported: true, changeNotifications: true } },
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function startServer(connection) {
|
|
90
|
+
const documents = new TextDocuments(TextDocument);
|
|
91
|
+
const openUris = new Map();
|
|
92
|
+
const savedVersions = new Map();
|
|
93
|
+
const folders = new Map();
|
|
94
|
+
// The latest workspace/configuration request per folder; an older answer arriving later is dropped.
|
|
95
|
+
const settingsRequests = new Map();
|
|
96
|
+
const published = new Map();
|
|
97
|
+
let capabilities = {};
|
|
98
|
+
let initialSettings = {};
|
|
99
|
+
let pushedSettings = {};
|
|
100
|
+
let progress;
|
|
101
|
+
let progressStarting = false;
|
|
102
|
+
let disposed = false;
|
|
103
|
+
function settingsFor(folder) {
|
|
104
|
+
return { ...defaultSettings, ...initialSettings, ...pushedSettings, ...folder.folderSettings };
|
|
105
|
+
}
|
|
106
|
+
function documentFor(filename) {
|
|
107
|
+
const key = fileKey(filename);
|
|
108
|
+
const uri = openUris.get(key);
|
|
109
|
+
const open = uri === undefined ? undefined : documents.get(uri);
|
|
110
|
+
if (!open)
|
|
111
|
+
return readDiskDocument(filename);
|
|
112
|
+
return createEditorDocument(filename, open.getText(), {
|
|
113
|
+
languageId: open.languageId,
|
|
114
|
+
version: open.version,
|
|
115
|
+
isDirty: savedVersions.get(key) !== open.version,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function uriFor(filename) {
|
|
119
|
+
return openUris.get(fileKey(filename)) ?? pathToFileURL(filename).href;
|
|
120
|
+
}
|
|
121
|
+
function deepestFirst() {
|
|
122
|
+
return [...folders.values()].sort((left, right) => right.root.length - left.root.length);
|
|
123
|
+
}
|
|
124
|
+
/** Every folder that contains the file, the deepest first. */
|
|
125
|
+
function foldersFor(filename) {
|
|
126
|
+
return deepestFirst().filter((folder) => isInside(folder.root, filename));
|
|
127
|
+
}
|
|
128
|
+
function folderFor(filename) {
|
|
129
|
+
return foldersFor(filename)[0];
|
|
130
|
+
}
|
|
131
|
+
// A folder with loadwise.enabled off also hides what an enclosing folder reports for its files.
|
|
132
|
+
function visibleFindings(entries) {
|
|
133
|
+
return uniqueFindings(entries).filter((entry) => {
|
|
134
|
+
const owner = folderFor(entry.filename);
|
|
135
|
+
return !owner || settingsFor(owner).enabled;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function entriesFor(uri) {
|
|
139
|
+
const filename = filenameOf(uri);
|
|
140
|
+
if (filename === undefined)
|
|
141
|
+
return [];
|
|
142
|
+
const key = fileKey(filename);
|
|
143
|
+
return visibleFindings(deepestFirst().flatMap((folder) => folder.entries.filter((entry) => fileKey(entry.filename) === key)));
|
|
144
|
+
}
|
|
145
|
+
function fixWorkspace() {
|
|
146
|
+
const workspaceEdit = capabilities.workspace?.workspaceEdit;
|
|
147
|
+
const supportsDocumentChanges = workspaceEdit?.documentChanges === true;
|
|
148
|
+
return {
|
|
149
|
+
documentFor,
|
|
150
|
+
uriFor,
|
|
151
|
+
keyOf: fileKey,
|
|
152
|
+
supportsDocumentChanges,
|
|
153
|
+
failureHandling: workspaceEdit?.failureHandling,
|
|
154
|
+
canCreateFiles: supportsDocumentChanges && Boolean(workspaceEdit?.resourceOperations?.includes('create')),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function refreshCodeLenses() {
|
|
158
|
+
if (!capabilities.workspace?.codeLens?.refreshSupport)
|
|
159
|
+
return;
|
|
160
|
+
connection.sendRequest(CodeLensRefreshRequest.type).catch(() => { });
|
|
161
|
+
}
|
|
162
|
+
// Unchanged files are not resent, and files whose findings are gone get an empty list.
|
|
163
|
+
function sendDiagnostics() {
|
|
164
|
+
const next = new Map();
|
|
165
|
+
for (const entry of visibleFindings(deepestFirst().flatMap((folder) => folder.entries))) {
|
|
166
|
+
const list = next.get(entry.uri);
|
|
167
|
+
if (list)
|
|
168
|
+
list.push(entry.diagnostic);
|
|
169
|
+
else
|
|
170
|
+
next.set(entry.uri, [entry.diagnostic]);
|
|
171
|
+
}
|
|
172
|
+
for (const uri of published.keys()) {
|
|
173
|
+
if (next.has(uri))
|
|
174
|
+
continue;
|
|
175
|
+
published.delete(uri);
|
|
176
|
+
connection.sendDiagnostics({ uri, diagnostics: [] }).catch(() => { });
|
|
177
|
+
}
|
|
178
|
+
for (const [uri, diagnostics] of next) {
|
|
179
|
+
const text = JSON.stringify(diagnostics);
|
|
180
|
+
if (published.get(uri) === text)
|
|
181
|
+
continue;
|
|
182
|
+
published.set(uri, text);
|
|
183
|
+
connection.sendDiagnostics({ uri, diagnostics }).catch(() => { });
|
|
184
|
+
}
|
|
185
|
+
refreshCodeLenses();
|
|
186
|
+
}
|
|
187
|
+
function republish(folder) {
|
|
188
|
+
const settings = settingsFor(folder);
|
|
189
|
+
// One read per file for the whole report, however many findings it has.
|
|
190
|
+
const read = new Map();
|
|
191
|
+
const cachedDocumentFor = (filename) => {
|
|
192
|
+
const key = fileKey(filename);
|
|
193
|
+
if (!read.has(key))
|
|
194
|
+
read.set(key, documentFor(filename));
|
|
195
|
+
return read.get(key);
|
|
196
|
+
};
|
|
197
|
+
folder.entries = [...folder.session.reports()].flatMap(({ source, findings }) => buildDiagnostics(folder.root, source, findings, {
|
|
198
|
+
// A nested folder's own dismissal file decides for its files.
|
|
199
|
+
isDismissed: (entry) => (folderFor(entry.file) ?? folder).dismissals.has(entry),
|
|
200
|
+
severity: settings.severity,
|
|
201
|
+
documentFor: cachedDocumentFor,
|
|
202
|
+
uriFor,
|
|
203
|
+
}));
|
|
204
|
+
sendDiagnostics();
|
|
205
|
+
}
|
|
206
|
+
function republishAll() {
|
|
207
|
+
for (const folder of folders.values())
|
|
208
|
+
republish(folder);
|
|
209
|
+
}
|
|
210
|
+
function updateProgress() {
|
|
211
|
+
const scanning = [...folders.values()].some((folder) => folder.session.isScanning());
|
|
212
|
+
if (!scanning && progress) {
|
|
213
|
+
progress.done();
|
|
214
|
+
progress = undefined;
|
|
215
|
+
}
|
|
216
|
+
if (!scanning || progress || progressStarting)
|
|
217
|
+
return;
|
|
218
|
+
progressStarting = true;
|
|
219
|
+
connection.window.createWorkDoneProgress().then((reporter) => {
|
|
220
|
+
progressStarting = false;
|
|
221
|
+
progress = reporter;
|
|
222
|
+
reporter.begin('Loadwise', undefined, 'Scanning');
|
|
223
|
+
updateProgress();
|
|
224
|
+
}, () => {
|
|
225
|
+
progressStarting = false;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
async function pullSettings(folder) {
|
|
229
|
+
if (!capabilities.workspace?.configuration)
|
|
230
|
+
return;
|
|
231
|
+
const request = (settingsRequests.get(folder) ?? 0) + 1;
|
|
232
|
+
settingsRequests.set(folder, request);
|
|
233
|
+
try {
|
|
234
|
+
const settings = await connection.workspace.getConfiguration({ scopeUri: folder.uri, section: 'loadwise' });
|
|
235
|
+
if (settingsRequests.get(folder) === request)
|
|
236
|
+
folder.folderSettings = settingsFrom(settings);
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
connection.console.error('Could not read the loadwise settings: ' + String(error));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async function addFolder({ uri, name }) {
|
|
243
|
+
const root = filenameOf(uri);
|
|
244
|
+
if (root === undefined || folders.has(fileKey(root)) || disposed)
|
|
245
|
+
return;
|
|
246
|
+
const folder = {
|
|
247
|
+
root,
|
|
248
|
+
uri,
|
|
249
|
+
name,
|
|
250
|
+
dismissals: createDismissals(root, { onProblem: (message) => connection.console.error(message) }),
|
|
251
|
+
entries: [],
|
|
252
|
+
folderSettings: {},
|
|
253
|
+
session: createScanSession({
|
|
254
|
+
root,
|
|
255
|
+
name,
|
|
256
|
+
defaultCli,
|
|
257
|
+
settings: () => settingsFor(folder),
|
|
258
|
+
log: (text) => connection.console.info(text),
|
|
259
|
+
logError: (text) => connection.console.error(text),
|
|
260
|
+
onReports: () => republish(folder),
|
|
261
|
+
onStatus: updateProgress,
|
|
262
|
+
}),
|
|
263
|
+
};
|
|
264
|
+
folders.set(fileKey(root), folder);
|
|
265
|
+
await pullSettings(folder);
|
|
266
|
+
// A nested folder takes over the dismissals and the enabled setting of its files from the
|
|
267
|
+
// folders around it.
|
|
268
|
+
republishAll();
|
|
269
|
+
folder.session.schedule();
|
|
270
|
+
}
|
|
271
|
+
function removeFolder({ uri }) {
|
|
272
|
+
const root = filenameOf(uri);
|
|
273
|
+
const folder = root === undefined ? undefined : folders.get(fileKey(root));
|
|
274
|
+
if (!folder || root === undefined)
|
|
275
|
+
return;
|
|
276
|
+
folder.session.dispose();
|
|
277
|
+
folders.delete(fileKey(root));
|
|
278
|
+
settingsRequests.delete(folder);
|
|
279
|
+
// The enclosing folders own its files again, with their own dismissals and settings.
|
|
280
|
+
republishAll();
|
|
281
|
+
sendDiagnostics();
|
|
282
|
+
updateProgress();
|
|
283
|
+
}
|
|
284
|
+
function disposeAll() {
|
|
285
|
+
disposed = true;
|
|
286
|
+
for (const folder of folders.values())
|
|
287
|
+
folder.session.dispose();
|
|
288
|
+
folders.clear();
|
|
289
|
+
}
|
|
290
|
+
// Groups the changes of one notification, so each folder rescans once. A file inside nested
|
|
291
|
+
// folders changes the scans of each of them.
|
|
292
|
+
function filesChanged(changes) {
|
|
293
|
+
const touched = new Map();
|
|
294
|
+
let dismissalsChanged = false;
|
|
295
|
+
for (const { filename, type } of changes) {
|
|
296
|
+
for (const folder of foldersFor(filename)) {
|
|
297
|
+
const kind = classifyChange(folder.root, filename, settingsFor(folder).project);
|
|
298
|
+
if (kind === 'ignored')
|
|
299
|
+
continue;
|
|
300
|
+
if (kind === 'dismissals') {
|
|
301
|
+
folder.dismissals.reload();
|
|
302
|
+
dismissalsChanged = true;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const flags = touched.get(folder) ?? { restart: false, filesAddedOrRemoved: false };
|
|
306
|
+
touched.set(folder, flags);
|
|
307
|
+
flags.restart ||= kind === 'config';
|
|
308
|
+
flags.filesAddedOrRemoved ||= type !== FileChangeType.Changed;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// A nested folder's dismissals also hide its files' findings from the parent folder.
|
|
312
|
+
if (dismissalsChanged)
|
|
313
|
+
republishAll();
|
|
314
|
+
for (const [folder, options] of touched)
|
|
315
|
+
folder.session.schedule(options);
|
|
316
|
+
}
|
|
317
|
+
function watchedFileEvents(events) {
|
|
318
|
+
return events.flatMap(({ uri, type }) => {
|
|
319
|
+
const filename = filenameOf(uri);
|
|
320
|
+
return filename === undefined ? [] : [{ filename, type }];
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
// An editor can open a buffer with unsaved edits, such as one restored from a previous session;
|
|
324
|
+
// only a buffer that matches the file on disk counts as saved.
|
|
325
|
+
function documentOpened(document) {
|
|
326
|
+
const filename = filenameOf(document.uri);
|
|
327
|
+
if (filename === undefined)
|
|
328
|
+
return;
|
|
329
|
+
openUris.set(fileKey(filename), document.uri);
|
|
330
|
+
if (readDiskDocument(filename)?.getText() === document.getText())
|
|
331
|
+
savedVersions.set(fileKey(filename), document.version);
|
|
332
|
+
else
|
|
333
|
+
savedVersions.delete(fileKey(filename));
|
|
334
|
+
foldersFor(filename).forEach(republish);
|
|
335
|
+
}
|
|
336
|
+
function documentClosed(uri) {
|
|
337
|
+
const filename = filenameOf(uri);
|
|
338
|
+
if (filename === undefined)
|
|
339
|
+
return;
|
|
340
|
+
openUris.delete(fileKey(filename));
|
|
341
|
+
savedVersions.delete(fileKey(filename));
|
|
342
|
+
foldersFor(filename).forEach(republish);
|
|
343
|
+
}
|
|
344
|
+
const context = {
|
|
345
|
+
connection,
|
|
346
|
+
folders: () => folders.values(),
|
|
347
|
+
folderFor,
|
|
348
|
+
entriesFor,
|
|
349
|
+
fixWorkspace,
|
|
350
|
+
// A dismissal in a nested folder also hides the finding the parent folder reports.
|
|
351
|
+
republish: republishAll,
|
|
352
|
+
};
|
|
353
|
+
connection.onInitialize((params) => {
|
|
354
|
+
capabilities = params.capabilities;
|
|
355
|
+
initialSettings = settingsFrom(params.initializationOptions);
|
|
356
|
+
const rootFilename = params.rootUri ? filenameOf(params.rootUri) : undefined;
|
|
357
|
+
const rootFolder = params.rootUri && rootFilename ? [{ uri: params.rootUri, name: path.basename(rootFilename) }] : [];
|
|
358
|
+
const initialFolders = params.workspaceFolders ?? rootFolder;
|
|
359
|
+
connection.onInitialized(async () => {
|
|
360
|
+
// A client that advertises dynamic registration can still refuse it; the folders are scanned either way.
|
|
361
|
+
if (capabilities.workspace?.didChangeWatchedFiles?.dynamicRegistration) {
|
|
362
|
+
await connection.client
|
|
363
|
+
.register(DidChangeWatchedFilesNotification.type, {
|
|
364
|
+
watchers: watchedGlobs.map((globPattern) => ({ globPattern })),
|
|
365
|
+
})
|
|
366
|
+
.catch((error) => connection.console.error('Could not watch files: ' + String(error)));
|
|
367
|
+
}
|
|
368
|
+
if (capabilities.workspace?.didChangeConfiguration?.dynamicRegistration) {
|
|
369
|
+
await connection.client
|
|
370
|
+
.register(DidChangeConfigurationNotification.type, { section: 'loadwise' })
|
|
371
|
+
.catch((error) => connection.console.error('Could not watch the loadwise settings: ' + String(error)));
|
|
372
|
+
}
|
|
373
|
+
if (capabilities.workspace?.workspaceFolders) {
|
|
374
|
+
connection.workspace.onDidChangeWorkspaceFolders(({ added, removed }) => {
|
|
375
|
+
removed.forEach(removeFolder);
|
|
376
|
+
for (const folder of added)
|
|
377
|
+
void addFolder(folder);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
await Promise.all(initialFolders.map(addFolder));
|
|
381
|
+
});
|
|
382
|
+
return { capabilities: serverCapabilities(), serverInfo: { name: 'loadwise', version: packageManifest.version } };
|
|
383
|
+
});
|
|
384
|
+
documents.onDidOpen(({ document }) => documentOpened(document));
|
|
385
|
+
documents.onDidClose(({ document }) => documentClosed(document.uri));
|
|
386
|
+
documents.onDidSave(({ document }) => {
|
|
387
|
+
const filename = filenameOf(document.uri);
|
|
388
|
+
if (filename === undefined)
|
|
389
|
+
return;
|
|
390
|
+
savedVersions.set(fileKey(filename), document.version);
|
|
391
|
+
filesChanged([{ filename, type: FileChangeType.Changed }]);
|
|
392
|
+
});
|
|
393
|
+
connection.onDidChangeWatchedFiles(({ changes }) => filesChanged(watchedFileEvents(changes)));
|
|
394
|
+
connection.onDidChangeConfiguration(async ({ settings }) => {
|
|
395
|
+
const scanSettings = new Map([...folders.values()].map((folder) => [folder, scanSettingsKey(settingsFor(folder))]));
|
|
396
|
+
if (settings !== null && settings !== undefined)
|
|
397
|
+
pushedSettings = settingsFrom(settings);
|
|
398
|
+
await Promise.all([...folders.values()].map(pullSettings));
|
|
399
|
+
republishAll();
|
|
400
|
+
for (const folder of folders.values()) {
|
|
401
|
+
const previous = scanSettings.get(folder);
|
|
402
|
+
// A folder added while the settings were read scans on its own.
|
|
403
|
+
if (previous !== undefined && previous !== scanSettingsKey(settingsFor(folder))) {
|
|
404
|
+
folder.session.schedule({ restart: true });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
connection.onHover(({ textDocument, position }) => {
|
|
409
|
+
const filename = filenameOf(textDocument.uri);
|
|
410
|
+
const document = filename === undefined ? undefined : documentFor(filename);
|
|
411
|
+
if (!document)
|
|
412
|
+
return null;
|
|
413
|
+
const hover = hoverFor(document, entriesFor(textDocument.uri), position);
|
|
414
|
+
return hover ? { contents: { kind: 'markdown', value: hover.markdown }, range: hover.range } : null;
|
|
415
|
+
});
|
|
416
|
+
connection.onCodeAction(({ textDocument, range, context: actionContext }) => codeActions(entriesFor(textDocument.uri), range, actionContext.only, textDocument.uri, fixWorkspace()));
|
|
417
|
+
connection.onCodeLens(({ textDocument }) => {
|
|
418
|
+
const filename = filenameOf(textDocument.uri);
|
|
419
|
+
const folder = filename === undefined ? undefined : folderFor(filename);
|
|
420
|
+
if (!folder || !settingsFor(folder).codeLens)
|
|
421
|
+
return [];
|
|
422
|
+
return codeLenses(entriesFor(textDocument.uri), textDocument.uri);
|
|
423
|
+
});
|
|
424
|
+
connection.onExecuteCommand(({ command, arguments: commandArguments }) => runCommand(context, command, commandArguments ?? []));
|
|
425
|
+
connection.onShutdown(disposeAll);
|
|
426
|
+
connection.onExit(disposeAll);
|
|
427
|
+
// The protocol library exits the process when the editor goes away; the checkers must not outlive it.
|
|
428
|
+
process.on('exit', disposeAll);
|
|
429
|
+
documents.listen(connection);
|
|
430
|
+
connection.listen();
|
|
431
|
+
}
|
|
432
|
+
function runLanguageServer(args) {
|
|
433
|
+
parseArguments(args);
|
|
434
|
+
startServer(createConnection(process.stdin, process.stdout));
|
|
435
|
+
return 0;
|
|
436
|
+
}
|
|
437
|
+
export { runLanguageServer };
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.exampleFor = exampleFor;
|
|
4
1
|
const guards = new Map([
|
|
5
2
|
[
|
|
6
3
|
'javascript',
|
|
@@ -227,3 +224,4 @@ const summary = summarize(await loadCount());`,
|
|
|
227
224
|
break;
|
|
228
225
|
}
|
|
229
226
|
}
|
|
227
|
+
export { exampleFor };
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.perfSiteComparison = perfSiteComparison;
|
|
4
1
|
const maxStatementLength = 360;
|
|
5
2
|
const maxFilterLines = 6;
|
|
6
3
|
const existenceMethods = { findOne: 'exists', findOneBy: 'existsBy' };
|
|
@@ -83,3 +80,4 @@ function perfSiteComparison(document, line, diagnostic) {
|
|
|
83
80
|
if (diagnostic.code === 'node-sync-fs-in-async')
|
|
84
81
|
return filesystemComparison(line, diagnostic);
|
|
85
82
|
}
|
|
83
|
+
export { perfSiteComparison };
|