@stateward/core 1.0.1 → 1.2.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/dist/backend/fingerprints.d.ts +22 -2
- package/dist/backend/fingerprints.js +160 -18
- package/dist/backend/program-record.d.ts +1 -1
- package/dist/backend/qualified-ids.d.ts +6 -0
- package/dist/backend/qualified-ids.js +66 -10
- package/dist/backend/typescript6.d.ts +28 -9
- package/dist/backend/typescript6.js +564 -28
- package/dist/baseline.js +10 -0
- package/dist/cache.d.ts +5 -4
- package/dist/cache.js +2 -2
- package/dist/checker.d.ts +14 -2
- package/dist/checker.js +40 -12
- package/dist/config-file.d.ts +6 -0
- package/dist/config-file.js +21 -2
- package/dist/config.d.ts +3 -1
- package/dist/config.js +164 -17
- package/dist/define.d.ts +2 -1
- package/dist/disk-cache.d.ts +24 -2
- package/dist/disk-cache.js +43 -5
- package/dist/engine.d.ts +27 -3
- package/dist/engine.js +224 -57
- package/dist/guarantee-markers.d.ts +10 -0
- package/dist/guarantee-markers.js +66 -0
- package/dist/guarantees.d.ts +44 -0
- package/dist/guarantees.js +84 -0
- package/dist/hint-lines.d.ts +24 -0
- package/dist/hint-lines.js +36 -0
- package/dist/ignores.d.ts +2 -4
- package/dist/ignores.js +31 -16
- package/dist/implementation.js +2 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +4 -0
- package/dist/internal.d.ts +8 -3
- package/dist/internal.js +6 -3
- package/dist/package-facts.d.ts +52 -0
- package/dist/package-facts.js +213 -0
- package/dist/plan.d.ts +17 -0
- package/dist/plan.js +22 -5
- package/dist/release.d.ts +68 -0
- package/dist/release.js +172 -0
- package/dist/renamed.d.ts +29 -0
- package/dist/renamed.js +48 -0
- package/dist/rollout.js +3 -1
- package/dist/testing-tsc.d.ts +11 -0
- package/dist/testing-tsc.js +125 -0
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +61 -8
- package/dist/types.d.ts +261 -2
- package/package.json +1 -1
package/dist/baseline.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { limitFindings } from "./engine.js";
|
|
5
|
+
import { renamedRule } from "./renamed.js";
|
|
5
6
|
export const BASELINE_FILE = "stateward.baseline.json";
|
|
6
7
|
/**
|
|
7
8
|
* Version 2 fingerprints a finding by its rule, file and code only, so rewording a message never
|
|
@@ -246,6 +247,15 @@ function parseEntry(value, file, version) {
|
|
|
246
247
|
entryFile,
|
|
247
248
|
typeof fingerprint === "string" ? fingerprint : "",
|
|
248
249
|
]);
|
|
250
|
+
// An entry of a renamed rule: its fingerprint hashed the old id, so it is matched by rule and
|
|
251
|
+
// file, under the new id, until the next `baseline` writes it again.
|
|
252
|
+
const renamed = renamedRule(ruleId);
|
|
253
|
+
if (renamed !== undefined) {
|
|
254
|
+
return {
|
|
255
|
+
entry: { kind: "legacy", ruleId: renamed, file: entryFile, message, count },
|
|
256
|
+
key: raw,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
249
259
|
if (!old && typeof fingerprint === "string") {
|
|
250
260
|
return {
|
|
251
261
|
entry: { kind: "fingerprint", ruleId, file: entryFile, fingerprint, message, count },
|
package/dist/cache.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { Finding, FixVerdict, Json, Rule, Severity } from "./types.js";
|
|
|
5
5
|
*
|
|
6
6
|
* - per own file, under its dependency fingerprint (backend/fingerprints.ts), and per rule under
|
|
7
7
|
* its rule key (`ruleKey`): the findings the rule reported on that file, and a project rule's
|
|
8
|
-
* summary; with the
|
|
8
|
+
* summary; with the hash of each declaration (or whole file) of another file the rules read there. A
|
|
9
9
|
* file whose fingerprint and reads are unchanged is not looked at again.
|
|
10
10
|
* - per project rule, under its rule key: a hash of all its summaries, and the findings its check
|
|
11
11
|
* produced from them. Equal summaries give equal findings (a project rule decides from its
|
|
@@ -16,7 +16,7 @@ import type { Finding, FixVerdict, Json, Rule, Severity } from "./types.js";
|
|
|
16
16
|
* server save it to disk between runs as a `CacheSnapshot` (disk-cache.ts).
|
|
17
17
|
*/
|
|
18
18
|
/** Bumped whenever what is stored, or how a key is computed, changes. */
|
|
19
|
-
export declare const CACHE_FORMAT =
|
|
19
|
+
export declare const CACHE_FORMAT = 7;
|
|
20
20
|
/**
|
|
21
21
|
* A project rule's summary of one file: the hash of its JSON text, and either that text or, once a
|
|
22
22
|
* rule or a hover needed the value (a run whose summaries are all unchanged never does), the parsed
|
|
@@ -56,8 +56,9 @@ export type FileEntry = {
|
|
|
56
56
|
fingerprint: string;
|
|
57
57
|
rules: ReadonlyMap<string, RuleResult>;
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* What the rules on this file read of other files (`TypeQueries.declarationOf`), by read id (a
|
|
60
|
+
* declaration's qualified id, or a file's own id `src/a.ts#` for its whole text:
|
|
61
|
+
* `ProjectUnit.readsOf`), with each one's hash then: the entry holds while each is the same.
|
|
61
62
|
*/
|
|
62
63
|
reads: ReadonlyMap<string, string>;
|
|
63
64
|
};
|
package/dist/cache.js
CHANGED
|
@@ -5,7 +5,7 @@ import { isQueryKind } from "./backend/program-record.js";
|
|
|
5
5
|
*
|
|
6
6
|
* - per own file, under its dependency fingerprint (backend/fingerprints.ts), and per rule under
|
|
7
7
|
* its rule key (`ruleKey`): the findings the rule reported on that file, and a project rule's
|
|
8
|
-
* summary; with the
|
|
8
|
+
* summary; with the hash of each declaration (or whole file) of another file the rules read there. A
|
|
9
9
|
* file whose fingerprint and reads are unchanged is not looked at again.
|
|
10
10
|
* - per project rule, under its rule key: a hash of all its summaries, and the findings its check
|
|
11
11
|
* produced from them. Equal summaries give equal findings (a project rule decides from its
|
|
@@ -16,7 +16,7 @@ import { isQueryKind } from "./backend/program-record.js";
|
|
|
16
16
|
* server save it to disk between runs as a `CacheSnapshot` (disk-cache.ts).
|
|
17
17
|
*/
|
|
18
18
|
/** Bumped whenever what is stored, or how a key is computed, changes. */
|
|
19
|
-
export const CACHE_FORMAT =
|
|
19
|
+
export const CACHE_FORMAT = 7;
|
|
20
20
|
/** Frozen all the way down: a cached summary is shared between runs, so no rule may change it. */
|
|
21
21
|
function deepFreeze(value) {
|
|
22
22
|
if (typeof value === "object" && value !== null) {
|
package/dist/checker.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ResultCache } from "./cache.js";
|
|
2
2
|
import type { TimingRecorder } from "./timing.js";
|
|
3
3
|
import type { CheckProgress } from "./engine.js";
|
|
4
|
-
import type { CheckResult, Config, EditorAction, EditorHint, Finding, FixForm, FixReview, FixSetReview, LibraryUse, Location, RuleSelection, TokenType } from "./types.js";
|
|
4
|
+
import type { CheckResult, Config, EditorAction, EditorHint, Finding, FixForm, FixReview, FixSetReview, LibraryUse, Location, StatedGuarantee, RuleSelection, TokenType } from "./types.js";
|
|
5
5
|
export type CheckInput = {
|
|
6
6
|
config: Config;
|
|
7
7
|
cwd: string;
|
|
@@ -65,8 +65,14 @@ export type Checker = {
|
|
|
65
65
|
* The fast pass: runs only the file rules, only on `files` (own files; others are ignored), on
|
|
66
66
|
* programs that reuse the previous ones. Findings are for those files only; project rules do not
|
|
67
67
|
* run, so their findings are the last `check`'s.
|
|
68
|
+
*
|
|
69
|
+
* The rules run on each file, unless it is in `reusable` and the checker's cache holds its
|
|
70
|
+
* results under its current dependency fingerprint and reads (as a full check would reuse them):
|
|
71
|
+
* then they are taken from there. The fast pass never writes the cache. Name only files that are
|
|
72
|
+
* likely unchanged (an editor's document just opened): the first such question computes the
|
|
73
|
+
* fingerprints of the whole program, which the next full check reuses.
|
|
68
74
|
*/
|
|
69
|
-
checkFiles(files: readonly string[], overlays?: ReadonlyMap<string, string
|
|
75
|
+
checkFiles(files: readonly string[], overlays?: ReadonlyMap<string, string>, reusable?: readonly string[]): CheckResult;
|
|
70
76
|
/** Ids of the active project rules, whose findings only `check` produces. */
|
|
71
77
|
readonly projectRuleIds: ReadonlySet<string>;
|
|
72
78
|
/**
|
|
@@ -95,6 +101,12 @@ export type Checker = {
|
|
|
95
101
|
* from the last full check: `stateward deps`. Empty before a check.
|
|
96
102
|
*/
|
|
97
103
|
libraryFacts(): readonly LibraryUse[];
|
|
104
|
+
/**
|
|
105
|
+
* The guarantees stated in the project, as the plugins that prove them read them
|
|
106
|
+
* (`EditorContributions.guarantees`), from the last full check: no new type checking. Empty
|
|
107
|
+
* before a check. Pair them with that check's findings with `proveGuarantees`.
|
|
108
|
+
*/
|
|
109
|
+
guarantees(): readonly StatedGuarantee[];
|
|
98
110
|
/**
|
|
99
111
|
* Tries each fix of `findings` on a copy of the code of the last run (fixes.ts) and returns the
|
|
100
112
|
* findings in the same order, each with only the fixes that compile. Verdicts are kept in the
|
package/dist/checker.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createBackend, toTypeScriptPath } from "./backend/typescript6.js";
|
|
2
2
|
import { emptyCache } from "./cache.js";
|
|
3
3
|
import { reviewFixes, verifyFixSet } from "./fixes.js";
|
|
4
|
+
import { legacyMarkerRewrite } from "./guarantee-markers.js";
|
|
4
5
|
import { drain, filePhaseRun, needsJsDoc, projectPhaseSteps, projectRuleIds, runFileRules, silentReasons, } from "./engine.js";
|
|
5
6
|
/** The default slice: short against the editor's 200 ms budget, long against a pause's cost. */
|
|
6
7
|
export const SLICE_MS = 20;
|
|
@@ -109,12 +110,14 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
|
|
|
109
110
|
sliceStarted = performance.now();
|
|
110
111
|
}
|
|
111
112
|
},
|
|
112
|
-
checkFiles(files, overlays = new Map()) {
|
|
113
|
+
checkFiles(files, overlays = new Map(), reusable = []) {
|
|
113
114
|
const started = performance.now();
|
|
114
115
|
const units = backend.reopen(overlays);
|
|
115
116
|
lastUnits = units;
|
|
116
117
|
known = undefined;
|
|
117
|
-
return runFileRules(config, cwd, units, new Set(files.map(toTypeScriptPath)), started, uncapped === undefined, ignores !== false
|
|
118
|
+
return runFileRules(config, cwd, units, new Set(files.map(toTypeScriptPath)), started, uncapped === undefined, ignores !== false, cache === false || reusable.length === 0
|
|
119
|
+
? undefined
|
|
120
|
+
: { cache, files: new Set(reusable.map(toTypeScriptPath)) });
|
|
118
121
|
},
|
|
119
122
|
// Read on demand: it consults the rollout file, which a run that ignores rollout
|
|
120
123
|
// (`plan`, `check --rule`) must not touch.
|
|
@@ -143,6 +146,11 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
|
|
|
143
146
|
projectSummaries: (ruleId) => summaries.get(ruleId)?.(),
|
|
144
147
|
}) ?? []);
|
|
145
148
|
},
|
|
149
|
+
guarantees() {
|
|
150
|
+
return config.plugins.flatMap((plugin) => plugin.editor?.guarantees?.({
|
|
151
|
+
projectSummaries: (ruleId) => summaries.get(ruleId)?.(),
|
|
152
|
+
}) ?? []);
|
|
153
|
+
},
|
|
146
154
|
reviewFixes(findings) {
|
|
147
155
|
const units = (lastUnits ??= backend.open(new Map()));
|
|
148
156
|
return reviewFixes(findings, units, cache === false ? localVerdicts : cache);
|
|
@@ -163,13 +171,18 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
|
|
|
163
171
|
const found = lookup(file);
|
|
164
172
|
if (found === undefined)
|
|
165
173
|
return [];
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
174
|
+
const rewrite = legacyMarkerRewrite(found.sourceFile, start, end);
|
|
175
|
+
const offered = [
|
|
176
|
+
...config.plugins.flatMap((plugin) => plugin.editor?.codeActions?.({
|
|
177
|
+
file,
|
|
178
|
+
start,
|
|
179
|
+
end,
|
|
180
|
+
...found,
|
|
181
|
+
projectSummaries: (ruleId) => summaries.get(ruleId)?.(),
|
|
182
|
+
}) ?? []),
|
|
183
|
+
// Core reads both marker forms, so it offers the rewrite itself: the markers are no pack's.
|
|
184
|
+
...(rewrite === undefined ? [] : [rewrite]),
|
|
185
|
+
];
|
|
173
186
|
// A finding only to reuse the fix review: it is never reported.
|
|
174
187
|
const findings = offered.map((action) => ({
|
|
175
188
|
kind: "problem",
|
|
@@ -179,13 +192,24 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
|
|
|
179
192
|
hint: action.title,
|
|
180
193
|
location: { file, start, end },
|
|
181
194
|
related: [],
|
|
182
|
-
fixes: [
|
|
195
|
+
fixes: [
|
|
196
|
+
{
|
|
197
|
+
title: action.title,
|
|
198
|
+
edits: action.edits,
|
|
199
|
+
alternatives: (action.alternatives ?? []).map((form) => ({
|
|
200
|
+
title: action.title,
|
|
201
|
+
edits: form.edits,
|
|
202
|
+
})),
|
|
203
|
+
},
|
|
204
|
+
],
|
|
183
205
|
}));
|
|
184
206
|
const units = (lastUnits ??= backend.open(new Map()));
|
|
185
207
|
const reviews = reviewFixes(findings, units, cache === false ? localVerdicts : cache);
|
|
186
|
-
|
|
208
|
+
// Each action as the first of its forms that compiles.
|
|
209
|
+
return offered.flatMap((action, i) => {
|
|
187
210
|
const reviewed = reviews[i]?.finding;
|
|
188
|
-
|
|
211
|
+
const form = reviewed?.kind === "problem" ? reviewed.fixes[0] : undefined;
|
|
212
|
+
return form === undefined ? [] : [{ title: action.title, edits: form.edits }];
|
|
189
213
|
});
|
|
190
214
|
},
|
|
191
215
|
inlayHints(file, enabled = () => true) {
|
|
@@ -202,6 +226,10 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
|
|
|
202
226
|
.sort((a, b) => a.offset - b.offset || (a.label < b.label ? -1 : a.label > b.label ? 1 : 0));
|
|
203
227
|
},
|
|
204
228
|
tokens(file) {
|
|
229
|
+
// No plugin colours anything: answered without building or binding a program (an editor
|
|
230
|
+
// asks for tokens as soon as a file opens, before the first check has built one).
|
|
231
|
+
if (!config.plugins.some((plugin) => plugin.editor?.tokens !== undefined))
|
|
232
|
+
return [];
|
|
205
233
|
const found = lookup(file);
|
|
206
234
|
if (found === undefined)
|
|
207
235
|
return [];
|
package/dist/config-file.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { Config, ParseResult } from "./types.js";
|
|
2
2
|
/** The path of the config file in `cwd`, if there is one. */
|
|
3
3
|
export declare function findConfigFile(cwd: string): string | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* One warning per other config file in the folder of `configFile`, when `configFile` is the one
|
|
6
|
+
* the search picks (`CONFIG_FILE_NAMES` order): the others are never read. A config named with
|
|
7
|
+
* `--config` is a choice, and gets none.
|
|
8
|
+
*/
|
|
9
|
+
export declare function ignoredConfigWarnings(configFile: string): readonly string[];
|
|
4
10
|
/**
|
|
5
11
|
* What went wrong while loading the config file, and what to do: a package it imports that is not
|
|
6
12
|
* installed, or a syntax error, each in one or two lines; any other failure keeps its own message.
|
package/dist/config-file.js
CHANGED
|
@@ -7,12 +7,25 @@ import { MessageChannel } from "node:worker_threads";
|
|
|
7
7
|
import { createJiti } from "jiti";
|
|
8
8
|
import { configLocalImports, foreignTypeScriptWarnings } from "./implementation.js";
|
|
9
9
|
import { parseConfig } from "./config.js";
|
|
10
|
+
import { renamedIdsIn } from "./renamed.js";
|
|
10
11
|
import { RuleDeclarationError, RuleLoadError, loadRules } from "./lazy.js";
|
|
11
12
|
import { CONFIG_FILE_NAMES, CONFIG_FILE_PATTERN } from "./vocabulary.js";
|
|
12
13
|
/** The path of the config file in `cwd`, if there is one. */
|
|
13
14
|
export function findConfigFile(cwd) {
|
|
14
15
|
return CONFIG_FILE_NAMES.map((name) => path.join(cwd, name)).find((file) => existsSync(file));
|
|
15
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* One warning per other config file in the folder of `configFile`, when `configFile` is the one
|
|
19
|
+
* the search picks (`CONFIG_FILE_NAMES` order): the others are never read. A config named with
|
|
20
|
+
* `--config` is a choice, and gets none.
|
|
21
|
+
*/
|
|
22
|
+
export function ignoredConfigWarnings(configFile) {
|
|
23
|
+
const dir = path.dirname(configFile);
|
|
24
|
+
if (findConfigFile(dir) !== configFile)
|
|
25
|
+
return [];
|
|
26
|
+
const used = path.basename(configFile);
|
|
27
|
+
return CONFIG_FILE_NAMES.filter((name) => name !== used && existsSync(path.join(dir, name))).map((name) => `\`${name}\` is ignored: \`${used}\` in the same folder is the config used; delete the one not wanted`);
|
|
28
|
+
}
|
|
16
29
|
/**
|
|
17
30
|
* What went wrong while loading the config file, and what to do: a package it imports that is not
|
|
18
31
|
* installed, or a syntax error, each in one or two lines; any other failure keeps its own message.
|
|
@@ -203,7 +216,9 @@ export async function loadConfigFile(configFile) {
|
|
|
203
216
|
errors: [`${name} must export the config: export default defineConfig({ ... })`],
|
|
204
217
|
};
|
|
205
218
|
}
|
|
206
|
-
|
|
219
|
+
// The first line that names each old rule id, for the notice that says where to rename it.
|
|
220
|
+
const places = renamedIdsIn(readFileSync(configFile, "utf8"));
|
|
221
|
+
const parsed = parseConfig(exported, path.dirname(configFile), path.basename(configFile), (id) => places.find((place) => place.before === id)?.line);
|
|
207
222
|
if (!parsed.ok)
|
|
208
223
|
return { ok: false, errors: parsed.errors.map((e) => `${configFile}: ${e}`) };
|
|
209
224
|
// The rules that are on are loaded now, so a check can run them; the rest never load.
|
|
@@ -216,7 +231,11 @@ export async function loadConfigFile(configFile) {
|
|
|
216
231
|
}
|
|
217
232
|
throw error;
|
|
218
233
|
}
|
|
219
|
-
const warnings = [
|
|
234
|
+
const warnings = [
|
|
235
|
+
...ignoredConfigWarnings(configFile),
|
|
236
|
+
...parsed.value.warnings,
|
|
237
|
+
...foreignTypeScriptWarnings(configFile),
|
|
238
|
+
];
|
|
220
239
|
return { ok: true, value: { ...parsed.value, warnings, configFile } };
|
|
221
240
|
}
|
|
222
241
|
//# sourceMappingURL=config-file.js.map
|
package/dist/config.d.ts
CHANGED
|
@@ -19,5 +19,7 @@ export declare function isMaxMemory(value: unknown): value is number;
|
|
|
19
19
|
* Validates untrusted config input once. `tsconfig` is resolved against `configDir`, so the
|
|
20
20
|
* returned value holds absolute paths (and absolute globs).
|
|
21
21
|
*/
|
|
22
|
-
export declare function parseConfig(given: unknown, configDir: string
|
|
22
|
+
export declare function parseConfig(given: unknown, configDir: string, configName?: string,
|
|
23
|
+
/** The line the config file names an old rule id on, so the notice says where to rename it. */
|
|
24
|
+
lineOf?: (id: string) => number | undefined): ParseResult<Config>;
|
|
23
25
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import { movedNotice, renameNotice, renamedRule } from "./renamed.js";
|
|
3
|
+
import { addonNames, groupIds, ruleIds } from "./severity.js";
|
|
3
4
|
import { PRESETS, RULE_TIERS, orList } from "./vocabulary.js";
|
|
4
5
|
import { defaultOptions, optionProblem, parseRuleOptions } from "./options.js";
|
|
5
6
|
const DEFAULT_TSCONFIG = "tsconfig.json";
|
|
@@ -78,6 +79,7 @@ const isSummarize = (value) => typeof value === "function";
|
|
|
78
79
|
const isSilentReason = (value) => typeof value === "function";
|
|
79
80
|
const isHover = (value) => typeof value === "function";
|
|
80
81
|
const isInlayHints = (value) => typeof value === "function";
|
|
82
|
+
const isGuarantees = (value) => typeof value === "function";
|
|
81
83
|
const isCodeActions = (value) => typeof value === "function";
|
|
82
84
|
const isTokens = (value) => typeof value === "function";
|
|
83
85
|
const isLibraryFacts = (value) => typeof value === "function";
|
|
@@ -200,6 +202,19 @@ function parseMeta(value, where, errors) {
|
|
|
200
202
|
return undefined;
|
|
201
203
|
return { ...info, explain: value.explain };
|
|
202
204
|
}
|
|
205
|
+
/** `RuleInfo.needsTypes` as given: undefined when left out, "invalid" when not a `LibraryTypes`. */
|
|
206
|
+
function parseNeedsTypes(value) {
|
|
207
|
+
if (value === undefined)
|
|
208
|
+
return undefined;
|
|
209
|
+
if (!isRecord(value))
|
|
210
|
+
return "invalid";
|
|
211
|
+
const { library, modules } = value;
|
|
212
|
+
if (typeof library !== "string" || library === "")
|
|
213
|
+
return "invalid";
|
|
214
|
+
if (!isStringArray(modules) || modules.length === 0)
|
|
215
|
+
return "invalid";
|
|
216
|
+
return { library, modules: [...modules] };
|
|
217
|
+
}
|
|
203
218
|
/** The metadata a pack declares up front (`RuleInfo`): everything but the `explain` text. */
|
|
204
219
|
function parseInfo(value, where, errors) {
|
|
205
220
|
const found = parseInfoFields(value, where, errors);
|
|
@@ -211,12 +226,29 @@ function parseInfo(value, where, errors) {
|
|
|
211
226
|
errors.push(`${where}.meta.sharedOptions must map option names to rule keys of the same pack`);
|
|
212
227
|
return undefined;
|
|
213
228
|
}
|
|
214
|
-
const
|
|
229
|
+
const sharing = shared === undefined
|
|
215
230
|
? found
|
|
216
231
|
: {
|
|
217
232
|
...found,
|
|
218
233
|
sharedOptions: Object.fromEntries(Object.entries(shared).map(([name, rule]) => [name, String(rule)])),
|
|
219
234
|
};
|
|
235
|
+
const { fixNeedsStrictNulls } = value;
|
|
236
|
+
const needsTypes = parseNeedsTypes(value.needsTypes);
|
|
237
|
+
if (needsTypes === "invalid") {
|
|
238
|
+
errors.push(`${where}.meta.needsTypes must be { library: "<name>", modules: ["<module>", …] } when present`);
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
if (fixNeedsStrictNulls !== undefined && fixNeedsStrictNulls !== true) {
|
|
242
|
+
errors.push(`${where}.meta.fixNeedsStrictNulls must be true when present`);
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
const typed = needsTypes === undefined
|
|
246
|
+
? sharing
|
|
247
|
+
: {
|
|
248
|
+
...sharing,
|
|
249
|
+
needsTypes,
|
|
250
|
+
};
|
|
251
|
+
const info = fixNeedsStrictNulls === true ? { ...typed, fixNeedsStrictNulls: true } : typed;
|
|
220
252
|
if (value.options === undefined)
|
|
221
253
|
return info;
|
|
222
254
|
const options = parseOptionSpecs(value.options, where, errors);
|
|
@@ -227,12 +259,16 @@ function parseInfoFields(value, where, errors) {
|
|
|
227
259
|
errors.push(`${where}.meta must be an object`);
|
|
228
260
|
return undefined;
|
|
229
261
|
}
|
|
230
|
-
const { description, defaultSeverity, tier, group, addon, heuristic, widenedBy } = value;
|
|
262
|
+
const { description, summary, defaultSeverity, tier, group, addon, heuristic, widenedBy } = value;
|
|
231
263
|
const { needsJsDoc, listUnknowns, yieldsTo } = value;
|
|
232
264
|
if (typeof description !== "string") {
|
|
233
265
|
errors.push(`${where}.meta.description must be a string`);
|
|
234
266
|
return undefined;
|
|
235
267
|
}
|
|
268
|
+
if (summary !== undefined && typeof summary !== "string") {
|
|
269
|
+
errors.push(`${where}.meta.summary must be a string when present`);
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
236
272
|
if (!isSeverity(defaultSeverity)) {
|
|
237
273
|
errors.push(`${where}.meta.defaultSeverity must be "error", "warning" or "info"`);
|
|
238
274
|
return undefined;
|
|
@@ -257,7 +293,8 @@ function parseInfoFields(value, where, errors) {
|
|
|
257
293
|
errors.push(`${where}.meta.group must name the rule's group in kebab-case, e.g. "switches"`);
|
|
258
294
|
return undefined;
|
|
259
295
|
}
|
|
260
|
-
const
|
|
296
|
+
const described = { description, defaultSeverity, tier, group };
|
|
297
|
+
const base = summary === undefined ? described : { ...described, summary };
|
|
261
298
|
const guessed = heuristic === true ? { ...base, heuristic: true } : base;
|
|
262
299
|
const reading = needsJsDoc === true ? { ...guessed, needsJsDoc: true } : guessed;
|
|
263
300
|
const listing = listUnknowns === true ? { ...reading, listUnknowns: true } : reading;
|
|
@@ -329,7 +366,7 @@ function parseEditor(value, where, errors) {
|
|
|
329
366
|
errors.push(`${where}.editor must be an object`);
|
|
330
367
|
return undefined;
|
|
331
368
|
}
|
|
332
|
-
const { hover, tokens, libraryFacts, inlayHints, codeActions } = value;
|
|
369
|
+
const { hover, tokens, libraryFacts, inlayHints, codeActions, guarantees } = value;
|
|
333
370
|
const editor = {};
|
|
334
371
|
if (codeActions !== undefined) {
|
|
335
372
|
if (!isCodeActions(codeActions)) {
|
|
@@ -352,6 +389,13 @@ function parseEditor(value, where, errors) {
|
|
|
352
389
|
}
|
|
353
390
|
editor.libraryFacts = libraryFacts;
|
|
354
391
|
}
|
|
392
|
+
if (guarantees !== undefined) {
|
|
393
|
+
if (!isGuarantees(guarantees)) {
|
|
394
|
+
errors.push(`${where}.editor.guarantees must be a function`);
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
editor.guarantees = guarantees;
|
|
398
|
+
}
|
|
355
399
|
if (hover !== undefined) {
|
|
356
400
|
if (!isHover(hover)) {
|
|
357
401
|
errors.push(`${where}.editor.hover must be a function`);
|
|
@@ -373,7 +417,7 @@ function parsePlugin(value, index, errors) {
|
|
|
373
417
|
errors.push(`plugins[${String(index)}] must be an object`);
|
|
374
418
|
return undefined;
|
|
375
419
|
}
|
|
376
|
-
const { name, description, rules, editor } = value;
|
|
420
|
+
const { name, description, rules, editor, facts } = value;
|
|
377
421
|
if (typeof name !== "string" || name === "" || name.includes("/")) {
|
|
378
422
|
errors.push(`plugins[${String(index)}].name must be a non-empty string without "/"`);
|
|
379
423
|
return undefined;
|
|
@@ -398,12 +442,28 @@ function parsePlugin(value, index, errors) {
|
|
|
398
442
|
}
|
|
399
443
|
if (!valid)
|
|
400
444
|
return undefined;
|
|
445
|
+
const shipped = shippedFacts(facts, where, errors);
|
|
446
|
+
if (shipped === null)
|
|
447
|
+
return undefined;
|
|
448
|
+
const parsed = {
|
|
449
|
+
name,
|
|
450
|
+
description,
|
|
451
|
+
rules: parsedRules,
|
|
452
|
+
...(shipped === undefined ? {} : { facts: shipped }),
|
|
453
|
+
};
|
|
401
454
|
if (editor === undefined)
|
|
402
|
-
return
|
|
455
|
+
return parsed;
|
|
403
456
|
const parsedEditor = parseEditor(editor, where, errors);
|
|
404
|
-
return parsedEditor === undefined
|
|
405
|
-
|
|
406
|
-
|
|
457
|
+
return parsedEditor === undefined ? undefined : { ...parsed, editor: parsedEditor };
|
|
458
|
+
}
|
|
459
|
+
/** A plugin's own facts (`Plugin.facts`), checked as the config's are; null when one is malformed. */
|
|
460
|
+
function shippedFacts(value, where, errors) {
|
|
461
|
+
if (value === undefined)
|
|
462
|
+
return undefined;
|
|
463
|
+
const found = [];
|
|
464
|
+
const facts = parseFacts(value, found);
|
|
465
|
+
errors.push(...found.map((error) => `${where}: ${error}`));
|
|
466
|
+
return found.length > 0 ? null : facts;
|
|
407
467
|
}
|
|
408
468
|
function parsePlugins(value, errors) {
|
|
409
469
|
if (!Array.isArray(value)) {
|
|
@@ -435,6 +495,11 @@ function splitRuleConfig(value) {
|
|
|
435
495
|
}
|
|
436
496
|
return undefined;
|
|
437
497
|
}
|
|
498
|
+
/** A rule id no loaded plugin has (a typo, or a rule a release removed), with the ones they have. */
|
|
499
|
+
function unknownRuleId(ruleId, known) {
|
|
500
|
+
const listed = [...known].sort().join(", ") || "none";
|
|
501
|
+
return `unknown rule id "${ruleId}"; rules of the loaded plugins: ${listed}`;
|
|
502
|
+
}
|
|
438
503
|
function parseRuleSettings(value, plugins, errors) {
|
|
439
504
|
if (!isRecord(value)) {
|
|
440
505
|
errors.push("rules must be an object");
|
|
@@ -446,7 +511,7 @@ function parseRuleSettings(value, plugins, errors) {
|
|
|
446
511
|
for (const [ruleId, entry] of Object.entries(value)) {
|
|
447
512
|
const split = splitRuleConfig(entry);
|
|
448
513
|
if (!known.has(ruleId))
|
|
449
|
-
errors.push(`rules:
|
|
514
|
+
errors.push(`rules: ${unknownRuleId(ruleId, known.keys())}`);
|
|
450
515
|
else if (split === undefined) {
|
|
451
516
|
errors.push(`rules["${ruleId}"] must be "error", "warning", "info", "off", or [severity, { options }]`);
|
|
452
517
|
}
|
|
@@ -527,7 +592,26 @@ function parseAddons(value, plugins, errors, name = "addons") {
|
|
|
527
592
|
}
|
|
528
593
|
return listed;
|
|
529
594
|
}
|
|
530
|
-
const FACT_KINDS = ["throws", "rejects"];
|
|
595
|
+
const FACT_KINDS = ["throws", "rejects", "returns", "neverRejects"];
|
|
596
|
+
const RETURNS_SHAPE = '"release" (call the returned function) or { release: "<method>" }';
|
|
597
|
+
/** `returns: "release"` or `returns: { release: "unsubscribe" }`: the release trait of a package function. */
|
|
598
|
+
function parseReturns(value, where, errors) {
|
|
599
|
+
if (value === "release")
|
|
600
|
+
return value;
|
|
601
|
+
if (isRecord(value)) {
|
|
602
|
+
const keys = Object.keys(value);
|
|
603
|
+
const method = value.release;
|
|
604
|
+
if (keys.length === 1 && typeof method === "string" && /^[A-Za-z_$][\w$]*$/.test(method)) {
|
|
605
|
+
return { release: method };
|
|
606
|
+
}
|
|
607
|
+
if (keys.length === 1 && typeof method === "string") {
|
|
608
|
+
errors.push(`${where}.returns.release must be a method name, such as "unsubscribe"`);
|
|
609
|
+
return undefined;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
errors.push(`${where}.returns must be ${RETURNS_SHAPE}`);
|
|
613
|
+
return undefined;
|
|
614
|
+
}
|
|
531
615
|
/** `facts: { axios: { get: { throws: ["AxiosError"] } } }`. */
|
|
532
616
|
function parseFacts(value, errors) {
|
|
533
617
|
if (value === undefined)
|
|
@@ -546,7 +630,7 @@ function parseFacts(value, errors) {
|
|
|
546
630
|
for (const [fn, entry] of Object.entries(functions)) {
|
|
547
631
|
const where = `facts["${name}"]["${fn}"]`;
|
|
548
632
|
if (!isRecord(entry)) {
|
|
549
|
-
errors.push(`${where} must be an object: { throws: [...], rejects: [...] }`);
|
|
633
|
+
errors.push(`${where} must be an object: { throws: [...], rejects: [...], returns: "release", neverRejects: true }`);
|
|
550
634
|
continue;
|
|
551
635
|
}
|
|
552
636
|
const one = {};
|
|
@@ -554,6 +638,17 @@ function parseFacts(value, errors) {
|
|
|
554
638
|
if (!FACT_KINDS.includes(kind)) {
|
|
555
639
|
errors.push(`${where}: unknown fact "${kind}"; facts are ${orList(FACT_KINDS)}`);
|
|
556
640
|
}
|
|
641
|
+
else if (kind === "neverRejects") {
|
|
642
|
+
if (list === true)
|
|
643
|
+
one.neverRejects = true;
|
|
644
|
+
else
|
|
645
|
+
errors.push(`${where}.neverRejects must be true (leave it out when it can reject)`);
|
|
646
|
+
}
|
|
647
|
+
else if (kind === "returns") {
|
|
648
|
+
const returns = parseReturns(list, where, errors);
|
|
649
|
+
if (returns !== undefined)
|
|
650
|
+
one.returns = returns;
|
|
651
|
+
}
|
|
557
652
|
else if (!isStringArray(list)) {
|
|
558
653
|
errors.push(`${where}.${kind} must be an array of class names`);
|
|
559
654
|
}
|
|
@@ -564,6 +659,9 @@ function parseFacts(value, errors) {
|
|
|
564
659
|
one.rejects = list;
|
|
565
660
|
}
|
|
566
661
|
}
|
|
662
|
+
if (one.neverRejects === true && (one.rejects ?? []).length > 0) {
|
|
663
|
+
errors.push(`${where}: neverRejects and a non-empty rejects list contradict each other`);
|
|
664
|
+
}
|
|
567
665
|
parsed[fn] = one;
|
|
568
666
|
}
|
|
569
667
|
facts[name] = parsed;
|
|
@@ -618,7 +716,7 @@ function parseOverrideRules(value, plugins, errors, name) {
|
|
|
618
716
|
const settings = {};
|
|
619
717
|
for (const [ruleId, entry] of Object.entries(value)) {
|
|
620
718
|
if (!known.has(ruleId)) {
|
|
621
|
-
errors.push(`${name}:
|
|
719
|
+
errors.push(`${name}: ${unknownRuleId(ruleId, known.keys())}`);
|
|
622
720
|
}
|
|
623
721
|
else if (Array.isArray(entry)) {
|
|
624
722
|
errors.push(`${name}["${ruleId}"] takes a severity or "off", not options; set a rule's options once, in the config's own rules`);
|
|
@@ -884,6 +982,48 @@ function mergeChain(chain, errors) {
|
|
|
884
982
|
merged.ignores = ignores;
|
|
885
983
|
return merged;
|
|
886
984
|
}
|
|
985
|
+
/**
|
|
986
|
+
* An object keyed by rule ids with each renamed id written as it is now (`renamed.ts`), unless a
|
|
987
|
+
* loaded plugin still has the old id; an entry under the new id wins over the old one. An entry
|
|
988
|
+
* whose rule moved to a pack no loaded plugin is is dropped. `notices` gets one line per old id
|
|
989
|
+
* found.
|
|
990
|
+
*/
|
|
991
|
+
function migratedRuleKeys(value, current, notices, where) {
|
|
992
|
+
if (!isRecord(value))
|
|
993
|
+
return value;
|
|
994
|
+
const renamed = {};
|
|
995
|
+
for (const [id, entry] of Object.entries(value)) {
|
|
996
|
+
const now = current.has(id) ? undefined : renamedRule(id);
|
|
997
|
+
if (now === undefined) {
|
|
998
|
+
renamed[id] = entry;
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
if (!current.has(now)) {
|
|
1002
|
+
// The rule moved to a pack this config does not load: the entry applies to nothing.
|
|
1003
|
+
notices.add(movedNotice(id, now, where(id)));
|
|
1004
|
+
continue;
|
|
1005
|
+
}
|
|
1006
|
+
notices.add(renameNotice(id, now, where(id)));
|
|
1007
|
+
if (!(now in value))
|
|
1008
|
+
renamed[now] = entry;
|
|
1009
|
+
}
|
|
1010
|
+
return renamed;
|
|
1011
|
+
}
|
|
1012
|
+
/** The config with renamed rule ids written as they are now, in `rules` and in each override's. */
|
|
1013
|
+
function migrateRuleIds(input, current, notices, where) {
|
|
1014
|
+
const migrated = { ...input };
|
|
1015
|
+
if (input.rules !== undefined) {
|
|
1016
|
+
migrated.rules = migratedRuleKeys(input.rules, current, notices, where);
|
|
1017
|
+
}
|
|
1018
|
+
const { overrides } = input;
|
|
1019
|
+
if (Array.isArray(overrides)) {
|
|
1020
|
+
const list = overrides;
|
|
1021
|
+
migrated.overrides = list.map((item) => isRecord(item) && item.rules !== undefined
|
|
1022
|
+
? { ...item, rules: migratedRuleKeys(item.rules, current, notices, where) }
|
|
1023
|
+
: item);
|
|
1024
|
+
}
|
|
1025
|
+
return migrated;
|
|
1026
|
+
}
|
|
887
1027
|
/** `tsconfig`: one path, or a non-empty list of paths and globs; undefined after an error. */
|
|
888
1028
|
function parseTsconfig(value, errors) {
|
|
889
1029
|
if (value === undefined)
|
|
@@ -903,14 +1043,21 @@ function parseTsconfig(value, errors) {
|
|
|
903
1043
|
* Validates untrusted config input once. `tsconfig` is resolved against `configDir`, so the
|
|
904
1044
|
* returned value holds absolute paths (and absolute globs).
|
|
905
1045
|
*/
|
|
906
|
-
export function parseConfig(given, configDir
|
|
1046
|
+
export function parseConfig(given, configDir, configName = "your config",
|
|
1047
|
+
/** The line the config file names an old rule id on, so the notice says where to rename it. */
|
|
1048
|
+
lineOf = () => undefined) {
|
|
907
1049
|
if (!isRecord(given))
|
|
908
1050
|
return { ok: false, errors: ["config must be an object"] };
|
|
909
1051
|
const errors = [];
|
|
910
1052
|
const merged = mergeChain(configChain(given, { name: "your config" }, [], errors), errors);
|
|
911
1053
|
const tsconfig = parseTsconfig(merged.tsconfig, errors);
|
|
912
1054
|
const plugins = parsePlugins(merged.plugins, errors);
|
|
913
|
-
const
|
|
1055
|
+
const notices = new Set();
|
|
1056
|
+
const where = (id) => {
|
|
1057
|
+
const line = lineOf(id);
|
|
1058
|
+
return line === undefined ? configName : `${configName}:${String(line)}`;
|
|
1059
|
+
};
|
|
1060
|
+
const input = migrateRuleIds(merged, new Set(ruleIds(plugins)), notices, where);
|
|
914
1061
|
const preset = parsePreset(input.preset, errors);
|
|
915
1062
|
const addons = parseAddons(input.addons, plugins, errors);
|
|
916
1063
|
const { rules, ruleOptions } = parseRuleSettings(input.rules ?? {}, plugins, errors);
|
|
@@ -946,7 +1093,7 @@ export function parseConfig(given, configDir) {
|
|
|
946
1093
|
ignores,
|
|
947
1094
|
unknowns,
|
|
948
1095
|
maxMemory,
|
|
949
|
-
warnings: [],
|
|
1096
|
+
warnings: [...notices],
|
|
950
1097
|
},
|
|
951
1098
|
};
|
|
952
1099
|
}
|
package/dist/define.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EditorContributions, FileRule, Json, NoOptions, OptionSpecs, PluginOf, ProjectRule, PluginRule } from "./types.js";
|
|
1
|
+
import type { EditorContributions, FileRule, Json, NoOptions, OptionSpecs, PackageFactsConfig, PluginOf, ProjectRule, PluginRule } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Identity helpers: they return what they are given, so TypeScript can infer the rule's options
|
|
4
4
|
* from `meta.options` and type `ctx.options` in `check`. A rule without `meta.options` takes no
|
|
@@ -12,5 +12,6 @@ export declare function definePlugin<const N extends string, const R extends Rea
|
|
|
12
12
|
description: string;
|
|
13
13
|
rules: R;
|
|
14
14
|
editor?: EditorContributions;
|
|
15
|
+
facts?: PackageFactsConfig;
|
|
15
16
|
}): PluginOf<N, R>;
|
|
16
17
|
//# sourceMappingURL=define.d.ts.map
|