@webpieces/webpieces-tooling 0.4.943 → 0.4.945
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 +91 -0
- package/package.json +15 -14
- package/src/local-rules/index.d.ts +9 -0
- package/src/local-rules/index.js +31 -0
- package/src/local-rules/index.js.map +1 -0
- package/src/local-rules/local-build-policy.d.ts +33 -0
- package/src/local-rules/local-build-policy.js +85 -0
- package/src/local-rules/local-build-policy.js.map +1 -0
- package/src/local-rules/local-rule-judge.d.ts +33 -0
- package/src/local-rules/local-rule-judge.js +77 -0
- package/src/local-rules/local-rule-judge.js.map +1 -0
- package/src/local-rules/local-rule-pack.d.ts +45 -0
- package/src/local-rules/local-rule-pack.js +132 -0
- package/src/local-rules/local-rule-pack.js.map +1 -0
- package/src/local-rules/local-rule.d.ts +86 -0
- package/src/local-rules/local-rule.js +84 -0
- package/src/local-rules/local-rule.js.map +1 -0
- package/src/local-rules/local-source-hook-policy.d.ts +18 -0
- package/src/local-rules/local-source-hook-policy.js +37 -0
- package/src/local-rules/local-source-hook-policy.js.map +1 -0
package/README.md
CHANGED
|
@@ -11,3 +11,94 @@ This package owns packaging and composition smoke checks. Feature suites remain
|
|
|
11
11
|
The repository itself is validated with its previously published catalog release. Adding this source package does not upgrade that installed toolchain or its committed dispatcher.
|
|
12
12
|
|
|
13
13
|
The ordinary test target runs the small aggregation and OpenClaw checks. Run `pnpm nx run webpieces-tooling:release-test` when changing `scripts/publish-packages.sh`; it exercises the release retry loop in a fake registry workspace and is kept separate from source-rule edit workloads.
|
|
14
|
+
|
|
15
|
+
## Repo-local rules
|
|
16
|
+
|
|
17
|
+
A repository can add rules of its own beside webpieces' built-in ones. A local rule is one TypeScript
|
|
18
|
+
class. webpieces runs it in BOTH places, with no setting choosing between them: the Write/Edit source
|
|
19
|
+
guard refuses the edit as it is written, and `wp-build` / the PR gate (`validate-code`) checks the
|
|
20
|
+
committed diff. A repository that does not want the write-time check turns the source guard off with its
|
|
21
|
+
existing switches. The two runners parse the same whole-file text with the same `SourceSyntax`, so they
|
|
22
|
+
report the same sites at the same lines.
|
|
23
|
+
|
|
24
|
+
A rule decides from the file's **syntax** only. The guard sees one proposed file and no compiler program,
|
|
25
|
+
so a rule that needs type information cannot give the same verdict in both places.
|
|
26
|
+
|
|
27
|
+
**1. Write the rule.** Import everything from `@webpieces/webpieces-tooling/local-rules`, including `ts`.
|
|
28
|
+
Walk the parse with that `ts`, never a `typescript` of your own, because node kinds differ between
|
|
29
|
+
TypeScript releases.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// webpieces-rules/no-banned-console.ts
|
|
33
|
+
import { ConfigObject, FieldDef, LocalRule, LocalRuleConfig, LocalRuleFile, LocalRuleFinding, Option, ts }
|
|
34
|
+
from '@webpieces/webpieces-tooling/local-rules';
|
|
35
|
+
|
|
36
|
+
export class NoBannedConsoleConfig extends LocalRuleConfig {
|
|
37
|
+
methods!: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class NoBannedConsoleRule extends LocalRule<NoBannedConsoleConfig> {
|
|
41
|
+
readonly id = 'no-banned-console';
|
|
42
|
+
readonly description = 'Production code must not call the banned console methods.';
|
|
43
|
+
readonly files = ['**/*.ts'];
|
|
44
|
+
readonly cures = [new Option('Throw a structured error, or log through the repo logger.', true)];
|
|
45
|
+
readonly settings = { methods: FieldDef.nonEmptyStrings() };
|
|
46
|
+
readonly seed = Object.assign(new ConfigObject(), { mode: 'NEW_AND_MODIFIED_CODE', methods: ['log'] });
|
|
47
|
+
|
|
48
|
+
check(file: LocalRuleFile, config: NoBannedConsoleConfig): readonly LocalRuleFinding[] {
|
|
49
|
+
const findings: LocalRuleFinding[] = [];
|
|
50
|
+
const visit = (node: ts.Node): void => {
|
|
51
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)
|
|
52
|
+
&& node.expression.expression.getText() === 'console'
|
|
53
|
+
&& config.methods.includes(node.expression.name.text)) {
|
|
54
|
+
findings.push(new LocalRuleFinding(file.lineOf(node), file.endLineOf(node),
|
|
55
|
+
`console.${node.expression.name.text}() is banned here.`));
|
|
56
|
+
}
|
|
57
|
+
ts.forEachChild(node, visit);
|
|
58
|
+
};
|
|
59
|
+
visit(file.source);
|
|
60
|
+
return findings;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**2. Make it a pack.** One module declares the pack and implements both runners:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// webpieces-rules/local-rules.ts
|
|
69
|
+
import { LocalRulePack } from '@webpieces/webpieces-tooling/local-rules';
|
|
70
|
+
import { NoBannedConsoleRule } from './no-banned-console';
|
|
71
|
+
|
|
72
|
+
const pack = new LocalRulePack('local-rules', __filename, [new NoBannedConsoleRule()]);
|
|
73
|
+
export const rulePackManifest = pack.rulePackManifest;
|
|
74
|
+
export const sourceRuleRuntime = pack.sourceRuleRuntime;
|
|
75
|
+
export const buildRuleRuntime = pack.buildRuleRuntime;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**3. Register it** in `webpieces.config.json`, beside the packs you already declare, with its own owner file:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{ "package": "./webpieces-rules/local-rules.ts", "config": ".webpieces-config/rules/local.json" }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The `.ts` module is transpiled in memory when it loads, so there is no build step. The next tool call
|
|
85
|
+
refuses the missing owner entry and prints the rule's seed to paste:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"no-banned-console": {
|
|
90
|
+
"mode": "NEW_AND_MODIFIED_CODE",
|
|
91
|
+
"methods": ["log"],
|
|
92
|
+
"turnOffRuleUntilEpoch": 0,
|
|
93
|
+
"turnOffRuleWhileOnBranch": null
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Nothing has a default. You must state `mode` and every setting the rule declares. `mode` takes the values
|
|
99
|
+
the built-in diff rules take: `OFF`, `NEW_AND_MODIFIED_CODE` (only findings on changed lines),
|
|
100
|
+
`NEW_AND_MODIFIED_FILES`, `MODIFIED_PROJECTS` and `RUN_EVERY_TIME`. The framework applies the rest in both
|
|
101
|
+
runners: `turnOffRuleUntilEpoch`, `turnOffRuleWhileOnBranch`, the optional `excludePaths` and `reason`, and
|
|
102
|
+
the per-site escape `// webpieces-disable no-banned-console -- <reason of at least 20 characters>` on the
|
|
103
|
+
reported line or the line above it. A disable with no reason does not exempt the site. Rule ids must be
|
|
104
|
+
kebab-case and must not collide with any other pack's.
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/webpieces-tooling",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.945",
|
|
4
4
|
"description": "Complete webpieces tooling installation and the combined OpenClaw hook adapter.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./src/index.js",
|
|
9
9
|
"./package.json": "./package.json",
|
|
10
|
-
"./openclaw-plugin": "./src/adapters/openclaw-plugin.js"
|
|
10
|
+
"./openclaw-plugin": "./src/adapters/openclaw-plugin.js",
|
|
11
|
+
"./local-rules": "./src/local-rules/index.js"
|
|
11
12
|
},
|
|
12
13
|
"files": [
|
|
13
14
|
"src/**/*",
|
|
@@ -15,17 +16,17 @@
|
|
|
15
16
|
"README.md"
|
|
16
17
|
],
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@webpieces/agent-workflow-rules": "0.4.
|
|
19
|
-
"@webpieces/ai-hook-rules": "0.4.
|
|
20
|
-
"@webpieces/code-rules": "0.4.
|
|
21
|
-
"@webpieces/eslint-rules": "0.4.
|
|
22
|
-
"@webpieces/hook-runtime": "0.4.
|
|
23
|
-
"@webpieces/nx-webpieces-rules": "0.4.
|
|
24
|
-
"@webpieces/pr-gate": "0.4.
|
|
25
|
-
"@webpieces/repo-workflow-core": "0.4.
|
|
26
|
-
"@webpieces/rules-config": "0.4.
|
|
27
|
-
"@webpieces/tooling-common": "0.4.
|
|
28
|
-
"@webpieces/rules-sdk": "0.4.
|
|
19
|
+
"@webpieces/agent-workflow-rules": "0.4.945",
|
|
20
|
+
"@webpieces/ai-hook-rules": "0.4.945",
|
|
21
|
+
"@webpieces/code-rules": "0.4.945",
|
|
22
|
+
"@webpieces/eslint-rules": "0.4.945",
|
|
23
|
+
"@webpieces/hook-runtime": "0.4.945",
|
|
24
|
+
"@webpieces/nx-webpieces-rules": "0.4.945",
|
|
25
|
+
"@webpieces/pr-gate": "0.4.945",
|
|
26
|
+
"@webpieces/repo-workflow-core": "0.4.945",
|
|
27
|
+
"@webpieces/rules-config": "0.4.945",
|
|
28
|
+
"@webpieces/tooling-common": "0.4.945",
|
|
29
|
+
"@webpieces/rules-sdk": "0.4.945"
|
|
29
30
|
},
|
|
30
31
|
"author": "Dean Hiller",
|
|
31
32
|
"license": "Apache-2.0",
|
|
@@ -38,7 +39,7 @@
|
|
|
38
39
|
"access": "public"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@webpieces/tooling-testkit": "0.4.
|
|
42
|
+
"@webpieces/tooling-testkit": "0.4.945"
|
|
42
43
|
},
|
|
43
44
|
"types": "./src/index.d.ts"
|
|
44
45
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { LocalRule, LocalRuleConfig, LocalRuleFile, LocalRuleFinding } from './local-rule';
|
|
2
|
+
export { LocalRulePack } from './local-rule-pack';
|
|
3
|
+
export { LocalRuleJudge, LocalRuleVerdict } from './local-rule-judge';
|
|
4
|
+
export { LocalSourceHookPolicy } from './local-source-hook-policy';
|
|
5
|
+
export { LocalBuildPolicy, LocatedLocalVerdict } from './local-build-policy';
|
|
6
|
+
export { FieldDef, ConfigObject, MODIFIED_CODE_MODES } from '@webpieces/rules-sdk';
|
|
7
|
+
export type { ModifiedCodeMode } from '@webpieces/rules-sdk';
|
|
8
|
+
export { Option } from '@webpieces/rules-config';
|
|
9
|
+
export { ts, MIN_DISABLE_REASON } from '@webpieces/tooling-common/source-syntax';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MIN_DISABLE_REASON = exports.ts = exports.Option = exports.MODIFIED_CODE_MODES = exports.ConfigObject = exports.FieldDef = exports.LocatedLocalVerdict = exports.LocalBuildPolicy = exports.LocalSourceHookPolicy = exports.LocalRuleVerdict = exports.LocalRuleJudge = exports.LocalRulePack = exports.LocalRuleFinding = exports.LocalRuleFile = exports.LocalRuleConfig = exports.LocalRule = void 0;
|
|
4
|
+
// `@webpieces/webpieces-tooling/local-rules` — the typed API a consumer repo writes its OWN rules with
|
|
5
|
+
// (#1314). Everything a local rule needs is exported here, so the repo depends on this one package and
|
|
6
|
+
// never pins a webpieces child package or a `typescript` of its own to walk the parse.
|
|
7
|
+
var local_rule_1 = require("./local-rule");
|
|
8
|
+
Object.defineProperty(exports, "LocalRule", { enumerable: true, get: function () { return local_rule_1.LocalRule; } });
|
|
9
|
+
Object.defineProperty(exports, "LocalRuleConfig", { enumerable: true, get: function () { return local_rule_1.LocalRuleConfig; } });
|
|
10
|
+
Object.defineProperty(exports, "LocalRuleFile", { enumerable: true, get: function () { return local_rule_1.LocalRuleFile; } });
|
|
11
|
+
Object.defineProperty(exports, "LocalRuleFinding", { enumerable: true, get: function () { return local_rule_1.LocalRuleFinding; } });
|
|
12
|
+
var local_rule_pack_1 = require("./local-rule-pack");
|
|
13
|
+
Object.defineProperty(exports, "LocalRulePack", { enumerable: true, get: function () { return local_rule_pack_1.LocalRulePack; } });
|
|
14
|
+
var local_rule_judge_1 = require("./local-rule-judge");
|
|
15
|
+
Object.defineProperty(exports, "LocalRuleJudge", { enumerable: true, get: function () { return local_rule_judge_1.LocalRuleJudge; } });
|
|
16
|
+
Object.defineProperty(exports, "LocalRuleVerdict", { enumerable: true, get: function () { return local_rule_judge_1.LocalRuleVerdict; } });
|
|
17
|
+
var local_source_hook_policy_1 = require("./local-source-hook-policy");
|
|
18
|
+
Object.defineProperty(exports, "LocalSourceHookPolicy", { enumerable: true, get: function () { return local_source_hook_policy_1.LocalSourceHookPolicy; } });
|
|
19
|
+
var local_build_policy_1 = require("./local-build-policy");
|
|
20
|
+
Object.defineProperty(exports, "LocalBuildPolicy", { enumerable: true, get: function () { return local_build_policy_1.LocalBuildPolicy; } });
|
|
21
|
+
Object.defineProperty(exports, "LocatedLocalVerdict", { enumerable: true, get: function () { return local_build_policy_1.LocatedLocalVerdict; } });
|
|
22
|
+
var rules_sdk_1 = require("@webpieces/rules-sdk");
|
|
23
|
+
Object.defineProperty(exports, "FieldDef", { enumerable: true, get: function () { return rules_sdk_1.FieldDef; } });
|
|
24
|
+
Object.defineProperty(exports, "ConfigObject", { enumerable: true, get: function () { return rules_sdk_1.ConfigObject; } });
|
|
25
|
+
Object.defineProperty(exports, "MODIFIED_CODE_MODES", { enumerable: true, get: function () { return rules_sdk_1.MODIFIED_CODE_MODES; } });
|
|
26
|
+
var rules_config_1 = require("@webpieces/rules-config");
|
|
27
|
+
Object.defineProperty(exports, "Option", { enumerable: true, get: function () { return rules_config_1.Option; } });
|
|
28
|
+
var source_syntax_1 = require("@webpieces/tooling-common/source-syntax");
|
|
29
|
+
Object.defineProperty(exports, "ts", { enumerable: true, get: function () { return source_syntax_1.ts; } });
|
|
30
|
+
Object.defineProperty(exports, "MIN_DISABLE_REASON", { enumerable: true, get: function () { return source_syntax_1.MIN_DISABLE_REASON; } });
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/index.ts"],"names":[],"mappings":";;;AAAA,uGAAuG;AACvG,uGAAuG;AACvG,uFAAuF;AACvF,2CAA2F;AAAlF,uGAAA,SAAS,OAAA;AAAE,6GAAA,eAAe,OAAA;AAAE,2GAAA,aAAa,OAAA;AAAE,8GAAA,gBAAgB,OAAA;AACpE,qDAAkD;AAAzC,gHAAA,aAAa,OAAA;AACtB,uDAAsE;AAA7D,kHAAA,cAAc,OAAA;AAAE,oHAAA,gBAAgB,OAAA;AACzC,uEAAmE;AAA1D,iIAAA,qBAAqB,OAAA;AAC9B,2DAA6E;AAApE,sHAAA,gBAAgB,OAAA;AAAE,yHAAA,mBAAmB,OAAA;AAC9C,kDAAmF;AAA1E,qGAAA,QAAQ,OAAA;AAAE,yGAAA,YAAY,OAAA;AAAE,gHAAA,mBAAmB,OAAA;AAEpD,wDAAiD;AAAxC,sGAAA,MAAM,OAAA;AACf,yEAAiF;AAAxE,mGAAA,EAAE,OAAA;AAAE,mHAAA,kBAAkB,OAAA","sourcesContent":["// `@webpieces/webpieces-tooling/local-rules` — the typed API a consumer repo writes its OWN rules with\n// (#1314). Everything a local rule needs is exported here, so the repo depends on this one package and\n// never pins a webpieces child package or a `typescript` of its own to walk the parse.\nexport { LocalRule, LocalRuleConfig, LocalRuleFile, LocalRuleFinding } from './local-rule';\nexport { LocalRulePack } from './local-rule-pack';\nexport { LocalRuleJudge, LocalRuleVerdict } from './local-rule-judge';\nexport { LocalSourceHookPolicy } from './local-source-hook-policy';\nexport { LocalBuildPolicy, LocatedLocalVerdict } from './local-build-policy';\nexport { FieldDef, ConfigObject, MODIFIED_CODE_MODES } from '@webpieces/rules-sdk';\nexport type { ModifiedCodeMode } from '@webpieces/rules-sdk';\nexport { Option } from '@webpieces/rules-config';\nexport { ts, MIN_DISABLE_REASON } from '@webpieces/tooling-common/source-syntax';\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BuildPolicy, BuildPolicyResult } from '@webpieces/rules-sdk';
|
|
2
|
+
import { AbstractRule } from '@webpieces/rules-config';
|
|
3
|
+
import { LocalRule, LocalRuleConfig } from './local-rule';
|
|
4
|
+
import { LocalRuleVerdict } from './local-rule-judge';
|
|
5
|
+
/** A verdict located in its workspace-relative file. Data-only. */
|
|
6
|
+
export declare class LocatedLocalVerdict {
|
|
7
|
+
readonly relFile: string;
|
|
8
|
+
readonly verdict: LocalRuleVerdict;
|
|
9
|
+
constructor(relFile: string, verdict: LocalRuleVerdict);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A local rule at the build gate (`wp-build` / the PR gate, through `validate-code`): the backstop for a
|
|
13
|
+
* repo that does not run the guard hooks. It reads each in-scope file WHOLE and calls the same
|
|
14
|
+
* {@link LocalRuleJudge} the Write/Edit guard calls, so both runners report the same sites at the same lines.
|
|
15
|
+
*
|
|
16
|
+
* The file set follows `mode` exactly as the built-in rules do (`ScanScope`): NEW_AND_MODIFIED_CODE judges
|
|
17
|
+
* only findings on changed lines of changed files, NEW_AND_MODIFIED_FILES every finding in a changed file,
|
|
18
|
+
* MODIFIED_PROJECTS / RUN_EVERY_TIME every file of each touched project / of the repo. The repo's
|
|
19
|
+
* top-level `excludePaths` and the rule's own are applied to that listing; OFF and the epoch and branch
|
|
20
|
+
* switches are checked by `shouldRun()` here and again by the build runner. A failure is ONE thrown
|
|
21
|
+
* `RuleFailError` listing every site, rendered by the build runner.
|
|
22
|
+
*/
|
|
23
|
+
export declare class LocalBuildPolicy extends AbstractRule<LocalRuleConfig> implements BuildPolicy {
|
|
24
|
+
private readonly rule;
|
|
25
|
+
private readonly globalExcludePaths;
|
|
26
|
+
private readonly judge;
|
|
27
|
+
private readonly diffScope;
|
|
28
|
+
constructor(config: LocalRuleConfig, rule: LocalRule, globalExcludePaths: readonly string[]);
|
|
29
|
+
run(workspaceRoot: string): Promise<BuildPolicyResult>;
|
|
30
|
+
/** undefined when the diff base cannot be found, so a diff-scoped mode has nothing to compare against. */
|
|
31
|
+
private scan;
|
|
32
|
+
private failure;
|
|
33
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalBuildPolicy = exports.LocatedLocalVerdict = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = tslib_1.__importStar(require("node:fs"));
|
|
6
|
+
const path = tslib_1.__importStar(require("node:path"));
|
|
7
|
+
const rules_sdk_1 = require("@webpieces/rules-sdk");
|
|
8
|
+
const rules_config_1 = require("@webpieces/rules-config");
|
|
9
|
+
const code_rules_1 = require("@webpieces/code-rules");
|
|
10
|
+
const local_rule_judge_1 = require("./local-rule-judge");
|
|
11
|
+
/** A verdict located in its workspace-relative file. Data-only. */
|
|
12
|
+
class LocatedLocalVerdict {
|
|
13
|
+
relFile;
|
|
14
|
+
verdict;
|
|
15
|
+
constructor(relFile, verdict) {
|
|
16
|
+
this.relFile = relFile;
|
|
17
|
+
this.verdict = verdict;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.LocatedLocalVerdict = LocatedLocalVerdict;
|
|
21
|
+
/**
|
|
22
|
+
* A local rule at the build gate (`wp-build` / the PR gate, through `validate-code`): the backstop for a
|
|
23
|
+
* repo that does not run the guard hooks. It reads each in-scope file WHOLE and calls the same
|
|
24
|
+
* {@link LocalRuleJudge} the Write/Edit guard calls, so both runners report the same sites at the same lines.
|
|
25
|
+
*
|
|
26
|
+
* The file set follows `mode` exactly as the built-in rules do (`ScanScope`): NEW_AND_MODIFIED_CODE judges
|
|
27
|
+
* only findings on changed lines of changed files, NEW_AND_MODIFIED_FILES every finding in a changed file,
|
|
28
|
+
* MODIFIED_PROJECTS / RUN_EVERY_TIME every file of each touched project / of the repo. The repo's
|
|
29
|
+
* top-level `excludePaths` and the rule's own are applied to that listing; OFF and the epoch and branch
|
|
30
|
+
* switches are checked by `shouldRun()` here and again by the build runner. A failure is ONE thrown
|
|
31
|
+
* `RuleFailError` listing every site, rendered by the build runner.
|
|
32
|
+
*/
|
|
33
|
+
class LocalBuildPolicy extends rules_config_1.AbstractRule {
|
|
34
|
+
rule;
|
|
35
|
+
globalExcludePaths;
|
|
36
|
+
judge;
|
|
37
|
+
diffScope = new rules_config_1.DiffScope();
|
|
38
|
+
constructor(config, rule, globalExcludePaths) {
|
|
39
|
+
super(config, rule.id, rule.id);
|
|
40
|
+
this.rule = rule;
|
|
41
|
+
this.globalExcludePaths = globalExcludePaths;
|
|
42
|
+
this.judge = new local_rule_judge_1.LocalRuleJudge(rule);
|
|
43
|
+
}
|
|
44
|
+
async run(workspaceRoot) {
|
|
45
|
+
const found = await new rules_config_1.RuleFileScope().runWithin(this.fileExclusions(this.globalExcludePaths), async () => this.scan(workspaceRoot));
|
|
46
|
+
if (found === undefined || found.length === 0)
|
|
47
|
+
return new rules_sdk_1.BuildPolicyResult(true);
|
|
48
|
+
throw this.failure(found);
|
|
49
|
+
}
|
|
50
|
+
/** undefined when the diff base cannot be found, so a diff-scoped mode has nothing to compare against. */
|
|
51
|
+
scan(workspaceRoot) {
|
|
52
|
+
const mode = this.config.mode;
|
|
53
|
+
if (mode === undefined || mode === 'OFF')
|
|
54
|
+
return [];
|
|
55
|
+
const range = this.diffScope.resolveBase(workspaceRoot);
|
|
56
|
+
if (range.base === undefined && mode !== 'RUN_EVERY_TIME') {
|
|
57
|
+
console.log(`\n⏭️ Skipping ${this.name} validation (could not detect base branch)\n`);
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const base = range.base ?? '';
|
|
61
|
+
const found = [];
|
|
62
|
+
for (const relFile of new code_rules_1.GateScanScope().files(workspaceRoot, mode, base, range.head, { tsOnly: false })) {
|
|
63
|
+
const absFile = path.join(workspaceRoot, relFile);
|
|
64
|
+
if (!this.judge.judges(relFile) || !fs.existsSync(absFile))
|
|
65
|
+
continue;
|
|
66
|
+
const changed = mode === 'NEW_AND_MODIFIED_CODE'
|
|
67
|
+
? this.diffScope.getChangedLineNumbers(this.diffScope.getFileDiff(workspaceRoot, relFile, base, range.head))
|
|
68
|
+
: undefined;
|
|
69
|
+
if (changed !== undefined && changed.size === 0)
|
|
70
|
+
continue;
|
|
71
|
+
for (const verdict of this.judge.judge(relFile, fs.readFileSync(absFile, 'utf8'), this.config, changed)) {
|
|
72
|
+
found.push(new LocatedLocalVerdict(relFile, verdict));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return found;
|
|
76
|
+
}
|
|
77
|
+
failure(found) {
|
|
78
|
+
const message = `${found.length} site(s) break the repo-local rule ${this.name} — ${this.rule.description}\n` +
|
|
79
|
+
found.map((each) => ` ${each.relFile}:${each.verdict.line} — ${each.verdict.message}\n ${each.verdict.snippet}`).join('\n') +
|
|
80
|
+
`\n Last resort, per site: ${this.judge.disableComment()}`;
|
|
81
|
+
return new rules_config_1.RuleFailError(this.name, message, undefined, undefined, this.rule.cures);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.LocalBuildPolicy = LocalBuildPolicy;
|
|
85
|
+
//# sourceMappingURL=local-build-policy.js.map
|
|
@@ -0,0 +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;AAChG,sDAAsD;AAEtD,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;;;;;;;;;;;GAWG;AACH,MAAa,gBAAiB,SAAQ,2BAA6B;IAM1C;IACA;IANJ,KAAK,CAAiB;IACtB,SAAS,GAAG,IAAI,wBAAS,EAAE,CAAC;IAE7C,YACI,MAAuB,EACN,IAAe,EACf,kBAAqC;QAEtD,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,I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,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"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LocalRule, LocalRuleConfig } from './local-rule';
|
|
2
|
+
/** One judged site, located and ready for either runner to render. Data-only. */
|
|
3
|
+
export declare class LocalRuleVerdict {
|
|
4
|
+
readonly line: number;
|
|
5
|
+
/** The reported line's source text, trimmed. */
|
|
6
|
+
readonly snippet: string;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
constructor(line: number,
|
|
9
|
+
/** The reported line's source text, trimmed. */
|
|
10
|
+
snippet: string, message: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The ONE place a local rule's findings become verdicts, called by BOTH runners with the same whole-file
|
|
14
|
+
* text — which is what makes the Write/Edit guard and the build gate agree, line for line.
|
|
15
|
+
*
|
|
16
|
+
* It owns everything the rule does not: which files the rule judges, the NEW_AND_MODIFIED_CODE scoping
|
|
17
|
+
* (a finding is kept when any line from `line` to `endLine` changed) and the per-site disable, which needs
|
|
18
|
+
* a written reason of at least {@link MIN_DISABLE_REASON} characters to exempt a site.
|
|
19
|
+
*/
|
|
20
|
+
export declare class LocalRuleJudge {
|
|
21
|
+
private readonly rule;
|
|
22
|
+
constructor(rule: LocalRule);
|
|
23
|
+
/** The per-site escape both runners print. */
|
|
24
|
+
disableComment(): string;
|
|
25
|
+
/** Does the rule judge this workspace-relative path at all? */
|
|
26
|
+
judges(relativePath: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The verdicts for one file's whole `text`. `changedLines` (1-based) is given only under
|
|
29
|
+
* NEW_AND_MODIFIED_CODE; every other active mode judges the whole file.
|
|
30
|
+
*/
|
|
31
|
+
judge(relativePath: string, text: string, config: LocalRuleConfig, changedLines?: ReadonlySet<number>): LocalRuleVerdict[];
|
|
32
|
+
private inScope;
|
|
33
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalRuleJudge = exports.LocalRuleVerdict = void 0;
|
|
4
|
+
const rules_config_1 = require("@webpieces/rules-config");
|
|
5
|
+
const source_syntax_1 = require("@webpieces/tooling-common/source-syntax");
|
|
6
|
+
const local_rule_1 = require("./local-rule");
|
|
7
|
+
/** One judged site, located and ready for either runner to render. Data-only. */
|
|
8
|
+
class LocalRuleVerdict {
|
|
9
|
+
line;
|
|
10
|
+
snippet;
|
|
11
|
+
message;
|
|
12
|
+
constructor(line,
|
|
13
|
+
/** The reported line's source text, trimmed. */
|
|
14
|
+
snippet, message) {
|
|
15
|
+
this.line = line;
|
|
16
|
+
this.snippet = snippet;
|
|
17
|
+
this.message = message;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.LocalRuleVerdict = LocalRuleVerdict;
|
|
21
|
+
/**
|
|
22
|
+
* The ONE place a local rule's findings become verdicts, called by BOTH runners with the same whole-file
|
|
23
|
+
* text — which is what makes the Write/Edit guard and the build gate agree, line for line.
|
|
24
|
+
*
|
|
25
|
+
* It owns everything the rule does not: which files the rule judges, the NEW_AND_MODIFIED_CODE scoping
|
|
26
|
+
* (a finding is kept when any line from `line` to `endLine` changed) and the per-site disable, which needs
|
|
27
|
+
* a written reason of at least {@link MIN_DISABLE_REASON} characters to exempt a site.
|
|
28
|
+
*/
|
|
29
|
+
class LocalRuleJudge {
|
|
30
|
+
rule;
|
|
31
|
+
constructor(rule) {
|
|
32
|
+
this.rule = rule;
|
|
33
|
+
}
|
|
34
|
+
/** The per-site escape both runners print. */
|
|
35
|
+
disableComment() {
|
|
36
|
+
return `// webpieces-disable ${this.rule.id} -- <why this one site may break the rule, at least ${source_syntax_1.MIN_DISABLE_REASON} characters>`;
|
|
37
|
+
}
|
|
38
|
+
/** Does the rule judge this workspace-relative path at all? */
|
|
39
|
+
judges(relativePath) {
|
|
40
|
+
const normalized = relativePath.split('\\').join('/');
|
|
41
|
+
return this.rule.files.some((glob) => (0, rules_config_1.globMatches)(glob, normalized));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The verdicts for one file's whole `text`. `changedLines` (1-based) is given only under
|
|
45
|
+
* NEW_AND_MODIFIED_CODE; every other active mode judges the whole file.
|
|
46
|
+
*/
|
|
47
|
+
judge(relativePath, text, config, changedLines) {
|
|
48
|
+
const file = new local_rule_1.LocalRuleFile(relativePath.split('\\').join('/'), new source_syntax_1.SourceSyntax(relativePath, text));
|
|
49
|
+
const lines = text.split('\n');
|
|
50
|
+
const disables = new source_syntax_1.SiteDisables(text, this.rule.id);
|
|
51
|
+
const verdicts = [];
|
|
52
|
+
for (const finding of this.rule.check(file, config)) {
|
|
53
|
+
if (!this.inScope(finding, changedLines))
|
|
54
|
+
continue;
|
|
55
|
+
const reason = disables.reasonAt(finding.line);
|
|
56
|
+
if (reason !== undefined && reason.length >= source_syntax_1.MIN_DISABLE_REASON)
|
|
57
|
+
continue;
|
|
58
|
+
const message = reason === undefined
|
|
59
|
+
? finding.message
|
|
60
|
+
: `${finding.message} The \`// webpieces-disable ${this.rule.id}\` aimed at it does not exempt it: it needs a ` +
|
|
61
|
+
`written reason after \` -- \` of at least ${source_syntax_1.MIN_DISABLE_REASON} characters, the only record of why this one site may break the rule.`;
|
|
62
|
+
verdicts.push(new LocalRuleVerdict(finding.line, (lines[finding.line - 1] ?? '').trim(), message));
|
|
63
|
+
}
|
|
64
|
+
return verdicts;
|
|
65
|
+
}
|
|
66
|
+
inScope(finding, changedLines) {
|
|
67
|
+
if (changedLines === undefined)
|
|
68
|
+
return true;
|
|
69
|
+
for (let line = finding.line; line <= Math.max(finding.line, finding.endLine); line++) {
|
|
70
|
+
if (changedLines.has(line))
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.LocalRuleJudge = LocalRuleJudge;
|
|
77
|
+
//# sourceMappingURL=local-rule-judge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-rule-judge.js","sourceRoot":"","sources":["../../../../../../packages/t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CAAC;IACjB,CAAC;CACJ;AA3CD,wCA2CC","sourcesContent":["import { globMatches } from '@webpieces/rules-config';\nimport { MIN_DISABLE_REASON, SiteDisables, SourceSyntax } from '@webpieces/tooling-common/source-syntax';\nimport { LocalRule, LocalRuleConfig, LocalRuleFile, LocalRuleFinding } from './local-rule';\n\n/** One judged site, located and ready for either runner to render. Data-only. */\nexport class LocalRuleVerdict {\n constructor(\n readonly line: number,\n /** The reported line's source text, trimmed. */\n readonly snippet: string,\n readonly message: string,\n ) {}\n}\n\n/**\n * The ONE place a local rule's findings become verdicts, called by BOTH runners with the same whole-file\n * text — which is what makes the Write/Edit guard and the build gate agree, line for line.\n *\n * It owns everything the rule does not: which files the rule judges, the NEW_AND_MODIFIED_CODE scoping\n * (a finding is kept when any line from `line` to `endLine` changed) and the per-site disable, which needs\n * a written reason of at least {@link MIN_DISABLE_REASON} characters to exempt a site.\n */\nexport class LocalRuleJudge {\n constructor(private readonly rule: LocalRule) {}\n\n /** The per-site escape both runners print. */\n disableComment(): string {\n return `// webpieces-disable ${this.rule.id} -- <why this one site may break the rule, at least ${MIN_DISABLE_REASON} characters>`;\n }\n\n /** Does the rule judge this workspace-relative path at all? */\n judges(relativePath: string): boolean {\n const normalized = relativePath.split('\\\\').join('/');\n return this.rule.files.some((glob: string) => globMatches(glob, normalized));\n }\n\n /**\n * The verdicts for one file's whole `text`. `changedLines` (1-based) is given only under\n * NEW_AND_MODIFIED_CODE; every other active mode judges the whole file.\n */\n judge(relativePath: string, text: string, config: LocalRuleConfig, changedLines?: ReadonlySet<number>): LocalRuleVerdict[] {\n const file = new LocalRuleFile(relativePath.split('\\\\').join('/'), new SourceSyntax(relativePath, text));\n const lines = text.split('\\n');\n const disables = new SiteDisables(text, this.rule.id);\n const verdicts: LocalRuleVerdict[] = [];\n for (const finding of this.rule.check(file, config)) {\n if (!this.inScope(finding, changedLines)) continue;\n const reason = disables.reasonAt(finding.line);\n if (reason !== undefined && reason.length >= MIN_DISABLE_REASON) continue;\n const message = reason === undefined\n ? finding.message\n : `${finding.message} The \\`// webpieces-disable ${this.rule.id}\\` aimed at it does not exempt it: it needs a ` +\n `written reason after \\` -- \\` of at least ${MIN_DISABLE_REASON} characters, the only record of why this one site may break the rule.`;\n verdicts.push(new LocalRuleVerdict(finding.line, (lines[finding.line - 1] ?? '').trim(), message));\n }\n return verdicts;\n }\n\n private inScope(finding: LocalRuleFinding, changedLines?: ReadonlySet<number>): boolean {\n if (changedLines === undefined) return true;\n for (let line = finding.line; line <= Math.max(finding.line, finding.endLine); line++) {\n if (changedLines.has(line)) return true;\n }\n return false;\n }\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { BuildRuleRuntime, RulePackManifest } from '@webpieces/rules-sdk';
|
|
2
|
+
import { HookRuleRuntime } from '@webpieces/hook-runtime';
|
|
3
|
+
import { LocalRule } from './local-rule';
|
|
4
|
+
/**
|
|
5
|
+
* A repo's OWN rule pack (#1314) — the sixth owner beside webpieces' five, built from {@link LocalRule}s.
|
|
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:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* // webpieces-rules/local-rules.ts
|
|
13
|
+
* import { LocalRulePack } from '@webpieces/webpieces-tooling/local-rules';
|
|
14
|
+
* import { NoTodoCommentRule } from './no-todo-comment';
|
|
15
|
+
*
|
|
16
|
+
* const pack = new LocalRulePack('local-rules', __filename, [new NoTodoCommentRule()]);
|
|
17
|
+
* export const rulePackManifest = pack.rulePackManifest;
|
|
18
|
+
* export const sourceRuleRuntime = pack.sourceRuleRuntime;
|
|
19
|
+
* export const buildRuleRuntime = pack.buildRuleRuntime;
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* declared in `webpieces.config.json` like any pack, with its own owner file:
|
|
23
|
+
* `{ "package": "./webpieces-rules/local-rules.ts", "config": ".webpieces-config/rules/local.json" }`.
|
|
24
|
+
* A `.ts` module is transpiled in memory when it is loaded; there is no build step.
|
|
25
|
+
*
|
|
26
|
+
* Each rule's schema is its `settings` plus a required `mode` (the scope vocabulary built-in diff rules
|
|
27
|
+
* use); the registry adds the framework-owned fields. Nothing has a default: a missing entry or value fails
|
|
28
|
+
* the config load naming the owner file and the seed to paste.
|
|
29
|
+
*/
|
|
30
|
+
export declare class LocalRulePack {
|
|
31
|
+
readonly rulePackManifest: RulePackManifest;
|
|
32
|
+
readonly sourceRuleRuntime: HookRuleRuntime;
|
|
33
|
+
readonly buildRuleRuntime: BuildRuleRuntime;
|
|
34
|
+
private readonly rules;
|
|
35
|
+
/**
|
|
36
|
+
* @param packageName the pack's owner name — how the registry and every refusal name it.
|
|
37
|
+
* @param moduleFile the declaring module's own `__filename`: both runtimes are loaded from it.
|
|
38
|
+
* @param rules the pack's rules; ids must be unique kebab-case and not collide with any other pack's.
|
|
39
|
+
*/
|
|
40
|
+
constructor(packageName: string, moduleFile: string, rules: readonly LocalRule[]);
|
|
41
|
+
/** The rule registered under `id`; the runtimes only ever ask for ids this pack's manifest declares. */
|
|
42
|
+
ruleFor(id: string): LocalRule;
|
|
43
|
+
private add;
|
|
44
|
+
private definition;
|
|
45
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalRulePack = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path = tslib_1.__importStar(require("node:path"));
|
|
6
|
+
const rules_sdk_1 = require("@webpieces/rules-sdk");
|
|
7
|
+
const rules_sdk_2 = require("@webpieces/rules-sdk");
|
|
8
|
+
const tooling_common_1 = require("@webpieces/tooling-common");
|
|
9
|
+
const local_source_hook_policy_1 = require("./local-source-hook-policy");
|
|
10
|
+
const local_build_policy_1 = require("./local-build-policy");
|
|
11
|
+
const RULE_ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
12
|
+
/** The version a local pack reports: it ships with the repo, never as an npm release. */
|
|
13
|
+
const LOCAL_PACK_VERSION = '0.0.0-local';
|
|
14
|
+
/** Validated owner settings for one local rule, or a refusal naming the owner file to repair. */
|
|
15
|
+
class LocalRuleConfigs {
|
|
16
|
+
configs;
|
|
17
|
+
constructor(configs) {
|
|
18
|
+
this.configs = configs;
|
|
19
|
+
}
|
|
20
|
+
configFor(rule) {
|
|
21
|
+
const config = this.configs[rule.id];
|
|
22
|
+
if (!config)
|
|
23
|
+
throw new tooling_common_1.InformAiError(`Missing explicit config for local rule ${rule.id}. Add its entry to the rule pack's owner file.`);
|
|
24
|
+
return config;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** The source-hook half: one {@link LocalSourceHookPolicy} per rule the runtime request names. */
|
|
28
|
+
class LocalSourceRuleRuntime {
|
|
29
|
+
pack;
|
|
30
|
+
constructor(pack) {
|
|
31
|
+
this.pack = pack;
|
|
32
|
+
}
|
|
33
|
+
create(request) {
|
|
34
|
+
const configs = new LocalRuleConfigs(request.configs);
|
|
35
|
+
return request.ruleIds.map((id) => {
|
|
36
|
+
const rule = this.pack.ruleFor(id);
|
|
37
|
+
return new local_source_hook_policy_1.LocalSourceHookPolicy(configs.configFor(rule), rule);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** The build half: one {@link LocalBuildPolicy} per rule the runtime request names. */
|
|
42
|
+
class LocalBuildRuleRuntime {
|
|
43
|
+
pack;
|
|
44
|
+
constructor(pack) {
|
|
45
|
+
this.pack = pack;
|
|
46
|
+
}
|
|
47
|
+
async create(request) {
|
|
48
|
+
const configs = new LocalRuleConfigs(request.configs);
|
|
49
|
+
return request.ruleIds.map((id) => {
|
|
50
|
+
const rule = this.pack.ruleFor(id);
|
|
51
|
+
return new local_build_policy_1.LocalBuildPolicy(configs.configFor(rule), rule, request.excludePaths);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A repo's OWN rule pack (#1314) — the sixth owner beside webpieces' five, built from {@link LocalRule}s.
|
|
57
|
+
*
|
|
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:
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* // webpieces-rules/local-rules.ts
|
|
64
|
+
* import { LocalRulePack } from '@webpieces/webpieces-tooling/local-rules';
|
|
65
|
+
* import { NoTodoCommentRule } from './no-todo-comment';
|
|
66
|
+
*
|
|
67
|
+
* const pack = new LocalRulePack('local-rules', __filename, [new NoTodoCommentRule()]);
|
|
68
|
+
* export const rulePackManifest = pack.rulePackManifest;
|
|
69
|
+
* export const sourceRuleRuntime = pack.sourceRuleRuntime;
|
|
70
|
+
* export const buildRuleRuntime = pack.buildRuleRuntime;
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* declared in `webpieces.config.json` like any pack, with its own owner file:
|
|
74
|
+
* `{ "package": "./webpieces-rules/local-rules.ts", "config": ".webpieces-config/rules/local.json" }`.
|
|
75
|
+
* A `.ts` module is transpiled in memory when it is loaded; there is no build step.
|
|
76
|
+
*
|
|
77
|
+
* Each rule's schema is its `settings` plus a required `mode` (the scope vocabulary built-in diff rules
|
|
78
|
+
* use); the registry adds the framework-owned fields. Nothing has a default: a missing entry or value fails
|
|
79
|
+
* the config load naming the owner file and the seed to paste.
|
|
80
|
+
*/
|
|
81
|
+
class LocalRulePack {
|
|
82
|
+
rulePackManifest;
|
|
83
|
+
sourceRuleRuntime;
|
|
84
|
+
buildRuleRuntime;
|
|
85
|
+
rules = new Map();
|
|
86
|
+
/**
|
|
87
|
+
* @param packageName the pack's owner name — how the registry and every refusal name it.
|
|
88
|
+
* @param moduleFile the declaring module's own `__filename`: both runtimes are loaded from it.
|
|
89
|
+
* @param rules the pack's rules; ids must be unique kebab-case and not collide with any other pack's.
|
|
90
|
+
*/
|
|
91
|
+
constructor(packageName, moduleFile, rules) {
|
|
92
|
+
if (!packageName.trim())
|
|
93
|
+
throw new tooling_common_1.InformAiError('A LocalRulePack needs a nonempty package name, e.g. "local-rules".');
|
|
94
|
+
if (!path.isAbsolute(moduleFile))
|
|
95
|
+
throw new tooling_common_1.InformAiError(`LocalRulePack ${packageName} needs its module's absolute path. Pass __filename as the second argument.`);
|
|
96
|
+
if (rules.length === 0)
|
|
97
|
+
throw new tooling_common_1.InformAiError(`LocalRulePack ${packageName} has no rules. Add at least one LocalRule.`);
|
|
98
|
+
for (const rule of rules)
|
|
99
|
+
this.add(packageName, rule);
|
|
100
|
+
this.rulePackManifest = new rules_sdk_1.RulePackManifest(packageName, LOCAL_PACK_VERSION, rules_sdk_1.RULE_PACK_API_VERSION, rules.map((rule) => this.definition(rule)), rules.flatMap((rule) => [
|
|
101
|
+
new rules_sdk_1.RuleContribution(rule.id, packageName, 'source-hook', moduleFile),
|
|
102
|
+
new rules_sdk_1.RuleContribution(rule.id, packageName, 'build', moduleFile),
|
|
103
|
+
]), [], []);
|
|
104
|
+
this.sourceRuleRuntime = new LocalSourceRuleRuntime(this);
|
|
105
|
+
this.buildRuleRuntime = new LocalBuildRuleRuntime(this);
|
|
106
|
+
}
|
|
107
|
+
/** The rule registered under `id`; the runtimes only ever ask for ids this pack's manifest declares. */
|
|
108
|
+
ruleFor(id) {
|
|
109
|
+
const rule = this.rules.get(id);
|
|
110
|
+
if (!rule)
|
|
111
|
+
throw new tooling_common_1.InformAiError(`Local rule pack has no rule ${id}. Repair its rule list.`);
|
|
112
|
+
return rule;
|
|
113
|
+
}
|
|
114
|
+
add(packageName, rule) {
|
|
115
|
+
if (!RULE_ID.test(rule.id))
|
|
116
|
+
throw new tooling_common_1.InformAiError(`Local rule id "${rule.id}" in ${packageName} must be kebab-case (e.g. no-todo-comment).`);
|
|
117
|
+
if (this.rules.has(rule.id))
|
|
118
|
+
throw new tooling_common_1.InformAiError(`Local rule ${rule.id} is declared twice in ${packageName}. Keep one.`);
|
|
119
|
+
for (const key of Object.keys(rule.settings)) {
|
|
120
|
+
if (key === 'mode' || Object.hasOwn(rules_sdk_2.BASE_RULE_SCHEMA, key))
|
|
121
|
+
throw new tooling_common_1.InformAiError(`Local rule ${rule.id} declares the framework-owned setting "${key}". Delete it from settings; every local rule gets mode, ${Object.keys(rules_sdk_2.BASE_RULE_SCHEMA).join(', ')}.`);
|
|
122
|
+
}
|
|
123
|
+
this.rules.set(rule.id, rule);
|
|
124
|
+
}
|
|
125
|
+
definition(rule) {
|
|
126
|
+
const schema = { ...rule.settings, mode: new rules_sdk_1.FieldDef('string', rules_sdk_1.MODIFIED_CODE_MODES) };
|
|
127
|
+
const seed = Object.assign(new rules_sdk_1.ConfigObject(), rule.seed, { turnOffRuleUntilEpoch: 0, turnOffRuleWhileOnBranch: null });
|
|
128
|
+
return new rules_sdk_1.OwnedRuleDefinition(rule.id, schema, rules_sdk_1.RULE_SCHEMA_API_VERSION, {}, seed, 'rules', new rules_sdk_1.RuleHelp(rule.description, `Fix the reported site, or set ${rule.id}.mode in {configFile}.`));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.LocalRulePack = LocalRulePack;
|
|
132
|
+
//# sourceMappingURL=local-rule-pack.js.map
|
|
@@ -0,0 +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,yFAAy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AAE,EAAE,IAAI,CAAC,CAAC;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"]}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { BaseRuleConfig, ConfigObject, FieldDef, ModifiedCodeMode } from '@webpieces/rules-sdk';
|
|
2
|
+
import { Option } from '@webpieces/rules-config';
|
|
3
|
+
import { SourceSyntax, ts } from '@webpieces/tooling-common/source-syntax';
|
|
4
|
+
/**
|
|
5
|
+
* The config entry of one repo-local rule (#1314): the pack's owner file maps the rule's id to it.
|
|
6
|
+
*
|
|
7
|
+
* `mode` is the same scope vocabulary `catch-must-propagate` uses — OFF | NEW_AND_MODIFIED_CODE |
|
|
8
|
+
* NEW_AND_MODIFIED_FILES | MODIFIED_PROJECTS | RUN_EVERY_TIME — and, like every field inherited from
|
|
9
|
+
* BaseRuleConfig (`turnOffRuleUntilEpoch`, `turnOffRuleWhileOnBranch`, `excludePaths`, `reason`), it is
|
|
10
|
+
* applied by the FRAMEWORK in both runners; a rule never reads it. A rule with settings of its own extends
|
|
11
|
+
* this class and declares them (`words!: string[]`), next to the FieldDefs in {@link LocalRule.settings}.
|
|
12
|
+
*/
|
|
13
|
+
export declare class LocalRuleConfig extends BaseRuleConfig {
|
|
14
|
+
mode?: ModifiedCodeMode;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The one file a local rule judges: its WHOLE text, parsed once by `SourceSyntax`. It is the same object
|
|
18
|
+
* in both runners — the Write/Edit guard builds it from the proposed post-edit file, the build gate from
|
|
19
|
+
* the committed file — so a rule that decides from this syntax gives the same verdict at the same line in
|
|
20
|
+
* both. Walk `source` with the `ts` exported beside this class, never a `typescript` of your own.
|
|
21
|
+
*/
|
|
22
|
+
export declare class LocalRuleFile {
|
|
23
|
+
/** Workspace-relative, '/'-separated. */
|
|
24
|
+
readonly relativePath: string;
|
|
25
|
+
readonly syntax: SourceSyntax;
|
|
26
|
+
constructor(
|
|
27
|
+
/** Workspace-relative, '/'-separated. */
|
|
28
|
+
relativePath: string, syntax: SourceSyntax);
|
|
29
|
+
get source(): ts.SourceFile;
|
|
30
|
+
get text(): string;
|
|
31
|
+
/** 1-based line of a node's first non-trivia character — where a finding is reported. */
|
|
32
|
+
lineOf(node: ts.Node): number;
|
|
33
|
+
/** 1-based line of a node's last character. */
|
|
34
|
+
endLineOf(node: ts.Node): number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* One violation a local rule found. `line` is where it is reported and where a per-site disable aims;
|
|
38
|
+
* `endLine` is the last line of the construct, so under NEW_AND_MODIFIED_CODE a change ANYWHERE in it
|
|
39
|
+
* judges it (pass `line` again for a one-line finding). Data-only.
|
|
40
|
+
*/
|
|
41
|
+
export declare class LocalRuleFinding {
|
|
42
|
+
readonly line: number;
|
|
43
|
+
readonly endLine: number;
|
|
44
|
+
/** Why THIS site breaks the rule — the diagnosis; the cures come from {@link LocalRule.cures}. */
|
|
45
|
+
readonly message: string;
|
|
46
|
+
constructor(line: number, endLine: number,
|
|
47
|
+
/** Why THIS site breaks the rule — the diagnosis; the cures come from {@link LocalRule.cures}. */
|
|
48
|
+
message: string);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A repo-local rule (#1314): ONE definition that webpieces runs in BOTH places — refused at Write/Edit
|
|
52
|
+
* time by the source guard, and checked by `wp-build` / the PR gate. There is no setting choosing between
|
|
53
|
+
* them; a repo that does not want the write-time check turns the source guard off with its existing
|
|
54
|
+
* switches.
|
|
55
|
+
*
|
|
56
|
+
* The rule decides from the file's SYNTAX alone. The guard sees one proposed file and no compiler program,
|
|
57
|
+
* so anything that needs type information (what an identifier resolves to across files) cannot give the
|
|
58
|
+
* same verdict in both runners and does not belong in a local rule.
|
|
59
|
+
*
|
|
60
|
+
* Everything else is the framework's, exactly as for a built-in rule: `mode` and the changed-line scoping,
|
|
61
|
+
* the epoch and branch switches, `excludePaths`, and the per-site
|
|
62
|
+
* `// webpieces-disable <id> -- <reason>` escape (a reason of at least 20 characters is required). A rule
|
|
63
|
+
* only finds sites.
|
|
64
|
+
*/
|
|
65
|
+
export declare abstract class LocalRule<C extends LocalRuleConfig = LocalRuleConfig> {
|
|
66
|
+
/** The rule's id: its key in the owner file and its name in every refusal and disable. Kebab-case. */
|
|
67
|
+
abstract readonly id: string;
|
|
68
|
+
/** One sentence saying what the rule requires. */
|
|
69
|
+
abstract readonly description: string;
|
|
70
|
+
/** Workspace-relative globs of the files it judges (`**\/*.ts`). Both runners judge exactly these. */
|
|
71
|
+
abstract readonly files: readonly string[];
|
|
72
|
+
/** The fixes both runners print under every finding; mark the one to reach for first `preferred`. */
|
|
73
|
+
abstract readonly cures: readonly Option[];
|
|
74
|
+
/**
|
|
75
|
+
* The rule's OWN settings, beyond `mode` and the framework-owned fields — `{}` when it has none. A
|
|
76
|
+
* required field has no default: a missing value fails the config load, naming the entry to paste.
|
|
77
|
+
*/
|
|
78
|
+
abstract readonly settings: Readonly<Record<string, FieldDef>>;
|
|
79
|
+
/**
|
|
80
|
+
* The reviewed entry written into the pack's owner file for a human to read: `mode` and every
|
|
81
|
+
* required setting. It is never applied by the loader — the owner file must state each value.
|
|
82
|
+
*/
|
|
83
|
+
abstract readonly seed: ConfigObject;
|
|
84
|
+
/** Every site in `file` that breaks the rule. Scoping and disables are applied to what this returns. */
|
|
85
|
+
abstract check(file: LocalRuleFile, config: C): readonly LocalRuleFinding[];
|
|
86
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalRule = exports.LocalRuleFinding = exports.LocalRuleFile = exports.LocalRuleConfig = void 0;
|
|
4
|
+
const rules_sdk_1 = require("@webpieces/rules-sdk");
|
|
5
|
+
/**
|
|
6
|
+
* The config entry of one repo-local rule (#1314): the pack's owner file maps the rule's id to it.
|
|
7
|
+
*
|
|
8
|
+
* `mode` is the same scope vocabulary `catch-must-propagate` uses — OFF | NEW_AND_MODIFIED_CODE |
|
|
9
|
+
* NEW_AND_MODIFIED_FILES | MODIFIED_PROJECTS | RUN_EVERY_TIME — and, like every field inherited from
|
|
10
|
+
* BaseRuleConfig (`turnOffRuleUntilEpoch`, `turnOffRuleWhileOnBranch`, `excludePaths`, `reason`), it is
|
|
11
|
+
* applied by the FRAMEWORK in both runners; a rule never reads it. A rule with settings of its own extends
|
|
12
|
+
* this class and declares them (`words!: string[]`), next to the FieldDefs in {@link LocalRule.settings}.
|
|
13
|
+
*/
|
|
14
|
+
class LocalRuleConfig extends rules_sdk_1.BaseRuleConfig {
|
|
15
|
+
}
|
|
16
|
+
exports.LocalRuleConfig = LocalRuleConfig;
|
|
17
|
+
/**
|
|
18
|
+
* The one file a local rule judges: its WHOLE text, parsed once by `SourceSyntax`. It is the same object
|
|
19
|
+
* in both runners — the Write/Edit guard builds it from the proposed post-edit file, the build gate from
|
|
20
|
+
* the committed file — so a rule that decides from this syntax gives the same verdict at the same line in
|
|
21
|
+
* both. Walk `source` with the `ts` exported beside this class, never a `typescript` of your own.
|
|
22
|
+
*/
|
|
23
|
+
class LocalRuleFile {
|
|
24
|
+
relativePath;
|
|
25
|
+
syntax;
|
|
26
|
+
constructor(
|
|
27
|
+
/** Workspace-relative, '/'-separated. */
|
|
28
|
+
relativePath, syntax) {
|
|
29
|
+
this.relativePath = relativePath;
|
|
30
|
+
this.syntax = syntax;
|
|
31
|
+
}
|
|
32
|
+
get source() {
|
|
33
|
+
return this.syntax.source;
|
|
34
|
+
}
|
|
35
|
+
get text() {
|
|
36
|
+
return this.syntax.source.text;
|
|
37
|
+
}
|
|
38
|
+
/** 1-based line of a node's first non-trivia character — where a finding is reported. */
|
|
39
|
+
lineOf(node) {
|
|
40
|
+
return this.syntax.lineOf(node);
|
|
41
|
+
}
|
|
42
|
+
/** 1-based line of a node's last character. */
|
|
43
|
+
endLineOf(node) {
|
|
44
|
+
return this.syntax.endLineOf(node);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.LocalRuleFile = LocalRuleFile;
|
|
48
|
+
/**
|
|
49
|
+
* One violation a local rule found. `line` is where it is reported and where a per-site disable aims;
|
|
50
|
+
* `endLine` is the last line of the construct, so under NEW_AND_MODIFIED_CODE a change ANYWHERE in it
|
|
51
|
+
* judges it (pass `line` again for a one-line finding). Data-only.
|
|
52
|
+
*/
|
|
53
|
+
class LocalRuleFinding {
|
|
54
|
+
line;
|
|
55
|
+
endLine;
|
|
56
|
+
message;
|
|
57
|
+
constructor(line, endLine,
|
|
58
|
+
/** Why THIS site breaks the rule — the diagnosis; the cures come from {@link LocalRule.cures}. */
|
|
59
|
+
message) {
|
|
60
|
+
this.line = line;
|
|
61
|
+
this.endLine = endLine;
|
|
62
|
+
this.message = message;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.LocalRuleFinding = LocalRuleFinding;
|
|
66
|
+
/**
|
|
67
|
+
* A repo-local rule (#1314): ONE definition that webpieces runs in BOTH places — refused at Write/Edit
|
|
68
|
+
* time by the source guard, and checked by `wp-build` / the PR gate. There is no setting choosing between
|
|
69
|
+
* them; a repo that does not want the write-time check turns the source guard off with its existing
|
|
70
|
+
* switches.
|
|
71
|
+
*
|
|
72
|
+
* The rule decides from the file's SYNTAX alone. The guard sees one proposed file and no compiler program,
|
|
73
|
+
* so anything that needs type information (what an identifier resolves to across files) cannot give the
|
|
74
|
+
* same verdict in both runners and does not belong in a local rule.
|
|
75
|
+
*
|
|
76
|
+
* Everything else is the framework's, exactly as for a built-in rule: `mode` and the changed-line scoping,
|
|
77
|
+
* the epoch and branch switches, `excludePaths`, and the per-site
|
|
78
|
+
* `// webpieces-disable <id> -- <reason>` escape (a reason of at least 20 characters is required). A rule
|
|
79
|
+
* only finds sites.
|
|
80
|
+
*/
|
|
81
|
+
class LocalRule {
|
|
82
|
+
}
|
|
83
|
+
exports.LocalRule = LocalRule;
|
|
84
|
+
//# sourceMappingURL=local-rule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-rule.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/local-rule.ts"],"names":[],"mappings":";;;AAAA,oDAAgG;AAIhG;;;;;;;;GAQG;AACH,MAAa,eAAgB,SAAQ,0BAAc;CAElD;AAFD,0CAEC;AAED;;;;;GAKG;AACH,MAAa,aAAa;IAGT;IACA;IAHb;IACI,yCAAyC;IAChC,YAAoB,EACpB,MAAoB;QADpB,iBAAY,GAAZ,YAAY,CAAQ;QACpB,WAAM,GAAN,MAAM,CAAc;IAC9B,CAAC;IAEJ,IAAI,MAAM;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;IACnC,CAAC;IAED,yFAAyF;IACzF,MAAM,CAAC,IAAa;QAChB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,+CAA+C;IAC/C,SAAS,CAAC,IAAa;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;CACJ;AAxBD,sCAwBC;AAED;;;;GAIG;AACH,MAAa,gBAAgB;IAEZ;IACA;IAEA;IAJb,YACa,IAAY,EACZ,OAAe;IACxB,kGAAkG;IACzF,OAAe;QAHf,SAAI,GAAJ,IAAI,CAAQ;QACZ,YAAO,GAAP,OAAO,CAAQ;QAEf,YAAO,GAAP,OAAO,CAAQ;IACzB,CAAC;CACP;AAPD,4CAOC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAsB,SAAS;CAsB9B;AAtBD,8BAsBC","sourcesContent":["import { BaseRuleConfig, ConfigObject, FieldDef, ModifiedCodeMode } from '@webpieces/rules-sdk';\nimport { Option } from '@webpieces/rules-config';\nimport { SourceSyntax, ts } from '@webpieces/tooling-common/source-syntax';\n\n/**\n * The config entry of one repo-local rule (#1314): the pack's owner file maps the rule's id to it.\n *\n * `mode` is the same scope vocabulary `catch-must-propagate` uses — OFF | NEW_AND_MODIFIED_CODE |\n * NEW_AND_MODIFIED_FILES | MODIFIED_PROJECTS | RUN_EVERY_TIME — and, like every field inherited from\n * BaseRuleConfig (`turnOffRuleUntilEpoch`, `turnOffRuleWhileOnBranch`, `excludePaths`, `reason`), it is\n * applied by the FRAMEWORK in both runners; a rule never reads it. A rule with settings of its own extends\n * this class and declares them (`words!: string[]`), next to the FieldDefs in {@link LocalRule.settings}.\n */\nexport class LocalRuleConfig extends BaseRuleConfig {\n declare mode?: ModifiedCodeMode;\n}\n\n/**\n * The one file a local rule judges: its WHOLE text, parsed once by `SourceSyntax`. It is the same object\n * in both runners — the Write/Edit guard builds it from the proposed post-edit file, the build gate from\n * the committed file — so a rule that decides from this syntax gives the same verdict at the same line in\n * both. Walk `source` with the `ts` exported beside this class, never a `typescript` of your own.\n */\nexport class LocalRuleFile {\n constructor(\n /** Workspace-relative, '/'-separated. */\n readonly relativePath: string,\n readonly syntax: SourceSyntax,\n ) {}\n\n get source(): ts.SourceFile {\n return this.syntax.source;\n }\n\n get text(): string {\n return this.syntax.source.text;\n }\n\n /** 1-based line of a node's first non-trivia character — where a finding is reported. */\n lineOf(node: ts.Node): number {\n return this.syntax.lineOf(node);\n }\n\n /** 1-based line of a node's last character. */\n endLineOf(node: ts.Node): number {\n return this.syntax.endLineOf(node);\n }\n}\n\n/**\n * One violation a local rule found. `line` is where it is reported and where a per-site disable aims;\n * `endLine` is the last line of the construct, so under NEW_AND_MODIFIED_CODE a change ANYWHERE in it\n * judges it (pass `line` again for a one-line finding). Data-only.\n */\nexport class LocalRuleFinding {\n constructor(\n readonly line: number,\n readonly endLine: number,\n /** Why THIS site breaks the rule — the diagnosis; the cures come from {@link LocalRule.cures}. */\n readonly message: string,\n ) {}\n}\n\n/**\n * A repo-local rule (#1314): ONE definition that webpieces runs in BOTH places — refused at Write/Edit\n * time by the source guard, and checked by `wp-build` / the PR gate. There is no setting choosing between\n * them; a repo that does not want the write-time check turns the source guard off with its existing\n * switches.\n *\n * The rule decides from the file's SYNTAX alone. The guard sees one proposed file and no compiler program,\n * so anything that needs type information (what an identifier resolves to across files) cannot give the\n * same verdict in both runners and does not belong in a local rule.\n *\n * Everything else is the framework's, exactly as for a built-in rule: `mode` and the changed-line scoping,\n * the epoch and branch switches, `excludePaths`, and the per-site\n * `// webpieces-disable <id> -- <reason>` escape (a reason of at least 20 characters is required). A rule\n * only finds sites.\n */\nexport abstract class LocalRule<C extends LocalRuleConfig = LocalRuleConfig> {\n /** The rule's id: its key in the owner file and its name in every refusal and disable. Kebab-case. */\n abstract readonly id: string;\n /** One sentence saying what the rule requires. */\n abstract readonly description: string;\n /** Workspace-relative globs of the files it judges (`**\\/*.ts`). Both runners judge exactly these. */\n abstract readonly files: readonly string[];\n /** The fixes both runners print under every finding; mark the one to reach for first `preferred`. */\n abstract readonly cures: readonly Option[];\n /**\n * The rule's OWN settings, beyond `mode` and the framework-owned fields — `{}` when it has none. A\n * required field has no default: a missing value fails the config load, naming the entry to paste.\n */\n abstract readonly settings: Readonly<Record<string, FieldDef>>;\n /**\n * The reviewed entry written into the pack's owner file for a human to read: `mode` and every\n * required setting. It is never applied by the loader — the owner file must state each value.\n */\n abstract readonly seed: ConfigObject;\n\n /** Every site in `file` that breaks the rule. Scoping and disables are applied to what this returns. */\n abstract check(file: LocalRuleFile, config: C): readonly LocalRuleFinding[];\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FileContext, FileRuleBase, FixHint, Violation } from '@webpieces/hook-runtime';
|
|
2
|
+
import { LocalRule, LocalRuleConfig } from './local-rule';
|
|
3
|
+
/**
|
|
4
|
+
* A local rule at Write/Edit time: it judges the PROPOSED file — the whole post-edit text that
|
|
5
|
+
* `ProposedFile` projects in memory — through the same {@link LocalRuleJudge} the build gate calls.
|
|
6
|
+
*
|
|
7
|
+
* Under NEW_AND_MODIFIED_CODE only a finding spanning a line this edit changed is refused; every other
|
|
8
|
+
* active mode judges the whole proposed file. OFF, the epoch and branch switches and `excludePaths` are
|
|
9
|
+
* applied by the hook runtime before `check` is ever called (`ConfiguredHookPolicy`).
|
|
10
|
+
*/
|
|
11
|
+
export declare class LocalSourceHookPolicy extends FileRuleBase<LocalRuleConfig> {
|
|
12
|
+
readonly description: string;
|
|
13
|
+
readonly files: readonly string[];
|
|
14
|
+
readonly fixHint: FixHint;
|
|
15
|
+
private readonly judge;
|
|
16
|
+
constructor(config: LocalRuleConfig, rule: LocalRule);
|
|
17
|
+
check(ctx: FileContext): readonly Violation[];
|
|
18
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalSourceHookPolicy = void 0;
|
|
4
|
+
const hook_runtime_1 = require("@webpieces/hook-runtime");
|
|
5
|
+
const local_rule_judge_1 = require("./local-rule-judge");
|
|
6
|
+
/**
|
|
7
|
+
* A local rule at Write/Edit time: it judges the PROPOSED file — the whole post-edit text that
|
|
8
|
+
* `ProposedFile` projects in memory — through the same {@link LocalRuleJudge} the build gate calls.
|
|
9
|
+
*
|
|
10
|
+
* Under NEW_AND_MODIFIED_CODE only a finding spanning a line this edit changed is refused; every other
|
|
11
|
+
* active mode judges the whole proposed file. OFF, the epoch and branch switches and `excludePaths` are
|
|
12
|
+
* applied by the hook runtime before `check` is ever called (`ConfiguredHookPolicy`).
|
|
13
|
+
*/
|
|
14
|
+
class LocalSourceHookPolicy extends hook_runtime_1.FileRuleBase {
|
|
15
|
+
description;
|
|
16
|
+
files;
|
|
17
|
+
fixHint;
|
|
18
|
+
judge;
|
|
19
|
+
constructor(config, rule) {
|
|
20
|
+
super(config, rule.id, rule.id);
|
|
21
|
+
this.description = rule.description;
|
|
22
|
+
this.files = rule.files;
|
|
23
|
+
this.judge = new local_rule_judge_1.LocalRuleJudge(rule);
|
|
24
|
+
this.fixHint = new hook_runtime_1.FixHint(rule.description, 'Fix it — pick one:', rule.cures, new hook_runtime_1.DisableEscape(true, this.judge.disableComment()));
|
|
25
|
+
}
|
|
26
|
+
check(ctx) {
|
|
27
|
+
if (ctx.proposedFile === null)
|
|
28
|
+
return [];
|
|
29
|
+
const content = ctx.proposedFile.read();
|
|
30
|
+
const changed = this.config.mode === 'NEW_AND_MODIFIED_CODE' ? content.changedLines() : undefined;
|
|
31
|
+
return this.judge
|
|
32
|
+
.judge(ctx.relativePath, content.text, this.config, changed)
|
|
33
|
+
.map((verdict) => new hook_runtime_1.Violation(verdict.line, verdict.snippet, verdict.message));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.LocalSourceHookPolicy = LocalSourceHookPolicy;
|
|
37
|
+
//# sourceMappingURL=local-source-hook-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-source-hook-policy.js","sourceRoot":"","sources":["../../../../../../packages/tooling/webpieces-tooling/src/local-rules/local-source-hook-policy.ts"],"names":[],"mappings":";;;AAAA,0DAAuG;AAEvG,yDAAsE;AAEtE;;;;;;;GAOG;AACH,MAAa,qBAAsB,SAAQ,2BAA6B;IAC3D,WAAW,CAAS;IACX,KAAK,CAAoB;IAClC,OAAO,CAAU;IACT,KAAK,CAAiB;IAEvC,YAAY,MAAuB,EAAE,IAAe;QAChD,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QAChC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACpC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,IAAI,iCAAc,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,IAAI,sBAAO,CACtB,IAAI,CAAC,WAAW,EAChB,oBAAoB,EACpB,IAAI,CAAC,KAAK,EACV,IAAI,4BAAa,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,CACvD,CAAC;IACN,CAAC;IAED,KAAK,CAAC,GAAgB;QAClB,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI;YAAE,OAAO,EAAE,CAAC;QACzC,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QACxC,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,uBAAuB,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QAClG,OAAO,IAAI,CAAC,KAAK;aACZ,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;aAC3D,GAAG,CAAC,CAAC,OAAyB,EAAE,EAAE,CAAC,IAAI,wBAAS,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAC3G,CAAC;CACJ;AA3BD,sDA2BC","sourcesContent":["import { DisableEscape, FileContext, FileRuleBase, FixHint, Violation } from '@webpieces/hook-runtime';\nimport { LocalRule, LocalRuleConfig } from './local-rule';\nimport { LocalRuleJudge, LocalRuleVerdict } from './local-rule-judge';\n\n/**\n * A local rule at Write/Edit time: it judges the PROPOSED file — the whole post-edit text that\n * `ProposedFile` projects in memory — through the same {@link LocalRuleJudge} the build gate calls.\n *\n * Under NEW_AND_MODIFIED_CODE only a finding spanning a line this edit changed is refused; every other\n * active mode judges the whole proposed file. OFF, the epoch and branch switches and `excludePaths` are\n * applied by the hook runtime before `check` is ever called (`ConfiguredHookPolicy`).\n */\nexport class LocalSourceHookPolicy extends FileRuleBase<LocalRuleConfig> {\n readonly description: string;\n override readonly files: readonly string[];\n readonly fixHint: FixHint;\n private readonly judge: LocalRuleJudge;\n\n constructor(config: LocalRuleConfig, rule: LocalRule) {\n super(config, rule.id, rule.id);\n this.description = rule.description;\n this.files = rule.files;\n this.judge = new LocalRuleJudge(rule);\n this.fixHint = new FixHint(\n rule.description,\n 'Fix it — pick one:',\n rule.cures,\n new DisableEscape(true, this.judge.disableComment()),\n );\n }\n\n check(ctx: FileContext): readonly Violation[] {\n if (ctx.proposedFile === null) return [];\n const content = ctx.proposedFile.read();\n const changed = this.config.mode === 'NEW_AND_MODIFIED_CODE' ? content.changedLines() : undefined;\n return this.judge\n .judge(ctx.relativePath, content.text, this.config, changed)\n .map((verdict: LocalRuleVerdict) => new Violation(verdict.line, verdict.snippet, verdict.message));\n }\n}\n"]}
|