@stateward/core 1.1.0 → 1.2.1
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/typescript6.js +375 -8
- package/dist/baseline.js +10 -0
- package/dist/checker.js +13 -7
- 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 +117 -13
- package/dist/define.d.ts +2 -1
- package/dist/engine.d.ts +2 -0
- package/dist/engine.js +38 -7
- package/dist/guarantee-markers.d.ts +10 -0
- package/dist/guarantee-markers.js +66 -0
- package/dist/guarantees.d.ts +5 -2
- package/dist/guarantees.js +28 -12
- package/dist/hint-lines.d.ts +24 -0
- package/dist/hint-lines.js +36 -0
- package/dist/ignores.js +20 -1
- package/dist/implementation.js +2 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +4 -0
- package/dist/internal.d.ts +4 -1
- package/dist/internal.js +3 -1
- package/dist/package-facts.d.ts +52 -0
- package/dist/package-facts.js +213 -0
- 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.js +48 -8
- package/dist/types.d.ts +164 -13
- package/package.json +1 -1
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";
|
|
@@ -416,7 +417,7 @@ function parsePlugin(value, index, errors) {
|
|
|
416
417
|
errors.push(`plugins[${String(index)}] must be an object`);
|
|
417
418
|
return undefined;
|
|
418
419
|
}
|
|
419
|
-
const { name, description, rules, editor } = value;
|
|
420
|
+
const { name, description, rules, editor, facts } = value;
|
|
420
421
|
if (typeof name !== "string" || name === "" || name.includes("/")) {
|
|
421
422
|
errors.push(`plugins[${String(index)}].name must be a non-empty string without "/"`);
|
|
422
423
|
return undefined;
|
|
@@ -441,12 +442,28 @@ function parsePlugin(value, index, errors) {
|
|
|
441
442
|
}
|
|
442
443
|
if (!valid)
|
|
443
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
|
+
};
|
|
444
454
|
if (editor === undefined)
|
|
445
|
-
return
|
|
455
|
+
return parsed;
|
|
446
456
|
const parsedEditor = parseEditor(editor, where, errors);
|
|
447
|
-
return parsedEditor === undefined
|
|
448
|
-
|
|
449
|
-
|
|
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;
|
|
450
467
|
}
|
|
451
468
|
function parsePlugins(value, errors) {
|
|
452
469
|
if (!Array.isArray(value)) {
|
|
@@ -478,6 +495,11 @@ function splitRuleConfig(value) {
|
|
|
478
495
|
}
|
|
479
496
|
return undefined;
|
|
480
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
|
+
}
|
|
481
503
|
function parseRuleSettings(value, plugins, errors) {
|
|
482
504
|
if (!isRecord(value)) {
|
|
483
505
|
errors.push("rules must be an object");
|
|
@@ -489,7 +511,7 @@ function parseRuleSettings(value, plugins, errors) {
|
|
|
489
511
|
for (const [ruleId, entry] of Object.entries(value)) {
|
|
490
512
|
const split = splitRuleConfig(entry);
|
|
491
513
|
if (!known.has(ruleId))
|
|
492
|
-
errors.push(`rules:
|
|
514
|
+
errors.push(`rules: ${unknownRuleId(ruleId, known.keys())}`);
|
|
493
515
|
else if (split === undefined) {
|
|
494
516
|
errors.push(`rules["${ruleId}"] must be "error", "warning", "info", "off", or [severity, { options }]`);
|
|
495
517
|
}
|
|
@@ -570,7 +592,26 @@ function parseAddons(value, plugins, errors, name = "addons") {
|
|
|
570
592
|
}
|
|
571
593
|
return listed;
|
|
572
594
|
}
|
|
573
|
-
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
|
+
}
|
|
574
615
|
/** `facts: { axios: { get: { throws: ["AxiosError"] } } }`. */
|
|
575
616
|
function parseFacts(value, errors) {
|
|
576
617
|
if (value === undefined)
|
|
@@ -589,7 +630,7 @@ function parseFacts(value, errors) {
|
|
|
589
630
|
for (const [fn, entry] of Object.entries(functions)) {
|
|
590
631
|
const where = `facts["${name}"]["${fn}"]`;
|
|
591
632
|
if (!isRecord(entry)) {
|
|
592
|
-
errors.push(`${where} must be an object: { throws: [...], rejects: [...] }`);
|
|
633
|
+
errors.push(`${where} must be an object: { throws: [...], rejects: [...], returns: "release", neverRejects: true }`);
|
|
593
634
|
continue;
|
|
594
635
|
}
|
|
595
636
|
const one = {};
|
|
@@ -597,6 +638,17 @@ function parseFacts(value, errors) {
|
|
|
597
638
|
if (!FACT_KINDS.includes(kind)) {
|
|
598
639
|
errors.push(`${where}: unknown fact "${kind}"; facts are ${orList(FACT_KINDS)}`);
|
|
599
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
|
+
}
|
|
600
652
|
else if (!isStringArray(list)) {
|
|
601
653
|
errors.push(`${where}.${kind} must be an array of class names`);
|
|
602
654
|
}
|
|
@@ -607,6 +659,9 @@ function parseFacts(value, errors) {
|
|
|
607
659
|
one.rejects = list;
|
|
608
660
|
}
|
|
609
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
|
+
}
|
|
610
665
|
parsed[fn] = one;
|
|
611
666
|
}
|
|
612
667
|
facts[name] = parsed;
|
|
@@ -661,7 +716,7 @@ function parseOverrideRules(value, plugins, errors, name) {
|
|
|
661
716
|
const settings = {};
|
|
662
717
|
for (const [ruleId, entry] of Object.entries(value)) {
|
|
663
718
|
if (!known.has(ruleId)) {
|
|
664
|
-
errors.push(`${name}:
|
|
719
|
+
errors.push(`${name}: ${unknownRuleId(ruleId, known.keys())}`);
|
|
665
720
|
}
|
|
666
721
|
else if (Array.isArray(entry)) {
|
|
667
722
|
errors.push(`${name}["${ruleId}"] takes a severity or "off", not options; set a rule's options once, in the config's own rules`);
|
|
@@ -927,6 +982,48 @@ function mergeChain(chain, errors) {
|
|
|
927
982
|
merged.ignores = ignores;
|
|
928
983
|
return merged;
|
|
929
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
|
+
}
|
|
930
1027
|
/** `tsconfig`: one path, or a non-empty list of paths and globs; undefined after an error. */
|
|
931
1028
|
function parseTsconfig(value, errors) {
|
|
932
1029
|
if (value === undefined)
|
|
@@ -946,14 +1043,21 @@ function parseTsconfig(value, errors) {
|
|
|
946
1043
|
* Validates untrusted config input once. `tsconfig` is resolved against `configDir`, so the
|
|
947
1044
|
* returned value holds absolute paths (and absolute globs).
|
|
948
1045
|
*/
|
|
949
|
-
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) {
|
|
950
1049
|
if (!isRecord(given))
|
|
951
1050
|
return { ok: false, errors: ["config must be an object"] };
|
|
952
1051
|
const errors = [];
|
|
953
1052
|
const merged = mergeChain(configChain(given, { name: "your config" }, [], errors), errors);
|
|
954
1053
|
const tsconfig = parseTsconfig(merged.tsconfig, errors);
|
|
955
1054
|
const plugins = parsePlugins(merged.plugins, errors);
|
|
956
|
-
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);
|
|
957
1061
|
const preset = parsePreset(input.preset, errors);
|
|
958
1062
|
const addons = parseAddons(input.addons, plugins, errors);
|
|
959
1063
|
const { rules, ruleOptions } = parseRuleSettings(input.rules ?? {}, plugins, errors);
|
|
@@ -989,7 +1093,7 @@ export function parseConfig(given, configDir) {
|
|
|
989
1093
|
ignores,
|
|
990
1094
|
unknowns,
|
|
991
1095
|
maxMemory,
|
|
992
|
-
warnings: [],
|
|
1096
|
+
warnings: [...notices],
|
|
993
1097
|
},
|
|
994
1098
|
};
|
|
995
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
|
package/dist/engine.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ type ActiveRule = {
|
|
|
30
30
|
options: Options;
|
|
31
31
|
/** The config's facts about packages, for every rule: part of its cache key. */
|
|
32
32
|
facts: PackageFactsConfig;
|
|
33
|
+
/** The facts the loaded plugins ship (`Plugin.facts`): part of its cache key too. */
|
|
34
|
+
shippedFacts: PackageFactsConfig;
|
|
33
35
|
};
|
|
34
36
|
/**
|
|
35
37
|
* The rules a run uses, with the settings of the files they run on. A rule that is on in the
|
package/dist/engine.js
CHANGED
|
@@ -159,6 +159,19 @@ function ignoreScope(config, ran) {
|
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
161
|
const FAILURE_HINT = "report this to the rule's authors; the rest of the check ran normally";
|
|
162
|
+
/**
|
|
163
|
+
* The facts every loaded plugin ships, joined in plugin order: per package, per function; a later
|
|
164
|
+
* plugin's line for the same function wins.
|
|
165
|
+
*/
|
|
166
|
+
function shippedFactsOf(plugins) {
|
|
167
|
+
const joined = {};
|
|
168
|
+
for (const plugin of plugins) {
|
|
169
|
+
for (const [name, functions] of Object.entries(plugin.facts ?? {})) {
|
|
170
|
+
joined[name] = { ...joined[name], ...functions };
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return joined;
|
|
174
|
+
}
|
|
162
175
|
function nodeLocation(node) {
|
|
163
176
|
return { file: node.getSourceFile().fileName, start: node.getStart(), end: node.getEnd() };
|
|
164
177
|
}
|
|
@@ -192,8 +205,12 @@ function entryOf(config, plugin, name, rule, severity, addons) {
|
|
|
192
205
|
addons,
|
|
193
206
|
options,
|
|
194
207
|
facts: config.facts,
|
|
208
|
+
shippedFacts: shippedFactsOf(config.plugins),
|
|
209
|
+
};
|
|
210
|
+
return {
|
|
211
|
+
...active,
|
|
212
|
+
key: ruleKey({ ...active, facts: { config: active.facts, shipped: active.shippedFacts } }),
|
|
195
213
|
};
|
|
196
|
-
return { ...active, key: ruleKey(active) };
|
|
197
214
|
}
|
|
198
215
|
function ruleSets(config, selection) {
|
|
199
216
|
const rolledOut = rolloutSet(config, selection);
|
|
@@ -340,7 +357,11 @@ function problemFinding(entry, problem) {
|
|
|
340
357
|
kind: "problem",
|
|
341
358
|
ruleId: entry.id,
|
|
342
359
|
severity: setting ??
|
|
343
|
-
(problem.
|
|
360
|
+
(problem.stated === true
|
|
361
|
+
? "error"
|
|
362
|
+
: problem.severity === undefined
|
|
363
|
+
? entry.severity
|
|
364
|
+
: lower(problem.severity, entry.severity)),
|
|
344
365
|
message: problem.message,
|
|
345
366
|
hint: problem.hint,
|
|
346
367
|
location: problem.location,
|
|
@@ -351,7 +372,7 @@ function problemFinding(entry, problem) {
|
|
|
351
372
|
};
|
|
352
373
|
}
|
|
353
374
|
function fileContext(entry, sourceFile, types, findings) {
|
|
354
|
-
const { id, addons, options, facts } = entry;
|
|
375
|
+
const { id, addons, options, facts, shippedFacts } = entry;
|
|
355
376
|
return {
|
|
356
377
|
file: sourceFile.fileName,
|
|
357
378
|
sourceFile,
|
|
@@ -359,6 +380,7 @@ function fileContext(entry, sourceFile, types, findings) {
|
|
|
359
380
|
addons,
|
|
360
381
|
options,
|
|
361
382
|
facts,
|
|
383
|
+
shippedFacts,
|
|
362
384
|
report(problem) {
|
|
363
385
|
const finding = problemFinding(entry, {
|
|
364
386
|
message: problem.message,
|
|
@@ -371,6 +393,7 @@ function fileContext(entry, sourceFile, types, findings) {
|
|
|
371
393
|
fixes: problem.fixes ?? [],
|
|
372
394
|
...(problem.findingKind === undefined ? {} : { findingKind: problem.findingKind }),
|
|
373
395
|
...(problem.severity === undefined ? {} : { severity: problem.severity }),
|
|
396
|
+
...(problem.stated === true ? { stated: true } : {}),
|
|
374
397
|
...(problem.about === undefined ? {} : { about: nodeLocation(problem.about) }),
|
|
375
398
|
});
|
|
376
399
|
if (finding !== undefined)
|
|
@@ -535,8 +558,9 @@ nextFiles, cache, ignores, timing) {
|
|
|
535
558
|
let fileCount = 0;
|
|
536
559
|
let done = 0;
|
|
537
560
|
for (const entry of rules) {
|
|
538
|
-
if (entry.rule.kind === "project")
|
|
561
|
+
if (entry.rule.kind === "project" || entry.rule.summarize !== undefined) {
|
|
539
562
|
summaries.set(entry.id, new Map());
|
|
563
|
+
}
|
|
540
564
|
}
|
|
541
565
|
for (const unit of units) {
|
|
542
566
|
fileCount += unit.files.length;
|
|
@@ -679,8 +703,14 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
|
|
|
679
703
|
let previous;
|
|
680
704
|
for (const entry of rules) {
|
|
681
705
|
const fileSummaries = summaries.get(entry.id);
|
|
682
|
-
if (
|
|
706
|
+
if (fileSummaries === undefined)
|
|
707
|
+
continue;
|
|
708
|
+
if (entry.rule.kind !== "project") {
|
|
709
|
+
// A file rule's summaries are for the editor's contributions only (`FileRule.summarize`).
|
|
710
|
+
const sorted = [...fileSummaries].sort(([a], [b]) => compareText(a, b));
|
|
711
|
+
keepSummaries?.set(entry.id, () => summaryMap(sorted));
|
|
683
712
|
continue;
|
|
713
|
+
}
|
|
684
714
|
if (done > 0)
|
|
685
715
|
yield {
|
|
686
716
|
phase: "project",
|
|
@@ -917,7 +947,7 @@ function runFileSide(rule, ctx) {
|
|
|
917
947
|
switch (rule.kind) {
|
|
918
948
|
case "file":
|
|
919
949
|
rule.check(ctx);
|
|
920
|
-
return
|
|
950
|
+
return rule.summarize?.(ctx);
|
|
921
951
|
case "project":
|
|
922
952
|
return rule.summarize(ctx);
|
|
923
953
|
default: {
|
|
@@ -1023,7 +1053,8 @@ export function runFileRules(config, cwd, units, files, started, capped = true,
|
|
|
1023
1053
|
break;
|
|
1024
1054
|
types ??= unit.typesFor(file);
|
|
1025
1055
|
try {
|
|
1026
|
-
|
|
1056
|
+
// The fast pass keeps no summaries: a file rule's are read after a full check.
|
|
1057
|
+
entry.rule.check(fileContext(entry, sourceFile, types, findings));
|
|
1027
1058
|
}
|
|
1028
1059
|
catch (error) {
|
|
1029
1060
|
findings.push(failureFinding(entry, startOfFile, error));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { EditorAction } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* "Rewrite the guarantee markers to the 1.2 form" on a 1.1.0 marker (`readonly __guarantee?:
|
|
5
|
+
* "owned"`): every one in the file becomes one optional property per trait
|
|
6
|
+
* (`readonly "__guarantee:owned"?: true`), so traits combine by intersection. Undefined when the
|
|
7
|
+
* range touches none. Both forms are read; this only lets a type state two traits.
|
|
8
|
+
*/
|
|
9
|
+
export declare function legacyMarkerRewrite(sourceFile: ts.SourceFile, start: number, end: number): EditorAction | undefined;
|
|
10
|
+
//# sourceMappingURL=guarantee-markers.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
/** The 1.1.0 marker property, `__guarantee?: "owned"`, and the prefix of the 1.2 one. */
|
|
3
|
+
const LEGACY = "__guarantee";
|
|
4
|
+
const PREFIX = "__guarantee:";
|
|
5
|
+
/** The names a 1.1.0 marker property states, or undefined when it is not one (required, not literals). */
|
|
6
|
+
function legacyNames(member) {
|
|
7
|
+
if (!ts.isPropertySignature(member) || member.questionToken === undefined)
|
|
8
|
+
return undefined;
|
|
9
|
+
const { name, type } = member;
|
|
10
|
+
if (!(ts.isIdentifier(name) || ts.isStringLiteral(name)) || name.text !== LEGACY)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (type === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
const parts = ts.isUnionTypeNode(type) ? type.types : [type];
|
|
15
|
+
const names = [];
|
|
16
|
+
for (const part of parts) {
|
|
17
|
+
if (!ts.isLiteralTypeNode(part) || !ts.isStringLiteral(part.literal))
|
|
18
|
+
return undefined;
|
|
19
|
+
names.push(part.literal.text);
|
|
20
|
+
}
|
|
21
|
+
return names;
|
|
22
|
+
}
|
|
23
|
+
/** Every 1.1.0 marker property in the file. */
|
|
24
|
+
function legacyMarkers(sourceFile) {
|
|
25
|
+
const found = [];
|
|
26
|
+
const visit = (node) => {
|
|
27
|
+
if (ts.isPropertySignature(node) && legacyNames(node) !== undefined)
|
|
28
|
+
found.push(node);
|
|
29
|
+
ts.forEachChild(node, visit);
|
|
30
|
+
};
|
|
31
|
+
visit(sourceFile);
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* "Rewrite the guarantee markers to the 1.2 form" on a 1.1.0 marker (`readonly __guarantee?:
|
|
36
|
+
* "owned"`): every one in the file becomes one optional property per trait
|
|
37
|
+
* (`readonly "__guarantee:owned"?: true`), so traits combine by intersection. Undefined when the
|
|
38
|
+
* range touches none. Both forms are read; this only lets a type state two traits.
|
|
39
|
+
*/
|
|
40
|
+
export function legacyMarkerRewrite(sourceFile, start, end) {
|
|
41
|
+
const markers = legacyMarkers(sourceFile);
|
|
42
|
+
const touched = markers.some((marker) => marker.getStart(sourceFile) <= end && start <= marker.getEnd());
|
|
43
|
+
if (!touched)
|
|
44
|
+
return undefined;
|
|
45
|
+
const edits = markers.flatMap((marker) => {
|
|
46
|
+
const names = legacyNames(marker) ?? [];
|
|
47
|
+
const readonly = marker.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword) ===
|
|
48
|
+
true;
|
|
49
|
+
const type = marker.type;
|
|
50
|
+
if (type === undefined)
|
|
51
|
+
return [];
|
|
52
|
+
const from = marker.name.getStart(sourceFile);
|
|
53
|
+
const text = names
|
|
54
|
+
.map((name, index) => `${index > 0 && readonly ? "readonly " : ""}"${PREFIX}${name}"?: true`)
|
|
55
|
+
.join("; ");
|
|
56
|
+
return [{ location: { file: sourceFile.fileName, start: from, end: type.end }, newText: text }];
|
|
57
|
+
});
|
|
58
|
+
const count = markers.length;
|
|
59
|
+
return {
|
|
60
|
+
title: count === 1
|
|
61
|
+
? "Rewrite the guarantee marker to the 1.2 form (one property per trait)"
|
|
62
|
+
: `Rewrite the ${String(count)} guarantee markers to the 1.2 form (one property per trait)`,
|
|
63
|
+
edits,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=guarantee-markers.js.map
|
package/dist/guarantees.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Finding, StatedGuarantee } from "./types.js";
|
|
1
|
+
import type { CoveredNoun, Finding, StatedGuarantee } from "./types.js";
|
|
2
2
|
type Problem = Extract<Finding, {
|
|
3
3
|
kind: "problem";
|
|
4
4
|
}>;
|
|
@@ -27,9 +27,12 @@ export type GuaranteeProof = {
|
|
|
27
27
|
* Each stated guarantee with its status, in file and offset order. `findings` are a check's
|
|
28
28
|
* (after ignore comments, before a baseline: an accepted error still leaves it unproven);
|
|
29
29
|
* `active` the rules that check ran. A finding is about a guarantee when it is a problem of the
|
|
30
|
-
* guarantee's rule, not an info,
|
|
30
|
+
* guarantee's rule, not an info, that names its declaration in a note or lies in a place it
|
|
31
|
+
* covers (`isAbout`).
|
|
31
32
|
*/
|
|
32
33
|
export declare function proveGuarantees(stated: readonly StatedGuarantee[], findings: readonly Finding[], active: ReadonlySet<string>): GuaranteeProof[];
|
|
34
|
+
/** "1 switch", "2 switches": the count and the noun in its form for it, as the noun gives both. */
|
|
35
|
+
export declare function counted(count: number, noun: CoveredNoun): string;
|
|
33
36
|
/**
|
|
34
37
|
* The status in a few words, the same in `stateward guarantees` and the editor: "proven for 3
|
|
35
38
|
* readers", "not proven: 2 errors", "unused: nothing reads it", "not checked: … is not on".
|
package/dist/guarantees.js
CHANGED
|
@@ -7,11 +7,30 @@ function compareStated(a, b) {
|
|
|
7
7
|
return a.location.file < b.location.file ? -1 : 1;
|
|
8
8
|
return a.location.start - b.location.start || (a.guarantee < b.guarantee ? -1 : 1);
|
|
9
9
|
}
|
|
10
|
+
function contains(place, inner) {
|
|
11
|
+
return place.file === inner.file && inner.start >= place.start && inner.end <= place.end;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Whether a finding is about a guarantee: a problem of the guarantee's rule with a related note
|
|
15
|
+
* at its declaration; or, when it has a note at no declaration of that rule, one that lies in a
|
|
16
|
+
* place the guarantee covers (a file rule notes only declarations in its own file, so a finding
|
|
17
|
+
* at a Provider in another file names the context by where it is).
|
|
18
|
+
*/
|
|
19
|
+
function isAbout(finding, guarantee, declarations) {
|
|
20
|
+
if (finding.ruleId !== guarantee.ruleId)
|
|
21
|
+
return false;
|
|
22
|
+
if (finding.related.some((note) => sameStart(note.location, guarantee.location)))
|
|
23
|
+
return true;
|
|
24
|
+
const namesAnother = declarations.some((other) => other.ruleId === finding.ruleId &&
|
|
25
|
+
finding.related.some((note) => sameStart(note.location, other.location)));
|
|
26
|
+
return (!namesAnother && guarantee.covered.some((place) => contains(place.location, finding.location)));
|
|
27
|
+
}
|
|
10
28
|
/**
|
|
11
29
|
* Each stated guarantee with its status, in file and offset order. `findings` are a check's
|
|
12
30
|
* (after ignore comments, before a baseline: an accepted error still leaves it unproven);
|
|
13
31
|
* `active` the rules that check ran. A finding is about a guarantee when it is a problem of the
|
|
14
|
-
* guarantee's rule, not an info,
|
|
32
|
+
* guarantee's rule, not an info, that names its declaration in a note or lies in a place it
|
|
33
|
+
* covers (`isAbout`).
|
|
15
34
|
*/
|
|
16
35
|
export function proveGuarantees(stated, findings, active) {
|
|
17
36
|
const problems = findings
|
|
@@ -21,8 +40,7 @@ export function proveGuarantees(stated, findings, active) {
|
|
|
21
40
|
if (!active.has(guarantee.ruleId))
|
|
22
41
|
return { stated: guarantee, status: { kind: "off" } };
|
|
23
42
|
const errors = problems
|
|
24
|
-
.filter((finding) => finding
|
|
25
|
-
finding.related.some((note) => sameStart(note.location, guarantee.location)))
|
|
43
|
+
.filter((finding) => isAbout(finding, guarantee, stated))
|
|
26
44
|
.sort(compareFindings);
|
|
27
45
|
if (errors.length > 0)
|
|
28
46
|
return { stated: guarantee, status: { kind: "unproven", errors } };
|
|
@@ -31,13 +49,11 @@ export function proveGuarantees(stated, findings, active) {
|
|
|
31
49
|
return { stated: guarantee, status: { kind: "proven" } };
|
|
32
50
|
});
|
|
33
51
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
/** "reader" for "readers": the noun of one covered place. */
|
|
38
|
-
function singular(covers) {
|
|
39
|
-
return covers.endsWith("s") ? covers.slice(0, -1) : covers;
|
|
52
|
+
/** "1 switch", "2 switches": the count and the noun in its form for it, as the noun gives both. */
|
|
53
|
+
export function counted(count, noun) {
|
|
54
|
+
return `${String(count)} ${count === 1 ? noun.one : noun.many}`;
|
|
40
55
|
}
|
|
56
|
+
const ERROR = { one: "error", many: "errors" };
|
|
41
57
|
/**
|
|
42
58
|
* The status in a few words, the same in `stateward guarantees` and the editor: "proven for 3
|
|
43
59
|
* readers", "not proven: 2 errors", "unused: nothing reads it", "not checked: … is not on".
|
|
@@ -46,11 +62,11 @@ export function describeStatus(proof) {
|
|
|
46
62
|
const { stated, status } = proof;
|
|
47
63
|
switch (status.kind) {
|
|
48
64
|
case "proven":
|
|
49
|
-
return `proven for ${
|
|
65
|
+
return `proven for ${counted(stated.covered.length, stated.covers)}`;
|
|
50
66
|
case "unproven":
|
|
51
|
-
return `not proven: ${
|
|
67
|
+
return `not proven: ${counted(status.errors.length, ERROR)}`;
|
|
52
68
|
case "unused":
|
|
53
|
-
return `unused: no ${
|
|
69
|
+
return `unused: no ${stated.covers.one}, so nothing to prove; use it or drop the marker`;
|
|
54
70
|
case "off":
|
|
55
71
|
return `not checked: ${stated.ruleId} is not on`;
|
|
56
72
|
default: {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a fix line (`Finding.hint`) is laid out (contract M2, M22): the first line is the fix, or
|
|
3
|
+
* `one of` when there are several ways; a line that starts with `- ` is one way; a line that
|
|
4
|
+
* starts with 4 spaces is a code example of the way above it, or of the first line.
|
|
5
|
+
*/
|
|
6
|
+
export type HintLine = {
|
|
7
|
+
readonly kind: "text" | "bullet" | "example";
|
|
8
|
+
/** The line as the hint has it (an example keeps its 4 spaces). */
|
|
9
|
+
readonly text: string;
|
|
10
|
+
/** Whether the line follows a bullet: an example of that way, indented under it. */
|
|
11
|
+
readonly underBullet: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare function hintLines(hint: string): readonly HintLine[];
|
|
14
|
+
/**
|
|
15
|
+
* The hint as it is printed under a label (`= fix: `), each line after the first starting with
|
|
16
|
+
* `indent`: bullets 2 further in, their examples 4 further than the bullet's own.
|
|
17
|
+
*/
|
|
18
|
+
export declare function laidOutHint(hint: string, indent: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* The hint without its code examples, for a place that cannot show code (a GitHub annotation): the
|
|
21
|
+
* first line and each way on its own line, without their closing colons.
|
|
22
|
+
*/
|
|
23
|
+
export declare function hintWithoutExamples(hint: string): string;
|
|
24
|
+
//# sourceMappingURL=hint-lines.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function hintLines(hint) {
|
|
2
|
+
let inBullet = false;
|
|
3
|
+
return hint.split("\n").map((text) => {
|
|
4
|
+
if (text.startsWith("- ")) {
|
|
5
|
+
inBullet = true;
|
|
6
|
+
return { kind: "bullet", text, underBullet: false };
|
|
7
|
+
}
|
|
8
|
+
if (text.startsWith(" "))
|
|
9
|
+
return { kind: "example", text, underBullet: inBullet };
|
|
10
|
+
inBullet = false;
|
|
11
|
+
return { kind: "text", text, underBullet: false };
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The hint as it is printed under a label (`= fix: `), each line after the first starting with
|
|
16
|
+
* `indent`: bullets 2 further in, their examples 4 further than the bullet's own.
|
|
17
|
+
*/
|
|
18
|
+
export function laidOutHint(hint, indent) {
|
|
19
|
+
return hintLines(hint)
|
|
20
|
+
.map((line, index) => {
|
|
21
|
+
const shift = line.kind === "bullet" ? " " : line.underBullet ? " " : "";
|
|
22
|
+
return index === 0 ? line.text : `${indent}${shift}${line.text}`;
|
|
23
|
+
})
|
|
24
|
+
.join("\n");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The hint without its code examples, for a place that cannot show code (a GitHub annotation): the
|
|
28
|
+
* first line and each way on its own line, without their closing colons.
|
|
29
|
+
*/
|
|
30
|
+
export function hintWithoutExamples(hint) {
|
|
31
|
+
return hintLines(hint)
|
|
32
|
+
.filter((line, index) => index === 0 || line.kind === "bullet")
|
|
33
|
+
.map((line) => line.text.replace(/:$/, ""))
|
|
34
|
+
.join("\n");
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=hint-lines.js.map
|
package/dist/ignores.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { summaryOf, summaryValue } from "./cache.js";
|
|
2
|
+
import { renamedRule } from "./renamed.js";
|
|
2
3
|
/**
|
|
3
4
|
* Ignore comments: `// stateward-ignore <rule>[, <rule>]:
|
|
4
5
|
* <reason>` covers the findings that start on the next line of code, and
|
|
@@ -308,6 +309,24 @@ function judge(file, directive, used, scope) {
|
|
|
308
309
|
if (rule.id === IGNORE_RULE_ID) {
|
|
309
310
|
found.push(commentFinding(file, rule, "error", "`stateward/ignore` findings cannot be ignored", "fix the comment the finding points at, or remove it", []));
|
|
310
311
|
}
|
|
312
|
+
else if (!scope.known.has(rule.id) && renamedRule(rule.id) !== undefined) {
|
|
313
|
+
// A renamed rule: the comment still works under the old id.
|
|
314
|
+
const now = renamedRule(rule.id) ?? rule.id;
|
|
315
|
+
// One cause, one message: an ignore that is not needed is removed, not renamed; two fixes on
|
|
316
|
+
// one comment would also clash in `--fix`.
|
|
317
|
+
if (!used.has(now) && scope.ran.has(now)) {
|
|
318
|
+
unused.push(rule.id);
|
|
319
|
+
if (scope.options.reportUnused !== "off")
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
found.push(commentFinding(file, rule, "warning", fits(`\`${rule.id}\` is now \`${now}\`; rename it in this ignore`, MAX_HEADLINE, "ignore comment names a rule by its old id"), fits(`change it to \`${now}\` (quick fix); the old id still works`, MAX_HINT, "change it to the new id (quick fix); the old id still works"), [
|
|
323
|
+
{
|
|
324
|
+
title: `Change to \`${now}\``,
|
|
325
|
+
safe: true,
|
|
326
|
+
edits: [{ location: { file, start: rule.start, end: rule.end }, newText: now }],
|
|
327
|
+
},
|
|
328
|
+
]));
|
|
329
|
+
}
|
|
311
330
|
else if (!scope.known.has(rule.id)) {
|
|
312
331
|
const guess = closest(rule.id, scope.known);
|
|
313
332
|
found.push(commentFinding(file, rule, "error", fits(`\`${rule.id}\` is not a stateward rule`, MAX_HEADLINE, "ignore comment names a rule that does not exist"), fits(guess === undefined
|
|
@@ -388,7 +407,7 @@ function coveredBy(list, ruleId, start, used) {
|
|
|
388
407
|
for (const directive of list) {
|
|
389
408
|
if (directive.file && !directive.placed)
|
|
390
409
|
continue;
|
|
391
|
-
if (!directive.rules.some((rule) => rule.id === ruleId))
|
|
410
|
+
if (!directive.rules.some((rule) => (renamedRule(rule.id) ?? rule.id) === ruleId))
|
|
392
411
|
continue;
|
|
393
412
|
const covers = directive.file || (directive.coverStart <= start && start <= directive.coverEnd);
|
|
394
413
|
if (!covers)
|
package/dist/implementation.js
CHANGED
|
@@ -278,7 +278,8 @@ export function foreignTypeScriptWarnings(configFile) {
|
|
|
278
278
|
continue;
|
|
279
279
|
const version = stringField(jsonOf(path.join(root, "package.json")), "version");
|
|
280
280
|
const shortVersion = /^\d+\.\d+/.exec(version)?.[0] ?? version;
|
|
281
|
-
|
|
281
|
+
// Both sides real: the local files are, and a config reached through a link is not.
|
|
282
|
+
const shown = path.relative(real(configDir), real(file)).split(path.sep).join("/");
|
|
282
283
|
warnings.push(`\`${shown}\` imports the app's TypeScript ${shortVersion}; use \`@stateward/core/typescript\`, or it may see nothing`);
|
|
283
284
|
}
|
|
284
285
|
return warnings;
|