@yearofthedan/warp 0.0.0-stage → 0.1.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.
Files changed (130) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +97 -2
  3. package/dist/analysis/complexity.d.ts +4 -0
  4. package/dist/analysis/complexity.js +84 -0
  5. package/dist/analysis/folders.d.ts +2 -0
  6. package/dist/analysis/folders.js +15 -0
  7. package/dist/analysis/imports.d.ts +13 -0
  8. package/dist/analysis/imports.js +135 -0
  9. package/dist/analysis/members.d.ts +4 -0
  10. package/dist/analysis/members.js +190 -0
  11. package/dist/analysis/source.d.ts +13 -0
  12. package/dist/analysis/source.js +29 -0
  13. package/dist/analysis/specifiers.d.ts +2 -0
  14. package/dist/analysis/specifiers.js +6 -0
  15. package/dist/analysis/typescript.d.ts +2 -0
  16. package/dist/analysis/typescript.js +105 -0
  17. package/dist/app/check.d.ts +23 -0
  18. package/dist/app/check.js +15 -0
  19. package/dist/app/checkModel.d.ts +5 -0
  20. package/dist/app/checkModel.js +30 -0
  21. package/dist/app/diff.d.ts +25 -0
  22. package/dist/app/diff.js +53 -0
  23. package/dist/app/facts.d.ts +7 -0
  24. package/dist/app/facts.js +55 -0
  25. package/dist/app/load.d.ts +13 -0
  26. package/dist/app/load.js +76 -0
  27. package/dist/app/ports.d.ts +41 -0
  28. package/dist/app/ports.js +1 -0
  29. package/dist/app/unclassified.d.ts +5 -0
  30. package/dist/app/unclassified.js +13 -0
  31. package/dist/builtins/cognitive-complexity/cognitiveComplexity.d.ts +6 -0
  32. package/dist/builtins/cognitive-complexity/cognitiveComplexity.js +26 -0
  33. package/dist/builtins/components.d.ts +2 -0
  34. package/dist/builtins/components.js +20 -0
  35. package/dist/builtins/dependency-direction/dependencyDirection.d.ts +2 -0
  36. package/dist/builtins/dependency-direction/dependencyDirection.js +65 -0
  37. package/dist/builtins/file-cohesion/fileCohesion.d.ts +6 -0
  38. package/dist/builtins/file-cohesion/fileCohesion.js +30 -0
  39. package/dist/builtins/function-cohesion/functionCohesion.d.ts +6 -0
  40. package/dist/builtins/function-cohesion/functionCohesion.js +30 -0
  41. package/dist/builtins/index.d.ts +4 -0
  42. package/dist/builtins/index.js +30 -0
  43. package/dist/builtins/loops.d.ts +25 -0
  44. package/dist/builtins/loops.js +64 -0
  45. package/dist/builtins/module-cycle/moduleCycle.d.ts +3 -0
  46. package/dist/builtins/module-cycle/moduleCycle.js +61 -0
  47. package/dist/builtins/module-externals/moduleExternals.d.ts +2 -0
  48. package/dist/builtins/module-externals/moduleExternals.js +41 -0
  49. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +2 -0
  50. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.js +47 -0
  51. package/dist/builtins/no-cycle/noCycle.d.ts +3 -0
  52. package/dist/builtins/no-cycle/noCycle.js +39 -0
  53. package/dist/builtins/no-double-export/noDoubleExport.d.ts +2 -0
  54. package/dist/builtins/no-double-export/noDoubleExport.js +38 -0
  55. package/dist/builtins/packages.d.ts +2 -0
  56. package/dist/builtins/packages.js +5 -0
  57. package/dist/builtins/place.d.ts +2 -0
  58. package/dist/builtins/place.js +6 -0
  59. package/dist/builtins/shared.d.ts +10 -0
  60. package/dist/builtins/shared.js +13 -0
  61. package/dist/builtins/unused-declaration/declarations.d.ts +32 -0
  62. package/dist/builtins/unused-declaration/declarations.js +26 -0
  63. package/dist/builtins/unused-declaration/empty.d.ts +3 -0
  64. package/dist/builtins/unused-declaration/empty.js +22 -0
  65. package/dist/builtins/unused-declaration/grants.d.ts +3 -0
  66. package/dist/builtins/unused-declaration/grants.js +48 -0
  67. package/dist/builtins/unused-declaration/surfaces.d.ts +3 -0
  68. package/dist/builtins/unused-declaration/surfaces.js +57 -0
  69. package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +2 -0
  70. package/dist/builtins/unused-declaration/unusedDeclaration.js +14 -0
  71. package/dist/cli.d.ts +2 -0
  72. package/dist/cli.js +168 -0
  73. package/dist/config/loadConfig.d.ts +7 -0
  74. package/dist/config/loadConfig.js +53 -0
  75. package/dist/config/toolDigest.d.ts +1 -0
  76. package/dist/config/toolDigest.js +16 -0
  77. package/dist/git/cache.d.ts +3 -0
  78. package/dist/git/cache.js +41 -0
  79. package/dist/git/repository.d.ts +3 -0
  80. package/dist/git/repository.js +81 -0
  81. package/dist/index.d.ts +8 -0
  82. package/dist/index.js +4 -0
  83. package/dist/model/archetype/builder.d.ts +183 -0
  84. package/dist/model/archetype/builder.js +85 -0
  85. package/dist/model/archetype/data.d.ts +92 -0
  86. package/dist/model/archetype/data.js +1 -0
  87. package/dist/model/archetype/draft.d.ts +37 -0
  88. package/dist/model/archetype/draft.js +275 -0
  89. package/dist/model/archetype/reach.d.ts +35 -0
  90. package/dist/model/archetype/reach.js +209 -0
  91. package/dist/model/archetype/references.d.ts +13 -0
  92. package/dist/model/archetype/references.js +166 -0
  93. package/dist/model/archetype/requirements.d.ts +5 -0
  94. package/dist/model/archetype/requirements.js +69 -0
  95. package/dist/model/archetype/resolve.d.ts +30 -0
  96. package/dist/model/archetype/resolve.js +311 -0
  97. package/dist/model/archetype/rulesets.d.ts +4 -0
  98. package/dist/model/archetype/rulesets.js +115 -0
  99. package/dist/model/archetype/settings.d.ts +21 -0
  100. package/dist/model/archetype/settings.js +64 -0
  101. package/dist/model/archetype/shared.d.ts +5 -0
  102. package/dist/model/archetype/shared.js +4 -0
  103. package/dist/model/configError.d.ts +3 -0
  104. package/dist/model/configError.js +3 -0
  105. package/dist/model/facts.d.ts +57 -0
  106. package/dist/model/facts.js +1 -0
  107. package/dist/reporters/claudeHook.d.ts +2 -0
  108. package/dist/reporters/claudeHook.js +10 -0
  109. package/dist/reporters/index.d.ts +2 -0
  110. package/dist/reporters/index.js +9 -0
  111. package/dist/reporters/json.d.ts +2 -0
  112. package/dist/reporters/json.js +4 -0
  113. package/dist/reporters/model.d.ts +4 -0
  114. package/dist/reporters/model.js +60 -0
  115. package/dist/reporters/omp.d.ts +2 -0
  116. package/dist/reporters/omp.js +8 -0
  117. package/dist/reporters/reporter.d.ts +26 -0
  118. package/dist/reporters/reporter.js +14 -0
  119. package/dist/reporters/sarif.d.ts +2 -0
  120. package/dist/reporters/sarif.js +44 -0
  121. package/dist/reporters/text.d.ts +6 -0
  122. package/dist/reporters/text.js +35 -0
  123. package/dist/rules/diff.d.ts +10 -0
  124. package/dist/rules/diff.js +31 -0
  125. package/dist/rules/registry.d.ts +5 -0
  126. package/dist/rules/registry.js +33 -0
  127. package/dist/rules/rule.d.ts +59 -0
  128. package/dist/rules/rule.js +18 -0
  129. package/integrations/omp/warp.ts +40 -0
  130. package/package.json +44 -4
@@ -0,0 +1,85 @@
1
+ import { ConfigError } from '../configError.js';
2
+ const recorded = Symbol('warp.recorded');
3
+ const record = (type, data) => ({ [recorded]: { type, data } });
4
+ export const recordedAs = (value, type) => {
5
+ const entry = value?.[recorded];
6
+ return entry?.type === type ? entry.data : undefined;
7
+ };
8
+ const ref = (data) => Object.freeze(record('ref', data));
9
+ const surfaces = (base) => new Proxy(ref(base), {
10
+ get: (target, key) => (typeof key === 'string' ? ref({ ...base, surface: key }) : Reflect.get(target, key)),
11
+ });
12
+ const names = (make) => new Proxy({}, { get: (_, key) => surfaces(make(String(key))) });
13
+ export const moduleRefs = () => names((module) => ({ kind: 'module', module }));
14
+ export const requiredRefs = () => names((name) => ({ kind: 'required', name }));
15
+ export const self = Object.freeze(record('self', true));
16
+ const modules = (entries) => ({
17
+ ...record('modules', entries),
18
+ module: (name, selector, archetype, ...extra) => modules([...entries, { name, selector, archetype, extra }]),
19
+ });
20
+ export const inlineName = (holder, module) => `${holder}.${module}`;
21
+ const handles = (holder, entries, tests) => ({
22
+ ...Object.fromEntries((Array.isArray(entries) ? entries : [])
23
+ .filter((entry) => typeof entry.name === 'string')
24
+ .map((entry) => {
25
+ const handle = record('handle', { archetype: holder, module: entry.name });
26
+ const anonymous = recordedAs(entry.archetype, 'archetype')?.definition;
27
+ const nested = anonymous && anonymous.name === undefined
28
+ ? {
29
+ modules: handles(inlineName(String(holder), entry.name), anonymous.modules, anonymous.tests),
30
+ }
31
+ : {};
32
+ return [entry.name, Object.freeze({ ...handle, ...nested })];
33
+ })),
34
+ ...(tests === undefined ? {} : { tests: Object.freeze(record('handle', { archetype: holder, module: 'tests' })) }),
35
+ });
36
+ const builder = (state) => {
37
+ const { definition } = state;
38
+ const define = (field) => (value) => builder({ ...state, definition: { ...definition, [field]: value } });
39
+ return {
40
+ modules: handles(definition.name, definition.modules, definition.tests),
41
+ name: definition.name,
42
+ ...record('archetype', state),
43
+ identify: define('identify'),
44
+ requires: define('requires'),
45
+ contains: (contains) => define('modules')(recordedAs(contains(modules([]), { required: requiredRefs(), self }), 'modules')),
46
+ exposes: define('exposes'),
47
+ entry: define('entry'),
48
+ provides: define('provides'),
49
+ description: define('description'),
50
+ tests: (first, ...rest) => {
51
+ const located = recordedAs(first, 'selector') !== undefined;
52
+ const [layer, ...extra] = located ? rest : [first, ...rest];
53
+ return define('tests')({ ...(located ? { locator: first } : {}), layer, extra });
54
+ },
55
+ using: (using) => builder({ ...state, using }),
56
+ };
57
+ };
58
+ const start = (name) => builder({ definition: { name, requires: {}, modules: [] } });
59
+ export function archetype(name) {
60
+ return start(name);
61
+ }
62
+ export const using = (using) => start(undefined).using(using);
63
+ export const exposes = (exposes) => start(undefined).exposes(exposes);
64
+ export const contains = (modules) => start(undefined).contains(modules);
65
+ export const all = () => Object.freeze(record('all', true));
66
+ const selector = (kind, patterns) => Object.freeze(patterns.length === 0 ? record('shape', kind) : record('selector', { kind, patterns }));
67
+ export function dir(...paths) {
68
+ return selector('dir', paths);
69
+ }
70
+ export function files(...patterns) {
71
+ return selector('files', patterns);
72
+ }
73
+ export const pkg = (pattern) => ref({ kind: 'package', pattern });
74
+ export const defineConfig = (config) => config;
75
+ const rulesetOf = (state) => ({
76
+ ...record('ruleset', state),
77
+ rules: (settings) => rulesetOf({ ...state, entries: [...state.entries, { settings }] }),
78
+ on: (target, settings) => rulesetOf({ ...state, entries: [...state.entries, { target, settings }] }),
79
+ extends: (base) => {
80
+ if (state.base !== undefined)
81
+ throw new ConfigError('ruleset().extends: a ruleset extends one base');
82
+ return rulesetOf({ ...state, base });
83
+ },
84
+ });
85
+ export const ruleset = () => rulesetOf({ entries: [] });
@@ -0,0 +1,92 @@
1
+ export interface ModuleRefData {
2
+ kind: 'module';
3
+ module: string;
4
+ surface?: string;
5
+ }
6
+ export interface RequiredRefData {
7
+ kind: 'required';
8
+ name: string;
9
+ surface?: string;
10
+ }
11
+ export interface PackageRefData {
12
+ kind: 'package';
13
+ pattern: string;
14
+ }
15
+ export type RefData = ModuleRefData | RequiredRefData | PackageRefData;
16
+ export type RuleSettingsData = 'off' | Record<string, boolean | Record<string, unknown>>;
17
+ export type ExposesData = {
18
+ kind: 'all';
19
+ } | {
20
+ kind: 'files';
21
+ patterns: string[];
22
+ entry?: true;
23
+ } | {
24
+ kind: 'surfaces';
25
+ surfaces: Record<string, RefData[]>;
26
+ };
27
+ export interface LocatorData {
28
+ kind: 'dir' | 'files';
29
+ patterns: string[];
30
+ }
31
+ export interface TestLayerData {
32
+ locator?: LocatorData;
33
+ modules: FilesModuleData[];
34
+ using: RefData[];
35
+ exposes?: ExposesData;
36
+ rules?: RuleSettingsData;
37
+ }
38
+ export interface FilesModuleData {
39
+ kind: 'files';
40
+ name: string;
41
+ patterns: string[];
42
+ using: RefData[];
43
+ exposes?: Exclude<ExposesData, {
44
+ kind: 'surfaces';
45
+ }>;
46
+ tests?: TestLayerData;
47
+ rules?: RuleSettingsData;
48
+ description?: string;
49
+ }
50
+ export interface DirModuleData {
51
+ kind: 'dir';
52
+ name: string;
53
+ paths: string[];
54
+ archetype: string;
55
+ using: RefData[];
56
+ rules?: RuleSettingsData;
57
+ description?: string;
58
+ }
59
+ export type ModuleData = FilesModuleData | DirModuleData;
60
+ export type RequirementData = {
61
+ kind: 'archetype';
62
+ archetype: string;
63
+ } | {
64
+ kind: 'dir';
65
+ } | {
66
+ kind: 'files';
67
+ };
68
+ export type IdentifyData = {
69
+ marker: string;
70
+ } | {
71
+ match: (dir: string, chain: readonly unknown[]) => false | Record<string, string>;
72
+ };
73
+ export interface ArchetypeData {
74
+ name: string;
75
+ inline?: true;
76
+ description?: string;
77
+ identify?: IdentifyData;
78
+ requires: Record<string, RequirementData>;
79
+ modules: ModuleData[];
80
+ exposes?: ExposesData;
81
+ provides: Record<string, ModuleRefData>;
82
+ tests?: TestLayerData;
83
+ rules?: RuleSettingsData;
84
+ }
85
+ export interface ConfigData {
86
+ file: string;
87
+ root: string;
88
+ archetypes: Record<string, ArchetypeData>;
89
+ tsconfig: string[];
90
+ tests?: string[];
91
+ rules?: RuleSettingsData;
92
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
1
+ import { type RecordedDefinition } from './builder.ts';
2
+ import type { ArchetypeData, LocatorData, ModuleData } from './data.ts';
3
+ import { type Fail } from './shared.ts';
4
+ export interface DraftTests {
5
+ locator?: LocatorData;
6
+ modules: DraftModule[];
7
+ using: unknown[];
8
+ exposes?: DraftExposes;
9
+ }
10
+ export interface DraftModule {
11
+ data: ModuleData;
12
+ using: unknown[];
13
+ tests?: DraftTests;
14
+ }
15
+ export type DraftExposes = {
16
+ kind: 'all';
17
+ } | {
18
+ kind: 'files';
19
+ patterns: string[];
20
+ entry?: true;
21
+ } | {
22
+ kind: 'surfaces';
23
+ surfaces: Record<string, unknown[]>;
24
+ };
25
+ export interface Draft {
26
+ data: ArchetypeData;
27
+ modules: DraftModule[];
28
+ exposes?: DraftExposes;
29
+ provides: Record<string, unknown>;
30
+ tests?: DraftTests;
31
+ }
32
+ export interface Collected extends RecordedDefinition {
33
+ inline?: true;
34
+ using?: unknown;
35
+ }
36
+ export declare const collect: (root: RecordedDefinition, fail: Fail) => Map<string, Collected>;
37
+ export declare const draft: (name: string, state: Collected, fail: Fail) => Draft;
@@ -0,0 +1,275 @@
1
+ import { inlineName, moduleRefs, requiredRefs, recordedAs, self, } from './builder.js';
2
+ import { isName, isRecord } from './shared.js';
3
+ export const collect = (root, fail) => {
4
+ const byName = new Map();
5
+ const pending = [root];
6
+ for (let definition = pending.shift(); definition; definition = pending.shift()) {
7
+ const known = byName.get(definition.name);
8
+ if (!isName(definition.name))
9
+ fail(`archetype(${JSON.stringify(definition.name)}): a name is a non-empty string`);
10
+ else if (known && known !== definition)
11
+ fail(`two archetypes are named ${definition.name}: give each its own name`);
12
+ else if (!known) {
13
+ byName.set(definition.name, definition);
14
+ pending.push(...reached(definition));
15
+ }
16
+ }
17
+ return byName;
18
+ };
19
+ const named = (value) => {
20
+ const definition = recordedAs(value, 'archetype')?.definition;
21
+ return definition && definition.name !== undefined ? [definition] : [];
22
+ };
23
+ const reached = (definition) => [
24
+ ...entriesOf(definition.modules).flatMap((entry) => named(entry.archetype)),
25
+ ...Object.values(isRecord(definition.requires) ? definition.requires : {}).flatMap(named),
26
+ ...inlineArchetypes(definition),
27
+ ];
28
+ const anonymousOf = (entry) => {
29
+ const definition = recordedAs(entry.archetype, 'archetype')?.definition;
30
+ return definition?.name === undefined ? definition : undefined;
31
+ };
32
+ const inlineArchetypes = (holder) => isName(holder.name)
33
+ ? entriesOf(holder.modules).flatMap((entry) => isInline(entry) && isName(entry.name)
34
+ ? [
35
+ {
36
+ ...anonymousOf(entry),
37
+ name: inlineName(holder.name, entry.name),
38
+ inline: true,
39
+ requires: holder.requires,
40
+ modules: anonymousOf(entry)?.modules ?? [],
41
+ description: undefined,
42
+ },
43
+ ]
44
+ : [])
45
+ : [];
46
+ const isInline = (entry) => (entry.archetype === undefined || anonymousOf(entry) !== undefined) &&
47
+ recordedAs(entry.selector, 'selector')?.kind === 'dir';
48
+ const entriesOf = (modules) => (Array.isArray(modules) ? modules : []);
49
+ const callback = (value) => (typeof value === 'function' ? value(moduleRefs()) : undefined);
50
+ export const draft = (name, state, fail) => {
51
+ checkFields(name, state, fail);
52
+ const identify = identifyData(name, state.identify, fail);
53
+ const requires = requirements(name, state.requires, fail);
54
+ const modules = draftModules(name, state.modules, fail);
55
+ const exposes = surfaceOf(name, state, fail);
56
+ const provides = state.provides === undefined ? {} : callback(state.provides);
57
+ if (!isRecord(provides))
58
+ fail(`${name}.provides: expected a callback returning modules by name, (m) => ({ strings: m.strings })`);
59
+ const tests = testsOf(name, state.tests, 'dir', fail);
60
+ return {
61
+ data: {
62
+ name,
63
+ ...(state.inline ? { inline: true } : {}),
64
+ description: state.description,
65
+ identify,
66
+ requires,
67
+ modules: modules.map((module) => module.data),
68
+ provides: {},
69
+ },
70
+ modules,
71
+ ...(exposes ? { exposes } : {}),
72
+ provides: isRecord(provides) ? provides : {},
73
+ ...(tests ? { tests } : {}),
74
+ };
75
+ };
76
+ const testsOf = (where, value, kind, fail) => {
77
+ if (value === undefined)
78
+ return undefined;
79
+ if (value.extra.length > 0)
80
+ fail(`${where}.tests: takes a locator and an archetype, .tests(dir('e2e'), using([...]))`);
81
+ const locator = value.locator === undefined ? undefined : selectorOf(`${where}.tests`, value.locator, fail);
82
+ const recorded = value.layer === undefined ? undefined : recordedAs(value.layer, 'archetype');
83
+ if (value.layer !== undefined && recorded?.definition.name !== undefined)
84
+ fail(`${where}.tests: a test layer is an archetype of its own: contains(...), using(...) or exposes(...)`);
85
+ const layer = recorded?.definition.name === undefined ? recorded?.definition : undefined;
86
+ if (layer)
87
+ checkLayer(`${where}.tests`, layer, fail);
88
+ const modules = layer
89
+ ? draftModules(`${where}.tests`, layer.modules, fail).map((module) => testModule(where, module, fail))
90
+ : [];
91
+ if (kind === 'files' && modules.length > 0)
92
+ fail(`${where}.tests: a files module's tests hold no modules; give them to its folder's archetype`);
93
+ if (kind === 'files' && !locator)
94
+ fail(`${where}.tests: a files module names where its tests are, .tests(files('*.logic.test.ts'))`);
95
+ const exposes = exposesOf(`${where}.tests.exposes`, layer?.exposes, true, fail);
96
+ return {
97
+ ...(locator ? { locator } : {}),
98
+ modules,
99
+ using: usingOf(`${where}.tests`, recorded?.using, fail),
100
+ ...(exposes ? { exposes } : {}),
101
+ };
102
+ };
103
+ const checkLayer = (where, layer, fail) => {
104
+ for (const field of ['identify', 'provides', 'tests', 'entry'])
105
+ if (layer[field] !== undefined)
106
+ fail(`${where}.${field}: a test layer holds test modules, its using and what it exposes`);
107
+ if (isRecord(layer.requires) && Object.keys(layer.requires).length > 0)
108
+ fail(`${where}.requires: a test layer sees what its holder requires`);
109
+ };
110
+ const testModule = (where, module, fail) => {
111
+ const at = `${where}.tests.${module.data.name}`;
112
+ if (module.data.kind !== 'files')
113
+ fail(`${at}: a test module is a files module, files('*.dsl.ts')`);
114
+ if (module.using.length > 0)
115
+ fail(`${at}: a test module sees what its layer sees; give the layer .using()`);
116
+ const data = module.data;
117
+ return { ...module, data: { ...data, exposes: data.exposes ?? { kind: 'all' } } };
118
+ };
119
+ const surfaceOf = (name, state, fail) => {
120
+ if (state.entry === undefined)
121
+ return exposesOf(`${name}.exposes`, state.exposes, true, fail);
122
+ if (state.exposes !== undefined)
123
+ fail(`${name}: an entry is the module's whole surface; give it .entry(...) or .exposes(...)`);
124
+ const selector = recordedAs(state.entry, 'selector');
125
+ if (selector?.kind === 'files' && selector.patterns.every(isName))
126
+ return { kind: 'files', patterns: selector.patterns, entry: true };
127
+ fail(`${name}.entry: expected the module's entry files, files('index.ts')`);
128
+ return undefined;
129
+ };
130
+ const exposesOf = (where, value, surfaces, fail) => {
131
+ if (value === undefined)
132
+ return undefined;
133
+ if (recordedAs(value, 'all'))
134
+ return { kind: 'all' };
135
+ const selector = recordedAs(value, 'selector');
136
+ if (selector?.kind === 'files' && selector.patterns.every(isName))
137
+ return { kind: 'files', patterns: selector.patterns };
138
+ const returned = surfaces ? callback(value) : undefined;
139
+ if (isRecord(returned))
140
+ return {
141
+ kind: 'surfaces',
142
+ surfaces: Object.fromEntries(Object.entries(returned).map(([surface, refs]) => [surface, [refs].flat()])),
143
+ };
144
+ const shapes = surfaces ? `all(), files('a.ts', ...) or (m) => ({ render: m.dom })` : `all() or files('a.ts', ...)`;
145
+ fail(`${where}: expected ${shapes}`);
146
+ return undefined;
147
+ };
148
+ const checkFields = (name, state, fail) => {
149
+ if (state.description !== undefined && typeof state.description !== 'string')
150
+ fail(`${name}.description: expected a string`);
151
+ if (!Array.isArray(state.modules))
152
+ fail(`${name}.contains: expected the modules chain, (m) => m.module(...)`);
153
+ };
154
+ const draftModules = (archetype, recorded, fail) => {
155
+ const modules = [];
156
+ for (const entry of entriesOf(recorded)) {
157
+ const module = draftModule(archetype, entry, fail);
158
+ if (modules.some((other) => other.data.name === module.data.name))
159
+ fail(`${archetype}: module '${module.data.name}' is already declared`);
160
+ else
161
+ modules.push(module);
162
+ }
163
+ return modules;
164
+ };
165
+ const draftModule = (archetype, entry, fail) => {
166
+ const { name } = entry;
167
+ if (!isName(name) || name.includes('.'))
168
+ fail(`${archetype}: a module's name is a non-empty string without dots, not ${JSON.stringify(name)}`);
169
+ const where = `${archetype}.${String(name)}`;
170
+ if (name === 'tests')
171
+ fail(`${where}: tests names a module's test layer; give the module another name`);
172
+ const { kind, patterns } = selectorOf(where, entry.selector, fail);
173
+ if (entry.extra.length > 0)
174
+ fail(`${where}: .module() takes a name, a selector and an archetype`);
175
+ const { named, anonymous, using } = archetypeOf(where, archetype, entry.archetype, fail);
176
+ const description = anonymous?.description;
177
+ const common = { name: name, using: [], ...(description === undefined ? {} : { description }) };
178
+ if (anonymous)
179
+ checkAnonymous(where, kind, anonymous, fail);
180
+ if (kind === 'dir')
181
+ return {
182
+ data: { kind, ...common, paths: patterns, archetype: named ?? inlineName(archetype, name) },
183
+ using,
184
+ };
185
+ if (named !== undefined)
186
+ fail(`${where}: an archetype makes a folder module; use dir(...), not files(...)`);
187
+ const exposes = exposesOf(`${where}.exposes`, anonymous?.exposes, false, fail);
188
+ const tests = testsOf(where, anonymous?.tests, 'files', fail);
189
+ return {
190
+ data: { kind, ...common, patterns, ...(exposes ? { exposes } : {}) },
191
+ using,
192
+ ...(tests ? { tests } : {}),
193
+ };
194
+ };
195
+ const archetypeOf = (where, holder, value, fail) => {
196
+ if (value === undefined)
197
+ return { using: [] };
198
+ if (value === self)
199
+ return { named: holder, using: [] };
200
+ const recorded = recordedAs(value, 'archetype');
201
+ if (!recorded) {
202
+ fail(`${where}: expected an archetype, such as component, using(...) or exposes(all())`);
203
+ return { using: [] };
204
+ }
205
+ const using = usingOf(where, recorded.using, fail);
206
+ const { definition } = recorded;
207
+ return definition.name === undefined ? { anonymous: definition, using } : { named: definition.name, using };
208
+ };
209
+ const checkAnonymous = (where, kind, definition, fail) => {
210
+ if (definition.description !== undefined && typeof definition.description !== 'string')
211
+ fail(`${where}.description: expected a string`);
212
+ if (isRecord(definition.requires) && Object.keys(definition.requires).length > 0)
213
+ fail(`${where}.requires: a module's own archetype sees what its holder requires; name the archetype to require more`);
214
+ if (kind === 'dir')
215
+ return;
216
+ const hasModules = Array.isArray(definition.modules) && definition.modules.length > 0;
217
+ const folderOnly = [
218
+ ...(hasModules ? ['contains'] : []),
219
+ ...['identify', 'provides', 'entry'].filter((field) => definition[field] !== undefined),
220
+ ];
221
+ for (const field of folderOnly)
222
+ fail(`${where}.${field}: a files module has no modules; use dir(...) for ${field}`);
223
+ };
224
+ const selectorOf = (where, value, fail) => {
225
+ const selector = recordedAs(value, 'selector');
226
+ if (!selector) {
227
+ fail(`${where}: expected a selector, files('*.ts') or dir('src/ui')`);
228
+ return { kind: 'files', patterns: [] };
229
+ }
230
+ const patterns = selector.patterns;
231
+ if (!patterns.every(isName))
232
+ fail(`${where}: ${selector.kind}() takes non-empty strings`);
233
+ if (selector.kind === 'dir')
234
+ checkDirPaths(where, patterns, fail);
235
+ return { kind: selector.kind, patterns };
236
+ };
237
+ const usingOf = (where, value, fail) => {
238
+ const using = typeof value === 'function' ? value(moduleRefs(), { required: requiredRefs() }) : (value ?? []);
239
+ if (Array.isArray(using))
240
+ return using;
241
+ fail(`${where}.using: expected a list of references, or a callback returning one`);
242
+ return [];
243
+ };
244
+ const checkDirPaths = (where, paths, fail) => {
245
+ for (const path of paths.filter((path) => isName(path) && !isDirPath(path)))
246
+ fail(`${where}: a dir path is folder names and {placeholders}, a whole segment each, not '${String(path)}'`);
247
+ };
248
+ const isDirPath = (path) => path.split('/').every((segment) => segment !== '.' && segment !== '..' && /^(\{\w+\}|[^{}*]+)$/.test(segment));
249
+ const requirements = (archetype, value, fail) => {
250
+ if (!isRecord(value)) {
251
+ fail(`${archetype}.requires: expected requirements by name, { testRunner, strings: dir() }`);
252
+ return {};
253
+ }
254
+ const result = {};
255
+ for (const [name, requirement] of Object.entries(value)) {
256
+ const shape = recordedAs(requirement, 'shape');
257
+ const required = recordedAs(requirement, 'archetype')?.definition.name;
258
+ if (shape)
259
+ result[name] = { kind: shape };
260
+ else if (isName(required))
261
+ result[name] = { kind: 'archetype', archetype: required };
262
+ else
263
+ fail(`${archetype}.requires.${name}: expected an archetype, dir() or files()`);
264
+ }
265
+ return result;
266
+ };
267
+ const identifyData = (archetype, value, fail) => {
268
+ const keys = isRecord(value) ? Object.keys(value) : [];
269
+ if (value === undefined || (keys.length === 1 && isName(value.marker)))
270
+ return value;
271
+ if (keys.length === 1 && typeof value.match === 'function')
272
+ return value;
273
+ fail(`${archetype}.identify: expected { marker: '{name}.dom.ts' } or { match: (dir, chain) => ... }`);
274
+ return undefined;
275
+ };
@@ -0,0 +1,35 @@
1
+ import type { ConfigData, ExposesData, RefData } from './data.ts';
2
+ import { type Model, type ModuleView } from './resolve.ts';
3
+ export interface Grant {
4
+ owner: string;
5
+ ref?: string;
6
+ files: ReadonlySet<string>;
7
+ package?: string;
8
+ declared?: RefData;
9
+ }
10
+ export declare const grantText: ({ owner, ref }: Grant) => string;
11
+ export type Refusal = {
12
+ reason: 'private';
13
+ hiddenBy: ModuleView;
14
+ } | {
15
+ reason: 'above';
16
+ } | {
17
+ reason: 'not-in-using';
18
+ } | {
19
+ reason: 'test';
20
+ };
21
+ export type Verdict = {
22
+ allowed: true;
23
+ grant: Grant;
24
+ } | ({
25
+ allowed: false;
26
+ } & Refusal);
27
+ export interface Verdicts {
28
+ reach(file: string): readonly Grant[] | undefined;
29
+ exposes(module: ModuleView): ExposesData | undefined;
30
+ surface(module: ModuleView, name?: string): ReadonlySet<string>;
31
+ verdict(from: string, to: string): Verdict | undefined;
32
+ files(module: ModuleView, ref: RefData): ReadonlySet<string>;
33
+ describe(module: ModuleView, ref: RefData): string;
34
+ }
35
+ export declare const verdicts: (config: ConfigData, model: Model) => Verdicts;