@yearofthedan/warp 0.2.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 (54) hide show
  1. package/README.md +24 -25
  2. package/dist/analysis/imports.d.ts +3 -1
  3. package/dist/analysis/imports.js +5 -1
  4. package/dist/analysis/typescript.js +2 -1
  5. package/dist/app/check.d.ts +3 -1
  6. package/dist/app/check.js +5 -3
  7. package/dist/app/checkModel.js +1 -1
  8. package/dist/app/configLines.d.ts +5 -0
  9. package/dist/app/configLines.js +24 -0
  10. package/dist/app/load.d.ts +2 -2
  11. package/dist/app/load.js +31 -43
  12. package/dist/builtins/advice.d.ts +8 -0
  13. package/dist/builtins/advice.js +42 -0
  14. package/dist/builtins/dependency-direction/dependencyDirection.js +33 -16
  15. package/dist/builtins/index.js +2 -0
  16. package/dist/builtins/module-externals/moduleExternals.js +11 -9
  17. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.js +16 -5
  18. package/dist/builtins/place.js +5 -3
  19. package/dist/builtins/unresolved-import/unresolvedImport.d.ts +2 -0
  20. package/dist/builtins/unresolved-import/unresolvedImport.js +15 -0
  21. package/dist/builtins/unused-declaration/declarations.js +1 -1
  22. package/dist/builtins/unused-declaration/empty.js +19 -11
  23. package/dist/builtins/unused-declaration/grants.js +22 -15
  24. package/dist/builtins/unused-declaration/surfaces.js +24 -22
  25. package/dist/cli.js +14 -11
  26. package/dist/config/loadConfig.d.ts +7 -1
  27. package/dist/config/loadConfig.js +83 -12
  28. package/dist/index.d.ts +0 -1
  29. package/dist/index.js +0 -1
  30. package/dist/model/archetype/data.d.ts +25 -29
  31. package/dist/model/archetype/data.js +1 -1
  32. package/dist/model/archetype/fixes.d.ts +35 -0
  33. package/dist/model/archetype/fixes.js +265 -0
  34. package/dist/model/archetype/language.d.ts +16 -0
  35. package/dist/model/archetype/language.js +419 -0
  36. package/dist/model/archetype/reach.d.ts +8 -3
  37. package/dist/model/archetype/reach.js +142 -122
  38. package/dist/model/archetype/references.d.ts +11 -5
  39. package/dist/model/archetype/references.js +58 -132
  40. package/dist/model/archetype/requirements.d.ts +3 -3
  41. package/dist/model/archetype/requirements.js +65 -61
  42. package/dist/model/archetype/resolve.d.ts +3 -3
  43. package/dist/model/archetype/resolve.js +64 -77
  44. package/dist/model/archetype/rulesets.d.ts +2 -1
  45. package/dist/model/archetype/rulesets.js +72 -49
  46. package/dist/model/archetype/settings.js +4 -19
  47. package/dist/model/facts.d.ts +6 -0
  48. package/dist/reporters/model.js +9 -9
  49. package/dist/rules/registry.js +1 -1
  50. package/package.json +3 -2
  51. package/dist/model/archetype/builder.d.ts +0 -183
  52. package/dist/model/archetype/builder.js +0 -85
  53. package/dist/model/archetype/draft.d.ts +0 -37
  54. package/dist/model/archetype/draft.js +0 -275
@@ -1,69 +1,73 @@
1
- import { recordedAs } from './builder.js';
2
- const usedRequirements = (drafts) => new Set(drafts
3
- .flatMap((draft) => [
4
- ...draft.modules.flatMap((module) => [...module.using, ...(module.tests?.using ?? [])]),
5
- ...(draft.tests?.using ?? []),
6
- ])
7
- .map((value) => recordedAs(value, 'ref'))
8
- .map((ref) => ref?.kind === 'required' && ref.name));
9
- export const checkUnused = (drafts, fail) => {
10
- for (const draft of drafts.filter((candidate) => !candidate.data.inline)) {
11
- const inline = drafts.filter((candidate) => candidate.data.name.startsWith(`${draft.data.name}.`));
12
- const used = usedRequirements([draft, ...inline]);
13
- for (const name of Object.keys(draft.data.requires)) {
14
- if (!used.has(name))
15
- fail(`${draft.data.name} requires ${name}, which no module lists in using`);
16
- }
1
+ import { exposedProblem, show } from './references.js';
2
+ const namedOf = (archetype) => archetype.split('.')[0];
3
+ const sitesOf = (archetypes) => Object.values(archetypes).flatMap((holder) => holder.modules
4
+ .filter((module) => module.kind === 'dir' && !archetypes[module.archetype]?.inline)
5
+ .map((module) => ({ holder, module })));
6
+ export const markTests = (archetypes, fail) => {
7
+ const sites = sitesOf(archetypes);
8
+ for (const archetype of Object.values(archetypes).filter((candidate) => !candidate.inline)) {
9
+ const using = sites.filter((site) => site.module.archetype === archetype.name);
10
+ const tests = using.filter((site) => site.module.test).length;
11
+ if (tests === 0)
12
+ continue;
13
+ if (tests < using.length)
14
+ fail(`${archetype.name} is used by production and test modules; an archetype holds one or the other`);
15
+ else
16
+ archetype.test = true;
17
17
  }
18
+ for (const archetype of Object.values(archetypes))
19
+ if (archetype.inline && archetypes[namedOf(archetype.name)]?.test)
20
+ archetype.test = true;
18
21
  };
19
- export const checkRequirements = (archetypes, root, fail) => {
20
- const provisions = Object.values(archetypes).flatMap((archetype) => Object.entries(archetype.provides).map(([name, ref]) => ({
21
- archetype: archetype.name,
22
- name,
23
- module: archetype.modules.find((module) => module.name === ref.module),
24
- used: false,
25
- })));
26
- walk({ archetypes, provisions, fail, missing: new Set(), visited: new Set() }, archetypes[root], '', new Map());
27
- return provisions
28
- .filter((p) => !p.used)
29
- .map((p) => `${p.archetype} provides ${p.name}, which nothing below requires`);
30
- };
31
- const walk = (state, archetype, path, above) => {
32
- const at = path === '' ? archetype.name : `${path} (${archetype.name})`;
33
- if (!archetype.inline) {
34
- for (const [name, requirement] of Object.entries(archetype.requires)) {
35
- answer(state, at, archetype.name, name, requirement, above.get(name));
36
- }
22
+ export const checkInputs = (archetypes, pending, fail) => {
23
+ const uses = pending.filter((entry) => entry.ref.kind === 'required');
24
+ const usesOf = (archetype) => uses.filter((entry) => namedOf(entry.archetype) === archetype);
25
+ for (const archetype of Object.values(archetypes).filter((candidate) => !candidate.inline)) {
26
+ const used = new Set(usesOf(archetype.name).map((entry) => entry.ref.name));
27
+ for (const name of [...archetype.requires, ...(archetype.testRequires ?? [])].filter((input) => !used.has(input)))
28
+ fail(`${archetype.name} requires ${name}, which none of its modules names in dependencies`);
37
29
  }
38
- const below = new Map(above);
39
- for (const provision of state.provisions.filter((p) => p.archetype === archetype.name))
40
- below.set(provision.name, provision);
41
- const signature = JSON.stringify([...below].map(([name, provision]) => [name, provision.archetype]));
42
- for (const module of archetype.modules) {
43
- if (module.kind !== 'dir' || state.visited.has(`${module.archetype}|${signature}`))
44
- continue;
45
- state.visited.add(`${module.archetype}|${signature}`);
46
- walk(state, state.archetypes[module.archetype], path === '' ? module.name : `${path}.${module.name}`, below);
30
+ for (const site of sitesOf(archetypes)) {
31
+ const archetype = archetypes[site.module.archetype];
32
+ if (archetype)
33
+ checkSite(archetypes, site, archetype, usesOf(archetype.name), fail);
47
34
  }
48
35
  };
49
- const answer = (state, at, archetype, name, requirement, provider) => {
50
- if (!provider) {
51
- if (!state.missing.has(`${archetype}.${name}`))
52
- state.fail(`${at} requires ${name}, which nothing above it provides`);
53
- state.missing.add(`${archetype}.${name}`);
54
- return;
55
- }
56
- provider.used = true;
57
- if (!fits(requirement, provider.module)) {
58
- state.fail(`${at} requires ${name} as ${requirementText(requirement)}; ${provider.archetype} provides ${name} from ${provider.module.name}, ${moduleText(state, provider.module)}`);
36
+ const checkSite = (archetypes, { holder, module }, archetype, uses, fail) => {
37
+ const where = `${holder.name}.${module.name}`;
38
+ for (const name of archetype.requires.filter((input) => !module.inputs?.[input]))
39
+ fail(`${where} uses ${archetype.name}, which requires ${name}; ${binding(module.inputs, name, where)}`);
40
+ for (const name of (archetype.testRequires ?? []).filter((input) => !module.testInputs?.[input]))
41
+ fail(`${where} uses ${archetype.name}, whose tests require ${name}; name it in ${where}.test.dependencies`);
42
+ const bound = { ...module.inputs, ...module.testInputs };
43
+ for (const entry of uses) {
44
+ const ref = entry.ref;
45
+ const given = bound[ref.name];
46
+ if (!given || ref.surface === undefined)
47
+ continue;
48
+ const problem = surfaceProblem(archetypes, holder, given, ref.surface, entry.tests);
49
+ if (problem)
50
+ fail(`${entry.where}: '${ref.name}.${ref.surface}': ${where} binds ${ref.name} to ${show(given)}; ${problem}`);
59
51
  }
60
52
  };
61
- const moduleText = (state, module) => {
62
- if (module.kind === 'files')
63
- return 'a files module';
64
- return state.archetypes[module.archetype].inline ? 'a dir module' : `modules of ${module.archetype}`;
53
+ const binding = (inputs, name, where) => inputs?.[`npm:${name}`]
54
+ ? `bind the package by name in ${where}.dependencies, { ${name}: npm:${name} }`
55
+ : `name it in ${where}.dependencies`;
56
+ const archetypeAt = (archetypes, holder, up) => archetypes[holder.name
57
+ .split('.')
58
+ .slice(0, Math.max(holder.name.split('.').length - up, 1))
59
+ .join('.')];
60
+ const surfaceProblem = (archetypes, holder, binding, surface, tests) => {
61
+ if (binding.kind === 'package')
62
+ return `a package has no surfaces`;
63
+ if (binding.surface !== undefined)
64
+ return `${show(binding)} is a surface already`;
65
+ if (binding.kind === 'required')
66
+ return undefined;
67
+ const at = archetypeAt(archetypes, holder, binding.up ?? 0);
68
+ const module = at.modules.find((candidate) => candidate.name === binding.module);
69
+ if (module?.kind !== 'dir')
70
+ return `${binding.module} has no named surfaces`;
71
+ const inside = archetypes[module.archetype];
72
+ return exposedProblem(binding.module, { exposes: inside.exposes, testExposes: Object.keys(inside.testExposes ?? {}) }, surface, tests);
65
73
  };
66
- const requirementText = (requirement) => requirement.kind === 'archetype' ? `modules of ${requirement.archetype}` : `${requirement.kind}()`;
67
- const fits = (requirement, module) => requirement.kind === 'archetype'
68
- ? module.kind === 'dir' && module.archetype === requirement.archetype
69
- : module.kind === requirement.kind;
@@ -6,7 +6,8 @@ export interface Folders {
6
6
  export interface ModuleView {
7
7
  id: string;
8
8
  name?: string;
9
- kind: 'dir' | 'files';
9
+ kind: 'dir' | 'files' | 'test';
10
+ test?: true;
10
11
  dir: string;
11
12
  archetype?: string;
12
13
  captures: Record<string, string>;
@@ -14,8 +15,6 @@ export interface ModuleView {
14
15
  children: ModuleView[];
15
16
  files: string[];
16
17
  looseFiles: string[];
17
- tests: string[];
18
- test?: true;
19
18
  }
20
19
  export interface Model {
21
20
  root: ModuleView;
@@ -24,6 +23,7 @@ export interface Model {
24
23
  isTest(file: string): boolean;
25
24
  }
26
25
  export declare const declarationOf: (config: ConfigData, module: ModuleView) => ModuleData | undefined;
26
+ export declare const rootId = "(root)";
27
27
  export declare const fillPattern: (pattern: string, captures: Record<string, string>) => string | undefined;
28
28
  export declare const resolveModel: (config: ConfigData, folders: Folders, files: readonly string[]) => Model;
29
29
  export declare const specificity: (pattern: string) => number;
@@ -1,6 +1,8 @@
1
1
  import { ConfigError } from '../configError.js';
2
2
  export const declarationOf = (config, module) => module.parent && config.archetypes[module.parent.archetype].modules.find((data) => data.name === module.name);
3
+ export const rootId = '(root)';
3
4
  const placeholders = /\{(\w+)\}/g;
5
+ const asWritten = (path) => path.replaceAll('{name}', '*');
4
6
  const join = (dir, name) => (dir === '' ? name : `${dir}/${name}`);
5
7
  const parentDir = (path) => path.slice(0, Math.max(path.lastIndexOf('/'), 0));
6
8
  const fill = (pattern, captures) => {
@@ -17,14 +19,8 @@ export const resolveModel = (config, folders, files) => {
17
19
  const resolution = { config, folders, byDir: new Map(), errors: new Set() };
18
20
  const root = build(resolution, { dir: '', archetype: config.archetypes[config.root], captures: {} });
19
21
  const moduleOf = new Map();
20
- const tests = new Set();
21
- const isTest = testMatcher(config.tests);
22
- for (const file of [...files].sort()) {
23
- const { module, test } = assign(resolution, file, isTest(file));
24
- if (test)
25
- tests.add(file);
26
- moduleOf.set(file, module);
27
- }
22
+ for (const file of [...files].sort())
23
+ moduleOf.set(file, assign(resolution, file));
28
24
  if (resolution.errors.size > 0)
29
25
  throw new ConfigError([...resolution.errors].join('\n'));
30
26
  order(config, root);
@@ -32,31 +28,14 @@ export const resolveModel = (config, folders, files) => {
32
28
  root,
33
29
  modules: [...walk(root)],
34
30
  moduleOf: (file) => moduleOf.get(file),
35
- isTest: (file) => tests.has(file),
31
+ isTest: (file) => moduleOf.get(file)?.test === true,
36
32
  };
37
33
  };
38
- const assign = (resolution, file, entry) => {
34
+ const assign = (resolution, file) => {
39
35
  const folder = deepest(resolution.byDir, file);
40
- const claim = claimOf(resolution, folder, file);
41
- if (claim) {
42
- const support = claim.kind === 'dir' ? place(resolution, claim, file, 'tests') : claim;
43
- if (support.test)
44
- support.files.push(file);
45
- else
46
- claim.tests.push(file);
47
- return { module: support.test ? support : claim, test: true };
48
- }
49
- if (entry) {
50
- folder.tests.push(file);
51
- return { module: folder, test: true };
52
- }
53
- const module = place(resolution, folder, file, 'all');
36
+ const module = claimOf(resolution, folder, file) ?? place(resolution, folder, file);
54
37
  module.files.push(file);
55
- return { module, test: module.test === true };
56
- };
57
- const testMatcher = (patterns = []) => {
58
- const regexes = patterns.map(globRegex);
59
- return (file) => regexes.some((regex) => regex.test(file));
38
+ return module;
60
39
  };
61
40
  const walk = function* (module) {
62
41
  yield module;
@@ -67,29 +46,22 @@ const order = (config, module) => {
67
46
  if (module.archetype === undefined)
68
47
  return;
69
48
  const archetype = config.archetypes[module.archetype];
70
- const declared = [
71
- ...archetype.modules.map((data) => data.name),
72
- ...(archetype.tests?.modules ?? []).map((data) => `tests.${data.name}`),
73
- ];
49
+ const declared = archetype.modules.map((data) => data.name);
74
50
  module.children.sort((a, b) => declared.indexOf(a.name) - declared.indexOf(b.name) || a.dir.localeCompare(b.dir));
75
51
  for (const child of module.children)
76
52
  order(config, child);
77
53
  };
78
- const place = (resolution, folder, file, among) => {
54
+ const place = (resolution, folder, file) => {
79
55
  const archetype = resolution.config.archetypes[folder.archetype];
80
- const candidates = [
81
- ...(among === 'all' ? archetype.modules : []),
82
- ...(archetype.tests?.modules ?? []).map((module) => ({ ...module, name: `tests.${module.name}` })),
83
- ];
84
- if (candidates.length === 0 || (among === 'all' && isEntry(archetype, folder, file)))
56
+ const production = archetype.modules.filter((module) => module.kind !== 'test');
57
+ if (production.length === 0 || isEntry(archetype, folder, file))
85
58
  return folder;
86
- const name = filesModule(resolution, folder, archetype, candidates, file);
59
+ const name = filesModule(resolution, folder, archetype, production, file);
87
60
  if (name === undefined) {
88
- if (among === 'all' && archetype.modules.length > 0)
89
- folder.looseFiles.push(file);
61
+ folder.looseFiles.push(file);
90
62
  return folder;
91
63
  }
92
- return childModule(folder, name);
64
+ return childModule(folder, name, 'files');
93
65
  };
94
66
  const isEntry = (archetype, folder, file) => {
95
67
  const { exposes } = archetype;
@@ -101,22 +73,21 @@ const isEntry = (archetype, folder, file) => {
101
73
  return filled !== undefined && globRegex(filled).test(path);
102
74
  });
103
75
  };
104
- const childModule = (folder, name) => {
76
+ const childModule = (folder, name, kind) => {
105
77
  const id = `${folder.id}:${name}`;
106
78
  let module = folder.children.find((child) => child.id === id);
107
79
  if (!module) {
108
80
  module = {
109
81
  id,
110
82
  name,
111
- kind: 'files',
83
+ kind,
84
+ ...(kind === 'test' ? { test: true } : {}),
112
85
  dir: folder.dir,
113
86
  captures: folder.captures,
114
87
  parent: folder,
115
88
  children: [],
116
89
  files: [],
117
90
  looseFiles: [],
118
- tests: [],
119
- ...(name.startsWith('tests.') ? { test: true } : {}),
120
91
  };
121
92
  folder.children.push(module);
122
93
  }
@@ -126,37 +97,42 @@ const claimOf = (resolution, folder, file) => {
126
97
  for (let at = folder; at; at = at.parent) {
127
98
  const archetype = resolution.config.archetypes[at.archetype];
128
99
  const level = at;
129
- const claims = [
130
- ...(locates(level, archetype.tests?.locator, file) ? [level] : []),
131
- ...archetype.modules
132
- .filter((declared) => declared.kind === 'files')
133
- .filter((declared) => locates(level, declared.tests?.locator, file))
134
- .map((declared) => childModule(level, declared.name)),
135
- ];
136
- if (claims.length > 1)
137
- resolution.errors.add(`${file}: claimed as tests by ${claims.map((claim) => claim.id).join(' and ')}; narrow one of their locators`);
138
- if (claims.length > 0)
139
- return claims[0];
100
+ const claims = archetype.modules
101
+ .filter((declared) => declared.kind === 'test')
102
+ .flatMap((declared) => {
103
+ const score = locates(level, declared.patterns, file);
104
+ return score === undefined ? [] : [{ name: declared.name, score }];
105
+ });
106
+ if (claims.length === 0) {
107
+ if (at.test)
108
+ return at;
109
+ continue;
110
+ }
111
+ const best = claims.reduce((top, claim) => (claim.score > top.score ? claim : top));
112
+ const tied = claims.filter((claim) => claim !== best && claim.score === best.score);
113
+ if (tied.length > 0)
114
+ resolution.errors.add(`${file}: claimed by ${archetype.name}'s test modules ${[best, ...tied].map((claim) => claim.name).join(' and ')}, which are equally specific; narrow one of their patterns`);
115
+ return childModule(level, best.name, 'test');
140
116
  }
141
117
  return undefined;
142
118
  };
143
- const locates = (module, locator, file) => {
144
- if (!locator)
145
- return false;
119
+ const locates = (module, patterns, file) => {
146
120
  const path = module.dir === '' ? file : file.slice(module.dir.length + 1);
147
- return locator.patterns.some((pattern) => {
121
+ const scores = patterns.flatMap((pattern) => {
148
122
  const filled = fillPattern(pattern, module.captures);
149
123
  if (filled === undefined)
150
- return false;
151
- return locator.kind === 'dir' ? path.startsWith(`${filled}/`) : globRegex(filled).test(path);
124
+ return [];
125
+ return globRegex(filled).test(path) ? [specificity(filled)] : [];
152
126
  });
127
+ return scores.length === 0 ? undefined : Math.max(...scores);
153
128
  };
154
129
  const pathOf = (module) => module.parent?.name === undefined ? module.name : `${pathOf(module.parent)}.${module.name}`;
155
- const build = (resolution, { dir, archetype, captures, parent, name }) => {
130
+ const build = (resolution, { dir, archetype, captures, parent, name, test }) => {
156
131
  const module = {
157
- id: dir === '' ? '.' : dir,
132
+ id: dir === '' ? rootId : dir,
158
133
  ...(name === undefined ? {} : { name }),
159
134
  kind: 'dir',
135
+ ...(test ? { test: true } : {}),
160
136
  dir,
161
137
  archetype: archetype.name,
162
138
  captures,
@@ -164,7 +140,6 @@ const build = (resolution, { dir, archetype, captures, parent, name }) => {
164
140
  children: [],
165
141
  files: [],
166
142
  looseFiles: [],
167
- tests: [],
168
143
  };
169
144
  resolution.byDir.set(dir, module);
170
145
  for (const [childDir, claims] of [...claimsIn(resolution, module, archetype)].sort(([a], [b]) => a.localeCompare(b))) {
@@ -181,6 +156,7 @@ const build = (resolution, { dir, archetype, captures, parent, name }) => {
181
156
  captures: winner.captures,
182
157
  parent: module,
183
158
  name: winner.module.name,
159
+ test: winner.module.test === true,
184
160
  }));
185
161
  }
186
162
  return module;
@@ -198,7 +174,7 @@ const claimsIn = (resolution, module, archetype) => {
198
174
  const claimsOf = (resolution, module, declared) => {
199
175
  const child = resolution.config.archetypes[declared.archetype];
200
176
  return declared.paths.flatMap((path) => matchFolders(resolution.folders, module.dir, path).flatMap((match) => {
201
- const captures = identify(resolution, child, match.dir, match.captures, module);
177
+ const captures = identify(resolution, child, match.dir, match.captures, module, { declared, path });
202
178
  return captures ? [{ module: declared, path, archetype: child, ...match, captures }] : [];
203
179
  }));
204
180
  };
@@ -217,22 +193,33 @@ const matchFolders = (folders, base, path) => {
217
193
  }
218
194
  return found;
219
195
  };
220
- const identify = (resolution, archetype, dir, captures, parent) => {
196
+ const identify = (resolution, archetype, dir, captures, parent, claim) => {
221
197
  const how = archetype.identify;
222
198
  if (how === undefined)
223
199
  return captures;
224
- if ('match' in how) {
225
- const matched = how.match(dir, chain(parent));
226
- return matched === false ? undefined : { ...captures, ...matched };
227
- }
228
200
  const marker = fill(how.marker, captures);
229
201
  if ('missing' in marker) {
230
- resolution.errors.add(`${dir}: ${archetype.name}'s marker '${how.marker}' names {${marker.missing}}, which its dir path doesn't capture`);
202
+ resolution.errors.add(`${dir}: ${archetype.name}'s marker '${how.marker}' names {${marker.missing}}, which its dir path doesn't capture` +
203
+ sharedHint(resolution, parent, claim, marker.missing));
231
204
  return undefined;
232
205
  }
233
206
  return resolution.folders.exists(join(dir, marker.filled)) ? captures : undefined;
234
207
  };
235
- const chain = (module) => (module.parent ? [...chain(module.parent), module] : [module]);
208
+ const sharedHint = (resolution, parent, { declared, path }, missing) => {
209
+ if (path.includes('{'))
210
+ return '';
211
+ const sibling = resolution.config.archetypes[parent.archetype].modules.find((module) => module.kind === 'dir' &&
212
+ module !== declared &&
213
+ module.archetype === declared.archetype &&
214
+ module.paths.some((candidate) => candidate.includes(`{${missing}}`)));
215
+ if (!sibling)
216
+ return '';
217
+ const folder = path.slice(path.lastIndexOf('/') + 1);
218
+ const shown = sibling.paths.map((candidate) => `'${asWritten(candidate)}'`).join(', ');
219
+ return (`. A literal dir binds no placeholders. If ${sibling.name} use ${declared.name}, it is shared: move ${folder} ` +
220
+ `out of the folders dir: ${shown} matches, for example into shared/${folder}, and declare it as its own module ` +
221
+ `with dir: 'shared/*'`);
222
+ };
236
223
  const compareRanks = (a, b) => {
237
224
  const index = a.findIndex((value, at) => value !== b[at]);
238
225
  return index < 0 ? 0 : a[index] - b[index];
@@ -241,7 +228,7 @@ const mostSpecific = (resolution, archetype, dir, claims) => {
241
228
  const best = claims.reduce((top, claim) => (compareRanks(claim.rank, top.rank) > 0 ? claim : top));
242
229
  const tied = claims.filter((claim) => claim.module !== best.module && compareRanks(claim.rank, best.rank) === 0);
243
230
  if (tied.length > 0) {
244
- const names = [best, ...tied].map((claim) => `${claim.module.name} ('${claim.path}')`).join(' and ');
231
+ const names = [best, ...tied].map((claim) => `${claim.module.name} ('${asWritten(claim.path)}')`).join(' and ');
245
232
  resolution.errors.add(`${dir}: ${archetype.name}'s dir modules ${names} are equally specific; narrow one of the paths`);
246
233
  }
247
234
  return best;
@@ -279,7 +266,7 @@ const filesModule = (resolution, module, archetype, candidates, file) => {
279
266
  const tie = (clash) => {
280
267
  const names = clash.map((match) => `${match.module} ('${match.pattern}')`).join(' and ');
281
268
  const folders = clash.every((match) => match.pattern.endsWith('/**'));
282
- const fix = folders ? 'declare the folders with dir(), where the deeper folder wins' : 'narrow one of the patterns';
269
+ const fix = folders ? 'declare the folders with dir, where the deeper folder wins' : 'narrow one of the patterns';
283
270
  return `files modules ${names} are equally specific; ${fix}`;
284
271
  };
285
272
  export const specificity = (pattern) => {
@@ -1,4 +1,5 @@
1
- import type { ConfigData, RuleSettingsData } from './data.ts';
1
+ import { type ConfigData, type RuleSettingsData } from './data.ts';
2
2
  import { type Fail } from './shared.ts';
3
3
  export type ParseRules = (value: unknown, at: string) => RuleSettingsData | undefined;
4
4
  export declare const withRuleset: (config: ConfigData, rulesets: unknown, name: string | undefined, parse: ParseRules, fail: Fail) => ConfigData;
5
+ export declare const recommended = "warp:recommended";
@@ -1,67 +1,97 @@
1
1
  import { ConfigError } from '../configError.js';
2
- import { recordedAs } from './builder.js';
3
- import { isName, isRecord, list } from './shared.js';
2
+ import { isTestData } from './data.js';
3
+ import { isRecord, list } from './shared.js';
4
4
  export const withRuleset = (config, rulesets, name, parse, fail) => {
5
5
  const named = rulesetsOf(rulesets, fail);
6
6
  if (name !== undefined && !named.has(name)) {
7
7
  const names = [...named.keys()];
8
8
  throw new ConfigError(`no ruleset called ${name}. ${names.length > 0 ? `Rulesets: ${names.join(', ')}` : 'The config declares none'}`);
9
9
  }
10
- const chosen = named.get(name ?? 'default');
11
- return chosen ? applied(config, layersOf(chosen, named, fail), parse, fail) : config;
10
+ const chosen = name ?? 'default';
11
+ if (!named.has(chosen))
12
+ return applied(config, [{ settings: 'off', at: 'rulesets' }], parse, fail);
13
+ const { layers, recommends } = layersOf(config, chosen, named, fail);
14
+ const base = recommends ? [] : [{ settings: 'off', at: `rulesets.${chosen}` }];
15
+ return applied(config, [...base, ...layers], parse, fail);
12
16
  };
17
+ export const recommended = 'warp:recommended';
18
+ const rulesetKeys = ['extends', 'rules', 'test', 'on'];
13
19
  const rulesetsOf = (value, fail) => {
14
20
  const named = new Map();
15
21
  if (value === undefined)
16
22
  return named;
17
23
  if (!isRecord(value)) {
18
- fail('config.rulesets: expected rulesets by name, { default: base, edit }');
24
+ fail('rulesets: expected rulesets by name, { default: { extends: [warp:recommended] } }');
19
25
  return named;
20
26
  }
21
27
  for (const [name, ruleset] of Object.entries(value)) {
22
- const recorded = recordedAs(ruleset, 'ruleset');
23
- if (recorded)
24
- named.set(name, recorded);
25
- else
26
- fail(`config.rulesets.${name}: expected a ruleset, ruleset().rules(...)`);
28
+ if (!isRecord(ruleset)) {
29
+ fail(`rulesets.${name}: expected a ruleset, { extends: [warp:recommended], rules: {...} }`);
30
+ continue;
31
+ }
32
+ for (const key of Object.keys(ruleset).filter((key) => !rulesetKeys.includes(key)))
33
+ fail(`rulesets.${name}: unknown key ${key}; a ruleset takes ${rulesetKeys.join(', ')}`);
34
+ named.set(name, ruleset);
27
35
  }
28
36
  return named;
29
37
  };
30
- const layersOf = (ruleset, named, fail) => {
31
- const label = (of) => [...named].find(([, candidate]) => candidate === of)?.[0] ?? 'extends';
32
- const chain = [];
33
- for (let current = ruleset; current;) {
34
- chain.unshift(current);
35
- if (current.base === undefined)
36
- break;
37
- const base = recordedAs(current.base, 'ruleset');
38
- if (!base)
39
- fail(`rulesets.${label(current)}.extends: expected a ruleset`);
40
- current = base;
38
+ const layersOf = (config, name, named, fail, seen = []) => {
39
+ const ruleset = named.get(name);
40
+ const at = `rulesets.${name}`;
41
+ const inherited = basesOf(config, name, named, fail, seen);
42
+ const layers = [...inherited.layers];
43
+ if (ruleset.rules !== undefined)
44
+ layers.push({ settings: ruleset.rules, at: `${at}.rules` });
45
+ if (ruleset.test !== undefined)
46
+ for (const target of testTargets(config))
47
+ layers.push({ target, settings: ruleset.test, at: `${at}.test` });
48
+ if (ruleset.on !== undefined)
49
+ layers.push(...onLayers(config, at, ruleset.on, fail));
50
+ return { layers, recommends: inherited.recommends };
51
+ };
52
+ const basesOf = (config, name, named, fail, seen) => {
53
+ const at = `rulesets.${name}`;
54
+ const declared = named.get(name).extends;
55
+ const bases = declared === undefined ? [] : [declared].flat();
56
+ const found = { layers: [], recommends: false };
57
+ for (const base of bases) {
58
+ if (base === recommended)
59
+ found.recommends = true;
60
+ else if (typeof base !== 'string' || !named.has(base))
61
+ fail(`${at}.extends: '${String(base)}' is no ruleset; extend ${list([recommended, ...named.keys()], 'nothing')}`);
62
+ else if (seen.includes(base))
63
+ fail(`${at}.extends: ${[...seen, name, base].join(' extends ')}, a loop`);
64
+ else {
65
+ const extended = layersOf(config, base, named, fail, [...seen, name]);
66
+ found.layers.push(...extended.layers);
67
+ found.recommends ||= extended.recommends;
68
+ }
69
+ }
70
+ return found;
71
+ };
72
+ const onLayers = (config, at, on, fail) => {
73
+ if (!isRecord(on)) {
74
+ fail(`${at}.on: expected settings by archetype or module, { app.adapters: {...} }`);
75
+ return [];
41
76
  }
42
- return chain.flatMap((of) => {
43
- const at = `rulesets.${label(of)}`;
44
- return of.entries.map(({ target, settings }) => {
45
- if (target === undefined)
46
- return { settings, at: `${at}.rules` };
47
- const resolved = targetOf(target);
48
- if (!resolved) {
49
- fail(`${at}.on: expected an archetype or one of its modules, such as component or component.modules.logic`);
50
- return { settings: undefined, at };
51
- }
52
- const shown = resolved.module === undefined
53
- ? resolved.archetype
54
- : `${resolved.archetype.replaceAll('.', '.modules.')}.modules.${resolved.module}`;
55
- return { target: resolved, settings, at: `${at}.on(${shown})` };
56
- });
77
+ return Object.entries(on).flatMap(([path, settings]) => {
78
+ const target = targetOf(config, path);
79
+ if (target)
80
+ return [{ target, settings, at: `${at}.on.${path}` }];
81
+ fail(`${at}.on.${path}: no archetype or module called ${path}; name an archetype, or archetype.module`);
82
+ return [];
57
83
  });
58
84
  };
59
- const targetOf = (value) => {
60
- const archetype = recordedAs(value, 'archetype');
61
- if (archetype)
62
- return isName(archetype.definition.name) ? { archetype: archetype.definition.name } : undefined;
63
- const handle = recordedAs(value, 'handle');
64
- return handle && { archetype: handle.archetype, module: handle.module };
85
+ const testTargets = (config) => Object.values(config.archetypes).flatMap((archetype) => archetype.modules.filter(isTestData).map((module) => ({ archetype: archetype.name, module: module.name })));
86
+ const targetOf = (config, path) => {
87
+ if (config.archetypes[path])
88
+ return { archetype: path };
89
+ const at = path.lastIndexOf('.');
90
+ const archetype = config.archetypes[path.slice(0, at)];
91
+ const module = path.slice(at + 1);
92
+ return at > 0 && archetype?.modules.some((declared) => declared.name === module)
93
+ ? { archetype: archetype.name, module }
94
+ : undefined;
65
95
  };
66
96
  const applied = (config, layers, parse, fail) => {
67
97
  let rules = config.rules;
@@ -86,13 +116,6 @@ const applyTo = (archetype, module, layer, at, fail) => {
86
116
  archetype.rules = merged(archetype.rules, layer);
87
117
  return;
88
118
  }
89
- if (module === 'tests') {
90
- if (archetype.tests)
91
- archetype.tests = { ...archetype.tests, rules: merged(archetype.tests.rules, layer) };
92
- else
93
- fail(`${at}: ${archetype.name} has no test layer`);
94
- return;
95
- }
96
119
  const index = archetype.modules.findIndex((declared) => declared.name === module);
97
120
  if (index < 0)
98
121
  fail(`${at}: ${archetype.name} has modules ${list(archetype.modules.map((declared) => declared.name), 'none')}`);
@@ -1,32 +1,17 @@
1
1
  import { declarationOf } from './resolve.js';
2
2
  import { isRecord } from './shared.js';
3
- const layerOf = (config, module) => {
4
- if (module.archetype !== undefined)
5
- return config.archetypes[module.archetype].tests;
6
- const declared = declarationOf(config, module);
7
- return declared?.kind === 'files' ? declared.tests : undefined;
8
- };
9
3
  const own = (config, module) => [
10
4
  declarationOf(config, module)?.rules,
11
5
  module.archetype === undefined ? undefined : config.archetypes[module.archetype].rules,
12
6
  ];
13
- const testModuleRules = (config, module) => layerOf(config, module.parent)?.modules.find((data) => `tests.${data.name}` === module.name)?.rules;
14
- const levels = (config, module) => {
15
- if (module.test)
16
- return [testModuleRules(config, module), ...testLevels(config, module.parent)];
17
- return [...own(config, module), ...(module.parent ? levels(config, module.parent) : [config.rules])];
18
- };
19
- const testLevels = (config, owner) => [
20
- layerOf(config, owner)?.rules,
21
- ...own(config, owner),
22
- ...(owner.parent ? testLevels(config, owner.parent) : [config.rules]),
7
+ const levels = (config, module) => [
8
+ ...own(config, module),
9
+ ...(module.parent ? levels(config, module.parent) : [config.rules]),
23
10
  ];
24
11
  export const settings = (config, model) => {
25
12
  const fileLevels = (file) => {
26
13
  const module = model.moduleOf(file);
27
- if (!module)
28
- return [config.rules];
29
- return model.isTest(file) && !module.test ? testLevels(config, module) : levels(config, module);
14
+ return module ? levels(config, module) : [config.rules];
30
15
  };
31
16
  return {
32
17
  atFile: (file, id, defaults) => ruleAt(fileLevels(file), id, defaults),