@yearofthedan/warp 0.4.1 → 0.5.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 (151) hide show
  1. package/README.md +5 -7
  2. package/dist/{analysis → adapters/analysis}/complexity.d.ts +1 -1
  3. package/dist/adapters/analysis/folders.d.ts +2 -0
  4. package/dist/{app/facts.d.ts → adapters/analysis/freshFacts.d.ts} +1 -2
  5. package/dist/{app/facts.js → adapters/analysis/freshFacts.js} +0 -10
  6. package/dist/{analysis → adapters/analysis}/imports.d.ts +2 -1
  7. package/dist/{analysis → adapters/analysis}/imports.js +52 -18
  8. package/dist/{analysis → adapters/analysis}/members.d.ts +1 -1
  9. package/dist/adapters/analysis/specifiers.d.ts +10 -0
  10. package/dist/adapters/analysis/specifiers.js +39 -0
  11. package/dist/adapters/analysis/typescript.d.ts +2 -0
  12. package/dist/{analysis → adapters/analysis}/typescript.js +10 -4
  13. package/dist/{config → adapters/config}/loadConfig.d.ts +2 -7
  14. package/dist/{config → adapters/config}/loadConfig.js +5 -4
  15. package/dist/{git → adapters/git}/cache.d.ts +1 -1
  16. package/dist/adapters/git/factsStore.d.ts +2 -0
  17. package/dist/adapters/git/factsStore.js +10 -0
  18. package/dist/{git → adapters/git}/repository.d.ts +1 -1
  19. package/dist/{config → adapters/git}/toolDigest.js +1 -1
  20. package/dist/{reporters → adapters/reporters}/model.d.ts +2 -2
  21. package/dist/{reporters → adapters/reporters}/reporter.d.ts +2 -2
  22. package/dist/{reporters → adapters/reporters}/text.d.ts +2 -2
  23. package/dist/app/check.d.ts +5 -6
  24. package/dist/app/check.js +3 -3
  25. package/dist/app/checkModel.d.ts +5 -5
  26. package/dist/app/checkModel.js +4 -5
  27. package/dist/app/configLines.d.ts +1 -1
  28. package/dist/app/diff.d.ts +1 -1
  29. package/dist/app/diff.js +1 -1
  30. package/dist/app/failures.d.ts +6 -0
  31. package/dist/app/failures.js +15 -0
  32. package/dist/app/load.d.ts +11 -5
  33. package/dist/app/load.js +22 -59
  34. package/dist/app/ports.d.ts +13 -3
  35. package/dist/builtins/advice.d.ts +4 -4
  36. package/dist/builtins/advice.js +1 -1
  37. package/dist/builtins/cognitive-complexity/{cognitiveComplexity.d.ts → rule.d.ts} +1 -1
  38. package/dist/builtins/cognitive-complexity/{cognitiveComplexity.js → rule.js} +1 -1
  39. package/dist/builtins/components.d.ts +1 -1
  40. package/dist/builtins/dependency-direction/rule.d.ts +2 -0
  41. package/dist/builtins/dependency-direction/{dependencyDirection.js → rule.js} +3 -3
  42. package/dist/builtins/file-cohesion/{fileCohesion.d.ts → rule.d.ts} +1 -1
  43. package/dist/builtins/file-cohesion/{fileCohesion.js → rule.js} +1 -1
  44. package/dist/builtins/function-cohesion/{functionCohesion.d.ts → rule.d.ts} +1 -1
  45. package/dist/builtins/function-cohesion/{functionCohesion.js → rule.js} +1 -1
  46. package/dist/builtins/index.d.ts +2 -2
  47. package/dist/builtins/index.js +18 -13
  48. package/dist/builtins/loops.d.ts +1 -1
  49. package/dist/builtins/loops.js +1 -1
  50. package/dist/builtins/module-cycle/{moduleCycle.d.ts → rule.d.ts} +1 -1
  51. package/dist/builtins/module-externals/rule.d.ts +2 -0
  52. package/dist/builtins/module-externals/{moduleExternals.js → rule.js} +3 -3
  53. package/dist/builtins/no-cross-module-reexport/rule.d.ts +2 -0
  54. package/dist/builtins/no-cross-module-reexport/{noCrossModuleReexport.js → rule.js} +1 -1
  55. package/dist/builtins/no-cycle/{noCycle.d.ts → rule.d.ts} +1 -1
  56. package/dist/builtins/no-double-export/rule.d.ts +2 -0
  57. package/dist/builtins/no-double-export/{noDoubleExport.js → rule.js} +1 -1
  58. package/dist/builtins/packages.d.ts +1 -1
  59. package/dist/builtins/place.d.ts +1 -1
  60. package/dist/builtins/shared.d.ts +1 -1
  61. package/dist/builtins/unclassified/rule.d.ts +2 -0
  62. package/dist/builtins/unclassified/rule.js +15 -0
  63. package/dist/builtins/unresolved-import/rule.d.ts +2 -0
  64. package/dist/builtins/unresolved-import/{unresolvedImport.js → rule.js} +1 -1
  65. package/dist/builtins/unused-declaration/declarations.d.ts +3 -3
  66. package/dist/builtins/unused-declaration/empty.d.ts +1 -1
  67. package/dist/builtins/unused-declaration/empty.js +1 -1
  68. package/dist/builtins/unused-declaration/grants.d.ts +1 -1
  69. package/dist/builtins/unused-declaration/grants.js +42 -10
  70. package/dist/builtins/unused-declaration/rule.d.ts +2 -0
  71. package/dist/builtins/unused-declaration/{unusedDeclaration.js → rule.js} +1 -1
  72. package/dist/builtins/unused-declaration/surfaces.d.ts +1 -1
  73. package/dist/builtins/unused-declaration/surfaces.js +1 -1
  74. package/dist/cli.js +34 -54
  75. package/dist/{model/archetype → domain/fixes}/fixes.d.ts +2 -2
  76. package/dist/{model/archetype → domain/fixes}/fixes.js +2 -6
  77. package/dist/domain/language/compile.d.ts +11 -0
  78. package/dist/domain/language/compile.js +58 -0
  79. package/dist/{model/archetype → domain/language}/references.d.ts +3 -6
  80. package/dist/{model/archetype → domain/language}/references.js +16 -21
  81. package/dist/{model/archetype → domain/language}/requirements.d.ts +3 -3
  82. package/dist/{model/archetype → domain/language}/requirements.js +2 -1
  83. package/dist/domain/language/rulesets.d.ts +10 -0
  84. package/dist/domain/language/rulesets.js +60 -0
  85. package/dist/{model/archetype/language.d.ts → domain/language/structure.d.ts} +4 -3
  86. package/dist/{model/archetype/language.js → domain/language/structure.js} +32 -12
  87. package/dist/{model/archetype → domain/model}/data.d.ts +9 -6
  88. package/dist/{model/archetype → domain/model}/reach.d.ts +1 -0
  89. package/dist/{model/archetype → domain/model}/reach.js +3 -7
  90. package/dist/{model/archetype → domain/model}/resolve.d.ts +2 -0
  91. package/dist/{model/archetype → domain/model}/resolve.js +6 -1
  92. package/dist/{model/archetype → domain/model}/settings.d.ts +0 -1
  93. package/dist/domain/model/settings.js +82 -0
  94. package/dist/domain/model/text.d.ts +4 -0
  95. package/dist/domain/model/text.js +13 -0
  96. package/dist/{rules → domain/rules}/registry.d.ts +1 -0
  97. package/dist/{rules → domain/rules}/registry.js +10 -0
  98. package/dist/{rules → domain/rules}/rule.d.ts +10 -4
  99. package/dist/index.d.ts +6 -6
  100. package/dist/index.js +2 -2
  101. package/package.json +1 -1
  102. package/dist/analysis/folders.d.ts +0 -2
  103. package/dist/analysis/specifiers.d.ts +0 -2
  104. package/dist/analysis/specifiers.js +0 -6
  105. package/dist/analysis/typescript.d.ts +0 -2
  106. package/dist/app/unclassified.d.ts +0 -5
  107. package/dist/app/unclassified.js +0 -13
  108. package/dist/builtins/dependency-direction/dependencyDirection.d.ts +0 -2
  109. package/dist/builtins/module-externals/moduleExternals.d.ts +0 -2
  110. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +0 -2
  111. package/dist/builtins/no-double-export/noDoubleExport.d.ts +0 -2
  112. package/dist/builtins/unresolved-import/unresolvedImport.d.ts +0 -2
  113. package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +0 -2
  114. package/dist/model/archetype/rulesets.d.ts +0 -5
  115. package/dist/model/archetype/rulesets.js +0 -139
  116. package/dist/model/archetype/settings.js +0 -54
  117. /package/dist/{analysis → adapters/analysis}/complexity.js +0 -0
  118. /package/dist/{analysis → adapters/analysis}/folders.js +0 -0
  119. /package/dist/{analysis → adapters/analysis}/members.js +0 -0
  120. /package/dist/{analysis → adapters/analysis}/source.d.ts +0 -0
  121. /package/dist/{analysis → adapters/analysis}/source.js +0 -0
  122. /package/dist/{git → adapters/git}/cache.js +0 -0
  123. /package/dist/{git → adapters/git}/repository.js +0 -0
  124. /package/dist/{config → adapters/git}/toolDigest.d.ts +0 -0
  125. /package/dist/{reporters → adapters/reporters}/agent.d.ts +0 -0
  126. /package/dist/{reporters → adapters/reporters}/agent.js +0 -0
  127. /package/dist/{reporters → adapters/reporters}/claudeHook.d.ts +0 -0
  128. /package/dist/{reporters → adapters/reporters}/claudeHook.js +0 -0
  129. /package/dist/{reporters → adapters/reporters}/index.d.ts +0 -0
  130. /package/dist/{reporters → adapters/reporters}/index.js +0 -0
  131. /package/dist/{reporters → adapters/reporters}/json.d.ts +0 -0
  132. /package/dist/{reporters → adapters/reporters}/json.js +0 -0
  133. /package/dist/{reporters → adapters/reporters}/model.js +0 -0
  134. /package/dist/{reporters → adapters/reporters}/omp.d.ts +0 -0
  135. /package/dist/{reporters → adapters/reporters}/omp.js +0 -0
  136. /package/dist/{reporters → adapters/reporters}/reporter.js +0 -0
  137. /package/dist/{reporters → adapters/reporters}/sarif.d.ts +0 -0
  138. /package/dist/{reporters → adapters/reporters}/sarif.js +0 -0
  139. /package/dist/{reporters → adapters/reporters}/text.js +0 -0
  140. /package/dist/builtins/module-cycle/{moduleCycle.js → rule.js} +0 -0
  141. /package/dist/builtins/no-cycle/{noCycle.js → rule.js} +0 -0
  142. /package/dist/{model → domain/facts}/facts.d.ts +0 -0
  143. /package/dist/{model → domain/facts}/facts.js +0 -0
  144. /package/dist/{model → domain/model}/configError.d.ts +0 -0
  145. /package/dist/{model → domain/model}/configError.js +0 -0
  146. /package/dist/{model/archetype → domain/model}/data.js +0 -0
  147. /package/dist/{model/archetype → domain/model}/shared.d.ts +0 -0
  148. /package/dist/{model/archetype → domain/model}/shared.js +0 -0
  149. /package/dist/{rules → domain/rules}/diff.d.ts +0 -0
  150. /package/dist/{rules → domain/rules}/diff.js +0 -0
  151. /package/dist/{rules → domain/rules}/rule.js +0 -0
package/dist/cli.js CHANGED
@@ -1,29 +1,30 @@
1
1
  #!/usr/bin/env node
2
2
  import { existsSync, readFileSync, realpathSync } from 'node:fs';
3
- import { basename, dirname, join, resolve } from 'node:path';
3
+ import { dirname, join, resolve } from 'node:path';
4
4
  import { parseArgs } from 'node:util';
5
- import { diskFolders } from './analysis/folders.js';
6
- import { typescriptAnalyzer } from './analysis/typescript.js';
5
+ import { diskFolders } from './adapters/analysis/folders.js';
6
+ import { typescriptAnalyzer } from './adapters/analysis/typescript.js';
7
7
  import { modelProject, projectCheck } from './app/check.js';
8
8
  import { placeProblems } from './app/configLines.js';
9
- import { factsStore } from './app/facts.js';
9
+ import { factsStore } from './adapters/git/factsStore.js';
10
+ import { failures } from './app/failures.js';
10
11
  import { diffProject, parseRange } from './app/diff.js';
11
- import { builtinRegistry, unclassifiedDocs } from './builtins/index.js';
12
- import { findConfig, loadConfig } from './config/loadConfig.js';
13
- import { toolDigest } from './config/toolDigest.js';
14
- import { gitDirCache } from './git/cache.js';
15
- import { gitRepository, projectPathOf } from './git/repository.js';
16
- import { anyRuleOn, rulesOf } from './model/archetype/settings.js';
17
- import { recommended } from './model/archetype/rulesets.js';
18
- import { loadConfigData } from './app/load.js';
19
- import { ConfigError } from './model/configError.js';
20
- import { reporters } from './reporters/index.js';
21
- import { modelJson, modelText } from './reporters/model.js';
22
- import { actionable } from './reporters/reporter.js';
12
+ import { warp } from './builtins/index.js';
13
+ import { findConfig, yamlConfig } from './adapters/config/loadConfig.js';
14
+ import { toolDigest } from './adapters/git/toolDigest.js';
15
+ import { gitDirCache } from './adapters/git/cache.js';
16
+ import { gitRepository, projectPathOf } from './adapters/git/repository.js';
17
+ import { anyRuleOn, rulesOf } from './domain/model/settings.js';
18
+ import { recommended } from './domain/language/compile.js';
19
+ import { loadConfig } from './app/load.js';
20
+ import { ConfigError } from './domain/model/configError.js';
21
+ import { reporters } from './adapters/reporters/index.js';
22
+ import { modelJson, modelText } from './adapters/reporters/model.js';
23
+ import { actionable } from './adapters/reporters/reporter.js';
23
24
  const usage = `usage:
24
- warp check [--config <file>] [--ruleset <name>] [--reporter <name>] [--fail]
25
- warp diff <base>[..<head> | ...<head>] [--since-last-run] [--config <file>] [--ruleset <name>] [--reporter <name>] [--fail]
26
- warp model [--config <file>] [--ruleset <name>] [--rules] [--json]
25
+ warp check [--config <file>] [--reporter <name>] [--fail]
26
+ warp diff <base>[..<head> | ...<head>] [--since-last-run] [--config <file>] [--reporter <name>] [--fail]
27
+ warp model [--config <file>] [--rules] [--json]
27
28
 
28
29
  check reports every finding in the working tree.
29
30
  diff reports findings that are new or worse at <head> than at <base>. Without <head>, compares the working tree.
@@ -32,16 +33,17 @@ diff reports findings that are new or worse at <head> than at <base>. Without
32
33
  model prints the modules the config finds, and which module each file is in.
33
34
 
34
35
  --config path to the config file (default: warp.yaml)
35
- --ruleset one of the config's rulesets (default: the one named default, if any)
36
36
  --reporter ${[...reporters.keys()].join(', ')} (default: text)
37
37
  --since-last-run with diff, compare with the last diff run that had this flag
38
38
  --fail exit 1 when there are findings (check) or new or worse findings (diff)
39
39
  --rules with model, list each module's rule settings that differ from the defaults
40
40
  --json print the model as JSON, with every module's resolved rule settings`;
41
+ let toolHash;
42
+ const tool = () => (toolHash ??= toolDigest());
41
43
  const adaptersAt = (root) => {
42
44
  const adapters = { analyzer: typescriptAnalyzer, foldersAt: diskFolders };
43
45
  try {
44
- return { ...adapters, facts: factsStore(gitDirCache(root), toolDigest(), gitRepository(root).projectPath()) };
46
+ return { ...adapters, facts: factsStore(gitDirCache(root), tool(), gitRepository(root).projectPath()) };
45
47
  }
46
48
  catch {
47
49
  return adapters;
@@ -53,7 +55,6 @@ const main = async (argv) => {
53
55
  allowPositionals: true,
54
56
  options: {
55
57
  config: { type: 'string' },
56
- ruleset: { type: 'string' },
57
58
  rules: { type: 'boolean', default: false },
58
59
  reporter: { type: 'string', default: 'text' },
59
60
  fail: { type: 'boolean', default: false },
@@ -83,22 +84,13 @@ const help = (code) => {
83
84
  const resolveConfig = async (options) => {
84
85
  const configFile = realpathSync(options.config ? resolve(options.config) : findConfig(process.cwd()));
85
86
  const root = dirname(configFile);
86
- const { raw, plugins, lines, digest } = await loadConfig(configFile);
87
- const builtins = [...builtinRegistry.values()];
88
- const file = basename(configFile);
89
- try {
90
- const { config, rules } = loadConfigData(raw, { builtins, plugins, ruleset: options.ruleset, file });
91
- for (const tsconfig of config.tsconfig.filter((path) => !existsSync(join(root, path))))
92
- throw new ConfigError(`tsconfig: no ${tsconfig} beside ${file}; add it, or list the project's tsconfig files under tsconfig`);
93
- return { root, config, rules, lines, digest };
94
- }
95
- catch (error) {
96
- if (error instanceof ConfigError)
97
- throw new ConfigError(placeProblems(lines, file, error.message));
98
- throw error;
99
- }
87
+ const source = yamlConfig(configFile);
88
+ const loaded = await loadConfig(source, { builtins: warp });
89
+ for (const tsconfig of loaded.config.tsconfig.filter((path) => !existsSync(join(root, path))))
90
+ throw new ConfigError(placeProblems(loaded.lines, source.file, `tsconfig: no ${tsconfig} beside ${source.file}; add it, or list the project's tsconfig files under tsconfig`));
91
+ return { root, ...loaded };
100
92
  };
101
- const checkOf = ({ root, config, rules, lines }) => projectCheck(adaptersAt(root), config, { rules, unclassifiedDocs, lines });
93
+ const checkOf = ({ root, config, rules, lines }) => projectCheck(adaptersAt(root), config, { rules, lines });
102
94
  const run = async (options, produce) => {
103
95
  const reporter = reporters.get(options.reporter);
104
96
  if (!reporter)
@@ -112,13 +104,13 @@ const run = async (options, produce) => {
112
104
  if (output)
113
105
  console.log(output);
114
106
  if (reporter.renderFailure)
115
- forgetFailure();
107
+ lastFailure().forget();
116
108
  return options.fail && actionable(report).length > 0 ? 1 : 0;
117
109
  }
118
110
  catch (error) {
119
111
  const code = failed(error, reporter.failureExitCode);
120
112
  const problem = describe(error);
121
- if (!reporter.renderFailure || !isNewFailure(problem))
113
+ if (!reporter.renderFailure || !lastFailure().isNew(problem))
122
114
  return code;
123
115
  console.log(reporter.renderFailure(problem));
124
116
  return 0;
@@ -134,8 +126,7 @@ const placed = ({ lines, config }, run) => {
134
126
  throw error;
135
127
  }
136
128
  };
137
- const nothingOn = (file) => `No rules are on, so nothing was checked. Turn some on in ${file}: rulesets: { default: { extends: [${recommended}] } }`;
138
- const failureKey = () => `${toolDigest()}-failure`;
129
+ const nothingOn = (file) => `No rules are on, so nothing was checked. Turn some on in ${file}: root: { ruleset: [${recommended}] }`;
139
130
  const failureCache = () => {
140
131
  try {
141
132
  return gitDirCache(process.cwd());
@@ -144,18 +135,7 @@ const failureCache = () => {
144
135
  return undefined;
145
136
  }
146
137
  };
147
- const isNewFailure = (problem) => {
148
- const cache = failureCache();
149
- if (cache?.get(failureKey()) === problem)
150
- return false;
151
- cache?.set(failureKey(), problem);
152
- return true;
153
- };
154
- const forgetFailure = () => {
155
- const cache = failureCache();
156
- if (cache?.get(failureKey()) !== undefined)
157
- cache.prune((key) => key !== failureKey());
158
- };
138
+ const lastFailure = () => failures(failureCache(), tool());
159
139
  const printModel = async (options) => {
160
140
  try {
161
141
  const resolved = await resolveConfig(options);
@@ -169,7 +149,7 @@ const printModel = async (options) => {
169
149
  return failed(error);
170
150
  }
171
151
  };
172
- const delta = (range, root, project, configDigest, sinceLastRun) => diffProject({ range: parseRange(range), root, project, configDigest, toolDigest: toolDigest(), sinceLastRun }, { repository: gitRepository(root), cache: gitDirCache(root) });
152
+ const delta = (range, root, project, configDigest, sinceLastRun) => diffProject({ range: parseRange(range), root, project, configDigest, toolDigest: tool(), sinceLastRun }, { repository: gitRepository(root), cache: gitDirCache(root) });
173
153
  const version = () => JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
174
154
  const describe = (error) => error instanceof ConfigError
175
155
  ? `warp config: ${error.message}`
@@ -1,5 +1,5 @@
1
- import type { ConfigData, RefData } from './data.ts';
2
- import type { Model, ModuleView } from './resolve.ts';
1
+ import type { ConfigData, RefData } from '../model/data.ts';
2
+ import { type Model, type ModuleView } from '../model/resolve.ts';
3
3
  export interface Site {
4
4
  label: string;
5
5
  holder: ModuleView;
@@ -1,8 +1,5 @@
1
- import { instanceOf, verdicts } from './reach.js';
2
- const ancestors = function* (module) {
3
- for (let at = module.parent; at; at = at.parent)
4
- yield at;
5
- };
1
+ import { instanceOf, verdicts } from '../model/reach.js';
2
+ import { ancestors, subtree } from '../model/resolve.js';
6
3
  const lineage = (module) => [module, ...ancestors(module)];
7
4
  export const namedArchetype = (config, name) => config.archetypes[name].inline ? namedArchetype(config, name.slice(0, name.lastIndexOf('.'))) : name;
8
5
  const withArchetype = (config, name, change) => ({
@@ -208,7 +205,6 @@ const exposeFilesModule = (search, config, hiddenBy) => {
208
205
  })),
209
206
  };
210
207
  };
211
- const subtree = (module) => [module, ...module.children.flatMap(subtree)];
212
208
  const uses = (config, model, from, under) => {
213
209
  const judge = verdicts(config, model);
214
210
  const reached = subtree(from).flatMap((module) => module.files.slice(0, 1).flatMap((file) => (judge.reach(file) ?? []).flatMap((grant) => [...grant.files])));
@@ -0,0 +1,11 @@
1
+ import type { ConfigData } from '../model/data.ts';
2
+ import { type Check } from './references.ts';
3
+ export { recommended } from './rulesets.ts';
4
+ export interface CompileOptions {
5
+ file?: string;
6
+ plugins?: readonly string[];
7
+ rulesets?: Readonly<Record<string, unknown>>;
8
+ ruleIds?: ReadonlySet<string> | undefined;
9
+ options?: Check['options'];
10
+ }
11
+ export declare const compileConfig: (raw: unknown, { file, plugins, rulesets, ruleIds, options }?: CompileOptions) => ConfigData;
@@ -0,0 +1,58 @@
1
+ import { ConfigError } from '../model/configError.js';
2
+ import { defined, isName, isRecord } from '../model/shared.js';
3
+ import { checkReferences, toRules } from './references.js';
4
+ import { checkInputs, markTests } from './requirements.js';
5
+ import { rulesets as namedRulesets } from './rulesets.js';
6
+ import { parseStructure } from './structure.js';
7
+ export { recommended } from './rulesets.js';
8
+ const keys = ['root', 'archetypes', 'rulesets', 'plugins', 'tsconfig'];
9
+ export const compileConfig = (raw, { file = 'warp.yaml', plugins = [], rulesets = {}, ruleIds, options = {} } = {}) => {
10
+ const errors = [];
11
+ const fail = (message) => {
12
+ if (!errors.includes(message))
13
+ errors.push(message);
14
+ };
15
+ const failed = () => {
16
+ if (errors.length > 0)
17
+ throw new ConfigError(errors.join('\n'));
18
+ };
19
+ if (!isRecord(raw))
20
+ throw new ConfigError(`${file}: expected a mapping, root: { modules: ... }`);
21
+ const unknownKeys = Object.keys(raw).filter((key) => !keys.includes(key));
22
+ for (const key of unknownKeys)
23
+ fail(`${key}: unknown key; a config takes ${keys.join(', ')}`);
24
+ checkPlugins(raw.plugins, plugins, fail);
25
+ const check = { fail, ruleIds, options };
26
+ const named = namedRulesets(raw.rulesets, rulesets, (value, at) => toRules(check, value, at), fail);
27
+ const structure = parseStructure(raw.root, raw.archetypes, fail, named.parse);
28
+ named.checkUsed();
29
+ failed();
30
+ checkReferences(structure.archetypes, structure.pending, fail);
31
+ markTests(structure.archetypes, fail);
32
+ checkInputs(structure.archetypes, structure.pending, fail);
33
+ const config = {
34
+ file,
35
+ root: structure.root,
36
+ archetypes: Object.fromEntries(Object.entries(structure.archetypes).map(([name, archetype]) => [name, archetypeData(archetype)])),
37
+ tsconfig: tsconfigs(raw.tsconfig, fail),
38
+ };
39
+ failed();
40
+ return config;
41
+ };
42
+ const archetypeData = (archetype) => defined({ ...archetype, modules: archetype.modules.map(defined) });
43
+ const checkPlugins = (declared, loaded, fail) => {
44
+ if (declared === undefined)
45
+ return;
46
+ if (!isRecord(declared) || !Object.values(declared).every(isName)) {
47
+ fail('plugins: expected plugin paths by namespace, { wt: ./rules/wt.ts }');
48
+ return;
49
+ }
50
+ for (const namespace of Object.keys(declared).filter((namespace) => !loaded.includes(namespace)))
51
+ fail(`plugins.${namespace}: not loaded; warp loads each plugin from its path before reading the config`);
52
+ };
53
+ const tsconfigs = (value, fail) => {
54
+ const paths = [value ?? 'tsconfig.json'].flat();
55
+ if (paths.length === 0 || !paths.every(isName))
56
+ fail('tsconfig: expected a path or a list of paths');
57
+ return paths;
58
+ };
@@ -1,19 +1,16 @@
1
- import { type ArchetypeData, type ExposesData, type RefData, type RuleSettingsData } from './data.ts';
2
- import type { PendingRef } from './language.ts';
3
- import { type Fail } from './shared.ts';
1
+ import { type ArchetypeData, type ExposesData, type RuleSettingsData } from '../model/data.ts';
2
+ import type { PendingRef } from './structure.ts';
3
+ import { type Fail } from '../model/shared.ts';
4
4
  export interface Check {
5
5
  fail: Fail;
6
6
  ruleIds?: ReadonlySet<string> | undefined;
7
7
  options: Readonly<Record<string, (raw: Record<string, unknown>, at: string) => unknown>>;
8
8
  }
9
- export declare const packageText: (pattern: string) => string;
10
- export declare const show: (ref: RefData) => string;
11
9
  export declare const checkReferences: (archetypes: Record<string, ArchetypeData>, pending: readonly PendingRef[], fail: Fail) => void;
12
10
  interface Surfaces {
13
11
  exposes: ExposesData | undefined;
14
12
  testExposes: readonly string[];
15
13
  }
16
14
  export declare const exposedProblem: (subject: string, { exposes, testExposes }: Surfaces, surface: string | undefined, tests: boolean) => string | undefined;
17
- export declare const exposesText: (exposes: ExposesData | undefined) => string;
18
15
  export declare const toRules: (check: Check, value: unknown, where: string) => RuleSettingsData | undefined;
19
16
  export {};
@@ -1,10 +1,7 @@
1
- import { ConfigError } from '../configError.js';
2
- import { isTestData } from './data.js';
3
- import { isRecord, list } from './shared.js';
4
- export const packageText = (pattern) => pattern.startsWith('node:') || pattern.startsWith('virtual:') ? pattern : `npm:${pattern}`;
5
- export const show = (ref) => ref.kind === 'package'
6
- ? packageText(ref.pattern)
7
- : `${ref.kind === 'module' && ref.top ? 'top.' : ''}${ref.kind === 'required' ? ref.name : ref.module}${ref.surface === undefined ? '' : `.${ref.surface}`}`;
1
+ import { ConfigError } from '../model/configError.js';
2
+ import { isTestData } from '../model/data.js';
3
+ import { isRecord, list } from '../model/shared.js';
4
+ import { exposesText, show } from '../model/text.js';
8
5
  export const checkReferences = (archetypes, pending, fail) => {
9
6
  for (const entry of pending) {
10
7
  const problem = refProblem(archetypes, entry);
@@ -48,35 +45,33 @@ export const exposedProblem = (subject, { exposes, testExposes }, surface, tests
48
45
  return `${subject} exposes ${list(named, 'nothing')}`;
49
46
  return `${subject} exposes ${exposesText(exposes)}, not named surfaces`;
50
47
  };
51
- export const exposesText = (exposes) => {
52
- if (exposes === undefined)
53
- return 'nothing';
54
- if (exposes.kind === 'all')
55
- return 'all its files';
56
- if (exposes.kind === 'files')
57
- return `{ files: [${exposes.patterns.join(', ')}] }`;
58
- return Object.keys(exposes.surfaces).join(', ');
59
- };
60
48
  export const toRules = (check, value, where) => {
61
- if (value === undefined || value === 'off')
62
- return value;
49
+ if (value === undefined)
50
+ return undefined;
51
+ if (value === 'off')
52
+ return { '*': false };
63
53
  if (!isRecord(value)) {
64
54
  check.fail(`${where}: expected off or rule settings by id`);
65
55
  return undefined;
66
56
  }
67
- return Object.fromEntries(Object.entries(value).map(([id, setting]) => [id, toSetting(check, id, setting, where)]));
57
+ return Object.fromEntries(Object.entries(value).flatMap(([id, setting]) => {
58
+ const parsed = toSetting(check, id, setting, where);
59
+ return parsed === undefined ? [] : [[id, parsed]];
60
+ }));
68
61
  };
69
62
  const toSetting = (check, id, setting, where) => {
70
63
  if (id !== '*' && check.ruleIds && !check.ruleIds.has(id))
71
64
  check.fail(`${where}: no rule called ${id}`);
65
+ else if (id === '*' && setting !== 'off' && setting !== false)
66
+ check.fail(`${where}.*: expected off; turn rules on by name, or extend a ruleset such as warp:recommended`);
72
67
  else if (setting === 'on' || setting === 'off')
73
68
  return setting === 'on';
74
69
  else if (typeof setting === 'boolean')
75
70
  return setting;
76
- else if (id !== '*' && isRecord(setting))
71
+ else if (isRecord(setting))
77
72
  return checkOptions(check, id, setting, where);
78
73
  else
79
- check.fail(`${where}.${id}: expected on, off${id === '*' ? '' : ' or options'}`);
74
+ check.fail(`${where}.${id}: expected on, off or options`);
80
75
  return undefined;
81
76
  };
82
77
  const checkOptions = (check, id, options, where) => {
@@ -1,5 +1,5 @@
1
- import type { ArchetypeData } from './data.ts';
2
- import type { PendingRef } from './language.ts';
3
- import type { Fail } from './shared.ts';
1
+ import type { ArchetypeData } from '../model/data.ts';
2
+ import type { PendingRef } from './structure.ts';
3
+ import type { Fail } from '../model/shared.ts';
4
4
  export declare const markTests: (archetypes: Record<string, ArchetypeData>, fail: Fail) => void;
5
5
  export declare const checkInputs: (archetypes: Record<string, ArchetypeData>, pending: readonly PendingRef[], fail: Fail) => void;
@@ -1,4 +1,5 @@
1
- import { exposedProblem, show } from './references.js';
1
+ import { exposedProblem } from './references.js';
2
+ import { show } from '../model/text.js';
2
3
  const namedOf = (archetype) => archetype.split('.')[0];
3
4
  const sitesOf = (archetypes) => Object.values(archetypes).flatMap((holder) => holder.modules
4
5
  .filter((module) => module.kind === 'dir' && !archetypes[module.archetype]?.inline)
@@ -0,0 +1,10 @@
1
+ import type { RuleSettingsData, RulesetData } from '../model/data.ts';
2
+ import { type Fail } from '../model/shared.ts';
3
+ export type ParseRules = (value: unknown, at: string) => RuleSettingsData | undefined;
4
+ export type ParseRuleset = (value: unknown, at: string) => RulesetData | undefined;
5
+ export interface Rulesets {
6
+ parse: ParseRuleset;
7
+ checkUsed(): void;
8
+ }
9
+ export declare const recommended = "warp:recommended";
10
+ export declare const rulesets: (named: unknown, shared: Readonly<Record<string, unknown>>, parseRules: ParseRules, fail: Fail) => Rulesets;
@@ -0,0 +1,60 @@
1
+ import { isRecord, list } from '../model/shared.js';
2
+ export const recommended = 'warp:recommended';
3
+ const rulesetKeys = ['extends', 'rules'];
4
+ export const rulesets = (named, shared, parseRules, fail) => {
5
+ const state = {
6
+ local: localRulesets(named, fail),
7
+ shared,
8
+ parseRules,
9
+ fail,
10
+ extended: new Set(),
11
+ };
12
+ return {
13
+ parse: (value, at) => (value === undefined ? undefined : flatten(state, value, at, [])),
14
+ checkUsed: () => {
15
+ for (const name of [...state.local.keys()].filter((name) => !state.extended.has(name)))
16
+ fail(`rulesets.${name}: nothing extends it; a named ruleset applies through a ruleset's extends`);
17
+ },
18
+ };
19
+ };
20
+ const localRulesets = (value, fail) => {
21
+ if (value === undefined)
22
+ return new Map();
23
+ if (!isRecord(value)) {
24
+ fail('rulesets: expected rulesets by name, { strict: { rules: {...} } }');
25
+ return new Map();
26
+ }
27
+ for (const name of Object.keys(value).filter((name) => name.includes(':')))
28
+ fail(`rulesets.${name}: a name has no ':', which marks a plugin's ruleset`);
29
+ return new Map(Object.entries(value).filter(([name]) => !name.includes(':')));
30
+ };
31
+ const flatten = (state, value, at, seen) => {
32
+ const def = Array.isArray(value) ? { extends: value } : value === 'off' ? { rules: 'off' } : value;
33
+ if (!isRecord(def)) {
34
+ state.fail(`${at}: expected a ruleset, off, [${recommended}] or { extends: [${recommended}], rules: {...} }`);
35
+ return [];
36
+ }
37
+ for (const key of Object.keys(def).filter((key) => !rulesetKeys.includes(key)))
38
+ state.fail(`${at}: unknown key ${key}; a ruleset takes ${rulesetKeys.join(', ')}`);
39
+ const bases = [def.extends ?? []].flat().flatMap((base) => baseOf(state, base, `${at}.extends`, seen));
40
+ const own = state.parseRules(def.rules, `${at}.rules`);
41
+ return own ? [...bases, own] : bases;
42
+ };
43
+ const baseOf = (state, base, at, seen) => {
44
+ const local = typeof base === 'string' && state.local.has(base);
45
+ if (typeof base !== 'string' || !(local || Object.hasOwn(state.shared, base))) {
46
+ const known = [...Object.keys(state.shared), ...state.local.keys()];
47
+ state.fail(`${at}: '${String(base)}' is no ruleset; extend ${list(known, 'nothing')}`);
48
+ return [];
49
+ }
50
+ if (seen.includes(base)) {
51
+ state.fail(`${at}: ${[...seen, base].join(' extends ')}, a loop`);
52
+ return [];
53
+ }
54
+ state.extended.add(base);
55
+ return resolveNamed(state, base, local, seen);
56
+ };
57
+ const resolveNamed = (state, name, local, seen) => {
58
+ const def = local ? state.local.get(name) : state.shared[name];
59
+ return flatten(state, def, local ? `rulesets.${name}` : name, [...seen, name]);
60
+ };
@@ -1,5 +1,6 @@
1
- import type { ArchetypeData, RefData } from './data.ts';
2
- import { type Fail } from './shared.ts';
1
+ import type { ArchetypeData, RefData } from '../model/data.ts';
2
+ import { type Fail } from '../model/shared.ts';
3
+ import type { ParseRuleset } from './rulesets.ts';
3
4
  export interface PendingRef {
4
5
  ref: RefData;
5
6
  where: string;
@@ -13,4 +14,4 @@ export interface Structure {
13
14
  pending: PendingRef[];
14
15
  }
15
16
  export declare const rootName = "root";
16
- export declare const parseStructure: (root: unknown, archetypes: unknown, fail: Fail) => Structure;
17
+ export declare const parseStructure: (root: unknown, archetypes: unknown, fail: Fail, ruleset: ParseRuleset) => Structure;
@@ -1,20 +1,31 @@
1
- import { isName, isRecord, list } from './shared.js';
1
+ import { isName, isRecord, list } from '../model/shared.js';
2
2
  export const rootName = 'root';
3
- const moduleKeys = ['description', 'dir', 'files', 'entry', 'archetype', 'dependencies', 'exposes', 'modules', 'test'];
4
- const archetypeKeys = ['description', 'marker', 'requires', 'entry', 'exposes', 'modules', 'test'];
5
- const rootKeys = ['description', 'modules', 'test'];
6
- const testKeys = ['requires', 'dependencies', 'provides', 'exposes', 'modules'];
3
+ const moduleKeys = [
4
+ 'description',
5
+ 'dir',
6
+ 'files',
7
+ 'entry',
8
+ 'archetype',
9
+ 'dependencies',
10
+ 'exposes',
11
+ 'modules',
12
+ 'test',
13
+ 'ruleset',
14
+ ];
15
+ const archetypeKeys = ['description', 'marker', 'requires', 'entry', 'exposes', 'modules', 'test', 'ruleset'];
16
+ const rootKeys = ['description', 'modules', 'test', 'ruleset'];
17
+ const testKeys = ['requires', 'dependencies', 'provides', 'exposes', 'modules', 'ruleset'];
7
18
  const kindOf = new Map([
8
19
  [archetypeKeys, 'an archetype'],
9
20
  [rootKeys, 'the root'],
10
21
  [testKeys, 'a test block'],
11
22
  ]);
12
- const testModuleKeys = ['description', 'dir', 'files', 'archetype', 'dependencies', 'exposes', 'modules'];
13
- export const parseStructure = (root, archetypes, fail) => {
23
+ const testModuleKeys = ['description', 'dir', 'files', 'archetype', 'dependencies', 'exposes', 'modules', 'ruleset'];
24
+ export const parseStructure = (root, archetypes, fail, ruleset) => {
14
25
  const defs = archetypes === undefined ? {} : isRecord(archetypes) ? archetypes : {};
15
26
  if (archetypes !== undefined && !isRecord(archetypes))
16
27
  fail('archetypes: expected archetypes by name');
17
- const state = { defs, archetypes: {}, pending: [], fail };
28
+ const state = { defs, archetypes: {}, pending: [], fail, ruleset };
18
29
  if (Object.hasOwn(defs, rootName))
19
30
  fail(`archetypes.${rootName}: root is the repository; name the archetype otherwise`);
20
31
  for (const [name, def] of Object.entries(defs)) {
@@ -63,6 +74,7 @@ const archetype = (state, name, def, options) => {
63
74
  const data = options.parent ? inlineData(state, name, where, test) : namedData(state, name, def, test);
64
75
  if (options.test)
65
76
  data.test = true;
77
+ Object.assign(data, rulesetsAt(state, where, def.ruleset, test.ruleset));
66
78
  const scope = {
67
79
  archetype: name,
68
80
  names: new Set([...Object.keys(modules), ...Object.keys(testModules), ...filesTestNames(modules)]),
@@ -98,6 +110,11 @@ const inlineData = (state, name, where, test) => {
98
110
  state.fail(`${where}.test.requires: only an archetype requires; its modules see what it requires`);
99
111
  return { name, inline: true, requires: [], modules: [] };
100
112
  };
113
+ const rulesetsAt = (state, where, ruleset, testRuleset) => {
114
+ const own = state.ruleset(ruleset, `${where}.ruleset`);
115
+ const tests = state.ruleset(testRuleset, `${where}.test.ruleset`);
116
+ return { ...(own ? { ruleset: own } : {}), ...(tests ? { testRuleset: tests } : {}) };
117
+ };
101
118
  const testBlock = (state, where, value) => {
102
119
  if (value === undefined)
103
120
  return {};
@@ -185,6 +202,7 @@ const testFiles = (state, scope, name, def, of) => {
185
202
  ...(of === undefined ? {} : { of }),
186
203
  using: references(state, scope, `${where}.dependencies`, def.dependencies, true),
187
204
  ...(exposed && exposed.kind !== 'surfaces' ? { exposes: exposed } : {}),
205
+ ...rulesetsAt(state, where, def.ruleset, undefined),
188
206
  ...description(state, where, def.description),
189
207
  };
190
208
  };
@@ -213,7 +231,7 @@ const instance = (state, scope, name, def, test) => {
213
231
  state.fail(`${where}.${key}: ${named} defines what is inside; declare ${key} there`);
214
232
  const own = bindings(state, scope, `${where}.dependencies`, def.dependencies, test);
215
233
  const testBlock = isRecord(def.test) ? def.test : {};
216
- for (const key of Object.keys(testBlock).filter((key) => key !== 'dependencies'))
234
+ for (const key of Object.keys(testBlock).filter((key) => key !== 'dependencies' && key !== 'ruleset'))
217
235
  state.fail(`${where}.test.${key}: ${named} defines its test code; declare ${key} there`);
218
236
  const tests = bindings(state, scope, `${where}.test.dependencies`, testBlock.dependencies, true);
219
237
  return {
@@ -225,6 +243,7 @@ const instance = (state, scope, name, def, test) => {
225
243
  using: Object.values(own),
226
244
  ...(Object.keys(own).length > 0 ? { inputs: own } : {}),
227
245
  ...(Object.keys(tests).length > 0 ? { testInputs: tests } : {}),
246
+ ...rulesetsAt(state, where, def.ruleset, testBlock.ruleset),
228
247
  ...description(state, where, def.description),
229
248
  };
230
249
  };
@@ -249,17 +268,18 @@ const filesModule = (state, scope, name, def) => {
249
268
  for (const key of ['modules', 'entry'].filter((key) => def[key] !== undefined))
250
269
  state.fail(`${where}.${key}: a files module has no modules; use dir for a folder of them`);
251
270
  const exposed = exposes(state, scope, `${where}.exposes`, def.exposes, undefined, false, false);
271
+ const test = isRecord(def.test) ? def.test : {};
252
272
  const module = {
253
273
  kind: 'files',
254
274
  name,
255
275
  patterns: patterns(state, `${where}.files`, def.files),
256
276
  using: references(state, scope, `${where}.dependencies`, def.dependencies, false),
257
277
  ...(exposed && exposed.kind !== 'surfaces' ? { exposes: exposed } : {}),
278
+ ...rulesetsAt(state, where, def.ruleset, test.ruleset),
258
279
  ...description(state, where, def.description),
259
280
  };
260
- const test = isRecord(def.test) ? def.test : {};
261
- for (const key of Object.keys(test).filter((key) => key !== 'modules'))
262
- state.fail(`${where}.test.${key}: a files module's test block holds its test modules only`);
281
+ for (const key of Object.keys(test).filter((key) => key !== 'modules' && key !== 'ruleset'))
282
+ state.fail(`${where}.test.${key}: a files module's test block holds its test modules and ruleset only`);
263
283
  const tests = test.modules === undefined ? {} : record(state, `${where}.test.modules`, test.modules);
264
284
  const lifted = Object.entries(tests).map(([inner, raw]) => {
265
285
  const testDef = record(state, `${where}.test.modules.${inner}`, raw);
@@ -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;