@yearofthedan/warp 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/README.md +6 -10
  2. package/dist/adapters/analysis/chains.d.ts +4 -0
  3. package/dist/adapters/analysis/chains.js +97 -0
  4. package/dist/adapters/analysis/classes.d.ts +3 -0
  5. package/dist/adapters/analysis/classes.js +109 -0
  6. package/dist/{analysis → adapters/analysis}/complexity.d.ts +1 -1
  7. package/dist/adapters/analysis/folders.d.ts +2 -0
  8. package/dist/{app/facts.d.ts → adapters/analysis/freshFacts.d.ts} +1 -2
  9. package/dist/{app/facts.js → adapters/analysis/freshFacts.js} +0 -10
  10. package/dist/{analysis → adapters/analysis}/imports.d.ts +2 -1
  11. package/dist/{analysis → adapters/analysis}/imports.js +60 -23
  12. package/dist/{analysis → adapters/analysis}/members.d.ts +1 -1
  13. package/dist/{analysis → adapters/analysis}/members.js +1 -1
  14. package/dist/{analysis → adapters/analysis}/source.d.ts +2 -2
  15. package/dist/{analysis → adapters/analysis}/source.js +2 -2
  16. package/dist/adapters/analysis/specifiers.d.ts +10 -0
  17. package/dist/adapters/analysis/specifiers.js +39 -0
  18. package/dist/adapters/analysis/typescript.d.ts +2 -0
  19. package/dist/{analysis → adapters/analysis}/typescript.js +14 -4
  20. package/dist/{config → adapters/config}/loadConfig.d.ts +2 -7
  21. package/dist/{config → adapters/config}/loadConfig.js +5 -4
  22. package/dist/{git → adapters/git}/cache.d.ts +1 -1
  23. package/dist/adapters/git/factsStore.d.ts +2 -0
  24. package/dist/adapters/git/factsStore.js +10 -0
  25. package/dist/{git → adapters/git}/repository.d.ts +1 -1
  26. package/dist/{config → adapters/git}/toolDigest.js +1 -1
  27. package/dist/{reporters → adapters/reporters}/model.d.ts +2 -2
  28. package/dist/{reporters → adapters/reporters}/reporter.d.ts +2 -2
  29. package/dist/{reporters → adapters/reporters}/text.d.ts +2 -2
  30. package/dist/app/check.d.ts +5 -6
  31. package/dist/app/check.js +3 -3
  32. package/dist/app/checkModel.d.ts +5 -5
  33. package/dist/app/checkModel.js +7 -8
  34. package/dist/app/configLines.d.ts +1 -1
  35. package/dist/app/diff.d.ts +1 -1
  36. package/dist/app/diff.js +1 -1
  37. package/dist/app/failures.d.ts +6 -0
  38. package/dist/app/failures.js +15 -0
  39. package/dist/app/load.d.ts +11 -5
  40. package/dist/app/load.js +23 -59
  41. package/dist/app/ports.d.ts +13 -3
  42. package/dist/builtins/advice.d.ts +4 -4
  43. package/dist/builtins/advice.js +1 -1
  44. package/dist/builtins/boundaries.d.ts +17 -0
  45. package/dist/builtins/boundaries.js +35 -0
  46. package/dist/builtins/class-cohesion/rule.d.ts +6 -0
  47. package/dist/builtins/class-cohesion/rule.js +34 -0
  48. package/dist/builtins/cognitive-complexity/{cognitiveComplexity.d.ts → rule.d.ts} +1 -1
  49. package/dist/builtins/cognitive-complexity/{cognitiveComplexity.js → rule.js} +1 -1
  50. package/dist/builtins/components.d.ts +1 -1
  51. package/dist/builtins/dependency-direction/rule.d.ts +2 -0
  52. package/dist/builtins/dependency-direction/{dependencyDirection.js → rule.js} +3 -3
  53. package/dist/builtins/export-consumers/rule.d.ts +6 -0
  54. package/dist/builtins/export-consumers/rule.js +65 -0
  55. package/dist/builtins/export-spread/rule.d.ts +3 -0
  56. package/dist/builtins/export-spread/rule.js +65 -0
  57. package/dist/builtins/export-surface/rule.d.ts +6 -0
  58. package/dist/builtins/export-surface/rule.js +60 -0
  59. package/dist/builtins/file-cohesion/{fileCohesion.d.ts → rule.d.ts} +1 -1
  60. package/dist/builtins/file-cohesion/{fileCohesion.js → rule.js} +1 -1
  61. package/dist/builtins/function-cohesion/{functionCohesion.d.ts → rule.d.ts} +1 -1
  62. package/dist/builtins/function-cohesion/{functionCohesion.js → rule.js} +1 -1
  63. package/dist/builtins/index.d.ts +2 -2
  64. package/dist/builtins/index.js +33 -13
  65. package/dist/builtins/loops.d.ts +1 -1
  66. package/dist/builtins/loops.js +1 -1
  67. package/dist/builtins/module-cycle/{moduleCycle.d.ts → rule.d.ts} +1 -1
  68. package/dist/builtins/module-dependencies/rule.d.ts +3 -0
  69. package/dist/builtins/module-dependencies/rule.js +67 -0
  70. package/dist/builtins/module-externals/rule.d.ts +2 -0
  71. package/dist/builtins/module-externals/{moduleExternals.js → rule.js} +3 -3
  72. package/dist/builtins/module-size/rule.d.ts +6 -0
  73. package/dist/builtins/module-size/rule.js +36 -0
  74. package/dist/builtins/no-cross-module-reexport/rule.d.ts +2 -0
  75. package/dist/builtins/no-cross-module-reexport/{noCrossModuleReexport.js → rule.js} +1 -1
  76. package/dist/builtins/no-cycle/{noCycle.d.ts → rule.d.ts} +1 -1
  77. package/dist/builtins/no-double-export/rule.d.ts +2 -0
  78. package/dist/builtins/no-double-export/{noDoubleExport.js → rule.js} +1 -1
  79. package/dist/builtins/packages.d.ts +1 -1
  80. package/dist/builtins/place.d.ts +1 -1
  81. package/dist/builtins/shared.d.ts +1 -1
  82. package/dist/builtins/unclassified/rule.d.ts +2 -0
  83. package/dist/builtins/unclassified/rule.js +15 -0
  84. package/dist/builtins/unresolved-import/rule.d.ts +2 -0
  85. package/dist/builtins/unresolved-import/{unresolvedImport.js → rule.js} +1 -1
  86. package/dist/builtins/unused-declaration/declarations.d.ts +3 -3
  87. package/dist/builtins/unused-declaration/empty.d.ts +1 -1
  88. package/dist/builtins/unused-declaration/empty.js +1 -1
  89. package/dist/builtins/unused-declaration/grants.d.ts +1 -1
  90. package/dist/builtins/unused-declaration/grants.js +42 -10
  91. package/dist/builtins/unused-declaration/rule.d.ts +2 -0
  92. package/dist/builtins/unused-declaration/{unusedDeclaration.js → rule.js} +1 -1
  93. package/dist/builtins/unused-declaration/surfaces.d.ts +1 -1
  94. package/dist/builtins/unused-declaration/surfaces.js +1 -1
  95. package/dist/cli.js +34 -54
  96. package/dist/{model → domain/facts}/facts.d.ts +20 -0
  97. package/dist/{model/archetype → domain/fixes}/fixes.d.ts +2 -2
  98. package/dist/{model/archetype → domain/fixes}/fixes.js +2 -6
  99. package/dist/domain/language/compile.d.ts +12 -0
  100. package/dist/domain/language/compile.js +58 -0
  101. package/dist/{model/archetype → domain/language}/references.d.ts +6 -7
  102. package/dist/{model/archetype → domain/language}/references.js +41 -26
  103. package/dist/{model/archetype → domain/language}/requirements.d.ts +3 -3
  104. package/dist/{model/archetype → domain/language}/requirements.js +2 -1
  105. package/dist/domain/language/rulesets.d.ts +11 -0
  106. package/dist/domain/language/rulesets.js +60 -0
  107. package/dist/{model/archetype/language.d.ts → domain/language/structure.d.ts} +4 -3
  108. package/dist/{model/archetype/language.js → domain/language/structure.js} +33 -12
  109. package/dist/{model/archetype → domain/model}/data.d.ts +9 -6
  110. package/dist/{model/archetype → domain/model}/reach.d.ts +2 -0
  111. package/dist/{model/archetype → domain/model}/reach.js +10 -7
  112. package/dist/{model/archetype → domain/model}/resolve.d.ts +2 -0
  113. package/dist/{model/archetype → domain/model}/resolve.js +6 -1
  114. package/dist/{model/archetype → domain/model}/settings.d.ts +3 -3
  115. package/dist/domain/model/settings.js +83 -0
  116. package/dist/domain/model/text.d.ts +4 -0
  117. package/dist/domain/model/text.js +13 -0
  118. package/dist/{rules → domain/rules}/registry.d.ts +1 -0
  119. package/dist/{rules → domain/rules}/registry.js +11 -1
  120. package/dist/{rules → domain/rules}/rule.d.ts +11 -4
  121. package/dist/{rules → domain/rules}/rule.js +1 -1
  122. package/dist/index.d.ts +6 -6
  123. package/dist/index.js +2 -2
  124. package/package.json +1 -1
  125. package/dist/analysis/folders.d.ts +0 -2
  126. package/dist/analysis/specifiers.d.ts +0 -2
  127. package/dist/analysis/specifiers.js +0 -6
  128. package/dist/analysis/typescript.d.ts +0 -2
  129. package/dist/app/unclassified.d.ts +0 -5
  130. package/dist/app/unclassified.js +0 -13
  131. package/dist/builtins/dependency-direction/dependencyDirection.d.ts +0 -2
  132. package/dist/builtins/module-externals/moduleExternals.d.ts +0 -2
  133. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +0 -2
  134. package/dist/builtins/no-double-export/noDoubleExport.d.ts +0 -2
  135. package/dist/builtins/unresolved-import/unresolvedImport.d.ts +0 -2
  136. package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +0 -2
  137. package/dist/model/archetype/rulesets.d.ts +0 -5
  138. package/dist/model/archetype/rulesets.js +0 -139
  139. package/dist/model/archetype/settings.js +0 -54
  140. /package/dist/{analysis → adapters/analysis}/complexity.js +0 -0
  141. /package/dist/{analysis → adapters/analysis}/folders.js +0 -0
  142. /package/dist/{git → adapters/git}/cache.js +0 -0
  143. /package/dist/{git → adapters/git}/repository.js +0 -0
  144. /package/dist/{config → adapters/git}/toolDigest.d.ts +0 -0
  145. /package/dist/{reporters → adapters/reporters}/agent.d.ts +0 -0
  146. /package/dist/{reporters → adapters/reporters}/agent.js +0 -0
  147. /package/dist/{reporters → adapters/reporters}/claudeHook.d.ts +0 -0
  148. /package/dist/{reporters → adapters/reporters}/claudeHook.js +0 -0
  149. /package/dist/{reporters → adapters/reporters}/index.d.ts +0 -0
  150. /package/dist/{reporters → adapters/reporters}/index.js +0 -0
  151. /package/dist/{reporters → adapters/reporters}/json.d.ts +0 -0
  152. /package/dist/{reporters → adapters/reporters}/json.js +0 -0
  153. /package/dist/{reporters → adapters/reporters}/model.js +0 -0
  154. /package/dist/{reporters → adapters/reporters}/omp.d.ts +0 -0
  155. /package/dist/{reporters → adapters/reporters}/omp.js +0 -0
  156. /package/dist/{reporters → adapters/reporters}/reporter.js +0 -0
  157. /package/dist/{reporters → adapters/reporters}/sarif.d.ts +0 -0
  158. /package/dist/{reporters → adapters/reporters}/sarif.js +0 -0
  159. /package/dist/{reporters → adapters/reporters}/text.js +0 -0
  160. /package/dist/builtins/module-cycle/{moduleCycle.js → rule.js} +0 -0
  161. /package/dist/builtins/no-cycle/{noCycle.js → rule.js} +0 -0
  162. /package/dist/{model → domain/errors}/configError.d.ts +0 -0
  163. /package/dist/{model → domain/errors}/configError.js +0 -0
  164. /package/dist/{model → domain/facts}/facts.js +0 -0
  165. /package/dist/{model/archetype → domain/model}/data.js +0 -0
  166. /package/dist/{model/archetype → domain/model}/shared.d.ts +0 -0
  167. /package/dist/{model/archetype → domain/model}/shared.js +0 -0
  168. /package/dist/{rules → domain/rules}/diff.d.ts +0 -0
  169. /package/dist/{rules → domain/rules}/diff.js +0 -0
@@ -15,7 +15,8 @@ export interface PackageRefData {
15
15
  pattern: string;
16
16
  }
17
17
  export type RefData = ModuleRefData | RequiredRefData | PackageRefData;
18
- export type RuleSettingsData = 'off' | Record<string, boolean | Record<string, unknown>>;
18
+ export type RuleSettingsData = Record<string, boolean | Record<string, unknown>>;
19
+ export type RulesetData = readonly RuleSettingsData[];
19
20
  export type ExposesData = {
20
21
  kind: 'all';
21
22
  } | {
@@ -34,7 +35,8 @@ export interface FilesModuleData {
34
35
  exposes?: Exclude<ExposesData, {
35
36
  kind: 'surfaces';
36
37
  }>;
37
- rules?: RuleSettingsData;
38
+ ruleset?: RulesetData;
39
+ testRuleset?: RulesetData;
38
40
  description?: string;
39
41
  }
40
42
  export interface DirModuleData {
@@ -46,7 +48,8 @@ export interface DirModuleData {
46
48
  using: RefData[];
47
49
  inputs?: Record<string, RefData>;
48
50
  testInputs?: Record<string, RefData>;
49
- rules?: RuleSettingsData;
51
+ ruleset?: RulesetData;
52
+ testRuleset?: RulesetData;
50
53
  description?: string;
51
54
  }
52
55
  export interface TestModuleData {
@@ -58,7 +61,7 @@ export interface TestModuleData {
58
61
  exposes?: Exclude<ExposesData, {
59
62
  kind: 'surfaces';
60
63
  }>;
61
- rules?: RuleSettingsData;
64
+ ruleset?: RulesetData;
62
65
  description?: string;
63
66
  }
64
67
  export type ModuleData = FilesModuleData | DirModuleData | TestModuleData;
@@ -78,12 +81,12 @@ export interface ArchetypeData {
78
81
  exposes?: ExposesData;
79
82
  testExposes?: Record<string, RefData[]>;
80
83
  testUsing?: RefData[];
81
- rules?: RuleSettingsData;
84
+ ruleset?: RulesetData;
85
+ testRuleset?: RulesetData;
82
86
  }
83
87
  export interface ConfigData {
84
88
  file: string;
85
89
  root: string;
86
90
  archetypes: Record<string, ArchetypeData>;
87
91
  tsconfig: string[];
88
- rules?: RuleSettingsData;
89
92
  }
@@ -28,6 +28,7 @@ export type Verdict = {
28
28
  } & Refusal);
29
29
  export interface Verdicts {
30
30
  reach(file: string): readonly Grant[] | undefined;
31
+ grants(module: ModuleView, view?: View): readonly Grant[];
31
32
  exposes(module: ModuleView): ExposesData | undefined;
32
33
  surface(module: ModuleView, name?: string, view?: View): ReadonlySet<string>;
33
34
  verdict(from: string, to: string): Verdict | undefined;
@@ -38,3 +39,4 @@ export declare const verdicts: (config: ConfigData, model: Model) => Verdicts;
38
39
  export declare const instanceOf: (state: {
39
40
  config: ConfigData;
40
41
  }, holder: ModuleView) => ModuleView;
42
+ export declare const modulesNamed: (config: ConfigData, holder: ModuleView, ref: RefData) => ModuleView[];
@@ -1,5 +1,5 @@
1
- import { show } from './references.js';
2
- import { declarationOf, fillPattern, globRegex } from './resolve.js';
1
+ import { show } from './text.js';
2
+ import { ancestors, declarationOf, fillPattern, globRegex, subtree } from './resolve.js';
3
3
  export const grantText = ({ owner, ref }) => (ref === undefined ? owner : `${owner} uses ${ref}`);
4
4
  const none = new Set();
5
5
  const union = (sets) => {
@@ -9,14 +9,11 @@ const union = (sets) => {
9
9
  all.add(file);
10
10
  return all;
11
11
  };
12
- const ancestors = function* (module) {
13
- for (let at = module.parent; at; at = at.parent)
14
- yield at;
15
- };
16
12
  export const verdicts = (config, model) => {
17
13
  const state = { config, model, surfaces: new Map(), reaches: new Map() };
18
14
  return {
19
15
  reach: (file) => reach(state, file),
16
+ grants: (module, view = 'production') => using(state, module, view),
20
17
  exposes: (module) => exposes(state, module),
21
18
  surface: (module, name, view = 'production') => surface(state, module, name, view),
22
19
  verdict: (from, to) => verdict(state, from, to),
@@ -105,6 +102,13 @@ const concrete = (state, holder, ref, view) => {
105
102
  const found = concrete(state, instance.parent, bound.kind === 'package' ? bound : { ...bound, surface }, view);
106
103
  return found && { ...found, via: [bound, ...found.via] };
107
104
  };
105
+ export const modulesNamed = (config, holder, ref) => {
106
+ const found = concrete({ config }, holder, ref, 'production');
107
+ if (!found || found.ref.kind !== 'module')
108
+ return [];
109
+ const { module } = found.ref;
110
+ return found.holder.children.filter((child) => child.name === module);
111
+ };
108
112
  const resolve = (state, module, ref, view) => {
109
113
  const found = concrete(state, module, ref, view);
110
114
  if (!found || found.ref.kind !== 'module')
@@ -194,7 +198,6 @@ const filesTests = (state, module, of) => {
194
198
  ...[holder, ...ancestors(holder)].flatMap((at) => testKit(state, at)),
195
199
  ];
196
200
  };
197
- const subtree = (module) => [module, ...module.children.flatMap(subtree)];
198
201
  const testKit = (state, at) => grants(state, at, `${at.archetype}.test.provides`, state.config.archetypes[at.archetype].testUsing ?? [], 'test');
199
202
  const using = (state, module, view) => {
200
203
  const declared = declarationOf(state.config, module);
@@ -28,3 +28,5 @@ export declare const fillPattern: (pattern: string, captures: Record<string, str
28
28
  export declare const resolveModel: (config: ConfigData, folders: Folders, files: readonly string[]) => Model;
29
29
  export declare const specificity: (pattern: string) => number;
30
30
  export declare const globRegex: (pattern: string) => RegExp;
31
+ export declare const ancestors: (module: ModuleView) => Generator<ModuleView>;
32
+ export declare const subtree: (module: ModuleView) => ModuleView[];
@@ -1,4 +1,4 @@
1
- import { ConfigError } from '../configError.js';
1
+ import { ConfigError } from '../errors/configError.js';
2
2
  export const declarationOf = (config, module) => module.parent && config.archetypes[module.parent.archetype].modules.find((data) => data.name === module.name);
3
3
  export const rootId = '(root)';
4
4
  const placeholders = /\{(\w+)\}/g;
@@ -301,3 +301,8 @@ export const globRegex = (pattern) => {
301
301
  globCache.set(pattern, regex);
302
302
  return regex;
303
303
  };
304
+ export const ancestors = function* (module) {
305
+ for (let at = module.parent; at; at = at.parent)
306
+ yield at;
307
+ };
308
+ export const subtree = (module) => [module, ...module.children.flatMap(subtree)];
@@ -5,9 +5,8 @@ export interface RuleAt {
5
5
  options: Record<string, unknown>;
6
6
  }
7
7
  export interface Settings {
8
- atFile(file: string, id: string, defaults?: object): RuleAt;
9
- atModule(module: ModuleView, id: string, defaults?: object): RuleAt;
10
- decidedAt(module: ModuleView, id: string): boolean | undefined;
8
+ atFile(file: string, id: string, defaults?: object, moduleOptions?: readonly string[]): RuleAt;
9
+ atModule(module: ModuleView, id: string, defaults?: object, moduleOptions?: readonly string[]): RuleAt;
11
10
  }
12
11
  export declare const settings: (config: ConfigData, model: Model) => Settings;
13
12
  export type ResolvedRules = Record<string, false | Record<string, unknown>>;
@@ -18,5 +17,6 @@ export interface RulesOf {
18
17
  export declare const rulesOf: (config: ConfigData, model: Model, rules: readonly {
19
18
  id: string;
20
19
  defaults?: object;
20
+ moduleOptions?: readonly string[];
21
21
  }[]) => RulesOf;
22
22
  export declare const anyRuleOn: (config: ConfigData) => boolean;
@@ -0,0 +1,83 @@
1
+ import { modulesNamed } from './reach.js';
2
+ import { declarationOf } from './resolve.js';
3
+ import { isRecord } from './shared.js';
4
+ const archetypeOf = (config, module) => module.archetype === undefined ? undefined : config.archetypes[module.archetype];
5
+ const layered = (config, module, test) => {
6
+ const pick = (place) => test ? place?.testRuleset : place?.ruleset;
7
+ const inside = { ruleset: pick(archetypeOf(config, module)), holder: module };
8
+ return module.parent ? [inside, { ruleset: pick(declarationOf(config, module)), holder: module.parent }] : [inside];
9
+ };
10
+ const production = (config, module) => [
11
+ ...(module.parent ? production(config, module.parent) : []),
12
+ ...layered(config, module, false),
13
+ ];
14
+ const tests = (config, module) => [
15
+ ...(module.parent ? tests(config, module.parent) : []),
16
+ ...layered(config, module, true),
17
+ ];
18
+ const filesModuleOf = (config, holder, test) => {
19
+ const of = declarationOf(config, test)?.of;
20
+ return of === undefined ? undefined : archetypeOf(config, holder).modules.find((module) => module.name === of);
21
+ };
22
+ const layersOf = (config, module) => {
23
+ if (!module.test)
24
+ return production(config, module);
25
+ const chain = [];
26
+ let holder = module;
27
+ for (; holder.test && holder.parent; holder = holder.parent)
28
+ chain.unshift(holder);
29
+ const files = filesModuleOf(config, holder, chain[0]);
30
+ return [
31
+ ...production(config, holder),
32
+ { ruleset: files?.ruleset, holder },
33
+ ...tests(config, holder),
34
+ { ruleset: files?.testRuleset, holder },
35
+ ...chain.flatMap((test) => layered(config, test, false)),
36
+ ];
37
+ };
38
+ const named = (config, { holder }, refs) => refs.flatMap((ref) => modulesNamed(config, holder, ref).map((module) => module.id));
39
+ const ruleAt = (config, layers, id, defaults = {}, moduleOptions = []) => {
40
+ const at = { on: false, options: { ...defaults } };
41
+ for (const layer of layers) {
42
+ const setting = Object.hasOwn(layer.rules, id) ? layer.rules[id] : layer.rules['*'];
43
+ if (setting === undefined)
44
+ continue;
45
+ at.on = setting !== false;
46
+ if (!isRecord(setting))
47
+ continue;
48
+ const modules = moduleOptions.filter((key) => Array.isArray(setting[key]));
49
+ Object.assign(at.options, setting, Object.fromEntries(modules.map((key) => [key, named(config, layer, setting[key])])));
50
+ }
51
+ return at;
52
+ };
53
+ export const settings = (config, model) => {
54
+ const cache = new Map();
55
+ const at = (module) => {
56
+ if (!module)
57
+ return [];
58
+ let found = cache.get(module);
59
+ if (!found)
60
+ cache.set(module, (found = layersOf(config, module).flatMap(({ ruleset, holder }) => (ruleset ?? []).map((rules) => ({ rules, holder })))));
61
+ return found;
62
+ };
63
+ return {
64
+ atFile: (file, id, defaults, moduleOptions) => ruleAt(config, at(model.moduleOf(file)), id, defaults, moduleOptions),
65
+ atModule: (module, id, defaults, moduleOptions) => ruleAt(config, at(module), id, defaults, moduleOptions),
66
+ };
67
+ };
68
+ export const rulesOf = (config, model, rules) => {
69
+ const settled = settings(config, model);
70
+ return {
71
+ defaults: Object.fromEntries(rules.map(({ id, defaults }) => [id, { ...defaults }])),
72
+ module: (module) => Object.fromEntries(rules.map(({ id, defaults, moduleOptions }) => {
73
+ const { on, options } = settled.atModule(module, id, defaults, moduleOptions);
74
+ return [id, on ? options : false];
75
+ })),
76
+ };
77
+ };
78
+ const turnsOn = (ruleset) => ruleset !== undefined && ruleset.some((layer) => Object.values(layer).some((setting) => setting !== false));
79
+ const rulesetsOf = (place) => [
80
+ place.ruleset,
81
+ 'testRuleset' in place ? place.testRuleset : undefined,
82
+ ];
83
+ export const anyRuleOn = (config) => Object.values(config.archetypes).some((archetype) => [archetype, ...archetype.modules].flatMap(rulesetsOf).some(turnsOn));
@@ -0,0 +1,4 @@
1
+ import type { ExposesData, RefData } from './data.ts';
2
+ export declare const packageText: (pattern: string) => string;
3
+ export declare const show: (ref: RefData) => string;
4
+ export declare const exposesText: (exposes: ExposesData | undefined) => string;
@@ -0,0 +1,13 @@
1
+ export const packageText = (pattern) => pattern.startsWith('node:') || pattern.startsWith('virtual:') ? pattern : `npm:${pattern}`;
2
+ export const show = (ref) => ref.kind === 'package'
3
+ ? packageText(ref.pattern)
4
+ : `${ref.kind === 'module' && ref.top ? 'top.' : ''}${ref.kind === 'required' ? ref.name : ref.module}${ref.surface === undefined ? '' : `.${ref.surface}`}`;
5
+ export const exposesText = (exposes) => {
6
+ if (exposes === undefined)
7
+ return 'nothing';
8
+ if (exposes.kind === 'all')
9
+ return 'all its files';
10
+ if (exposes.kind === 'files')
11
+ return `{ files: [${exposes.patterns.join(', ')}] }`;
12
+ return Object.keys(exposes.surfaces).join(', ');
13
+ };
@@ -2,4 +2,5 @@ import type { Rule } from './rule.ts';
2
2
  export declare const registry: <R extends {
3
3
  id: string;
4
4
  }>(rules: readonly R[]) => ReadonlyMap<string, R>;
5
+ export declare const pluginRulesets: (plugins: Record<string, unknown>) => Record<string, unknown>;
5
6
  export declare const pluginRules: (plugins: Record<string, unknown>) => Rule[];
@@ -1,4 +1,4 @@
1
- import { ConfigError } from '../model/configError.js';
1
+ import { ConfigError } from '../errors/configError.js';
2
2
  export const registry = (rules) => {
3
3
  const byId = new Map();
4
4
  for (const rule of rules) {
@@ -9,10 +9,20 @@ export const registry = (rules) => {
9
9
  return byId;
10
10
  };
11
11
  const name = /^[a-z][a-z0-9-]*$/;
12
+ export const pluginRulesets = (plugins) => Object.fromEntries(Object.entries(plugins).flatMap(([namespace, plugin]) => {
13
+ const rulesets = plugin.rulesets;
14
+ if (rulesets === undefined)
15
+ return [];
16
+ if (typeof rulesets !== 'object' || rulesets === null || Array.isArray(rulesets))
17
+ throw new ConfigError(`plugins.${namespace}: expected rulesets by name, { recommended: { rules: {...} } }`);
18
+ return Object.entries(rulesets).map(([ruleset, definition]) => [`${namespace}:${ruleset}`, definition]);
19
+ }));
12
20
  export const pluginRules = (plugins) => Object.entries(plugins).flatMap(([namespace, plugin]) => namespaced(namespace, plugin, `plugins.${namespace}`));
13
21
  const namespaced = (namespace, plugin, at) => {
14
22
  if (!name.test(namespace))
15
23
  throw new ConfigError(`${at}: a namespace is lowercase letters, digits and dashes`);
24
+ if (namespace === 'warp')
25
+ throw new ConfigError(`${at}: warp is the namespace of warp's own rulesets; name it otherwise`);
16
26
  const rules = typeof plugin === 'object' && plugin !== null ? plugin.rules : undefined;
17
27
  if (!Array.isArray(rules))
18
28
  throw new ConfigError(`${at}: expected a plugin, { rules: Rule[] }, as the default export of its file`);
@@ -1,7 +1,7 @@
1
- import type { ConfigData } from '../model/archetype/data.ts';
2
- import type { Verdicts } from '../model/archetype/reach.ts';
3
- import type { Model, ModuleView } from '../model/archetype/resolve.ts';
4
- import type { FileFacts, Project } from '../model/facts.ts';
1
+ import type { ConfigData } from '../model/data.ts';
2
+ import type { Verdicts } from '../model/reach.ts';
3
+ import type { Model, ModuleView } from '../model/resolve.ts';
4
+ import type { FileFacts, Project } from '../facts/facts.ts';
5
5
  export interface Site {
6
6
  file: string;
7
7
  line: number;
@@ -40,6 +40,7 @@ interface RuleBase<Options extends object> {
40
40
  id: string;
41
41
  docs?: string;
42
42
  defaults?: Options;
43
+ moduleOptions?: readonly string[];
43
44
  parseOptions?: (raw: Record<string, unknown>, at: string) => Partial<Options>;
44
45
  }
45
46
  export interface FileRule<Options extends object = object> extends RuleBase<Options> {
@@ -51,8 +52,14 @@ export interface ProjectRule<Options extends object = object> extends RuleBase<O
51
52
  check: (context: ProjectContext<Options>) => Finding[];
52
53
  }
53
54
  export type Rule = FileRule<any> | ProjectRule<any>;
55
+ export type RuleSetting = 'on' | 'off' | boolean | Record<string, unknown>;
56
+ export interface RulesetDefinition {
57
+ extends?: readonly string[];
58
+ rules?: Readonly<Record<string, RuleSetting>>;
59
+ }
54
60
  export interface Plugin {
55
61
  rules: readonly Rule[];
62
+ rulesets?: Readonly<Record<string, RulesetDefinition>>;
56
63
  }
57
64
  export declare const noOptions: (raw: Record<string, unknown>, at: string) => Partial<object>;
58
65
  export declare const wholeNumberOptions: <Options extends Record<string, number>>(defaults: Options) => (raw: Record<string, unknown>, at: string) => Partial<Options>;
@@ -1,4 +1,4 @@
1
- import { ConfigError } from '../model/configError.js';
1
+ import { ConfigError } from '../errors/configError.js';
2
2
  export const noOptions = (raw, at) => {
3
3
  const keys = Object.keys(raw);
4
4
  if (keys.length > 0)
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { loadConfigData, type LoadedConfigData } from './app/load.ts';
2
- export type * from './model/archetype/data.ts';
3
- export type { Complexity, FunctionFacts, ImportEdge, FileFacts, Member, MemberGraph, MemberKind, } from './model/facts.ts';
4
- export type { Model, ModuleView } from './model/archetype/resolve.ts';
5
- export type { Grant, Refusal, Verdict, Verdicts } from './model/archetype/reach.ts';
6
- export { ConfigError } from './model/configError.ts';
7
- export { type Finding, type FileContext, type FileRule, type Plugin, type ProjectContext, type ProjectRule, type Rule, type Setting, wholeNumberOptions, } from './rules/rule.ts';
2
+ export type * from './domain/model/data.ts';
3
+ export type { ClassFacts, Complexity, FunctionFacts, ImportEdge, FileFacts, Member, MemberGraph, MemberKind, } from './domain/facts/facts.ts';
4
+ export type { Model, ModuleView } from './domain/model/resolve.ts';
5
+ export type { Grant, Refusal, Verdict, Verdicts } from './domain/model/reach.ts';
6
+ export { ConfigError } from './domain/errors/configError.ts';
7
+ export { type Finding, type FileContext, type FileRule, type Plugin, type RuleSetting, type RulesetDefinition, type ProjectContext, type ProjectRule, type Rule, type Setting, wholeNumberOptions, } from './domain/rules/rule.ts';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { loadConfigData } from './app/load.js';
2
- export { ConfigError } from './model/configError.js';
3
- export { wholeNumberOptions, } from './rules/rule.js';
2
+ export { ConfigError } from './domain/errors/configError.js';
3
+ export { wholeNumberOptions, } from './domain/rules/rule.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yearofthedan/warp",
3
- "version": "0.4.1",
3
+ "version": "0.5.1",
4
4
  "description": "Architectural fitness checks for TypeScript codebases",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,2 +0,0 @@
1
- import type { Folders } from '../model/archetype/resolve.ts';
2
- export declare const diskFolders: (root: string) => Folders;
@@ -1,2 +0,0 @@
1
- import type { SourceFile } from 'typescript/ast';
2
- export declare const moduleSpecifiers: (sourceFile: SourceFile) => string[];
@@ -1,6 +0,0 @@
1
- import { isExportDeclaration, isImportDeclaration, isStringLiteralLikeNode } from 'typescript/ast/is';
2
- export const moduleSpecifiers = (sourceFile) => sourceFile.statements.flatMap((statement) => (isImportDeclaration(statement) || isExportDeclaration(statement)) &&
3
- statement.moduleSpecifier &&
4
- isStringLiteralLikeNode(statement.moduleSpecifier)
5
- ? [statement.moduleSpecifier.text]
6
- : []);
@@ -1,2 +0,0 @@
1
- import type { Analyzer } from '../app/ports.ts';
2
- export declare const typescriptAnalyzer: Analyzer;
@@ -1,5 +0,0 @@
1
- import type { Finding } from '../rules/rule.ts';
2
- import type { Model } from '../model/archetype/resolve.ts';
3
- import type { Settings } from '../model/archetype/settings.ts';
4
- export declare const unclassified = "unclassified";
5
- export declare const unclassifiedFindings: (model: Model, settled: Settings, docs: string | undefined) => Finding[];
@@ -1,13 +0,0 @@
1
- export const unclassified = 'unclassified';
2
- export const unclassifiedFindings = (model, settled, docs) => model.modules.flatMap((module) => (settled.decidedAt(module, unclassified) ?? module.parent === undefined)
3
- ? module.looseFiles.map((file) => ({
4
- rule: unclassified,
5
- key: `${unclassified}:${file}`,
6
- file,
7
- line: 1,
8
- value: 1,
9
- message: `${file} is in no module of ${module.parent ? `${module.id} (${module.archetype})` : 'the root'}, so nothing says what it may import. ` +
10
- `Add a files module to ${module.archetype} that matches it, or move it into a module's folder.`,
11
- ...(docs ? { docs } : {}),
12
- }))
13
- : []);
@@ -1,2 +0,0 @@
1
- import { type ProjectRule } from '../../rules/rule.ts';
2
- export declare const dependencyDirection: ProjectRule;
@@ -1,2 +0,0 @@
1
- import { type ProjectRule } from '../../rules/rule.ts';
2
- export declare const moduleExternals: ProjectRule;
@@ -1,2 +0,0 @@
1
- import { type ProjectRule } from '../../rules/rule.ts';
2
- export declare const noCrossModuleReexport: ProjectRule;
@@ -1,2 +0,0 @@
1
- import { type ProjectRule } from '../../rules/rule.ts';
2
- export declare const noDoubleExport: ProjectRule;
@@ -1,2 +0,0 @@
1
- import { type FileRule } from '../../rules/rule.ts';
2
- export declare const unresolvedImport: FileRule;
@@ -1,2 +0,0 @@
1
- import { type ProjectRule } from '../../rules/rule.ts';
2
- export declare const unusedDeclaration: ProjectRule;
@@ -1,5 +0,0 @@
1
- import { type ConfigData, type RuleSettingsData } from './data.ts';
2
- import { type Fail } from './shared.ts';
3
- export type ParseRules = (value: unknown, at: string) => RuleSettingsData | undefined;
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,139 +0,0 @@
1
- import { ConfigError } from '../configError.js';
2
- import { isTestData } from './data.js';
3
- import { isRecord, list } from './shared.js';
4
- export const withRuleset = (config, rulesets, name, parse, fail) => {
5
- const named = rulesetsOf(rulesets, fail);
6
- if (name !== undefined && !named.has(name)) {
7
- const names = [...named.keys()];
8
- throw new ConfigError(`rulesets: no ruleset called ${name}. ${names.length > 0 ? `Rulesets: ${names.join(', ')}` : 'The config declares none'}`);
9
- }
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);
16
- };
17
- export const recommended = 'warp:recommended';
18
- const rulesetKeys = ['extends', 'rules', 'test', 'on'];
19
- const rulesetsOf = (value, fail) => {
20
- const named = new Map();
21
- if (value === undefined)
22
- return named;
23
- if (!isRecord(value)) {
24
- fail('rulesets: expected rulesets by name, { default: { extends: [warp:recommended] } }');
25
- return named;
26
- }
27
- for (const [name, ruleset] of Object.entries(value)) {
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);
35
- }
36
- return named;
37
- };
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 [];
76
- }
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 [];
83
- });
84
- };
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
- for (let at = path.lastIndexOf('.'); at > 0; at = path.lastIndexOf('.', at - 1)) {
90
- const archetype = config.archetypes[path.slice(0, at)];
91
- const module = path.slice(at + 1);
92
- if (archetype?.modules.some((declared) => declared.name === module))
93
- return { archetype: archetype.name, module };
94
- }
95
- return undefined;
96
- };
97
- const applied = (config, layers, parse, fail) => {
98
- let rules = config.rules;
99
- const archetypes = Object.fromEntries(Object.entries(config.archetypes).map(([name, data]) => [name, { ...data, modules: [...data.modules] }]));
100
- for (const { target, settings, at } of layers) {
101
- const layer = parse(settings, at);
102
- if (!target) {
103
- rules = merged(rules, layer);
104
- continue;
105
- }
106
- const archetype = archetypes[target.archetype];
107
- if (!archetype) {
108
- fail(`${at}: ${target.archetype} is not in the config`);
109
- continue;
110
- }
111
- applyTo(archetype, target.module, layer, at, fail);
112
- }
113
- return rules === undefined ? { ...config, archetypes } : { ...config, archetypes, rules };
114
- };
115
- const applyTo = (archetype, module, layer, at, fail) => {
116
- if (module === undefined) {
117
- archetype.rules = merged(archetype.rules, layer);
118
- return;
119
- }
120
- const index = archetype.modules.findIndex((declared) => declared.name === module);
121
- if (index < 0)
122
- fail(`${at}: ${archetype.name} has modules ${list(archetype.modules.map((declared) => declared.name), 'none')}`);
123
- else
124
- archetype.modules[index] = { ...archetype.modules[index], rules: merged(archetype.modules[index].rules, layer) };
125
- };
126
- const merged = (base, layer) => {
127
- if (layer === undefined)
128
- return base;
129
- if (layer === 'off')
130
- return 'off';
131
- const result = base === 'off' ? { '*': false } : { ...base };
132
- for (const [id, setting] of Object.entries(layer)) {
133
- const was = result[id];
134
- if (setting === true && isRecord(was))
135
- continue;
136
- result[id] = isRecord(setting) && isRecord(was) ? { ...was, ...setting } : setting;
137
- }
138
- return result;
139
- };
@@ -1,54 +0,0 @@
1
- import { declarationOf } from './resolve.js';
2
- import { isRecord } from './shared.js';
3
- const own = (config, module) => [
4
- declarationOf(config, module)?.rules,
5
- module.archetype === undefined ? undefined : config.archetypes[module.archetype].rules,
6
- ];
7
- const levels = (config, module) => [
8
- ...own(config, module),
9
- ...(module.parent ? levels(config, module.parent) : [config.rules]),
10
- ];
11
- export const settings = (config, model) => {
12
- const fileLevels = (file) => {
13
- const module = model.moduleOf(file);
14
- return module ? levels(config, module) : [config.rules];
15
- };
16
- return {
17
- atFile: (file, id, defaults) => ruleAt(fileLevels(file), id, defaults),
18
- atModule: (module, id, defaults) => ruleAt(levels(config, module), id, defaults),
19
- decidedAt: (module, id) => decided(levels(config, module), id),
20
- };
21
- };
22
- const decision = (level, id) => {
23
- if (level === undefined)
24
- return undefined;
25
- if (level === 'off')
26
- return false;
27
- if (Object.hasOwn(level, id))
28
- return level[id] !== false;
29
- return level['*'];
30
- };
31
- const optionsAt = (level, id) => {
32
- const setting = typeof level === 'object' ? level[id] : undefined;
33
- return isRecord(setting) ? setting : undefined;
34
- };
35
- const decided = (levels, id) => levels.map((level) => decision(level, id)).find((said) => said !== undefined);
36
- const ruleAt = (levels, id, defaults = {}) => ({
37
- on: decided(levels, id) ?? true,
38
- options: Object.assign({}, defaults, ...levels.map((level) => optionsAt(level, id)).reverse()),
39
- });
40
- export const rulesOf = (config, model, rules) => {
41
- const settled = settings(config, model);
42
- return {
43
- defaults: Object.fromEntries(rules.map(({ id, defaults }) => [id, { ...defaults }])),
44
- module: (module) => Object.fromEntries(rules.map(({ id, defaults }) => {
45
- const { on, options } = settled.atModule(module, id, defaults);
46
- return [id, on ? options : false];
47
- })),
48
- };
49
- };
50
- const turnsOn = (rules) => rules !== undefined && rules !== 'off' && Object.values(rules).some((setting) => setting !== false);
51
- export const anyRuleOn = (config) => config.rules === undefined ||
52
- (config.rules !== 'off' && config.rules['*'] !== false) ||
53
- turnsOn(config.rules) ||
54
- Object.values(config.archetypes).some((archetype) => turnsOn(archetype.rules) || archetype.modules.some((module) => turnsOn(module.rules)));
File without changes
File without changes