@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
@@ -1,3 +1,3 @@
1
- import type { Cache } from '../app/ports.ts';
1
+ import type { Cache } from '../../app/ports.ts';
2
2
  export declare const maxAgeMs: number;
3
3
  export declare const gitDirCache: <T>(projectRoot: string, now?: () => number) => Cache<T>;
@@ -0,0 +1,2 @@
1
+ import type { Cache, CachedFacts, FactsStore } from '../../app/ports.ts';
2
+ export declare const factsStore: (cache: Cache<CachedFacts>, toolDigest: string, projectPath: string) => FactsStore;
@@ -0,0 +1,10 @@
1
+ export const factsStore = (cache, toolDigest, projectPath) => {
2
+ const key = `${toolDigest}-facts-${projectPath.replaceAll('/', '_')}`;
3
+ return {
4
+ read: () => cache.get(key),
5
+ write(facts) {
6
+ cache.set(key, facts);
7
+ cache.prune((other) => other.startsWith(`${toolDigest}-`));
8
+ },
9
+ };
10
+ };
@@ -1,3 +1,3 @@
1
- import type { Repository } from '../app/ports.ts';
1
+ import type { Repository } from '../../app/ports.ts';
2
2
  export declare const gitRepository: (projectRoot: string) => Repository;
3
3
  export declare const projectPathOf: (projectRoot: string) => string;
@@ -3,7 +3,7 @@ import { readdirSync, readFileSync } from 'node:fs';
3
3
  import { createRequire } from 'node:module';
4
4
  import { join } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
- const sourceDir = fileURLToPath(new URL('../', import.meta.url));
6
+ const sourceDir = fileURLToPath(new URL('../../', import.meta.url));
7
7
  const code = /(?<!\.test)\.(ts|js)$/;
8
8
  export const toolDigest = () => {
9
9
  const digest = createHash('sha256').update(createRequire(import.meta.url)('typescript/package.json').version);
@@ -1,4 +1,4 @@
1
- import type { Model } from '../model/archetype/resolve.ts';
2
- import type { RulesOf } from '../model/archetype/settings.ts';
1
+ import type { Model } from '../../domain/model/resolve.ts';
2
+ import type { RulesOf } from '../../domain/model/settings.ts';
3
3
  export declare const modelText: (model: Model, rules?: RulesOf) => string;
4
4
  export declare const modelJson: (model: Model, rules?: RulesOf) => string;
@@ -1,5 +1,5 @@
1
- import type { Delta } from '../rules/diff.ts';
2
- import type { Finding } from '../rules/rule.ts';
1
+ import type { Delta } from '../../domain/rules/diff.ts';
2
+ import type { Finding } from '../../domain/rules/rule.ts';
3
3
  export type Report = {
4
4
  mode: 'check';
5
5
  findings: Finding[];
@@ -1,5 +1,5 @@
1
- import type { Delta } from '../rules/diff.ts';
2
- import type { Finding } from '../rules/rule.ts';
1
+ import type { Delta } from '../../domain/rules/diff.ts';
2
+ import type { Finding } from '../../domain/rules/rule.ts';
3
3
  import type { Reporter } from './reporter.ts';
4
4
  export declare const textReporter: Reporter;
5
5
  export declare const diffText: (delta: Delta) => string;
@@ -1,8 +1,8 @@
1
- import type { ConfigData } from '../model/archetype/data.ts';
1
+ import type { ConfigData } from '../domain/model/data.ts';
2
2
  import { type ConfigLines } from './configLines.ts';
3
- import { type Model } from '../model/archetype/resolve.ts';
4
- import type { Project } from '../model/facts.ts';
5
- import type { Finding, Rule } from '../rules/rule.ts';
3
+ import { type Model } from '../domain/model/resolve.ts';
4
+ import type { Project } from '../domain/facts/facts.ts';
5
+ import type { Finding, Rule } from '../domain/rules/rule.ts';
6
6
  import type { Analyzer, FactsStore, FoldersAt } from './ports.ts';
7
7
  export interface ProjectCheck {
8
8
  config: unknown;
@@ -18,8 +18,7 @@ export declare const modelProject: ({ analyzer, foldersAt, facts }: Adapters, ro
18
18
  model: Model;
19
19
  project: Project;
20
20
  };
21
- export declare const projectCheck: (adapters: Adapters, config: ConfigData, { rules, unclassifiedDocs, lines, }?: {
21
+ export declare const projectCheck: (adapters: Adapters, config: ConfigData, { rules, lines }?: {
22
22
  rules?: readonly Rule[];
23
- unclassifiedDocs?: string;
24
23
  lines?: ConfigLines;
25
24
  }) => ProjectCheck;
package/dist/app/check.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import { checkModel } from './checkModel.js';
2
2
  import { placeFindings } from './configLines.js';
3
- import { resolveModel } from '../model/archetype/resolve.js';
3
+ import { resolveModel } from '../domain/model/resolve.js';
4
4
  export const modelProject = ({ analyzer, foldersAt, facts }, root, config) => {
5
5
  const analyzed = analyzer.analyze(root, config.tsconfig, facts);
6
6
  const project = { files: analyzed.files.filter((file) => file.path !== config.file) };
7
7
  const files = project.files.map((file) => file.path);
8
8
  return { model: resolveModel(config, foldersAt(root), files), project };
9
9
  };
10
- export const projectCheck = (adapters, config, { rules = [], unclassifiedDocs, lines = new Map(), } = {}) => ({
10
+ export const projectCheck = (adapters, config, { rules = [], lines = new Map() } = {}) => ({
11
11
  config,
12
12
  requiredFile: config.tsconfig[0],
13
13
  check: (root) => {
14
14
  const { model, project } = modelProject(adapters, root, config);
15
- return placeFindings(lines, config.file, checkModel(config, model, project, rules, unclassifiedDocs));
15
+ return placeFindings(lines, config.file, checkModel(config, model, project, rules));
16
16
  },
17
17
  });
@@ -1,5 +1,5 @@
1
- import type { Project } from '../model/facts.ts';
2
- import type { Rule, Finding } from '../rules/rule.ts';
3
- import type { ConfigData } from '../model/archetype/data.ts';
4
- import type { Model } from '../model/archetype/resolve.ts';
5
- export declare const checkModel: (config: ConfigData, model: Model, project: Project, rules?: readonly Rule[], unclassifiedDocs?: string) => Finding[];
1
+ import type { Project } from '../domain/facts/facts.ts';
2
+ import type { Rule, Finding } from '../domain/rules/rule.ts';
3
+ import type { ConfigData } from '../domain/model/data.ts';
4
+ import type { Model } from '../domain/model/resolve.ts';
5
+ export declare const checkModel: (config: ConfigData, model: Model, project: Project, rules?: readonly Rule[]) => Finding[];
@@ -1,14 +1,13 @@
1
- import { verdicts } from '../model/archetype/reach.js';
2
- import { settings } from '../model/archetype/settings.js';
3
- import { unclassifiedFindings } from './unclassified.js';
4
- export const checkModel = (config, model, project, rules = [], unclassifiedDocs) => {
1
+ import { verdicts } from '../domain/model/reach.js';
2
+ import { settings } from '../domain/model/settings.js';
3
+ export const checkModel = (config, model, project, rules = []) => {
5
4
  const settled = settings(config, model);
6
5
  const judged = verdicts(config, model);
7
6
  const found = rules.flatMap((rule) => {
8
7
  const findings = run(rule, { project, config, model, verdicts: judged }, settled);
9
8
  return rule.docs ? findings.map((finding) => ({ ...finding, docs: rule.docs })) : findings;
10
9
  });
11
- return [...unclassifiedFindings(model, settled, unclassifiedDocs), ...found].map((finding) => placed(model, finding));
10
+ return found.map((finding) => placed(model, finding));
12
11
  };
13
12
  const placed = (model, finding) => {
14
13
  const module = model.moduleOf(finding.file);
@@ -20,11 +19,11 @@ const run = (rule, shared, settled) => {
20
19
  if (rule.scope === 'project')
21
20
  return rule.check({
22
21
  ...shared,
23
- atFile: (file) => settled.atFile(file, rule.id, rule.defaults),
24
- atModule: (module) => settled.atModule(module, rule.id, rule.defaults),
22
+ atFile: (file) => settled.atFile(file, rule.id, rule.defaults, rule.moduleOptions),
23
+ atModule: (module) => settled.atModule(module, rule.id, rule.defaults, rule.moduleOptions),
25
24
  });
26
25
  return shared.project.files.flatMap((file) => {
27
- const { on, options } = settled.atFile(file.path, rule.id, rule.defaults);
26
+ const { on, options } = settled.atFile(file.path, rule.id, rule.defaults, rule.moduleOptions);
28
27
  return on ? rule.check({ file, options }) : [];
29
28
  });
30
29
  };
@@ -1,4 +1,4 @@
1
- import type { Finding } from '../rules/rule.ts';
1
+ import type { Finding } from '../domain/rules/rule.ts';
2
2
  export type ConfigLines = ReadonlyMap<string, number>;
3
3
  export declare const lineAt: (lines: ConfigLines, text: string) => number | undefined;
4
4
  export declare const placeProblems: (lines: ConfigLines, file: string, message: string) => string;
@@ -1,4 +1,4 @@
1
- import { type Delta } from '../rules/diff.ts';
1
+ import { type Delta } from '../domain/rules/diff.ts';
2
2
  import type { ProjectCheck } from './check.ts';
3
3
  import type { FindingsCache, Repository } from './ports.ts';
4
4
  export interface Range {
package/dist/app/diff.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { anchorAtChange, diff } from '../rules/diff.js';
2
+ import { anchorAtChange, diff } from '../domain/rules/diff.js';
3
3
  export class InvalidRange extends Error {
4
4
  }
5
5
  export const parseRange = (range) => {
@@ -0,0 +1,6 @@
1
+ import type { Cache } from './ports.ts';
2
+ export interface Failures {
3
+ isNew(problem: string): boolean;
4
+ forget(): void;
5
+ }
6
+ export declare const failures: (cache: Cache<string> | undefined, toolDigest: string) => Failures;
@@ -0,0 +1,15 @@
1
+ export const failures = (cache, toolDigest) => {
2
+ const key = `${toolDigest}-failure`;
3
+ return {
4
+ isNew(problem) {
5
+ if (cache?.get(key) === problem)
6
+ return false;
7
+ cache?.set(key, problem);
8
+ return true;
9
+ },
10
+ forget() {
11
+ if (cache?.get(key) !== undefined)
12
+ cache.prune((other) => other !== key);
13
+ },
14
+ };
15
+ };
@@ -1,13 +1,19 @@
1
- import { type Rule } from '../rules/rule.ts';
2
- import type { ConfigData } from '../model/archetype/data.ts';
1
+ import type { ConfigData } from '../domain/model/data.ts';
2
+ import { type Plugin, type Rule } from '../domain/rules/rule.ts';
3
+ import { type ConfigLines } from './configLines.ts';
4
+ import type { ConfigSource } from './ports.ts';
3
5
  export interface LoadedConfigData {
4
6
  config: ConfigData;
5
7
  rules: Rule[];
6
8
  }
7
9
  export interface LoadOptions {
8
- builtins?: readonly Rule[];
9
- ruleset?: string;
10
+ builtins?: Plugin;
10
11
  file?: string;
11
12
  plugins?: Record<string, unknown>;
12
13
  }
13
- export declare const loadConfigData: (raw: unknown, { builtins, ruleset, file, plugins }?: LoadOptions) => LoadedConfigData;
14
+ export declare const loadConfigData: (raw: unknown, { builtins, file, plugins }?: LoadOptions) => LoadedConfigData;
15
+ export interface LoadedConfig extends LoadedConfigData {
16
+ lines: ConfigLines;
17
+ digest: string;
18
+ }
19
+ export declare const loadConfig: (source: ConfigSource, options?: Omit<LoadOptions, 'file' | 'plugins'>) => Promise<LoadedConfig>;
package/dist/app/load.js CHANGED
@@ -1,64 +1,28 @@
1
- import { pluginRules, registry } from '../rules/registry.js';
2
- import { ConfigError } from '../model/configError.js';
3
- import { noOptions } from '../rules/rule.js';
4
- import { parseStructure } from '../model/archetype/language.js';
5
- import { checkReferences, toRules } from '../model/archetype/references.js';
6
- import { checkInputs, markTests } from '../model/archetype/requirements.js';
7
- import { unclassified } from './unclassified.js';
8
- import { withRuleset } from '../model/archetype/rulesets.js';
9
- import { defined, isName, isRecord } from '../model/archetype/shared.js';
10
- const keys = ['root', 'archetypes', 'rulesets', 'plugins', 'tsconfig'];
11
- export const loadConfigData = (raw, { builtins, ruleset, file = 'warp.yaml', plugins = {} } = {}) => {
12
- const errors = [];
13
- const fail = (message) => {
14
- if (!errors.includes(message))
15
- errors.push(message);
16
- };
17
- const failed = () => {
18
- if (errors.length > 0)
19
- throw new ConfigError(errors.join('\n'));
20
- };
21
- if (!isRecord(raw))
22
- throw new ConfigError(`${file}: expected a mapping, root: { modules: ... }`);
23
- const unknownKeys = Object.keys(raw).filter((key) => !keys.includes(key));
24
- for (const key of unknownKeys)
25
- fail(`${key}: unknown key; a config takes ${keys.join(', ')}`);
26
- checkPlugins(raw.plugins, plugins, fail);
27
- const rules = [...registry([...(builtins ?? []), ...pluginRules(plugins)]).values()];
28
- const structure = parseStructure(raw.root, raw.archetypes, fail);
29
- failed();
30
- checkReferences(structure.archetypes, structure.pending, fail);
31
- markTests(structure.archetypes, fail);
32
- checkInputs(structure.archetypes, structure.pending, fail);
33
- const check = {
34
- fail,
35
- ruleIds: builtins && new Set([...rules.map((rule) => rule.id), unclassified]),
1
+ import { compileConfig } from '../domain/language/compile.js';
2
+ import { ConfigError } from '../domain/errors/configError.js';
3
+ import { pluginRules, pluginRulesets, registry } from '../domain/rules/registry.js';
4
+ import { noOptions } from '../domain/rules/rule.js';
5
+ import { placeProblems } from './configLines.js';
6
+ export const loadConfigData = (raw, { builtins, file, plugins = {} } = {}) => {
7
+ const rules = [...registry([...(builtins?.rules ?? []), ...pluginRules(plugins)]).values()];
8
+ const config = compileConfig(raw, {
9
+ ...(file === undefined ? {} : { file }),
10
+ plugins: Object.keys(plugins),
11
+ rulesets: pluginRulesets(builtins ? { ...plugins, warp: builtins } : plugins),
12
+ ruleIds: builtins && new Set(rules.map((rule) => rule.id)),
36
13
  options: Object.fromEntries(rules.map((rule) => [rule.id, rule.parseOptions ?? noOptions])),
37
- };
38
- const data = {
39
- file,
40
- root: structure.root,
41
- archetypes: Object.fromEntries(Object.entries(structure.archetypes).map(([name, archetype]) => [name, archetypeData(archetype)])),
42
- tsconfig: tsconfigs(raw.tsconfig, fail),
43
- };
44
- const config = withRuleset(data, raw.rulesets, ruleset, (value, at) => toRules(check, value, at), fail);
45
- failed();
14
+ moduleOptions: Object.fromEntries(rules.flatMap((rule) => (rule.moduleOptions ? [[rule.id, rule.moduleOptions]] : []))),
15
+ });
46
16
  return { config, rules };
47
17
  };
48
- const archetypeData = (archetype) => defined({ ...archetype, modules: archetype.modules.map(defined) });
49
- const checkPlugins = (declared, loaded, fail) => {
50
- if (declared === undefined)
51
- return;
52
- if (!isRecord(declared) || !Object.values(declared).every(isName)) {
53
- fail('plugins: expected plugin paths by namespace, { wt: ./rules/wt.ts }');
54
- return;
18
+ export const loadConfig = async (source, options = {}) => {
19
+ const { raw, plugins, lines, digest } = await source.read();
20
+ try {
21
+ return { ...loadConfigData(raw, { ...options, plugins, file: source.file }), lines, digest };
22
+ }
23
+ catch (error) {
24
+ if (error instanceof ConfigError)
25
+ throw new ConfigError(placeProblems(lines, source.file, error.message));
26
+ throw error;
55
27
  }
56
- for (const namespace of Object.keys(declared).filter((namespace) => !Object.hasOwn(loaded, namespace)))
57
- fail(`plugins.${namespace}: not loaded; warp loads each plugin from its path before reading the config`);
58
- };
59
- const tsconfigs = (value, fail) => {
60
- const paths = [value ?? 'tsconfig.json'].flat();
61
- if (paths.length === 0 || !paths.every(isName))
62
- fail('tsconfig: expected a path or a list of paths');
63
- return paths;
64
28
  };
@@ -1,6 +1,6 @@
1
- import type { Folders } from '../model/archetype/resolve.ts';
2
- import type { FileFacts, Project } from '../model/facts.ts';
3
- import type { Finding } from '../rules/rule.ts';
1
+ import type { Folders } from '../domain/model/resolve.ts';
2
+ import type { FileFacts, Project } from '../domain/facts/facts.ts';
3
+ import type { Finding } from '../domain/rules/rule.ts';
4
4
  export interface Analyzer {
5
5
  analyze(root: string, tsconfig: readonly string[], facts?: FactsStore): Project;
6
6
  }
@@ -39,3 +39,13 @@ export interface CachedFile {
39
39
  specifiers?: string[];
40
40
  facts?: FileFacts;
41
41
  }
42
+ export interface ConfigSource {
43
+ file: string;
44
+ read(): Promise<ConfigText>;
45
+ }
46
+ export interface ConfigText {
47
+ raw: unknown;
48
+ plugins: Record<string, unknown>;
49
+ lines: ReadonlyMap<string, number>;
50
+ digest: string;
51
+ }
@@ -1,7 +1,7 @@
1
- import type { RefData } from '../model/archetype/data.ts';
2
- import type { Fix } from '../model/archetype/fixes.ts';
3
- import type { Verdicts } from '../model/archetype/reach.ts';
4
- import type { Model } from '../model/archetype/resolve.ts';
1
+ import type { RefData } from '../domain/model/data.ts';
2
+ import type { Fix } from '../domain/fixes/fixes.ts';
3
+ import type { Verdicts } from '../domain/model/reach.ts';
4
+ import type { Model } from '../domain/model/resolve.ts';
5
5
  export declare const refText: (ref: RefData) => string;
6
6
  export declare const sentence: (parts: readonly string[]) => string;
7
7
  export declare const reachSize: (verdicts: Verdicts, file: string) => number;
@@ -1,4 +1,4 @@
1
- import { show } from '../model/archetype/references.js';
1
+ import { show } from '../domain/model/text.js';
2
2
  export const refText = (ref) => show(ref);
3
3
  export const sentence = (parts) => parts.length < 2 ? (parts[0] ?? '') : `${parts.slice(0, -1).join(', ')} and ${parts.at(-1)}`;
4
4
  export const reachSize = (verdicts, file) => new Set((verdicts.reach(file) ?? []).flatMap((grant) => (grant.ref === undefined ? [] : [grant.ref]))).size;
@@ -0,0 +1,17 @@
1
+ import type { FileFacts, ImportEdge } from '../domain/facts/facts.ts';
2
+ import type { Model, ModuleView } from '../domain/model/resolve.ts';
3
+ export declare const within: (module: ModuleView | undefined, holder: ModuleView) => boolean;
4
+ export declare const crossed: (from: ModuleView, to: ModuleView) => ModuleView | undefined;
5
+ export interface Use {
6
+ file: string;
7
+ edge: ImportEdge;
8
+ name: string;
9
+ }
10
+ export declare const valueUses: (model: Model, files: readonly FileFacts[]) => Use[];
11
+ export interface LimitOptions {
12
+ max: number;
13
+ ignore: string[];
14
+ }
15
+ export declare const limitOptions: (defaults: LimitOptions) => (raw: Record<string, unknown>, at: string) => Partial<LimitOptions>;
16
+ export declare const ignored: (model: Model, ids: readonly string[]) => ModuleView[];
17
+ export declare const listed: (items: readonly string[]) => string;
@@ -0,0 +1,35 @@
1
+ import { ConfigError } from '../domain/errors/configError.js';
2
+ import { wholeNumberOptions } from '../domain/rules/rule.js';
3
+ const chain = (module) => (module.parent ? [...chain(module.parent), module] : [module]);
4
+ export const within = (module, holder) => {
5
+ for (let at = module; at; at = at.parent)
6
+ if (at === holder)
7
+ return true;
8
+ return false;
9
+ };
10
+ export const crossed = (from, to) => {
11
+ const [source, target] = [chain(from), chain(to)];
12
+ const shared = target.findIndex((module, at) => module !== source[at]);
13
+ if (shared !== -1)
14
+ return target[shared];
15
+ return source.length > target.length ? to : undefined;
16
+ };
17
+ export const valueUses = (model, files) => {
18
+ const production = new Set(files.map((file) => file.path).filter((path) => !model.isTest(path)));
19
+ return files.flatMap(({ path, imports }) => production.has(path)
20
+ ? imports
21
+ .filter((edge) => !edge.typeOnly && !edge.reExport && production.has(edge.to))
22
+ .flatMap((edge) => edge.names.filter((name) => name !== '*').map((name) => ({ file: path, edge, name })))
23
+ : []);
24
+ };
25
+ export const limitOptions = (defaults) => (raw, at) => {
26
+ const { ignore, ...rest } = raw;
27
+ const max = wholeNumberOptions({ max: defaults.max })(rest, at);
28
+ if (ignore === undefined)
29
+ return max;
30
+ if (!Array.isArray(ignore) || ignore.some((entry) => typeof entry !== 'string'))
31
+ throw new ConfigError(`${at}.ignore: expected a list of module names`);
32
+ return { ...max, ignore: ignore };
33
+ };
34
+ export const ignored = (model, ids) => model.modules.filter((module) => ids.includes(module.id));
35
+ export const listed = (items) => items.length === 1 ? items[0] : `${items.slice(0, -1).join(', ')} and ${items.at(-1)}`;
@@ -0,0 +1,6 @@
1
+ import { type FileRule } from '../../domain/rules/rule.ts';
2
+ declare const defaults: {
3
+ maxLines: number;
4
+ };
5
+ export declare const classCohesion: FileRule<typeof defaults>;
6
+ export {};
@@ -0,0 +1,34 @@
1
+ import { components } from '../components.js';
2
+ import { wholeNumberOptions } from '../../domain/rules/rule.js';
3
+ import { group, withOrdinals } from '../shared.js';
4
+ const defaults = { maxLines: 100 };
5
+ export const classCohesion = {
6
+ id: 'class-cohesion',
7
+ defaults,
8
+ parseOptions: wholeNumberOptions(defaults),
9
+ check: ({ file, options }) => withOrdinals(file.classes).flatMap(([cls, name]) => {
10
+ const methods = statefulMethods(cls);
11
+ const judged = methods.flat().reduce((sum, member) => sum + member.endLine - member.line + 1, 0);
12
+ if (judged <= options.maxLines || methods.length <= 1)
13
+ return [];
14
+ const groups = methods.map(group);
15
+ return [
16
+ {
17
+ rule: 'class-cohesion',
18
+ key: `class-cohesion:${file.path}:${name}`,
19
+ file: file.path,
20
+ line: cls.line,
21
+ value: groups.length,
22
+ limit: 1,
23
+ message: `Class ${cls.name} in ${file.path}:${cls.line} is ${cls.lines} lines, ${judged} of them in methods that use its state ` +
24
+ `(tripwire ${options.maxLines}), and its methods fall into ${groups.length} groups that share no fields or calls: ` +
25
+ `${groups.map(describe).join('; ')}. Each group can become its own class.`,
26
+ details: { lines: cls.lines, judgedLines: judged, tripwire: options.maxLines, groups },
27
+ },
28
+ ];
29
+ }),
30
+ };
31
+ const statefulMethods = (cls) => components(cls.members, ['function'])
32
+ .filter((members) => members.some((member) => member.kind === 'state'))
33
+ .map((members) => members.filter((member) => member.kind === 'function'));
34
+ const describe = (g) => `[${g.names.join(', ')}] (lines ${g.lines[0]}-${g.lines[1]})`;
@@ -1,4 +1,4 @@
1
- import { type FileRule } from '../../rules/rule.ts';
1
+ import { type FileRule } from '../../domain/rules/rule.ts';
2
2
  declare const defaults: {
3
3
  max: number;
4
4
  };
@@ -1,4 +1,4 @@
1
- import { wholeNumberOptions } from '../../rules/rule.js';
1
+ import { wholeNumberOptions } from '../../domain/rules/rule.js';
2
2
  import { withOrdinals } from '../shared.js';
3
3
  const defaults = { max: 15 };
4
4
  export const cognitiveComplexity = {
@@ -1,2 +1,2 @@
1
- import type { Member, MemberGraph, MemberKind } from '../model/facts.ts';
1
+ import type { Member, MemberGraph, MemberKind } from '../domain/facts/facts.ts';
2
2
  export declare const components: (graph: MemberGraph, counted: readonly MemberKind[]) => Member[][];
@@ -0,0 +1,2 @@
1
+ import { type ProjectRule } from '../../domain/rules/rule.ts';
2
+ export declare const dependencyDirection: ProjectRule;
@@ -1,9 +1,9 @@
1
- import { exposesText } from '../../model/archetype/references.js';
1
+ import { exposesText } from '../../domain/model/text.js';
2
2
  import { fixSteps, reachSize, sentence } from '../advice.js';
3
3
  import { place } from '../place.js';
4
4
  import { title } from '../shared.js';
5
- import { fixesFor } from '../../model/archetype/fixes.js';
6
- import { noOptions } from '../../rules/rule.js';
5
+ import { fixesFor } from '../../domain/fixes/fixes.js';
6
+ import { noOptions } from '../../domain/rules/rule.js';
7
7
  const id = 'dependency-direction';
8
8
  export const dependencyDirection = {
9
9
  id,
@@ -0,0 +1,6 @@
1
+ import { type ProjectRule } from '../../domain/rules/rule.ts';
2
+ declare const defaults: {
3
+ max: number;
4
+ };
5
+ export declare const exportConsumers: ProjectRule<typeof defaults>;
6
+ export {};
@@ -0,0 +1,65 @@
1
+ import { wholeNumberOptions } from '../../domain/rules/rule.js';
2
+ import { crossed, listed } from '../boundaries.js';
3
+ const id = 'export-consumers';
4
+ const defaults = { max: 3 };
5
+ export const exportConsumers = {
6
+ id,
7
+ scope: 'project',
8
+ defaults,
9
+ parseOptions: wholeNumberOptions(defaults),
10
+ check: ({ model, project, atModule }) => {
11
+ const exports = new Map();
12
+ for (const { file, line, target } of uses(model, project.files)) {
13
+ const consumer = crossed(model.moduleOf(target.file), model.moduleOf(file));
14
+ if (!consumer)
15
+ continue;
16
+ const name = target.member === undefined ? target.name : `${target.name}#${target.member}`;
17
+ const key = `${target.file}#${name}`;
18
+ const found = exports.get(key) ?? { file: target.file, declared: target.name, name, consumers: new Map() };
19
+ exports.set(key, found);
20
+ found.consumers.set(consumer, [...(found.consumers.get(consumer) ?? []), { file, line }]);
21
+ }
22
+ const lines = new Map(project.files.map((file) => [file.path, file.members.members]));
23
+ return [...exports.values()].flatMap((found) => {
24
+ const module = model.moduleOf(found.file);
25
+ const { on, options } = atModule(module);
26
+ if (!on || found.consumers.size <= options.max)
27
+ return [];
28
+ const line = lines.get(found.file).find((member) => member.name === found.declared)?.line ?? 1;
29
+ return [finding(found, module, line, options.max)];
30
+ });
31
+ },
32
+ };
33
+ const uses = (model, files) => {
34
+ const production = new Set(files.map((file) => file.path).filter((path) => !model.isTest(path)));
35
+ return files
36
+ .filter(({ path }) => production.has(path))
37
+ .flatMap(({ path, chains }) => chains.flatMap(({ line, root, hops }) => {
38
+ const target = hops[0] ?? (root?.name === '*' ? undefined : root);
39
+ return target && production.has(target.file) ? [{ file: path, line, target }] : [];
40
+ }));
41
+ };
42
+ const finding = ({ file, name, consumers }, module, line, max) => {
43
+ const boundaries = [...consumers.keys()].map((consumer) => consumer.id).sort();
44
+ return {
45
+ rule: id,
46
+ key: `${id}:${file}#${name}`,
47
+ file,
48
+ line,
49
+ value: consumers.size,
50
+ limit: max,
51
+ message: `${name}, exported by ${module.id}, is used across ${consumers.size} boundaries: ${listed(boundaries)}. ` +
52
+ `Its limit here is ${max}. Give its consumers one way in, or move it to a module meant to be shared and ` +
53
+ `turn ${id} off there.`,
54
+ sites: [...consumers.values()].map(([first]) => first).toSorted((a, b) => a.file.localeCompare(b.file)),
55
+ parts: boundaries,
56
+ details: {
57
+ module: module.id,
58
+ name,
59
+ consumers: [...consumers].map(([consumer, sites]) => ({
60
+ module: consumer.id,
61
+ files: [...new Set(sites.map((site) => site.file))],
62
+ })),
63
+ },
64
+ };
65
+ };
@@ -0,0 +1,3 @@
1
+ import type { ProjectRule } from '../../domain/rules/rule.ts';
2
+ import { type LimitOptions } from '../boundaries.ts';
3
+ export declare const exportSpread: ProjectRule<LimitOptions>;