loadwise 0.5.1 → 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 +542 -627
- package/bin/loadwise.js +4 -0
- package/lib/accessibility/frontend-detect.js +2 -1
- package/lib/accessibility/rule-ids.js +12 -9
- 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 +9 -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 +6 -0
- 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 +9 -16
- 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,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 };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
// Runs `loadwise perf -p <config> --watch --json`. The TypeScript watch compiler re-analyzes only
|
|
3
|
+
// what changed, including edits from git, other editors or code generators, and prints one compact
|
|
4
|
+
// JSON report per line.
|
|
5
|
+
function startPerfWatch({ script, project, cwd, env, onFindings, onError, onExit }) {
|
|
6
|
+
const child = spawn(process.execPath, [script, 'perf', '-p', project, '--watch', '--json'], {
|
|
7
|
+
cwd,
|
|
8
|
+
env,
|
|
9
|
+
windowsHide: true,
|
|
10
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
11
|
+
});
|
|
12
|
+
const maxPendingBytes = 64 * 1024 * 1024;
|
|
13
|
+
let pending = '';
|
|
14
|
+
let stopped = false;
|
|
15
|
+
function readReport(line) {
|
|
16
|
+
let report;
|
|
17
|
+
try {
|
|
18
|
+
report = JSON.parse(line);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
onError(new Error('Watch mode printed an unreadable report: ' + (error instanceof Error ? error.message : String(error))));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (report && Array.isArray(report.findings))
|
|
25
|
+
onFindings(report.findings);
|
|
26
|
+
else
|
|
27
|
+
onError(new Error('Watch mode returned an invalid findings report.'));
|
|
28
|
+
}
|
|
29
|
+
child.stdout.setEncoding('utf8');
|
|
30
|
+
child.stdout.on('data', (chunk) => {
|
|
31
|
+
if (child.killed)
|
|
32
|
+
return;
|
|
33
|
+
if (pending.length + chunk.length > maxPendingBytes) {
|
|
34
|
+
pending = '';
|
|
35
|
+
onError(new Error('Watch mode output exceeded maximum buffer size.'));
|
|
36
|
+
child.kill('SIGKILL');
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
pending += chunk;
|
|
40
|
+
let newline;
|
|
41
|
+
while ((newline = pending.indexOf('\n')) !== -1) {
|
|
42
|
+
const line = pending.slice(0, newline).trim();
|
|
43
|
+
pending = pending.slice(newline + 1);
|
|
44
|
+
if (line)
|
|
45
|
+
readReport(line);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
child.stderr.setEncoding('utf8');
|
|
49
|
+
child.stderr.on('data', (text) => {
|
|
50
|
+
if (child.killed)
|
|
51
|
+
return;
|
|
52
|
+
if (text.trim())
|
|
53
|
+
onError(new Error(text.trim()));
|
|
54
|
+
});
|
|
55
|
+
function finish(error) {
|
|
56
|
+
if (stopped)
|
|
57
|
+
return;
|
|
58
|
+
stopped = true;
|
|
59
|
+
onExit(error);
|
|
60
|
+
}
|
|
61
|
+
child.on('error', finish);
|
|
62
|
+
child.on('exit', (code, signal) => finish(new Error('Watch mode exited with ' + (signal ?? 'code ' + code) + '.')));
|
|
63
|
+
return {
|
|
64
|
+
pid: child.pid,
|
|
65
|
+
stop() {
|
|
66
|
+
if (stopped)
|
|
67
|
+
return;
|
|
68
|
+
stopped = true;
|
|
69
|
+
child.kill();
|
|
70
|
+
const killTimer = setTimeout(() => {
|
|
71
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
72
|
+
child.kill('SIGKILL');
|
|
73
|
+
}, 2000);
|
|
74
|
+
killTimer.unref();
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export { startPerfWatch };
|