@yearofthedan/warp 0.3.0 → 0.4.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 (41) hide show
  1. package/README.md +20 -22
  2. package/dist/app/check.d.ts +3 -1
  3. package/dist/app/check.js +3 -2
  4. package/dist/app/configLines.d.ts +5 -0
  5. package/dist/app/configLines.js +24 -0
  6. package/dist/app/load.d.ts +2 -2
  7. package/dist/app/load.js +31 -43
  8. package/dist/builtins/advice.js +9 -9
  9. package/dist/builtins/dependency-direction/dependencyDirection.js +1 -1
  10. package/dist/builtins/module-externals/moduleExternals.js +3 -2
  11. package/dist/builtins/unused-declaration/empty.js +11 -10
  12. package/dist/builtins/unused-declaration/grants.js +17 -7
  13. package/dist/builtins/unused-declaration/surfaces.js +16 -6
  14. package/dist/cli.js +14 -11
  15. package/dist/config/loadConfig.d.ts +7 -1
  16. package/dist/config/loadConfig.js +83 -12
  17. package/dist/index.d.ts +0 -1
  18. package/dist/index.js +0 -1
  19. package/dist/model/archetype/data.d.ts +13 -23
  20. package/dist/model/archetype/data.js +1 -1
  21. package/dist/model/archetype/fixes.d.ts +5 -7
  22. package/dist/model/archetype/fixes.js +121 -90
  23. package/dist/model/archetype/language.d.ts +16 -0
  24. package/dist/model/archetype/language.js +419 -0
  25. package/dist/model/archetype/reach.d.ts +8 -3
  26. package/dist/model/archetype/reach.js +122 -56
  27. package/dist/model/archetype/references.d.ts +11 -5
  28. package/dist/model/archetype/references.js +59 -118
  29. package/dist/model/archetype/requirements.d.ts +3 -3
  30. package/dist/model/archetype/requirements.js +65 -58
  31. package/dist/model/archetype/resolve.d.ts +1 -0
  32. package/dist/model/archetype/resolve.js +23 -54
  33. package/dist/model/archetype/rulesets.d.ts +2 -1
  34. package/dist/model/archetype/rulesets.js +72 -42
  35. package/dist/reporters/model.js +4 -3
  36. package/dist/rules/registry.js +1 -1
  37. package/package.json +3 -2
  38. package/dist/model/archetype/builder.d.ts +0 -183
  39. package/dist/model/archetype/builder.js +0 -78
  40. package/dist/model/archetype/draft.d.ts +0 -30
  41. package/dist/model/archetype/draft.js +0 -264
@@ -1,30 +0,0 @@
1
- import { type RecordedDefinition } from './builder.ts';
2
- import type { ArchetypeData, ModuleData } from './data.ts';
3
- import { type Fail } from './shared.ts';
4
- export interface DraftModule {
5
- data: ModuleData;
6
- using: unknown[];
7
- }
8
- export type DraftExposes = {
9
- kind: 'all';
10
- } | {
11
- kind: 'files';
12
- patterns: string[];
13
- entry?: true;
14
- } | {
15
- kind: 'surfaces';
16
- surfaces: Record<string, unknown[]>;
17
- };
18
- export interface Draft {
19
- data: ArchetypeData;
20
- modules: DraftModule[];
21
- exposes?: DraftExposes;
22
- provides: Record<string, unknown>;
23
- testUsing: unknown[];
24
- }
25
- export interface Collected extends RecordedDefinition {
26
- inline?: true;
27
- using?: unknown;
28
- }
29
- export declare const collect: (root: RecordedDefinition, fail: Fail) => Map<string, Collected>;
30
- export declare const draft: (name: string, state: Collected, fail: Fail) => Draft;
@@ -1,264 +0,0 @@
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) => (entry.test ? [] : 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.test &&
47
- (entry.archetype === undefined || anonymousOf(entry) !== undefined) &&
48
- recordedAs(entry.selector, 'selector')?.kind === 'dir';
49
- const entriesOf = (modules) => (Array.isArray(modules) ? modules : []);
50
- const callback = (value) => (typeof value === 'function' ? value(moduleRefs()) : undefined);
51
- export const draft = (name, state, fail) => {
52
- checkFields(name, state, fail);
53
- const identify = identifyData(name, state.identify, fail);
54
- const requires = requirements(name, state.requires, fail);
55
- const modules = draftModules(name, state.modules, fail);
56
- const exposes = surfaceOf(name, state, fail);
57
- const provides = state.provides === undefined ? {} : callback(state.provides);
58
- if (!isRecord(provides))
59
- fail(`${name}.provides: expected a callback returning modules by name, (m) => ({ strings: m.strings })`);
60
- const testUsing = usingOf(`${name}.testUsing`, state.testUsing, fail);
61
- return {
62
- data: {
63
- name,
64
- ...(state.inline ? { inline: true } : {}),
65
- description: state.description,
66
- identify,
67
- requires,
68
- modules: modules.map((module) => module.data),
69
- provides: {},
70
- },
71
- modules,
72
- ...(exposes ? { exposes } : {}),
73
- provides: isRecord(provides) ? provides : {},
74
- testUsing,
75
- };
76
- };
77
- const surfaceOf = (name, state, fail) => {
78
- if (state.entry === undefined)
79
- return exposesOf(`${name}.exposes`, state.exposes, true, fail);
80
- if (state.exposes !== undefined)
81
- fail(`${name}: an entry is the module's whole surface; give it .entry(...) or .exposes(...)`);
82
- const selector = recordedAs(state.entry, 'selector');
83
- if (selector?.kind === 'files' && selector.patterns.every(isName))
84
- return { kind: 'files', patterns: selector.patterns, entry: true };
85
- fail(`${name}.entry: expected the module's entry files, files('index.ts')`);
86
- return undefined;
87
- };
88
- const exposesOf = (where, value, surfaces, fail) => {
89
- if (value === undefined)
90
- return undefined;
91
- if (recordedAs(value, 'all'))
92
- return { kind: 'all' };
93
- const selector = recordedAs(value, 'selector');
94
- if (selector?.kind === 'files' && selector.patterns.every(isName))
95
- return { kind: 'files', patterns: selector.patterns };
96
- const returned = surfaces ? callback(value) : undefined;
97
- if (isRecord(returned))
98
- return {
99
- kind: 'surfaces',
100
- surfaces: Object.fromEntries(Object.entries(returned).map(([surface, refs]) => [surface, [refs].flat()])),
101
- };
102
- const shapes = surfaces ? `all(), files('a.ts', ...) or (m) => ({ render: m.dom })` : `all() or files('a.ts', ...)`;
103
- fail(`${where}: expected ${shapes}`);
104
- return undefined;
105
- };
106
- const checkFields = (name, state, fail) => {
107
- if (state.description !== undefined && typeof state.description !== 'string')
108
- fail(`${name}.description: expected a string`);
109
- if (!Array.isArray(state.modules))
110
- fail(`${name}.contains: expected the modules chain, (m) => m.module(...)`);
111
- };
112
- const draftModules = (archetype, recorded, fail) => {
113
- const modules = [];
114
- for (const entry of entriesOf(recorded)) {
115
- const module = draftModule(archetype, entry, fail);
116
- if (modules.some((other) => other.data.name === module.data.name))
117
- fail(`${archetype}: module '${module.data.name}' is already declared`);
118
- else
119
- modules.push(module);
120
- }
121
- return modules;
122
- };
123
- const draftModule = (archetype, entry, fail) => {
124
- const { name } = entry;
125
- if (!isName(name) || name.includes('.'))
126
- fail(`${archetype}: a module's name is a non-empty string without dots, not ${JSON.stringify(name)}`);
127
- const where = `${archetype}.${String(name)}`;
128
- if (entry.test)
129
- return draftTestModule(archetype, entry, where, fail);
130
- const { kind, patterns } = selectorOf(where, entry.selector, fail);
131
- if (entry.extra.length > 0)
132
- fail(`${where}: .module() takes a name, a selector and an archetype`);
133
- const { named, anonymous, using } = archetypeOf(where, archetype, entry.archetype, fail);
134
- const description = anonymous?.description;
135
- const common = { name: name, using: [], ...(description === undefined ? {} : { description }) };
136
- if (anonymous)
137
- checkAnonymous(where, kind, anonymous, fail);
138
- if (kind === 'dir')
139
- return {
140
- data: { kind, ...common, paths: patterns, archetype: named ?? inlineName(archetype, name) },
141
- using,
142
- };
143
- if (named !== undefined)
144
- fail(`${where}: an archetype makes a folder module; use dir(...), not files(...)`);
145
- const exposes = exposesOf(`${where}.exposes`, anonymous?.exposes, false, fail);
146
- return { data: { kind, ...common, patterns, ...(exposes ? { exposes } : {}) }, using };
147
- };
148
- const draftTestModule = (holder, entry, where, fail) => {
149
- const locator = locatorOf(where, entry.selector, fail);
150
- if (entry.extra.length > 0)
151
- fail(`${where}: .testModule() takes a name, a selector and an archetype`);
152
- const { named, anonymous, using } = archetypeOf(where, holder, entry.archetype, fail);
153
- if (named !== undefined)
154
- fail(`${where}: a test module's archetype is its own: using(...), exposes(...) or description(...)`);
155
- if (anonymous)
156
- checkAnonymous(where, 'test', anonymous, fail);
157
- const exposes = exposesOf(`${where}.exposes`, anonymous?.exposes, false, fail);
158
- const description = anonymous?.description;
159
- return {
160
- data: {
161
- kind: 'test',
162
- name: entry.name,
163
- locator,
164
- using: [],
165
- ...(exposes ? { exposes } : {}),
166
- ...(description === undefined ? {} : { description }),
167
- },
168
- using,
169
- };
170
- };
171
- const locatorOf = (where, value, fail) => {
172
- const selector = recordedAs(value, 'selector');
173
- if (selector?.kind !== 'mirror')
174
- return selectorOf(where, value, fail, 'test');
175
- const [from, to] = selector.patterns;
176
- if (!isMirrorPath(from) || !isMirrorPath(to))
177
- fail(`${where}: mirror() takes two folder paths without placeholders, mirror('src', 'test')`);
178
- return { kind: 'mirror', from: String(from), to: String(to) };
179
- };
180
- const isMirrorPath = (path) => isName(path) && !path.includes('{') && isDirPath(path);
181
- const archetypeOf = (where, holder, value, fail) => {
182
- if (value === undefined)
183
- return { using: [] };
184
- if (value === self)
185
- return { named: holder, using: [] };
186
- const recorded = recordedAs(value, 'archetype');
187
- if (!recorded) {
188
- fail(`${where}: expected an archetype, such as component, using(...) or exposes(all())`);
189
- return { using: [] };
190
- }
191
- const using = usingOf(`${where}.using`, recorded.using, fail);
192
- const { definition } = recorded;
193
- return definition.name === undefined ? { anonymous: definition, using } : { named: definition.name, using };
194
- };
195
- const checkAnonymous = (where, kind, definition, fail) => {
196
- if (definition.description !== undefined && typeof definition.description !== 'string')
197
- fail(`${where}.description: expected a string`);
198
- if (isRecord(definition.requires) && Object.keys(definition.requires).length > 0)
199
- fail(`${where}.requires: a module's own archetype sees what its holder requires; name the archetype to require more`);
200
- if (kind === 'dir')
201
- return;
202
- const hasModules = Array.isArray(definition.modules) && definition.modules.length > 0;
203
- const folderOnly = [
204
- ...(hasModules ? ['contains'] : []),
205
- ...['identify', 'provides', 'entry', 'testUsing'].filter((field) => definition[field] !== undefined),
206
- ];
207
- for (const field of folderOnly)
208
- fail(kind === 'test'
209
- ? `${where}.${field}: a test module holds test code; its archetype takes using(...), exposes(...) and description(...)`
210
- : `${where}.${field}: a files module has no modules; use dir(...) for ${field}`);
211
- };
212
- const selectorOf = (where, value, fail, of = 'module') => {
213
- const selector = recordedAs(value, 'selector');
214
- if (!selector || selector.kind === 'mirror') {
215
- const shapes = of === 'test' ? "files('*.test.ts'), dir('e2e') or mirror('src', 'test')" : "files('*.ts') or dir('src/ui')";
216
- fail(`${where}: expected a selector, ${shapes}`);
217
- return { kind: 'files', patterns: [] };
218
- }
219
- const patterns = selector.patterns;
220
- if (!patterns.every(isName))
221
- fail(`${where}: ${selector.kind}() takes non-empty strings`);
222
- if (selector.kind === 'dir')
223
- checkDirPaths(where, patterns, fail);
224
- return { kind: selector.kind, patterns };
225
- };
226
- const usingOf = (where, value, fail) => {
227
- const using = typeof value === 'function' ? value(moduleRefs(), { required: requiredRefs() }) : (value ?? []);
228
- if (Array.isArray(using))
229
- return using;
230
- fail(`${where}: expected a list of references, or a callback returning one`);
231
- return [];
232
- };
233
- const checkDirPaths = (where, paths, fail) => {
234
- for (const path of paths.filter((path) => isName(path) && !isDirPath(path)))
235
- fail(`${where}: a dir path is folder names and {placeholders}, a whole segment each, not '${String(path)}'`);
236
- };
237
- const isDirPath = (path) => path.split('/').every((segment) => segment !== '.' && segment !== '..' && /^(\{\w+\}|[^{}*]+)$/.test(segment));
238
- const requirements = (archetype, value, fail) => {
239
- if (!isRecord(value)) {
240
- fail(`${archetype}.requires: expected requirements by name, { testRunner, strings: dir() }`);
241
- return {};
242
- }
243
- const result = {};
244
- for (const [name, requirement] of Object.entries(value)) {
245
- const shape = recordedAs(requirement, 'shape');
246
- const required = recordedAs(requirement, 'archetype')?.definition.name;
247
- if (shape)
248
- result[name] = { kind: shape };
249
- else if (isName(required))
250
- result[name] = { kind: 'archetype', archetype: required };
251
- else
252
- fail(`${archetype}.requires.${name}: expected an archetype, dir() or files()`);
253
- }
254
- return result;
255
- };
256
- const identifyData = (archetype, value, fail) => {
257
- const keys = isRecord(value) ? Object.keys(value) : [];
258
- if (value === undefined || (keys.length === 1 && isName(value.marker)))
259
- return value;
260
- if (keys.length === 1 && typeof value.match === 'function')
261
- return value;
262
- fail(`${archetype}.identify: expected { marker: '{name}.dom.ts' } or { match: (dir, chain) => ... }`);
263
- return undefined;
264
- };