tslaws 0.6.4 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/dist/api.d.ts +10 -0
- package/dist/api.js +7 -0
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +2 -6
- package/dist/check.d.ts +2 -0
- package/dist/check.js +218 -56
- package/dist/cli.d.ts +18 -0
- package/dist/cli.js +189 -56
- package/dist/compile.d.ts +12 -0
- package/dist/compile.js +207 -6
- package/dist/coverage.d.ts +21 -1
- package/dist/coverage.js +33 -23
- package/dist/derived.d.ts +8 -0
- package/dist/derived.js +52 -0
- package/dist/e2e.d.ts +0 -9
- package/dist/e2e.js +0 -20
- package/dist/generators.d.ts +8 -8
- package/dist/generators.js +59 -99
- package/dist/github.d.ts +121 -0
- package/dist/github.js +266 -0
- package/dist/githubCli.d.ts +46 -0
- package/dist/githubCli.js +502 -0
- package/dist/globs.d.ts +3 -0
- package/dist/globs.js +24 -0
- package/dist/init.js +5 -5
- package/dist/lawFiles.d.ts +21 -0
- package/dist/lawFiles.js +95 -0
- package/dist/lawKinds.d.ts +38 -0
- package/dist/lawKinds.js +210 -0
- package/dist/lawNames.d.ts +6 -0
- package/dist/lawNames.js +22 -0
- package/dist/list.d.ts +22 -0
- package/dist/list.js +36 -0
- package/dist/lock.d.ts +39 -3
- package/dist/lock.js +107 -4
- package/dist/lower.d.ts +10 -0
- package/dist/lower.js +178 -0
- package/dist/mcp.d.ts +2 -8
- package/dist/mcp.js +40 -61
- package/dist/migrate/arbitraries.d.ts +23 -0
- package/dist/migrate/arbitraries.js +202 -0
- package/dist/migrate/convert.d.ts +20 -0
- package/dist/migrate/convert.js +915 -0
- package/dist/migrate/framework.d.ts +48 -0
- package/dist/migrate/framework.js +249 -0
- package/dist/migrate/index.d.ts +16 -0
- package/dist/migrate/index.js +155 -0
- package/dist/migrate/report.d.ts +35 -0
- package/dist/migrate/report.js +40 -0
- package/dist/migrate/source.d.ts +42 -0
- package/dist/migrate/source.js +163 -0
- package/dist/migrate/tests.d.ts +44 -0
- package/dist/migrate/tests.js +184 -0
- package/dist/plan.d.ts +50 -0
- package/dist/plan.js +118 -0
- package/dist/pool.d.ts +21 -0
- package/dist/pool.js +54 -0
- package/dist/promote.d.ts +30 -2
- package/dist/promote.js +133 -7
- package/dist/property.d.ts +12 -26
- package/dist/property.js +33 -74
- package/dist/propertyRun.d.ts +35 -0
- package/dist/propertyRun.js +79 -0
- package/dist/propertyWorker.js +7 -9
- package/dist/proposals.d.ts +43 -0
- package/dist/proposals.js +192 -0
- package/dist/regressions.js +23 -2
- package/dist/reporters.d.ts +3 -0
- package/dist/reporters.js +17 -0
- package/dist/smt.d.ts +16 -0
- package/dist/smt.js +52 -9
- package/dist/solvers.d.ts +31 -0
- package/dist/solvers.js +121 -0
- package/dist/symbolic.js +2 -2
- package/dist/types.d.ts +19 -0
- package/hooks/AGENTS.snippet.md +2 -2
- package/package.json +9 -7
package/dist/lawFiles.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Which files are laws files. Every `*.laws.ts` is one; with a `laws` setting in tslaws.json, so is any file the globs
|
|
2
|
+
// match that declares a top-level `export law`. Proposals are separate: they live in `.tslaws/proposals/`, outside the
|
|
3
|
+
// tree the lock covers (in-tree `*.proposed.laws.ts` files are the usual proposals and keep working).
|
|
4
|
+
import { existsSync, lstatSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
5
|
+
import { join, relative } from 'node:path';
|
|
6
|
+
import { readConfig } from "./coverage.js";
|
|
7
|
+
import { globMatcher } from "./globs.js";
|
|
8
|
+
import { isProposedLawFile } from "./lawNames.js";
|
|
9
|
+
import { parseLawFile } from "./promote.js";
|
|
10
|
+
/** Directories no walk enters. */
|
|
11
|
+
export const ignoredDirs = new Set(['node_modules', '.tslaws', '.git']);
|
|
12
|
+
/** The tool-owned directory proposals live in, relative to the project root. */
|
|
13
|
+
export const proposalsDir = '.tslaws/proposals';
|
|
14
|
+
const slash = (path) => path.replaceAll('\\', '/');
|
|
15
|
+
/** Whether the text declares a law at the top level (cheap test first, then the real scan). */
|
|
16
|
+
export function declaresLaw(text) {
|
|
17
|
+
return /\bexport\s+(?:async\s+)?law\b/.test(text) && parseLawFile(text).laws.length > 0;
|
|
18
|
+
}
|
|
19
|
+
function walk(cwd, visit) {
|
|
20
|
+
const step = (dir) => {
|
|
21
|
+
for (const entry of readdirSync(dir).sort()) {
|
|
22
|
+
if (ignoredDirs.has(entry))
|
|
23
|
+
continue;
|
|
24
|
+
const full = join(dir, entry);
|
|
25
|
+
if (statSync(full).isDirectory())
|
|
26
|
+
step(full);
|
|
27
|
+
else
|
|
28
|
+
visit(slash(relative(cwd, full)));
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
step(cwd);
|
|
32
|
+
}
|
|
33
|
+
/** A predicate for files the `laws` globs of tslaws.json pick, which still have to declare a law to count. */
|
|
34
|
+
export function customLawFile(cwd, globs = readConfig(cwd).laws) {
|
|
35
|
+
if (globs === undefined || globs.length === 0)
|
|
36
|
+
return () => false;
|
|
37
|
+
const matches = globMatcher(globs);
|
|
38
|
+
return (file) => {
|
|
39
|
+
if (!/\.(ts|tsx|mts|cts)$/.test(file) || /\.d\.(ts|mts|cts)$/.test(file) || !matches(file))
|
|
40
|
+
return false;
|
|
41
|
+
try {
|
|
42
|
+
return declaresLaw(readFileSync(join(cwd, file), 'utf8'));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The laws files of the tree, project-relative and sorted: `*.laws.ts` plus the files the `laws` globs find. With
|
|
51
|
+
* `proposed`, the in-tree `*.proposed.laws.ts` files instead (the usual proposals; `discoverProposals` finds the optional ones in `.tslaws/proposals`).
|
|
52
|
+
*/
|
|
53
|
+
export function discoverLaws(cwd, proposed = false) {
|
|
54
|
+
const custom = customLawFile(cwd);
|
|
55
|
+
const found = [];
|
|
56
|
+
walk(cwd, (file) => {
|
|
57
|
+
const name = file.slice(file.lastIndexOf('/') + 1);
|
|
58
|
+
if (name.endsWith('.laws.ts')) {
|
|
59
|
+
if (isProposedLawFile(name) === proposed)
|
|
60
|
+
found.push(file);
|
|
61
|
+
}
|
|
62
|
+
else if (!proposed && custom(file))
|
|
63
|
+
found.push(file);
|
|
64
|
+
});
|
|
65
|
+
return found.sort();
|
|
66
|
+
}
|
|
67
|
+
/** The files in `.tslaws/proposals/` (any name ending `.ts`), project-relative and sorted. */
|
|
68
|
+
export function discoverProposals(cwd) {
|
|
69
|
+
const dir = join(cwd, proposalsDir);
|
|
70
|
+
if (!existsSync(dir))
|
|
71
|
+
return [];
|
|
72
|
+
return readdirSync(dir)
|
|
73
|
+
.filter((entry) => /\.(ts|mts|cts)$/.test(entry) && !/\.d\.(ts|mts|cts)$/.test(entry) && lstatSync(join(dir, entry)).isFile())
|
|
74
|
+
.map((entry) => `${proposalsDir}/${entry}`)
|
|
75
|
+
.sort();
|
|
76
|
+
}
|
|
77
|
+
/** Whether a project-relative path is a laws file of the tree (not a proposal). */
|
|
78
|
+
export function isLawsFile(cwd, file) {
|
|
79
|
+
const name = slash(file);
|
|
80
|
+
return name.endsWith('.laws.ts') || customLawFile(cwd)(name);
|
|
81
|
+
}
|
|
82
|
+
/** Whether a project-relative path is inside the proposals directory. */
|
|
83
|
+
export function inProposals(file) {
|
|
84
|
+
return slash(file).startsWith(`${proposalsDir}/`);
|
|
85
|
+
}
|
|
86
|
+
/** Whether a (possibly new) path is a name a laws file may have: `*.laws.ts`, or a path a `laws` glob picks. */
|
|
87
|
+
export function namesLawsFile(cwd, file) {
|
|
88
|
+
const name = slash(file);
|
|
89
|
+
if (isProposedLawFile(name))
|
|
90
|
+
return false;
|
|
91
|
+
if (name.endsWith('.laws.ts'))
|
|
92
|
+
return true;
|
|
93
|
+
const globs = readConfig(cwd).laws;
|
|
94
|
+
return globs !== undefined && /\.(ts|mts|cts)$/.test(name) && globMatcher(globs)(name);
|
|
95
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type LawKind = 'node' | 'browser' | 'e2e';
|
|
2
|
+
/** Where a laws file's level comes from: its name (the default) or its import graph (opt-in). */
|
|
3
|
+
export type LevelsMode = 'names' | 'imports';
|
|
4
|
+
export interface ImportFacts {
|
|
5
|
+
/** The chain of project files from the laws file to the one that imports a browser law entry point, if any. */
|
|
6
|
+
browser: string[] | undefined;
|
|
7
|
+
e2e: string[] | undefined;
|
|
8
|
+
}
|
|
9
|
+
export interface KindVerdict {
|
|
10
|
+
kind: LawKind;
|
|
11
|
+
/** Set when the file cannot run at one level; the message names the files involved. */
|
|
12
|
+
error?: string;
|
|
13
|
+
/** Things worth saying that do not stop the file: a name hint that the imports contradict or do not back. */
|
|
14
|
+
notices: string[];
|
|
15
|
+
imports: ImportFacts;
|
|
16
|
+
}
|
|
17
|
+
export interface Specifier {
|
|
18
|
+
value: string;
|
|
19
|
+
/** Offsets of the text between the quotes, for a rewrite. */
|
|
20
|
+
start: number;
|
|
21
|
+
end: number;
|
|
22
|
+
/** Used only as a type: erased from the output, so it loads nothing at run time. */
|
|
23
|
+
typeOnly: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** Every module specifier of a file: imports, re-exports, `import x = require()`, `require()` and `import()`. */
|
|
26
|
+
export declare function moduleSpecifiers(text: string, fileName?: string): Specifier[];
|
|
27
|
+
/** The module specifiers a file loads at run time: static imports, re-exports, `require` and dynamic `import()`. */
|
|
28
|
+
export declare function runtimeSpecifiers(text: string, fileName?: string): Specifier[];
|
|
29
|
+
/** Walks the import graph from `file` and finds the first chain that reaches each level's entry point. */
|
|
30
|
+
export declare function importFacts(cwd: string, file: string): ImportFacts;
|
|
31
|
+
/** Classifies a laws file (project-relative path) by its name (default) or, with `levels: "imports"`, its imports. */
|
|
32
|
+
export declare function classifyLawFile(cwd: string, file: string, mode?: LevelsMode): KindVerdict;
|
|
33
|
+
/** The project sources a file imports directly, project-relative, in the order they are imported. */
|
|
34
|
+
export declare function directProjectImports(cwd: string, file: string): string[];
|
|
35
|
+
/** The resolved level of each laws file, as the lock records it; none when levels come from names (the default). */
|
|
36
|
+
export declare function lockLevels(cwd: string, laws: {
|
|
37
|
+
file: string;
|
|
38
|
+
}[], mode: LevelsMode): Record<string, LawKind> | undefined;
|
package/dist/lawKinds.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// The level a laws file runs at. By default (`levels: "names"` in tslaws.json) it comes from the file name:
|
|
2
|
+
// `*.browser.laws.ts` is a browser law (an end-to-end law when it imports `tslaws/e2e`), `*.e2e.laws.ts` is end to end,
|
|
3
|
+
// anything else is a Node law. With `levels: "imports"` it comes from what the file imports, as follows.
|
|
4
|
+
//
|
|
5
|
+
// From what it imports. A file that reaches the browser law API (`tslaws/browser`, or one
|
|
6
|
+
// of the `tslaws/adapters/*` entry points) is a browser law; a file that reaches the end-to-end API (`tslaws/e2e`) is an
|
|
7
|
+
// end-to-end law; a file that reaches both is refused; a file that reaches neither is a Node law. "Reaches" follows the
|
|
8
|
+
// project's own import graph: a helper module the law imports, or a re-export, counts. Imports used only as types, and
|
|
9
|
+
// modules under node_modules, do not.
|
|
10
|
+
//
|
|
11
|
+
// The `.browser.laws.ts` and `.e2e.laws.ts` names stay as hints. They never override an import, and a hint that
|
|
12
|
+
// disagrees with the imports is reported.
|
|
13
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
14
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
15
|
+
import ts from 'typescript';
|
|
16
|
+
import { suffixKind } from "./lawNames.js";
|
|
17
|
+
const browserEntry = /^tslaws\/(?:browser|adapters\/(?:dom|react|angular|element))$/;
|
|
18
|
+
const e2eEntry = 'tslaws/e2e';
|
|
19
|
+
/** Every module specifier of a file: imports, re-exports, `import x = require()`, `require()` and `import()`. */
|
|
20
|
+
export function moduleSpecifiers(text, fileName = 'laws.ts') {
|
|
21
|
+
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
22
|
+
const found = [];
|
|
23
|
+
const literal = (node, typeOnly) => {
|
|
24
|
+
if (node !== undefined && ts.isStringLiteralLike(node))
|
|
25
|
+
found.push({ value: node.text, start: node.getStart(source) + 1, end: node.getEnd() - 1, typeOnly });
|
|
26
|
+
};
|
|
27
|
+
const visit = (node) => {
|
|
28
|
+
if (ts.isImportDeclaration(node)) {
|
|
29
|
+
const clause = node.importClause;
|
|
30
|
+
const bindings = clause?.namedBindings;
|
|
31
|
+
const typeOnly = clause !== undefined &&
|
|
32
|
+
(clause.isTypeOnly ||
|
|
33
|
+
(clause.name === undefined &&
|
|
34
|
+
bindings !== undefined &&
|
|
35
|
+
ts.isNamedImports(bindings) &&
|
|
36
|
+
bindings.elements.length > 0 &&
|
|
37
|
+
bindings.elements.every((e) => e.isTypeOnly)));
|
|
38
|
+
literal(node.moduleSpecifier, typeOnly);
|
|
39
|
+
}
|
|
40
|
+
else if (ts.isExportDeclaration(node)) {
|
|
41
|
+
const clause = node.exportClause;
|
|
42
|
+
literal(node.moduleSpecifier, node.isTypeOnly ||
|
|
43
|
+
(clause !== undefined &&
|
|
44
|
+
ts.isNamedExports(clause) &&
|
|
45
|
+
clause.elements.length > 0 &&
|
|
46
|
+
clause.elements.every((e) => e.isTypeOnly)));
|
|
47
|
+
}
|
|
48
|
+
else if (ts.isImportEqualsDeclaration(node)) {
|
|
49
|
+
if (ts.isExternalModuleReference(node.moduleReference))
|
|
50
|
+
literal(node.moduleReference.expression, node.isTypeOnly);
|
|
51
|
+
}
|
|
52
|
+
else if (ts.isCallExpression(node) && node.arguments.length >= 1) {
|
|
53
|
+
const callee = node.expression;
|
|
54
|
+
if (callee.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(callee) && callee.text === 'require'))
|
|
55
|
+
literal(node.arguments[0], false);
|
|
56
|
+
}
|
|
57
|
+
else if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument)) {
|
|
58
|
+
literal(node.argument.literal, true);
|
|
59
|
+
}
|
|
60
|
+
ts.forEachChild(node, visit);
|
|
61
|
+
};
|
|
62
|
+
visit(source);
|
|
63
|
+
return found;
|
|
64
|
+
}
|
|
65
|
+
/** The module specifiers a file loads at run time: static imports, re-exports, `require` and dynamic `import()`. */
|
|
66
|
+
export function runtimeSpecifiers(text, fileName = 'laws.ts') {
|
|
67
|
+
return moduleSpecifiers(text, fileName).filter((s) => !s.typeOnly);
|
|
68
|
+
}
|
|
69
|
+
const resolveOptions = {
|
|
70
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
71
|
+
module: ts.ModuleKind.ESNext,
|
|
72
|
+
target: ts.ScriptTarget.ES2022,
|
|
73
|
+
allowJs: true,
|
|
74
|
+
resolveJsonModule: false,
|
|
75
|
+
};
|
|
76
|
+
/** The project source a specifier names, or undefined for a package, a declaration file or something unresolved. */
|
|
77
|
+
function projectSource(specifier, from) {
|
|
78
|
+
if (!specifier.startsWith('.') && !specifier.startsWith('/'))
|
|
79
|
+
return undefined;
|
|
80
|
+
const resolved = ts.resolveModuleName(specifier, from, resolveOptions, ts.sys).resolvedModule;
|
|
81
|
+
if (resolved === undefined || resolved.isExternalLibraryImport === true)
|
|
82
|
+
return undefined;
|
|
83
|
+
const file = resolved.resolvedFileName;
|
|
84
|
+
if (/[\\/]node_modules[\\/]/.test(file) || /\.d\.[cm]?ts$/.test(file) || !existsSync(file))
|
|
85
|
+
return undefined;
|
|
86
|
+
return file;
|
|
87
|
+
}
|
|
88
|
+
/** Walks the import graph from `file` and finds the first chain that reaches each level's entry point. */
|
|
89
|
+
export function importFacts(cwd, file) {
|
|
90
|
+
const start = resolve(cwd, file);
|
|
91
|
+
const facts = { browser: undefined, e2e: undefined };
|
|
92
|
+
const seen = new Set([start]);
|
|
93
|
+
const queue = [{ path: start, chain: [file.replaceAll('\\', '/')] }];
|
|
94
|
+
for (let next = queue.shift(); next !== undefined; next = queue.shift()) {
|
|
95
|
+
let text;
|
|
96
|
+
try {
|
|
97
|
+
text = readFileSync(next.path, 'utf8');
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
for (const { value } of runtimeSpecifiers(text, next.path)) {
|
|
103
|
+
if (browserEntry.test(value))
|
|
104
|
+
facts.browser ??= next.chain;
|
|
105
|
+
else if (value === e2eEntry)
|
|
106
|
+
facts.e2e ??= next.chain;
|
|
107
|
+
const target = projectSource(value, join(dirname(next.path), 'x.ts'));
|
|
108
|
+
if (target === undefined || seen.has(target))
|
|
109
|
+
continue;
|
|
110
|
+
seen.add(target);
|
|
111
|
+
const inside = relative(resolve(cwd), resolve(target));
|
|
112
|
+
const shown = inside.startsWith('..') || isAbsolute(inside) ? target : inside;
|
|
113
|
+
queue.push({ path: target, chain: [...next.chain, shown.replaceAll('\\', '/')] });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return facts;
|
|
117
|
+
}
|
|
118
|
+
const via = (chain) => (chain.length <= 1 ? 'directly' : `through ${chain.slice(1).join(' > ')}`);
|
|
119
|
+
const importsModule = (text, module) => new RegExp(`(?:from|import)\\s*['"]${module}['"]`).test(text);
|
|
120
|
+
/** The level a file's name gives it: the way levels were always decided, and still the default. */
|
|
121
|
+
function classifyByName(cwd, file) {
|
|
122
|
+
const imports = { browser: undefined, e2e: undefined };
|
|
123
|
+
let text = '';
|
|
124
|
+
try {
|
|
125
|
+
text = readFileSync(resolve(cwd, file), 'utf8');
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// The compiler already reports an unreadable file.
|
|
129
|
+
}
|
|
130
|
+
const base = file.replaceAll('\\', '/');
|
|
131
|
+
if (importsModule(text, 'tslaws/e2e') && importsModule(text, 'tslaws/browser'))
|
|
132
|
+
return {
|
|
133
|
+
kind: 'node',
|
|
134
|
+
imports,
|
|
135
|
+
notices: [],
|
|
136
|
+
error: `${base}: a browser laws file is either component level (tslaws/browser) or end-to-end (tslaws/e2e), not both`,
|
|
137
|
+
};
|
|
138
|
+
const hint = suffixKind(file);
|
|
139
|
+
const kind = hint === 'e2e' ? 'e2e' : hint === 'browser' ? (importsModule(text, 'tslaws/e2e') ? 'e2e' : 'browser') : 'node';
|
|
140
|
+
return { kind, imports, notices: [] };
|
|
141
|
+
}
|
|
142
|
+
/** Classifies a laws file (project-relative path) by its name (default) or, with `levels: "imports"`, its imports. */
|
|
143
|
+
export function classifyLawFile(cwd, file, mode = 'names') {
|
|
144
|
+
if (mode === 'names')
|
|
145
|
+
return classifyByName(cwd, file);
|
|
146
|
+
return classifyByImports(cwd, file);
|
|
147
|
+
}
|
|
148
|
+
/** Classifies a laws file by its imports, checking the name hint against them. */
|
|
149
|
+
function classifyByImports(cwd, file) {
|
|
150
|
+
const imports = importFacts(cwd, file);
|
|
151
|
+
const hint = suffixKind(file);
|
|
152
|
+
const notices = [];
|
|
153
|
+
const base = file.replaceAll('\\', '/');
|
|
154
|
+
if (imports.browser !== undefined && imports.e2e !== undefined)
|
|
155
|
+
return {
|
|
156
|
+
kind: 'node',
|
|
157
|
+
imports,
|
|
158
|
+
notices,
|
|
159
|
+
error: `${base}: a laws file is either component level (tslaws/browser) or end-to-end (tslaws/e2e), not both ` +
|
|
160
|
+
`(browser API reached ${via(imports.browser)}; end-to-end API reached ${via(imports.e2e)})`,
|
|
161
|
+
};
|
|
162
|
+
const kind = imports.e2e !== undefined ? 'e2e' : imports.browser !== undefined ? 'browser' : 'node';
|
|
163
|
+
if (hint === undefined || hint === kind)
|
|
164
|
+
return { kind, imports, notices };
|
|
165
|
+
if (kind === 'node') {
|
|
166
|
+
// Nothing imported says which level, so the name still stands: a law may use the page's globals directly.
|
|
167
|
+
notices.push(`${base}: the name says ${hint}, but nothing it imports is the ${hint === 'e2e' ? 'end-to-end' : 'browser'} law API; ` +
|
|
168
|
+
`the name is used as the level. Import ${hint === 'e2e' ? 'tslaws/e2e' : 'tslaws/browser'} (the name is only a hint)`);
|
|
169
|
+
return { kind: hint, imports, notices };
|
|
170
|
+
}
|
|
171
|
+
if (hint === 'browser') {
|
|
172
|
+
// A browser-named file that drives an app is an end-to-end law, as it always was.
|
|
173
|
+
notices.push(`${base}: the name says browser, but it reaches the end-to-end API ${via(imports.e2e)}; it runs end to end`);
|
|
174
|
+
return { kind, imports, notices };
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
kind,
|
|
178
|
+
imports,
|
|
179
|
+
notices,
|
|
180
|
+
error: `${base}: the name says end-to-end, but it reaches the browser API ${via(imports.browser)} and not tslaws/e2e; rename it or fix its imports`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/** The project sources a file imports directly, project-relative, in the order they are imported. */
|
|
184
|
+
export function directProjectImports(cwd, file) {
|
|
185
|
+
const from = resolve(cwd, file);
|
|
186
|
+
const found = [];
|
|
187
|
+
let text;
|
|
188
|
+
try {
|
|
189
|
+
text = readFileSync(from, 'utf8');
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
return found;
|
|
193
|
+
}
|
|
194
|
+
for (const { value } of moduleSpecifiers(text, from)) {
|
|
195
|
+
const target = projectSource(value, from);
|
|
196
|
+
if (target === undefined)
|
|
197
|
+
continue;
|
|
198
|
+
const inside = relative(resolve(cwd), resolve(target)).replaceAll('\\', '/');
|
|
199
|
+
if (!inside.startsWith('..') && !isAbsolute(inside) && !found.includes(inside))
|
|
200
|
+
found.push(inside);
|
|
201
|
+
}
|
|
202
|
+
return found;
|
|
203
|
+
}
|
|
204
|
+
/** The resolved level of each laws file, as the lock records it; none when levels come from names (the default). */
|
|
205
|
+
export function lockLevels(cwd, laws, mode) {
|
|
206
|
+
if (mode !== 'imports')
|
|
207
|
+
return undefined;
|
|
208
|
+
const files = [...new Set(laws.map((l) => l.file.replaceAll(String.fromCharCode(92), '/')))];
|
|
209
|
+
return Object.fromEntries(files.map((f) => [f, classifyLawFile(cwd, f, mode).kind]));
|
|
210
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Whether the file is a proposed laws file, of any kind. */
|
|
2
|
+
export declare function isProposedLawFile(file: string): boolean;
|
|
3
|
+
/** The accepted name a proposed file stands for; other names come back unchanged. */
|
|
4
|
+
export declare function acceptedLawName(file: string): string;
|
|
5
|
+
/** The level a file name hints at: `.browser.laws.ts` and `.e2e.laws.ts` (proposed or accepted). Only a hint; imports decide. */
|
|
6
|
+
export declare function suffixKind(file: string): 'browser' | 'e2e' | undefined;
|
package/dist/lawNames.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Law file names. By default a law's level (Node, browser or end to end) comes from the name: `.browser.` and `.e2e.`
|
|
2
|
+
// before `.laws.ts`; with `levels: "imports"` in tslaws.json it comes from the imports (see lawKinds.ts) and the suffixes are
|
|
3
|
+
// hints. A proposed file is the accepted name with `.proposed` before `.laws`: `a.proposed.laws.ts` proposes
|
|
4
|
+
// `a.laws.ts`, `a.browser.proposed.laws.ts` proposes `a.browser.laws.ts` and `a.e2e.proposed.laws.ts` proposes
|
|
5
|
+
// `a.e2e.laws.ts`. A proposed file keeps the kind of the file it proposes, so it runs at the same level.
|
|
6
|
+
/** Whether the file is a proposed laws file, of any kind. */
|
|
7
|
+
export function isProposedLawFile(file) {
|
|
8
|
+
return file.endsWith('.proposed.laws.ts');
|
|
9
|
+
}
|
|
10
|
+
/** The accepted name a proposed file stands for; other names come back unchanged. */
|
|
11
|
+
export function acceptedLawName(file) {
|
|
12
|
+
return isProposedLawFile(file) ? `${file.slice(0, -'.proposed.laws.ts'.length)}.laws.ts` : file;
|
|
13
|
+
}
|
|
14
|
+
/** The level a file name hints at: `.browser.laws.ts` and `.e2e.laws.ts` (proposed or accepted). Only a hint; imports decide. */
|
|
15
|
+
export function suffixKind(file) {
|
|
16
|
+
const accepted = acceptedLawName(file);
|
|
17
|
+
if (/\.e2e\.laws\.[cm]?ts$/.test(accepted))
|
|
18
|
+
return 'e2e';
|
|
19
|
+
if (/\.browser\.laws\.[cm]?ts$/.test(accepted))
|
|
20
|
+
return 'browser';
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
package/dist/list.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type LawKind } from './lawKinds.ts';
|
|
2
|
+
export interface ListedFile {
|
|
3
|
+
/** Project-relative path with forward slashes. */
|
|
4
|
+
file: string;
|
|
5
|
+
/** The level it runs at: from its name by default, from what it imports with `levels: "imports"`. */
|
|
6
|
+
kind: LawKind;
|
|
7
|
+
/**
|
|
8
|
+
* `accepted`: the lock has laws of this file. `unaccepted`: in the tree, but the lock has none of it, so `check`
|
|
9
|
+
* fails it until a person runs `tslaws accept`. `proposed`: a proposal (in `.tslaws/proposals`, or an
|
|
10
|
+
* in-tree `*.proposed.laws.ts`); no lock, no coverage.
|
|
11
|
+
*/
|
|
12
|
+
status: 'accepted' | 'unaccepted' | 'proposed';
|
|
13
|
+
/** Set when the file cannot run at one level (it reaches both APIs). */
|
|
14
|
+
error?: string;
|
|
15
|
+
/** Where a proposal lives: `proposals` (the directory) or `name` (an in-tree `*.proposed.laws.ts` name). */
|
|
16
|
+
proposal?: 'proposals' | 'name';
|
|
17
|
+
notices: string[];
|
|
18
|
+
}
|
|
19
|
+
/** Every laws file and proposal of the project, sorted by path, accepted and unaccepted before proposed. */
|
|
20
|
+
export declare function listLawFiles(cwd: string): ListedFile[];
|
|
21
|
+
/** One line per file: `kind status path`. */
|
|
22
|
+
export declare function renderList(files: ListedFile[]): string;
|
package/dist/list.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// `tslaws list`: the laws files of the project with the level each runs at and where it stands, so an editor or an agent
|
|
2
|
+
// can ask the tool instead of matching file names. It reads files and the lock; it compiles and runs nothing.
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { discoverLaws, discoverProposals } from "./lawFiles.js";
|
|
5
|
+
import { readConfig } from "./coverage.js";
|
|
6
|
+
import { classifyLawFile } from "./lawKinds.js";
|
|
7
|
+
import { readLock } from "./lock.js";
|
|
8
|
+
/** Every laws file and proposal of the project, sorted by path, accepted and unaccepted before proposed. */
|
|
9
|
+
export function listLawFiles(cwd) {
|
|
10
|
+
const mode = readConfig(cwd).levels ?? 'names';
|
|
11
|
+
const lock = readLock(join(cwd, 'tsc-laws.lock'));
|
|
12
|
+
const locked = new Set(Object.keys(lock?.laws ?? {}).map((key) => key.slice(0, key.indexOf('::'))));
|
|
13
|
+
const entry = (file, status, proposal) => {
|
|
14
|
+
const verdict = classifyLawFile(cwd, file, mode);
|
|
15
|
+
return {
|
|
16
|
+
file,
|
|
17
|
+
kind: verdict.kind,
|
|
18
|
+
status,
|
|
19
|
+
...(verdict.error === undefined ? {} : { error: verdict.error }),
|
|
20
|
+
...(proposal === undefined ? {} : { proposal }),
|
|
21
|
+
notices: verdict.notices,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
return [
|
|
25
|
+
...discoverLaws(cwd).map((file) => entry(file, locked.has(file) ? 'accepted' : 'unaccepted')),
|
|
26
|
+
...discoverLaws(cwd, true).map((file) => entry(file, 'proposed', 'name')),
|
|
27
|
+
...discoverProposals(cwd).map((file) => entry(file, 'proposed', 'proposals')),
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
/** One line per file: `kind status path`. */
|
|
31
|
+
export function renderList(files) {
|
|
32
|
+
if (files.length === 0)
|
|
33
|
+
return 'no laws files found\n';
|
|
34
|
+
const width = Math.max(...files.map((f) => f.kind.length));
|
|
35
|
+
return `${files.map((f) => `${f.kind.padEnd(width)} ${f.status.padEnd(10)} ${f.file}${f.error === undefined ? '' : ` (${f.error})`}`).join('\n')}\n`;
|
|
36
|
+
}
|
package/dist/lock.d.ts
CHANGED
|
@@ -4,13 +4,49 @@ export interface Lock {
|
|
|
4
4
|
laws: LockEntries;
|
|
5
5
|
/** `file::name` of the functions a person exempted from coverage. */
|
|
6
6
|
exempt: string[];
|
|
7
|
+
/**
|
|
8
|
+
* Only in a lock accepted with `levels: "imports"`: the level each laws file resolved to. A lock accepted by name
|
|
9
|
+
* (the default) has no such section and keeps the format it always had.
|
|
10
|
+
*/
|
|
11
|
+
levels?: Record<string, string>;
|
|
7
12
|
}
|
|
8
13
|
export declare function lawKey(file: string, name: string): string;
|
|
9
14
|
export declare function lawHash(law: ManifestLaw): string;
|
|
10
15
|
export declare function readLock(path: string): Lock | undefined;
|
|
11
|
-
export declare function writeLock(path: string, laws: ManifestLaw[], exempt?: string[]): void;
|
|
16
|
+
export declare function writeLock(path: string, laws: ManifestLaw[], exempt?: string[], levels?: Record<string, string>): void;
|
|
12
17
|
export interface LockViolation {
|
|
13
18
|
key: string;
|
|
14
|
-
reason: 'changed' | 'added' | 'removed';
|
|
19
|
+
reason: 'changed' | 'added' | 'removed' | 'level';
|
|
15
20
|
}
|
|
16
|
-
|
|
21
|
+
/**
|
|
22
|
+
* `levels` is the level each laws file resolves to now, or undefined when levels come from names and the lock has no
|
|
23
|
+
* levels section (then nothing about levels is compared). A law whose file's level is not the locked one, or is not
|
|
24
|
+
* locked at all, is a violation until a person accepts again.
|
|
25
|
+
*/
|
|
26
|
+
export declare function compareLock(lock: LockEntries | undefined, laws: ManifestLaw[], levels?: {
|
|
27
|
+
locked: Record<string, string> | undefined;
|
|
28
|
+
now: Record<string, string>;
|
|
29
|
+
}): LockViolation[];
|
|
30
|
+
/**
|
|
31
|
+
* One difference between the lock and the project: a law added, changed, removed or moved to another level, or a
|
|
32
|
+
* coverage exemption added or removed. `fingerprint` names the exact content a person accepts, so an approval given for
|
|
33
|
+
* one version of a law never accepts a later edit of it.
|
|
34
|
+
*/
|
|
35
|
+
export interface LockChange {
|
|
36
|
+
key: string;
|
|
37
|
+
kind: 'law' | 'exempt';
|
|
38
|
+
reason: 'added' | 'changed' | 'removed' | 'level';
|
|
39
|
+
fingerprint: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Every change a person would record by accepting the project as it is now. `levels` is the level of each laws file
|
|
43
|
+
* now, or undefined when the lock does not record levels.
|
|
44
|
+
*/
|
|
45
|
+
export declare function lockChanges(lock: Lock | undefined, laws: ManifestLaw[], exempt: string[], levels?: Record<string, string>): LockChange[];
|
|
46
|
+
/**
|
|
47
|
+
* The lock after accepting only the changes whose fingerprints are in `accepted`; every other entry keeps what the lock
|
|
48
|
+
* had. Levels are recorded per laws file, so a file's level is taken from now once every law change in it is accepted.
|
|
49
|
+
*/
|
|
50
|
+
export declare function acceptChanges(lock: Lock | undefined, laws: ManifestLaw[], exempt: string[], levels: Record<string, string> | undefined, accepted: ReadonlySet<string>): Lock;
|
|
51
|
+
/** Writes a lock value in the same canonical form as `writeLock`. */
|
|
52
|
+
export declare function writeLockValue(path: string, lock: Lock): void;
|
package/dist/lock.js
CHANGED
|
@@ -13,15 +13,29 @@ export function readLock(path) {
|
|
|
13
13
|
if (!existsSync(path))
|
|
14
14
|
return undefined;
|
|
15
15
|
const lock = JSON.parse(readFileSync(path, 'utf8'));
|
|
16
|
-
return { laws: lock.laws, exempt: lock.exempt ?? [] };
|
|
16
|
+
return { laws: lock.laws, exempt: lock.exempt ?? [], ...(lock.levels === undefined ? {} : { levels: lock.levels }) };
|
|
17
17
|
}
|
|
18
|
-
export function writeLock(path, laws, exempt = []) {
|
|
18
|
+
export function writeLock(path, laws, exempt = [], levels) {
|
|
19
19
|
const entries = {};
|
|
20
20
|
for (const law of [...laws].sort((a, b) => lawKey(a.file, a.name).localeCompare(lawKey(b.file, b.name))))
|
|
21
21
|
entries[lawKey(law.file, law.name)] = lawHash(law);
|
|
22
|
-
|
|
22
|
+
const recorded = {};
|
|
23
|
+
for (const file of Object.keys(levels ?? {}).sort())
|
|
24
|
+
recorded[file.replaceAll('\\', '/')] = levels[file];
|
|
25
|
+
const body = {
|
|
26
|
+
version: 1,
|
|
27
|
+
laws: entries,
|
|
28
|
+
exempt: [...exempt].sort(),
|
|
29
|
+
...(levels === undefined ? {} : { levels: recorded }),
|
|
30
|
+
};
|
|
31
|
+
writeFileSync(path, JSON.stringify(body, null, 2) + '\n');
|
|
23
32
|
}
|
|
24
|
-
|
|
33
|
+
/**
|
|
34
|
+
* `levels` is the level each laws file resolves to now, or undefined when levels come from names and the lock has no
|
|
35
|
+
* levels section (then nothing about levels is compared). A law whose file's level is not the locked one, or is not
|
|
36
|
+
* locked at all, is a violation until a person accepts again.
|
|
37
|
+
*/
|
|
38
|
+
export function compareLock(lock, laws, levels) {
|
|
25
39
|
const violations = [];
|
|
26
40
|
const seen = new Set();
|
|
27
41
|
for (const law of laws) {
|
|
@@ -32,9 +46,98 @@ export function compareLock(lock, laws) {
|
|
|
32
46
|
violations.push({ key, reason: 'added' });
|
|
33
47
|
else if (locked !== lawHash(law))
|
|
34
48
|
violations.push({ key, reason: 'changed' });
|
|
49
|
+
else if (levels !== undefined) {
|
|
50
|
+
const file = law.file.replaceAll('\\', '/');
|
|
51
|
+
if (levels.locked?.[file] !== levels.now[file])
|
|
52
|
+
violations.push({ key, reason: 'level' });
|
|
53
|
+
}
|
|
35
54
|
}
|
|
36
55
|
for (const key of Object.keys(lock ?? {}))
|
|
37
56
|
if (!seen.has(key))
|
|
38
57
|
violations.push({ key, reason: 'removed' });
|
|
39
58
|
return violations;
|
|
40
59
|
}
|
|
60
|
+
const fingerprintOf = (...parts) => createHash('sha256').update(parts.join('\u0000')).digest('hex').slice(0, 16);
|
|
61
|
+
/**
|
|
62
|
+
* Every change a person would record by accepting the project as it is now. `levels` is the level of each laws file
|
|
63
|
+
* now, or undefined when the lock does not record levels.
|
|
64
|
+
*/
|
|
65
|
+
export function lockChanges(lock, laws, exempt, levels) {
|
|
66
|
+
const byKey = new Map(laws.map((law) => [lawKey(law.file, law.name), law]));
|
|
67
|
+
const compared = compareLock(lock?.laws, laws, levels === undefined ? undefined : { locked: lock?.levels, now: levels });
|
|
68
|
+
const changes = compared.map((violation) => {
|
|
69
|
+
const law = byKey.get(violation.key);
|
|
70
|
+
const hash = law === undefined ? 'removed' : lawHash(law);
|
|
71
|
+
const level = law === undefined ? '' : (levels?.[law.file.replaceAll('\\', '/')] ?? '');
|
|
72
|
+
return {
|
|
73
|
+
key: violation.key,
|
|
74
|
+
kind: 'law',
|
|
75
|
+
reason: violation.reason,
|
|
76
|
+
fingerprint: fingerprintOf('law', violation.key, hash, level),
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
const locked = new Set(lock?.exempt ?? []);
|
|
80
|
+
const now = new Set(exempt);
|
|
81
|
+
for (const key of [...now].filter((k) => !locked.has(k)).sort())
|
|
82
|
+
changes.push({ key, kind: 'exempt', reason: 'added', fingerprint: fingerprintOf('exempt', key, 'added') });
|
|
83
|
+
for (const key of [...locked].filter((k) => !now.has(k)).sort())
|
|
84
|
+
changes.push({ key, kind: 'exempt', reason: 'removed', fingerprint: fingerprintOf('exempt', key, 'removed') });
|
|
85
|
+
return changes;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The lock after accepting only the changes whose fingerprints are in `accepted`; every other entry keeps what the lock
|
|
89
|
+
* had. Levels are recorded per laws file, so a file's level is taken from now once every law change in it is accepted.
|
|
90
|
+
*/
|
|
91
|
+
export function acceptChanges(lock, laws, exempt, levels, accepted) {
|
|
92
|
+
const changes = lockChanges(lock, laws, exempt, levels);
|
|
93
|
+
const taken = changes.filter((c) => accepted.has(c.fingerprint));
|
|
94
|
+
const byKey = new Map(laws.map((law) => [lawKey(law.file, law.name), law]));
|
|
95
|
+
const entries = { ...lock?.laws };
|
|
96
|
+
const exemptions = new Set(lock?.exempt ?? []);
|
|
97
|
+
for (const change of taken) {
|
|
98
|
+
if (change.kind === 'exempt') {
|
|
99
|
+
if (change.reason === 'added')
|
|
100
|
+
exemptions.add(change.key);
|
|
101
|
+
else
|
|
102
|
+
exemptions.delete(change.key);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const law = byKey.get(change.key);
|
|
106
|
+
if (law === undefined)
|
|
107
|
+
delete entries[change.key];
|
|
108
|
+
else
|
|
109
|
+
entries[change.key] = lawHash(law);
|
|
110
|
+
}
|
|
111
|
+
let recorded;
|
|
112
|
+
if (levels !== undefined) {
|
|
113
|
+
recorded = { ...lock?.levels };
|
|
114
|
+
const fileOf = (key) => key.slice(0, key.lastIndexOf('::'));
|
|
115
|
+
const open = new Set(changes.filter((c) => c.kind === 'law' && !accepted.has(c.fingerprint)).map((c) => fileOf(c.key)));
|
|
116
|
+
for (const [file, level] of Object.entries(levels))
|
|
117
|
+
if (!open.has(file))
|
|
118
|
+
recorded[file] = level;
|
|
119
|
+
for (const file of Object.keys(recorded))
|
|
120
|
+
if (!(file in levels) && !Object.keys(entries).some((key) => fileOf(key) === file))
|
|
121
|
+
delete recorded[file];
|
|
122
|
+
}
|
|
123
|
+
return { laws: entries, exempt: [...exemptions], ...(recorded === undefined ? {} : { levels: recorded }) };
|
|
124
|
+
}
|
|
125
|
+
/** Writes a lock value in the same canonical form as `writeLock`. */
|
|
126
|
+
export function writeLockValue(path, lock) {
|
|
127
|
+
const laws = {};
|
|
128
|
+
for (const key of Object.keys(lock.laws).sort((a, b) => a.localeCompare(b)))
|
|
129
|
+
laws[key] = lock.laws[key];
|
|
130
|
+
const body = {
|
|
131
|
+
version: 1,
|
|
132
|
+
laws,
|
|
133
|
+
exempt: [...lock.exempt].sort(),
|
|
134
|
+
...(lock.levels === undefined
|
|
135
|
+
? {}
|
|
136
|
+
: {
|
|
137
|
+
levels: Object.fromEntries(Object.keys(lock.levels)
|
|
138
|
+
.sort()
|
|
139
|
+
.map((f) => [f, lock.levels[f]])),
|
|
140
|
+
}),
|
|
141
|
+
};
|
|
142
|
+
writeFileSync(path, JSON.stringify(body, null, 2) + '\n');
|
|
143
|
+
}
|
package/dist/lower.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ManifestLaw } from './types.ts';
|
|
2
|
+
export interface Lowered {
|
|
3
|
+
/** The file text with every law call replaced by an exported function. */
|
|
4
|
+
text: string;
|
|
5
|
+
laws: ManifestLaw[];
|
|
6
|
+
}
|
|
7
|
+
/** The pre-call syntax (`export law name(...)`), which only the tsc-laws compiler parses. */
|
|
8
|
+
export declare function usesLegacySyntax(text: string): boolean;
|
|
9
|
+
/** Lowers the law calls of one file; `file` is the project-relative path the results carry. */
|
|
10
|
+
export declare function lowerLawFile(file: string, text: string): Lowered;
|