@webpieces/webpieces-tooling 0.4.952 → 0.4.954

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.
package/README.md CHANGED
@@ -73,8 +73,12 @@ const pack = new LocalRulePack('local-rules', __filename, [new NoBannedConsoleRu
73
73
  export const rulePackManifest = pack.rulePackManifest;
74
74
  export const sourceRuleRuntime = pack.sourceRuleRuntime;
75
75
  export const buildRuleRuntime = pack.buildRuleRuntime;
76
+ export const buildDebugRuntime = pack.buildDebugRuntime;
76
77
  ```
77
78
 
79
+ The fourth export is the one-rule debug run (below); without it `wp-validate-code --rule=<id>` refuses
80
+ the pack's rules.
81
+
78
82
  **3. Register it** in `webpieces.config.json`, beside the packs you already declare, with its own owner file:
79
83
 
80
84
  ```json
@@ -111,3 +115,17 @@ runners: `turnOffRuleUntilEpoch`, `turnOffRuleWhileOnBranch`, the optional `excl
111
115
  the per-site escape `// webpieces-disable no-banned-console -- <reason of at least 20 characters>` on the
112
116
  reported line or the line above it. A disable with no reason does not exempt the site. Rule ids must be
113
117
  kebab-case and must not collide with any other pack's.
118
+
119
+ **Count what is left with the debug run** (#1330), exactly as for a built-in code rule and with no config
120
+ edit:
121
+
122
+ ```bash
123
+ pnpm exec wp-validate-code --rule=no-banned-console --mode=RUN_EVERY_TIME
124
+ pnpm exec wp-validate-code --rule=no-banned-console --mode=RUN_EVERY_TIME --projects=my-app,my-lib
125
+ ```
126
+
127
+ It runs only that rule, through the same build policy the gate runs, so per-site disables, the repo's
128
+ `excludePaths` and the rule's own apply unchanged. It ignores the two escape hatches for that run, prints
129
+ the rule's own report and then one site count per project, writes nothing, and exits non-zero while any
130
+ site remains. `RUN_EVERY_TIME` judges every file in the repo and never consults the diff. An unknown
131
+ `--rule` is refused with the list of rules that accept a debug run, local ones included.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webpieces/webpieces-tooling",
3
- "version": "0.4.952",
3
+ "version": "0.4.954",
4
4
  "description": "Complete webpieces tooling installation and the combined OpenClaw hook adapter.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -16,17 +16,17 @@
16
16
  "README.md"
17
17
  ],
18
18
  "dependencies": {
19
- "@webpieces/agent-workflow-rules": "0.4.952",
20
- "@webpieces/ai-hook-rules": "0.4.952",
21
- "@webpieces/code-rules": "0.4.952",
22
- "@webpieces/eslint-rules": "0.4.952",
23
- "@webpieces/hook-runtime": "0.4.952",
24
- "@webpieces/nx-webpieces-rules": "0.4.952",
25
- "@webpieces/pr-gate": "0.4.952",
26
- "@webpieces/repo-workflow-core": "0.4.952",
27
- "@webpieces/rules-config": "0.4.952",
28
- "@webpieces/tooling-common": "0.4.952",
29
- "@webpieces/rules-sdk": "0.4.952"
19
+ "@webpieces/agent-workflow-rules": "0.4.954",
20
+ "@webpieces/ai-hook-rules": "0.4.954",
21
+ "@webpieces/code-rules": "0.4.954",
22
+ "@webpieces/eslint-rules": "0.4.954",
23
+ "@webpieces/hook-runtime": "0.4.954",
24
+ "@webpieces/nx-webpieces-rules": "0.4.954",
25
+ "@webpieces/pr-gate": "0.4.954",
26
+ "@webpieces/repo-workflow-core": "0.4.954",
27
+ "@webpieces/rules-config": "0.4.954",
28
+ "@webpieces/tooling-common": "0.4.954",
29
+ "@webpieces/rules-sdk": "0.4.954"
30
30
  },
31
31
  "author": "Dean Hiller",
32
32
  "license": "Apache-2.0",
@@ -39,7 +39,7 @@
39
39
  "access": "public"
40
40
  },
41
41
  "devDependencies": {
42
- "@webpieces/tooling-testkit": "0.4.952"
42
+ "@webpieces/tooling-testkit": "0.4.954"
43
43
  },
44
44
  "types": "./src/index.d.ts"
45
45
  }
@@ -1,5 +1,6 @@
1
1
  import { BuildPolicy, BuildPolicyResult } from '@webpieces/rules-sdk';
2
2
  import { AbstractRule } from '@webpieces/rules-config';
3
+ import { ScanScope } from '@webpieces/code-rules';
3
4
  import { LocalRule, LocalRuleConfig } from './local-rule';
4
5
  import { LocalRuleVerdict } from './local-rule-judge';
5
6
  /** A verdict located in its workspace-relative file. Data-only. */
@@ -19,13 +20,18 @@ export declare class LocatedLocalVerdict {
19
20
  * top-level `excludePaths` and the rule's own are applied to that listing; OFF and the epoch and branch
20
21
  * switches are checked by `shouldRun()` here and again by the build runner. A failure is ONE thrown
21
22
  * `RuleFailError` listing every site, rendered by the build runner.
23
+ *
24
+ * The files come from the {@link ScanScope} it is handed: the gate's (`GateScanScope`, no restriction), or a
25
+ * debug run's (#1330), restricted to `--projects`. Every site found is recorded there too, so a debug run
26
+ * counts them per project exactly as it does for a built-in rule.
22
27
  */
23
28
  export declare class LocalBuildPolicy extends AbstractRule<LocalRuleConfig> implements BuildPolicy {
24
29
  private readonly rule;
25
30
  private readonly globalExcludePaths;
31
+ private readonly scanScope;
26
32
  private readonly judge;
27
33
  private readonly diffScope;
28
- constructor(config: LocalRuleConfig, rule: LocalRule, globalExcludePaths: readonly string[]);
34
+ constructor(config: LocalRuleConfig, rule: LocalRule, globalExcludePaths: readonly string[], scanScope: ScanScope);
29
35
  run(workspaceRoot: string): Promise<BuildPolicyResult>;
30
36
  /** undefined when the diff base cannot be found, so a diff-scoped mode has nothing to compare against. */
31
37
  private scan;
@@ -6,7 +6,6 @@ const fs = tslib_1.__importStar(require("node:fs"));
6
6
  const path = tslib_1.__importStar(require("node:path"));
7
7
  const rules_sdk_1 = require("@webpieces/rules-sdk");
8
8
  const rules_config_1 = require("@webpieces/rules-config");
9
- const code_rules_1 = require("@webpieces/code-rules");
10
9
  const local_rule_judge_1 = require("./local-rule-judge");
11
10
  /** A verdict located in its workspace-relative file. Data-only. */
12
11
  class LocatedLocalVerdict {
@@ -29,22 +28,29 @@ exports.LocatedLocalVerdict = LocatedLocalVerdict;
29
28
  * top-level `excludePaths` and the rule's own are applied to that listing; OFF and the epoch and branch
30
29
  * switches are checked by `shouldRun()` here and again by the build runner. A failure is ONE thrown
31
30
  * `RuleFailError` listing every site, rendered by the build runner.
31
+ *
32
+ * The files come from the {@link ScanScope} it is handed: the gate's (`GateScanScope`, no restriction), or a
33
+ * debug run's (#1330), restricted to `--projects`. Every site found is recorded there too, so a debug run
34
+ * counts them per project exactly as it does for a built-in rule.
32
35
  */
33
36
  class LocalBuildPolicy extends rules_config_1.AbstractRule {
34
37
  rule;
35
38
  globalExcludePaths;
39
+ scanScope;
36
40
  judge;
37
41
  diffScope = new rules_config_1.DiffScope();
38
- constructor(config, rule, globalExcludePaths) {
42
+ constructor(config, rule, globalExcludePaths, scanScope) {
39
43
  super(config, rule.id, rule.id);
40
44
  this.rule = rule;
41
45
  this.globalExcludePaths = globalExcludePaths;
46
+ this.scanScope = scanScope;
42
47
  this.judge = new local_rule_judge_1.LocalRuleJudge(rule);
43
48
  }
44
49
  async run(workspaceRoot) {
45
50
  const found = await new rules_config_1.RuleFileScope().runWithin(this.fileExclusions(this.globalExcludePaths), async () => this.scan(workspaceRoot));
46
51
  if (found === undefined || found.length === 0)
47
52
  return new rules_sdk_1.BuildPolicyResult(true);
53
+ this.scanScope.recordSites(this.name, found.map((each) => each.relFile));
48
54
  throw this.failure(found);
49
55
  }
50
56
  /** undefined when the diff base cannot be found, so a diff-scoped mode has nothing to compare against. */
@@ -59,7 +65,7 @@ class LocalBuildPolicy extends rules_config_1.AbstractRule {
59
65
  }
60
66
  const base = range.base ?? '';
61
67
  const found = [];
62
- for (const relFile of new code_rules_1.GateScanScope().files(workspaceRoot, mode, base, range.head, { tsOnly: false })) {
68
+ for (const relFile of this.scanScope.files(workspaceRoot, mode, base, range.head, { tsOnly: false })) {
63
69
  const absFile = path.join(workspaceRoot, relFile);
64
70
  if (!this.judge.judges(relFile) || !fs.existsSync(absFile))
65
71
  continue;
@@ -1 +1 @@
1
- {"version":3,"file":"local-build-policy.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/local-build-policy.ts"],"names":[],"mappings":";;;;AAAA,oDAA8B;AAC9B,wDAAkC;AAClC,oDAAsE;AACtE,0DAAgG;AACh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AAC,IAAI,CAAC,IAAI,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;YAC1D,CAAC;QACL,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,OAAO,CAAC,KAAqC;QACjD,MAAM,OAAO,GACT,GAAG,KAAK,CAAC,MAAM,sCAAsC,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI;YAC7F,KAAK,CAAC,GAAG,CAAC,CAAC,IAAyB,EAAE,EAAE,CACpC,KAAK,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,WAAW,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;YACjH,8BAA8B,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,CAAC;QAChE,OAAO,IAAI,4BAAa,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxF,CAAC;CACJ;AApDD,4CAoDC","sourcesContent":["import * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { BuildPolicy, BuildPolicyResult } from '@webpieces/rules-sdk';\nimport { AbstractRule, DiffScope, RuleFailError, RuleFileScope } from '@webpieces/rules-config';\nimport { GateScanScope } from '@webpieces/code-rules';\nimport { LocalRule, LocalRuleConfig } from './local-rule';\nimport { LocalRuleJudge, LocalRuleVerdict } from './local-rule-judge';\n\n/** A verdict located in its workspace-relative file. Data-only. */\nexport class LocatedLocalVerdict {\n constructor(\n readonly relFile: string,\n readonly verdict: LocalRuleVerdict,\n ) {}\n}\n\n/**\n * A local rule at the build gate (`wp-build` / the PR gate, through `validate-code`): the backstop for a\n * repo that does not run the guard hooks. It reads each in-scope file WHOLE and calls the same\n * {@link LocalRuleJudge} the Write/Edit guard calls, so both runners report the same sites at the same lines.\n *\n * The file set follows `mode` exactly as the built-in rules do (`ScanScope`): NEW_AND_MODIFIED_CODE judges\n * only findings on changed lines of changed files, NEW_AND_MODIFIED_FILES every finding in a changed file,\n * MODIFIED_PROJECTS / RUN_EVERY_TIME every file of each touched project / of the repo. The repo's\n * top-level `excludePaths` and the rule's own are applied to that listing; OFF and the epoch and branch\n * switches are checked by `shouldRun()` here and again by the build runner. A failure is ONE thrown\n * `RuleFailError` listing every site, rendered by the build runner.\n */\nexport class LocalBuildPolicy extends AbstractRule<LocalRuleConfig> implements BuildPolicy {\n private readonly judge: LocalRuleJudge;\n private readonly diffScope = new DiffScope();\n\n constructor(\n config: LocalRuleConfig,\n private readonly rule: LocalRule,\n private readonly globalExcludePaths: readonly string[],\n ) {\n super(config, rule.id, rule.id);\n this.judge = new LocalRuleJudge(rule);\n }\n\n async run(workspaceRoot: string): Promise<BuildPolicyResult> {\n const found = await new RuleFileScope().runWithin(this.fileExclusions(this.globalExcludePaths), async () => this.scan(workspaceRoot));\n if (found === undefined || found.length === 0) return new BuildPolicyResult(true);\n throw this.failure(found);\n }\n\n /** undefined when the diff base cannot be found, so a diff-scoped mode has nothing to compare against. */\n private scan(workspaceRoot: string): LocatedLocalVerdict[] | undefined {\n const mode = this.config.mode;\n if (mode === undefined || mode === 'OFF') return [];\n const range = this.diffScope.resolveBase(workspaceRoot);\n if (range.base === undefined && mode !== 'RUN_EVERY_TIME') {\n console.log(`\\n⏭️ Skipping ${this.name} validation (could not detect base branch)\\n`);\n return undefined;\n }\n const base = range.base ?? '';\n const found: LocatedLocalVerdict[] = [];\n for (const relFile of new GateScanScope().files(workspaceRoot, mode, base, range.head, { tsOnly: false })) {\n const absFile = path.join(workspaceRoot, relFile);\n if (!this.judge.judges(relFile) || !fs.existsSync(absFile)) continue;\n const changed = mode === 'NEW_AND_MODIFIED_CODE'\n ? this.diffScope.getChangedLineNumbers(this.diffScope.getFileDiff(workspaceRoot, relFile, base, range.head))\n : undefined;\n if (changed !== undefined && changed.size === 0) continue;\n for (const verdict of this.judge.judge(relFile, fs.readFileSync(absFile, 'utf8'), this.config, changed)) {\n found.push(new LocatedLocalVerdict(relFile, verdict));\n }\n }\n return found;\n }\n\n private failure(found: readonly LocatedLocalVerdict[]): RuleFailError {\n const message =\n `${found.length} site(s) break the repo-local rule ${this.name} — ${this.rule.description}\\n` +\n found.map((each: LocatedLocalVerdict) =>\n ` ${each.relFile}:${each.verdict.line} — ${each.verdict.message}\\n ${each.verdict.snippet}`).join('\\n') +\n `\\n Last resort, per site: ${this.judge.disableComment()}`;\n return new RuleFailError(this.name, message, undefined, undefined, this.rule.cures);\n }\n}\n"]}
1
+ {"version":3,"file":"local-build-policy.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/local-build-policy.ts"],"names":[],"mappings":";;;;AAAA,oDAA8B;AAC9B,wDAAkC;AAClC,oDAAsE;AACtE,0DAAgG;AAGhG,yDAAsE;AAEtE,mEAAmE;AACnE,MAAa,mBAAmB;IAEf;IACA;IAFb,YACa,OAAe,EACf,OAAyB;QADzB,YAAO,GAAP,OAAO,CAAQ;QACf,YAAO,GAAP,OAAO,CAAkB;IACnC,CAAC;CACP;AALD,kDAKC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAa,gBAAiB,SAAQ,2BAA6B;IAM1C;IACA;IACA;IAPJ,KAAK,CAAiB;IACtB,SAAS,GAAG,IAAI,wBAAS,EAAE,CAAC;IAE7C,YACI,MAAuB,EACN,IAAe,EACf,kBAAqC,EACrC,SAAoB;QAErC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QAJf,SAAI,GAAJ,IAAI,CAAW;QACf,uBAAkB,GAAlB,kBAAkB,CAAmB;QACrC,cAAS,GAAT,SAAS,CAAW;QAGrC,IAAI,CAAC,KAAK,GAAG,IAAI,iCAAc,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED,K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AAO,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;YACjH,8BAA8B,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,CAAC;QAChE,OAAO,IAAI,4BAAa,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxF,CAAC;CACJ;AAtDD,4CAsDC","sourcesContent":["import * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { BuildPolicy, BuildPolicyResult } from '@webpieces/rules-sdk';\nimport { AbstractRule, DiffScope, RuleFailError, RuleFileScope } from '@webpieces/rules-config';\nimport { ScanScope } from '@webpieces/code-rules';\nimport { LocalRule, LocalRuleConfig } from './local-rule';\nimport { LocalRuleJudge, LocalRuleVerdict } from './local-rule-judge';\n\n/** A verdict located in its workspace-relative file. Data-only. */\nexport class LocatedLocalVerdict {\n constructor(\n readonly relFile: string,\n readonly verdict: LocalRuleVerdict,\n ) {}\n}\n\n/**\n * A local rule at the build gate (`wp-build` / the PR gate, through `validate-code`): the backstop for a\n * repo that does not run the guard hooks. It reads each in-scope file WHOLE and calls the same\n * {@link LocalRuleJudge} the Write/Edit guard calls, so both runners report the same sites at the same lines.\n *\n * The file set follows `mode` exactly as the built-in rules do (`ScanScope`): NEW_AND_MODIFIED_CODE judges\n * only findings on changed lines of changed files, NEW_AND_MODIFIED_FILES every finding in a changed file,\n * MODIFIED_PROJECTS / RUN_EVERY_TIME every file of each touched project / of the repo. The repo's\n * top-level `excludePaths` and the rule's own are applied to that listing; OFF and the epoch and branch\n * switches are checked by `shouldRun()` here and again by the build runner. A failure is ONE thrown\n * `RuleFailError` listing every site, rendered by the build runner.\n *\n * The files come from the {@link ScanScope} it is handed: the gate's (`GateScanScope`, no restriction), or a\n * debug run's (#1330), restricted to `--projects`. Every site found is recorded there too, so a debug run\n * counts them per project exactly as it does for a built-in rule.\n */\nexport class LocalBuildPolicy extends AbstractRule<LocalRuleConfig> implements BuildPolicy {\n private readonly judge: LocalRuleJudge;\n private readonly diffScope = new DiffScope();\n\n constructor(\n config: LocalRuleConfig,\n private readonly rule: LocalRule,\n private readonly globalExcludePaths: readonly string[],\n private readonly scanScope: ScanScope,\n ) {\n super(config, rule.id, rule.id);\n this.judge = new LocalRuleJudge(rule);\n }\n\n async run(workspaceRoot: string): Promise<BuildPolicyResult> {\n const found = await new RuleFileScope().runWithin(this.fileExclusions(this.globalExcludePaths), async () => this.scan(workspaceRoot));\n if (found === undefined || found.length === 0) return new BuildPolicyResult(true);\n this.scanScope.recordSites(this.name, found.map((each: LocatedLocalVerdict) => each.relFile));\n throw this.failure(found);\n }\n\n /** undefined when the diff base cannot be found, so a diff-scoped mode has nothing to compare against. */\n private scan(workspaceRoot: string): LocatedLocalVerdict[] | undefined {\n const mode = this.config.mode;\n if (mode === undefined || mode === 'OFF') return [];\n const range = this.diffScope.resolveBase(workspaceRoot);\n if (range.base === undefined && mode !== 'RUN_EVERY_TIME') {\n console.log(`\\n⏭️ Skipping ${this.name} validation (could not detect base branch)\\n`);\n return undefined;\n }\n const base = range.base ?? '';\n const found: LocatedLocalVerdict[] = [];\n for (const relFile of this.scanScope.files(workspaceRoot, mode, base, range.head, { tsOnly: false })) {\n const absFile = path.join(workspaceRoot, relFile);\n if (!this.judge.judges(relFile) || !fs.existsSync(absFile)) continue;\n const changed = mode === 'NEW_AND_MODIFIED_CODE'\n ? this.diffScope.getChangedLineNumbers(this.diffScope.getFileDiff(workspaceRoot, relFile, base, range.head))\n : undefined;\n if (changed !== undefined && changed.size === 0) continue;\n for (const verdict of this.judge.judge(relFile, fs.readFileSync(absFile, 'utf8'), this.config, changed)) {\n found.push(new LocatedLocalVerdict(relFile, verdict));\n }\n }\n return found;\n }\n\n private failure(found: readonly LocatedLocalVerdict[]): RuleFailError {\n const message =\n `${found.length} site(s) break the repo-local rule ${this.name} — ${this.rule.description}\\n` +\n found.map((each: LocatedLocalVerdict) =>\n ` ${each.relFile}:${each.verdict.line} — ${each.verdict.message}\\n ${each.verdict.snippet}`).join('\\n') +\n `\\n Last resort, per site: ${this.judge.disableComment()}`;\n return new RuleFailError(this.name, message, undefined, undefined, this.rule.cures);\n }\n}\n"]}
@@ -1,12 +1,13 @@
1
- import { BuildRuleRuntime, RulePackManifest } from '@webpieces/rules-sdk';
1
+ import { BuildDebugRuntime, BuildRuleRuntime, RulePackManifest } from '@webpieces/rules-sdk';
2
2
  import { HookRuleRuntime } from '@webpieces/hook-runtime';
3
3
  import { LocalRule } from './local-rule';
4
4
  /**
5
5
  * A repo's OWN rule pack (#1314) — the sixth owner beside webpieces' five, built from {@link LocalRule}s.
6
6
  *
7
- * One module both declares the pack and implements it. It exports the three names the framework loads —
8
- * `rulePackManifest`, `sourceRuleRuntime` and `buildRuleRuntime` — and every rule is contributed to BOTH
9
- * runners, the Write/Edit source guard and the build gate, so there is no when-to-run setting:
7
+ * One module both declares the pack and implements it. It exports the four names the framework loads —
8
+ * `rulePackManifest`, `sourceRuleRuntime`, `buildRuleRuntime` and `buildDebugRuntime` (the one-rule
9
+ * `wp-validate-code --rule=<id>` debug run, #1330) — and every rule is contributed to BOTH runners, the
10
+ * Write/Edit source guard and the build gate, so there is no when-to-run setting:
10
11
  *
11
12
  * ```ts
12
13
  * // webpieces-rules/local-rules.ts
@@ -17,6 +18,7 @@ import { LocalRule } from './local-rule';
17
18
  * export const rulePackManifest = pack.rulePackManifest;
18
19
  * export const sourceRuleRuntime = pack.sourceRuleRuntime;
19
20
  * export const buildRuleRuntime = pack.buildRuleRuntime;
21
+ * export const buildDebugRuntime = pack.buildDebugRuntime;
20
22
  * ```
21
23
  *
22
24
  * declared in `webpieces.config.json` like any pack, with its own owner file:
@@ -31,6 +33,7 @@ export declare class LocalRulePack {
31
33
  readonly rulePackManifest: RulePackManifest;
32
34
  readonly sourceRuleRuntime: HookRuleRuntime;
33
35
  readonly buildRuleRuntime: BuildRuleRuntime;
36
+ readonly buildDebugRuntime: BuildDebugRuntime;
34
37
  private readonly rules;
35
38
  /**
36
39
  * @param packageName the pack's owner name — how the registry and every refusal name it.
@@ -6,6 +6,9 @@ const path = tslib_1.__importStar(require("node:path"));
6
6
  const rules_sdk_1 = require("@webpieces/rules-sdk");
7
7
  const rules_sdk_2 = require("@webpieces/rules-sdk");
8
8
  const tooling_common_1 = require("@webpieces/tooling-common");
9
+ const to_error_1 = require("@webpieces/tooling-common/to-error");
10
+ const rules_config_1 = require("@webpieces/rules-config");
11
+ const code_rules_1 = require("@webpieces/code-rules");
9
12
  const local_source_hook_policy_1 = require("./local-source-hook-policy");
10
13
  const local_build_policy_1 = require("./local-build-policy");
11
14
  const RULE_ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
@@ -48,16 +51,62 @@ class LocalBuildRuleRuntime {
48
51
  const configs = new LocalRuleConfigs(request.configs);
49
52
  return request.ruleIds.map((id) => {
50
53
  const rule = this.pack.ruleFor(id);
51
- return new local_build_policy_1.LocalBuildPolicy(configs.configFor(rule), rule, request.excludePaths);
54
+ return new local_build_policy_1.LocalBuildPolicy(configs.configFor(rule), rule, request.excludePaths, new code_rules_1.GateScanScope());
52
55
  });
53
56
  }
54
57
  }
58
+ /**
59
+ * The one-rule DEBUG run of a local rule (#1330) — `wp-validate-code --rule=<id> --mode=RUN_EVERY_TIME
60
+ * --projects=a,b` — with the same refusals, banner, in-memory override and per-project count a built-in
61
+ * code rule gets ({@link RuleDebugRun}). The rule runs through the same {@link LocalBuildPolicy} the gate
62
+ * runs, so disables, the repo's `excludePaths` and the rule's own apply exactly as they do there; only the
63
+ * mode (when `--mode` is given) and the two escape hatches are overridden, in memory. RUN_EVERY_TIME lists
64
+ * every file in the repo and never consults the diff.
65
+ */
66
+ class LocalBuildDebugRuntime {
67
+ pack;
68
+ debug = new code_rules_1.RuleDebugRun();
69
+ constructor(pack) {
70
+ this.pack = pack;
71
+ }
72
+ async run(request) {
73
+ const root = request.workspaceRoot;
74
+ const asked = new code_rules_1.RunRequestParser().parse(request.selection.rule, request.selection.mode, request.selection.projects);
75
+ const rule = this.pack.ruleFor(request.selection.rule);
76
+ const loaded = (0, rules_config_1.loadAndValidate)(root);
77
+ const config = new LocalRuleConfigs(loaded.rulesConfig).configFor(rule);
78
+ const plan = this.debug.plan(root, new code_rules_1.DebugAsk(rule.id, asked.mode, config.mode ?? 'OFF', rules_sdk_1.MODIFIED_CODE_MODES, asked.projects));
79
+ this.debug.printBanner(plan);
80
+ const diffScope = new rules_config_1.DiffScope();
81
+ const scan = new code_rules_1.ScanScope(diffScope, new code_rules_1.ProjectCatalog(diffScope), new code_rules_1.ScanRestriction(plan.projects));
82
+ const policy = new local_build_policy_1.LocalBuildPolicy(this.debug.overridden(config, plan), rule, loaded.excludePaths.paths, scan);
83
+ const ruleSucceeded = await this.runPrintingFailure(policy, root);
84
+ const sites = this.debug.printCounts(root, plan, scan, ruleSucceeded);
85
+ return new rules_sdk_1.BuildPolicyResult(ruleSucceeded && sites === 0);
86
+ }
87
+ /** The rule's own failure text is printed BEFORE the counts, exactly as a built-in rule's is. */
88
+ async runPrintingFailure(policy, root) {
89
+ // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- the policy's RuleFailError IS the debug run's report; anything else propagates
90
+ try {
91
+ return (await policy.run(root)).success;
92
+ // webpieces-disable no-any-unknown -- a thrown value is narrowed to the policy's RuleFailError, anything else is rethrown
93
+ }
94
+ catch (err) {
95
+ const error = (0, to_error_1.toError)(err);
96
+ if (!(error instanceof rules_config_1.RuleFailError))
97
+ throw error;
98
+ console.error((0, rules_config_1.renderRuleFailForHuman)(error));
99
+ return false;
100
+ }
101
+ }
102
+ }
55
103
  /**
56
104
  * A repo's OWN rule pack (#1314) — the sixth owner beside webpieces' five, built from {@link LocalRule}s.
57
105
  *
58
- * One module both declares the pack and implements it. It exports the three names the framework loads —
59
- * `rulePackManifest`, `sourceRuleRuntime` and `buildRuleRuntime` — and every rule is contributed to BOTH
60
- * runners, the Write/Edit source guard and the build gate, so there is no when-to-run setting:
106
+ * One module both declares the pack and implements it. It exports the four names the framework loads —
107
+ * `rulePackManifest`, `sourceRuleRuntime`, `buildRuleRuntime` and `buildDebugRuntime` (the one-rule
108
+ * `wp-validate-code --rule=<id>` debug run, #1330) — and every rule is contributed to BOTH runners, the
109
+ * Write/Edit source guard and the build gate, so there is no when-to-run setting:
61
110
  *
62
111
  * ```ts
63
112
  * // webpieces-rules/local-rules.ts
@@ -68,6 +117,7 @@ class LocalBuildRuleRuntime {
68
117
  * export const rulePackManifest = pack.rulePackManifest;
69
118
  * export const sourceRuleRuntime = pack.sourceRuleRuntime;
70
119
  * export const buildRuleRuntime = pack.buildRuleRuntime;
120
+ * export const buildDebugRuntime = pack.buildDebugRuntime;
71
121
  * ```
72
122
  *
73
123
  * declared in `webpieces.config.json` like any pack, with its own owner file:
@@ -82,6 +132,7 @@ class LocalRulePack {
82
132
  rulePackManifest;
83
133
  sourceRuleRuntime;
84
134
  buildRuleRuntime;
135
+ buildDebugRuntime;
85
136
  rules = new Map();
86
137
  /**
87
138
  * @param packageName the pack's owner name — how the registry and every refusal name it.
@@ -103,6 +154,7 @@ class LocalRulePack {
103
154
  ]), [], []);
104
155
  this.sourceRuleRuntime = new LocalSourceRuleRuntime(this);
105
156
  this.buildRuleRuntime = new LocalBuildRuleRuntime(this);
157
+ this.buildDebugRuntime = new LocalBuildDebugRuntime(this);
106
158
  }
107
159
  /** The rule registered under `id`; the runtimes only ever ask for ids this pack's manifest declares. */
108
160
  ruleFor(id) {
@@ -1 +1 @@
1
- {"version":3,"file":"local-rule-pack.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/local-rule-pack.ts"],"names":[],"mappings":";;;;AAAA,wDAAkC;AAClC,oDAa8B;AAC9B,oDAAwD;AAExD,8DAA0D;AAE1D,yEAAmE;AACnE,6DAAwD;AAExD,MAAM,OAAO,GAAG,iCAAiC,CAAC;AAElD,yFAAyF;AACzF,MAAM,kBAAkB,GAA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C;IAClC,CAAC;IAEO,UAAU,CAAC,IAAe;QAC9B,MAAM,MAAM,GAA6B,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,oBAAQ,CAAC,QAAQ,EAAE,+BAAmB,CAAC,EAAE,CAAC;QACjH,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,wBAAY,EAAE,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,qBAAqB,EAAE,CAAC,EAAE,wBAAwB,EAAE,IAAI,EAAE,CAAC,CAAC;QACxH,OAAO,IAAI,+BAAmB,CAC1B,IAAI,CAAC,EAAE,EACP,MAAM,EACN,mCAAuB,EACvB,EAAE,EACF,IAAI,EACJ,OAAO,EACP,IAAI,oBAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,iCAAiC,IAAI,CAAC,EAAE,wBAAwB,CAAC,CACnG,CAAC;IACN,CAAC;CACJ;AAlED,sCAkEC","sourcesContent":["import * as path from 'node:path';\nimport {\n BuildPolicy,\n BuildRuleRuntime,\n ConfigObject,\n FieldDef,\n MODIFIED_CODE_MODES,\n OwnedRuleDefinition,\n PolicyRuntimeRequest,\n RULE_PACK_API_VERSION,\n RULE_SCHEMA_API_VERSION,\n RuleContribution,\n RuleHelp,\n RulePackManifest,\n} from '@webpieces/rules-sdk';\nimport { BASE_RULE_SCHEMA } from '@webpieces/rules-sdk';\nimport { HookPolicyRuntimeRequest, HookRuleRuntime, Rule } from '@webpieces/hook-runtime';\nimport { InformAiError } from '@webpieces/tooling-common';\nimport { LocalRule, LocalRuleConfig } from './local-rule';\nimport { LocalSourceHookPolicy } from './local-source-hook-policy';\nimport { LocalBuildPolicy } from './local-build-policy';\n\nconst RULE_ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;\n\n/** The version a local pack reports: it ships with the repo, never as an npm release. */\nconst LOCAL_PACK_VERSION = '0.0.0-local';\n\n/** Validated owner settings for one local rule, or a refusal naming the owner file to repair. */\nclass LocalRuleConfigs {\n constructor(private readonly configs: Readonly<Record<string, ConfigObject>>) {}\n\n configFor(rule: LocalRule): LocalRuleConfig {\n const config = this.configs[rule.id];\n if (!config)\n throw new InformAiError(`Missing explicit config for local rule ${rule.id}. Add its entry to the rule pack's owner file.`);\n return config as LocalRuleConfig;\n }\n}\n\n/** The source-hook half: one {@link LocalSourceHookPolicy} per rule the runtime request names. */\nclass LocalSourceRuleRuntime implements HookRuleRuntime {\n constructor(private readonly pack: LocalRulePack) {}\n\n create(request: HookPolicyRuntimeRequest): readonly Rule[] {\n const configs = new LocalRuleConfigs(request.configs);\n return request.ruleIds.map((id: string) => {\n const rule = this.pack.ruleFor(id);\n return new LocalSourceHookPolicy(configs.configFor(rule), rule);\n });\n }\n}\n\n/** The build half: one {@link LocalBuildPolicy} per rule the runtime request names. */\nclass LocalBuildRuleRuntime implements BuildRuleRuntime {\n constructor(private readonly pack: LocalRulePack) {}\n\n async create(request: PolicyRuntimeRequest): Promise<readonly BuildPolicy[]> {\n const configs = new LocalRuleConfigs(request.configs);\n return request.ruleIds.map((id: string) => {\n const rule = this.pack.ruleFor(id);\n return new LocalBuildPolicy(configs.configFor(rule), rule, request.excludePaths);\n });\n }\n}\n\n/**\n * A repo's OWN rule pack (#1314) — the sixth owner beside webpieces' five, built from {@link LocalRule}s.\n *\n * One module both declares the pack and implements it. It exports the three names the framework loads —\n * `rulePackManifest`, `sourceRuleRuntime` and `buildRuleRuntime` — and every rule is contributed to BOTH\n * runners, the Write/Edit source guard and the build gate, so there is no when-to-run setting:\n *\n * ```ts\n * // webpieces-rules/local-rules.ts\n * import { LocalRulePack } from '@webpieces/webpieces-tooling/local-rules';\n * import { NoTodoCommentRule } from './no-todo-comment';\n *\n * const pack = new LocalRulePack('local-rules', __filename, [new NoTodoCommentRule()]);\n * export const rulePackManifest = pack.rulePackManifest;\n * export const sourceRuleRuntime = pack.sourceRuleRuntime;\n * export const buildRuleRuntime = pack.buildRuleRuntime;\n * ```\n *\n * declared in `webpieces.config.json` like any pack, with its own owner file:\n * `{ \"package\": \"./webpieces-rules/local-rules.ts\", \"config\": \".webpieces-config/rules/local.json\" }`.\n * A `.ts` module is transpiled in memory when it is loaded; there is no build step.\n *\n * Each rule's schema is its `settings` plus a required `mode` (the scope vocabulary built-in diff rules\n * use); the registry adds the framework-owned fields. Nothing has a default: a missing entry or value fails\n * the config load naming the owner file and the seed to paste.\n */\nexport class LocalRulePack {\n readonly rulePackManifest: RulePackManifest;\n readonly sourceRuleRuntime: HookRuleRuntime;\n readonly buildRuleRuntime: BuildRuleRuntime;\n private readonly rules = new Map<string, LocalRule>();\n\n /**\n * @param packageName the pack's owner name — how the registry and every refusal name it.\n * @param moduleFile the declaring module's own `__filename`: both runtimes are loaded from it.\n * @param rules the pack's rules; ids must be unique kebab-case and not collide with any other pack's.\n */\n constructor(packageName: string, moduleFile: string, rules: readonly LocalRule[]) {\n if (!packageName.trim()) throw new InformAiError('A LocalRulePack needs a nonempty package name, e.g. \"local-rules\".');\n if (!path.isAbsolute(moduleFile))\n throw new InformAiError(`LocalRulePack ${packageName} needs its module's absolute path. Pass __filename as the second argument.`);\n if (rules.length === 0) throw new InformAiError(`LocalRulePack ${packageName} has no rules. Add at least one LocalRule.`);\n for (const rule of rules) this.add(packageName, rule);\n this.rulePackManifest = new RulePackManifest(\n packageName,\n LOCAL_PACK_VERSION,\n RULE_PACK_API_VERSION,\n rules.map((rule: LocalRule) => this.definition(rule)),\n rules.flatMap((rule: LocalRule) => [\n new RuleContribution(rule.id, packageName, 'source-hook', moduleFile),\n new RuleContribution(rule.id, packageName, 'build', moduleFile),\n ]),\n [],\n [],\n );\n this.sourceRuleRuntime = new LocalSourceRuleRuntime(this);\n this.buildRuleRuntime = new LocalBuildRuleRuntime(this);\n }\n\n /** The rule registered under `id`; the runtimes only ever ask for ids this pack's manifest declares. */\n ruleFor(id: string): LocalRule {\n const rule = this.rules.get(id);\n if (!rule) throw new InformAiError(`Local rule pack has no rule ${id}. Repair its rule list.`);\n return rule;\n }\n\n private add(packageName: string, rule: LocalRule): void {\n if (!RULE_ID.test(rule.id))\n throw new InformAiError(`Local rule id \"${rule.id}\" in ${packageName} must be kebab-case (e.g. no-todo-comment).`);\n if (this.rules.has(rule.id)) throw new InformAiError(`Local rule ${rule.id} is declared twice in ${packageName}. Keep one.`);\n for (const key of Object.keys(rule.settings)) {\n if (key === 'mode' || Object.hasOwn(BASE_RULE_SCHEMA, key))\n throw new InformAiError(\n `Local rule ${rule.id} declares the framework-owned setting \"${key}\". Delete it from settings; every local rule gets mode, ${Object.keys(BASE_RULE_SCHEMA).join(', ')}.`,\n );\n }\n this.rules.set(rule.id, rule);\n }\n\n private definition(rule: LocalRule): OwnedRuleDefinition {\n const schema: Record<string, FieldDef> = { ...rule.settings, mode: new FieldDef('string', MODIFIED_CODE_MODES) };\n const seed = Object.assign(new ConfigObject(), rule.seed, { turnOffRuleUntilEpoch: 0, turnOffRuleWhileOnBranch: null });\n return new OwnedRuleDefinition(\n rule.id,\n schema,\n RULE_SCHEMA_API_VERSION,\n {},\n seed,\n 'rules',\n new RuleHelp(rule.description, `Fix the reported site, or set ${rule.id}.mode in {configFile}.`),\n );\n }\n}\n"]}
1
+ {"version":3,"file":"local-rule-pack.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/local-rule-pack.ts"],"names":[],"mappings":";;;;AAAA,wDAAkC;AAClC,oDAgB8B;AAC9B,oDAAwD;AAExD,8DAA0D;AAC1D,iEAA6D;AAC7D,0DAA4G;AAC5G,sDAQ+B;AAE/B,yEAAmE;AACnE,6DAAwD;AAExD,MAAM,OAAO,GAAG,iCAAiC,CAAC;AAElD,yFAAyF;AACzF,MAAM,kBAAkB,GAAG,aAAa,CAAC;AAEzC,iGAAiG;AACjG,MAAM,gBAAgB;IACW;IAA7B,YAA6B,OAA+C;QAA/C,YAAO,GAAP,OAAO,CAAwC;IAAG,CAAC;IAEhF,SAAS,CAAC,IAAe;QACrB,MAAM,MAAM,GAAG,IAA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B,CAC1B,IAAI,CAAC,EAAE,EACP,MAAM,EACN,mCAAuB,EACvB,EAAE,EACF,IAAI,EACJ,OAAO,EACP,IAAI,oBAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,iCAAiC,IAAI,CAAC,EAAE,wBAAwB,CAAC,CACnG,CAAC;IACN,CAAC;CACJ;AApED,sCAoEC","sourcesContent":["import * as path from 'node:path';\nimport {\n BuildDebugRequest,\n BuildDebugRuntime,\n BuildPolicy,\n BuildPolicyResult,\n BuildRuleRuntime,\n ConfigObject,\n FieldDef,\n MODIFIED_CODE_MODES,\n OwnedRuleDefinition,\n PolicyRuntimeRequest,\n RULE_PACK_API_VERSION,\n RULE_SCHEMA_API_VERSION,\n RuleContribution,\n RuleHelp,\n RulePackManifest,\n} from '@webpieces/rules-sdk';\nimport { BASE_RULE_SCHEMA } from '@webpieces/rules-sdk';\nimport { HookPolicyRuntimeRequest, HookRuleRuntime, Rule } from '@webpieces/hook-runtime';\nimport { InformAiError } from '@webpieces/tooling-common';\nimport { toError } from '@webpieces/tooling-common/to-error';\nimport { DiffScope, RuleFailError, loadAndValidate, renderRuleFailForHuman } from '@webpieces/rules-config';\nimport {\n DebugAsk,\n GateScanScope,\n ProjectCatalog,\n RuleDebugRun,\n RunRequestParser,\n ScanRestriction,\n ScanScope,\n} from '@webpieces/code-rules';\nimport { LocalRule, LocalRuleConfig } from './local-rule';\nimport { LocalSourceHookPolicy } from './local-source-hook-policy';\nimport { LocalBuildPolicy } from './local-build-policy';\n\nconst RULE_ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;\n\n/** The version a local pack reports: it ships with the repo, never as an npm release. */\nconst LOCAL_PACK_VERSION = '0.0.0-local';\n\n/** Validated owner settings for one local rule, or a refusal naming the owner file to repair. */\nclass LocalRuleConfigs {\n constructor(private readonly configs: Readonly<Record<string, ConfigObject>>) {}\n\n configFor(rule: LocalRule): LocalRuleConfig {\n const config = this.configs[rule.id];\n if (!config)\n throw new InformAiError(`Missing explicit config for local rule ${rule.id}. Add its entry to the rule pack's owner file.`);\n return config as LocalRuleConfig;\n }\n}\n\n/** The source-hook half: one {@link LocalSourceHookPolicy} per rule the runtime request names. */\nclass LocalSourceRuleRuntime implements HookRuleRuntime {\n constructor(private readonly pack: LocalRulePack) {}\n\n create(request: HookPolicyRuntimeRequest): readonly Rule[] {\n const configs = new LocalRuleConfigs(request.configs);\n return request.ruleIds.map((id: string) => {\n const rule = this.pack.ruleFor(id);\n return new LocalSourceHookPolicy(configs.configFor(rule), rule);\n });\n }\n}\n\n/** The build half: one {@link LocalBuildPolicy} per rule the runtime request names. */\nclass LocalBuildRuleRuntime implements BuildRuleRuntime {\n constructor(private readonly pack: LocalRulePack) {}\n\n async create(request: PolicyRuntimeRequest): Promise<readonly BuildPolicy[]> {\n const configs = new LocalRuleConfigs(request.configs);\n return request.ruleIds.map((id: string) => {\n const rule = this.pack.ruleFor(id);\n return new LocalBuildPolicy(configs.configFor(rule), rule, request.excludePaths, new GateScanScope());\n });\n }\n}\n\n/**\n * The one-rule DEBUG run of a local rule (#1330) — `wp-validate-code --rule=<id> --mode=RUN_EVERY_TIME\n * --projects=a,b` — with the same refusals, banner, in-memory override and per-project count a built-in\n * code rule gets ({@link RuleDebugRun}). The rule runs through the same {@link LocalBuildPolicy} the gate\n * runs, so disables, the repo's `excludePaths` and the rule's own apply exactly as they do there; only the\n * mode (when `--mode` is given) and the two escape hatches are overridden, in memory. RUN_EVERY_TIME lists\n * every file in the repo and never consults the diff.\n */\nclass LocalBuildDebugRuntime implements BuildDebugRuntime {\n private readonly debug = new RuleDebugRun();\n\n constructor(private readonly pack: LocalRulePack) {}\n\n async run(request: BuildDebugRequest): Promise<BuildPolicyResult> {\n const root = request.workspaceRoot;\n const asked = new RunRequestParser().parse(request.selection.rule, request.selection.mode, request.selection.projects);\n const rule = this.pack.ruleFor(request.selection.rule);\n const loaded = loadAndValidate(root);\n const config = new LocalRuleConfigs(loaded.rulesConfig as Record<string, ConfigObject>).configFor(rule);\n const plan = this.debug.plan(root, new DebugAsk(rule.id, asked.mode, config.mode ?? 'OFF', MODIFIED_CODE_MODES, asked.projects));\n this.debug.printBanner(plan);\n const diffScope = new DiffScope();\n const scan = new ScanScope(diffScope, new ProjectCatalog(diffScope), new ScanRestriction(plan.projects));\n const policy = new LocalBuildPolicy(this.debug.overridden(config, plan), rule, loaded.excludePaths.paths, scan);\n const ruleSucceeded = await this.runPrintingFailure(policy, root);\n const sites = this.debug.printCounts(root, plan, scan, ruleSucceeded);\n return new BuildPolicyResult(ruleSucceeded && sites === 0);\n }\n\n /** The rule's own failure text is printed BEFORE the counts, exactly as a built-in rule's is. */\n private async runPrintingFailure(policy: LocalBuildPolicy, root: string): Promise<boolean> {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- the policy's RuleFailError IS the debug run's report; anything else propagates\n try {\n return (await policy.run(root)).success;\n // webpieces-disable no-any-unknown -- a thrown value is narrowed to the policy's RuleFailError, anything else is rethrown\n } catch (err: unknown) {\n const error = toError(err);\n if (!(error instanceof RuleFailError)) throw error;\n console.error(renderRuleFailForHuman(error));\n return false;\n }\n }\n}\n\n/**\n * A repo's OWN rule pack (#1314) — the sixth owner beside webpieces' five, built from {@link LocalRule}s.\n *\n * One module both declares the pack and implements it. It exports the four names the framework loads —\n * `rulePackManifest`, `sourceRuleRuntime`, `buildRuleRuntime` and `buildDebugRuntime` (the one-rule\n * `wp-validate-code --rule=<id>` debug run, #1330) — and every rule is contributed to BOTH runners, the\n * Write/Edit source guard and the build gate, so there is no when-to-run setting:\n *\n * ```ts\n * // webpieces-rules/local-rules.ts\n * import { LocalRulePack } from '@webpieces/webpieces-tooling/local-rules';\n * import { NoTodoCommentRule } from './no-todo-comment';\n *\n * const pack = new LocalRulePack('local-rules', __filename, [new NoTodoCommentRule()]);\n * export const rulePackManifest = pack.rulePackManifest;\n * export const sourceRuleRuntime = pack.sourceRuleRuntime;\n * export const buildRuleRuntime = pack.buildRuleRuntime;\n * export const buildDebugRuntime = pack.buildDebugRuntime;\n * ```\n *\n * declared in `webpieces.config.json` like any pack, with its own owner file:\n * `{ \"package\": \"./webpieces-rules/local-rules.ts\", \"config\": \".webpieces-config/rules/local.json\" }`.\n * A `.ts` module is transpiled in memory when it is loaded; there is no build step.\n *\n * Each rule's schema is its `settings` plus a required `mode` (the scope vocabulary built-in diff rules\n * use); the registry adds the framework-owned fields. Nothing has a default: a missing entry or value fails\n * the config load naming the owner file and the seed to paste.\n */\nexport class LocalRulePack {\n readonly rulePackManifest: RulePackManifest;\n readonly sourceRuleRuntime: HookRuleRuntime;\n readonly buildRuleRuntime: BuildRuleRuntime;\n readonly buildDebugRuntime: BuildDebugRuntime;\n private readonly rules = new Map<string, LocalRule>();\n\n /**\n * @param packageName the pack's owner name — how the registry and every refusal name it.\n * @param moduleFile the declaring module's own `__filename`: both runtimes are loaded from it.\n * @param rules the pack's rules; ids must be unique kebab-case and not collide with any other pack's.\n */\n constructor(packageName: string, moduleFile: string, rules: readonly LocalRule[]) {\n if (!packageName.trim()) throw new InformAiError('A LocalRulePack needs a nonempty package name, e.g. \"local-rules\".');\n if (!path.isAbsolute(moduleFile))\n throw new InformAiError(`LocalRulePack ${packageName} needs its module's absolute path. Pass __filename as the second argument.`);\n if (rules.length === 0) throw new InformAiError(`LocalRulePack ${packageName} has no rules. Add at least one LocalRule.`);\n for (const rule of rules) this.add(packageName, rule);\n this.rulePackManifest = new RulePackManifest(\n packageName,\n LOCAL_PACK_VERSION,\n RULE_PACK_API_VERSION,\n rules.map((rule: LocalRule) => this.definition(rule)),\n rules.flatMap((rule: LocalRule) => [\n new RuleContribution(rule.id, packageName, 'source-hook', moduleFile),\n new RuleContribution(rule.id, packageName, 'build', moduleFile),\n ]),\n [],\n [],\n );\n this.sourceRuleRuntime = new LocalSourceRuleRuntime(this);\n this.buildRuleRuntime = new LocalBuildRuleRuntime(this);\n this.buildDebugRuntime = new LocalBuildDebugRuntime(this);\n }\n\n /** The rule registered under `id`; the runtimes only ever ask for ids this pack's manifest declares. */\n ruleFor(id: string): LocalRule {\n const rule = this.rules.get(id);\n if (!rule) throw new InformAiError(`Local rule pack has no rule ${id}. Repair its rule list.`);\n return rule;\n }\n\n private add(packageName: string, rule: LocalRule): void {\n if (!RULE_ID.test(rule.id))\n throw new InformAiError(`Local rule id \"${rule.id}\" in ${packageName} must be kebab-case (e.g. no-todo-comment).`);\n if (this.rules.has(rule.id)) throw new InformAiError(`Local rule ${rule.id} is declared twice in ${packageName}. Keep one.`);\n for (const key of Object.keys(rule.settings)) {\n if (key === 'mode' || Object.hasOwn(BASE_RULE_SCHEMA, key))\n throw new InformAiError(\n `Local rule ${rule.id} declares the framework-owned setting \"${key}\". Delete it from settings; every local rule gets mode, ${Object.keys(BASE_RULE_SCHEMA).join(', ')}.`,\n );\n }\n this.rules.set(rule.id, rule);\n }\n\n private definition(rule: LocalRule): OwnedRuleDefinition {\n const schema: Record<string, FieldDef> = { ...rule.settings, mode: new FieldDef('string', MODIFIED_CODE_MODES) };\n const seed = Object.assign(new ConfigObject(), rule.seed, { turnOffRuleUntilEpoch: 0, turnOffRuleWhileOnBranch: null });\n return new OwnedRuleDefinition(\n rule.id,\n schema,\n RULE_SCHEMA_API_VERSION,\n {},\n seed,\n 'rules',\n new RuleHelp(rule.description, `Fix the reported site, or set ${rule.id}.mode in {configFile}.`),\n );\n }\n}\n"]}