@gobing-ai/ts-rule-engine 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dist/config/extensions.d.ts +46 -0
  2. package/dist/config/extensions.d.ts.map +1 -0
  3. package/dist/config/extensions.js +63 -0
  4. package/dist/config/loader.d.ts +15 -5
  5. package/dist/config/loader.d.ts.map +1 -1
  6. package/dist/config/loader.js +127 -33
  7. package/dist/engine.d.ts +26 -1
  8. package/dist/engine.d.ts.map +1 -1
  9. package/dist/engine.js +79 -0
  10. package/dist/evaluators/coverage-gate-evaluator.d.ts +21 -0
  11. package/dist/evaluators/coverage-gate-evaluator.d.ts.map +1 -0
  12. package/dist/evaluators/coverage-gate-evaluator.js +103 -0
  13. package/dist/evaluators/exit-code-evaluator.d.ts +1 -1
  14. package/dist/evaluators/exit-code-evaluator.d.ts.map +1 -1
  15. package/dist/evaluators/exit-code-evaluator.js +22 -9
  16. package/dist/evaluators/file-utils.d.ts +8 -0
  17. package/dist/evaluators/file-utils.d.ts.map +1 -1
  18. package/dist/evaluators/file-utils.js +40 -0
  19. package/dist/evaluators/forbidden-import-evaluator.d.ts +16 -3
  20. package/dist/evaluators/forbidden-import-evaluator.d.ts.map +1 -1
  21. package/dist/evaluators/forbidden-import-evaluator.js +71 -6
  22. package/dist/evaluators/import-boundary-evaluator.d.ts +19 -0
  23. package/dist/evaluators/import-boundary-evaluator.d.ts.map +1 -0
  24. package/dist/evaluators/import-boundary-evaluator.js +85 -0
  25. package/dist/evaluators/path-evaluator.d.ts +15 -2
  26. package/dist/evaluators/path-evaluator.d.ts.map +1 -1
  27. package/dist/evaluators/path-evaluator.js +49 -3
  28. package/dist/evaluators/regex-evaluator.d.ts.map +1 -1
  29. package/dist/evaluators/regex-evaluator.js +43 -8
  30. package/dist/evaluators/schema-artifact-evaluator.d.ts +21 -0
  31. package/dist/evaluators/schema-artifact-evaluator.d.ts.map +1 -0
  32. package/dist/evaluators/schema-artifact-evaluator.js +102 -0
  33. package/dist/evaluators/secrets-scanner-evaluator.d.ts +13 -2
  34. package/dist/evaluators/secrets-scanner-evaluator.d.ts.map +1 -1
  35. package/dist/evaluators/secrets-scanner-evaluator.js +72 -9
  36. package/dist/evaluators/sg-evaluator.d.ts +19 -0
  37. package/dist/evaluators/sg-evaluator.d.ts.map +1 -0
  38. package/dist/evaluators/sg-evaluator.js +112 -0
  39. package/dist/evaluators/test-location-evaluator.d.ts +32 -0
  40. package/dist/evaluators/test-location-evaluator.d.ts.map +1 -0
  41. package/dist/evaluators/test-location-evaluator.js +105 -0
  42. package/dist/evaluators/tsdoc-export-evaluator.d.ts +15 -0
  43. package/dist/evaluators/tsdoc-export-evaluator.d.ts.map +1 -0
  44. package/dist/evaluators/tsdoc-export-evaluator.js +91 -0
  45. package/dist/fixers/fixers.d.ts +86 -0
  46. package/dist/fixers/fixers.d.ts.map +1 -0
  47. package/dist/fixers/fixers.js +230 -0
  48. package/dist/fixers/test-stub-fixer.d.ts +49 -0
  49. package/dist/fixers/test-stub-fixer.d.ts.map +1 -0
  50. package/dist/fixers/test-stub-fixer.js +91 -0
  51. package/dist/host/builtins.d.ts.map +1 -1
  52. package/dist/host/builtins.js +17 -0
  53. package/dist/host/rule-engine-host.d.ts +3 -0
  54. package/dist/host/rule-engine-host.d.ts.map +1 -1
  55. package/dist/host/rule-engine-host.js +3 -0
  56. package/dist/index.d.ts +4 -0
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +4 -0
  59. package/dist/resolvers/test-path-resolver.d.ts +72 -0
  60. package/dist/resolvers/test-path-resolver.d.ts.map +1 -0
  61. package/dist/resolvers/test-path-resolver.js +112 -0
  62. package/dist/types.d.ts +30 -0
  63. package/dist/types.d.ts.map +1 -1
  64. package/dist/types.js +8 -0
  65. package/package.json +3 -3
  66. package/src/config/extensions.ts +108 -0
  67. package/src/config/loader.ts +140 -35
  68. package/src/engine.ts +99 -2
  69. package/src/evaluators/coverage-gate-evaluator.ts +137 -0
  70. package/src/evaluators/exit-code-evaluator.ts +27 -9
  71. package/src/evaluators/file-utils.ts +38 -0
  72. package/src/evaluators/forbidden-import-evaluator.ts +101 -7
  73. package/src/evaluators/import-boundary-evaluator.ts +135 -0
  74. package/src/evaluators/path-evaluator.ts +66 -3
  75. package/src/evaluators/regex-evaluator.ts +53 -12
  76. package/src/evaluators/schema-artifact-evaluator.ts +134 -0
  77. package/src/evaluators/secrets-scanner-evaluator.ts +89 -11
  78. package/src/evaluators/sg-evaluator.ts +133 -0
  79. package/src/evaluators/test-location-evaluator.ts +127 -0
  80. package/src/evaluators/tsdoc-export-evaluator.ts +111 -0
  81. package/src/fixers/fixers.ts +294 -0
  82. package/src/fixers/test-stub-fixer.ts +118 -0
  83. package/src/host/builtins.ts +22 -0
  84. package/src/host/rule-engine-host.ts +4 -0
  85. package/src/index.ts +4 -0
  86. package/src/resolvers/test-path-resolver.ts +133 -0
  87. package/src/types.ts +34 -0
@@ -6,28 +6,41 @@ export class ExitCodeEvaluator {
6
6
  constructor(executor = new NodeProcessExecutor()) {
7
7
  this.executor = executor;
8
8
  }
9
- /** Run configured command and emit a finding on non-zero exit. */
9
+ /** Run configured command and emit a finding unless the exit code matches `successCode`. */
10
10
  async evaluate(rule, context) {
11
11
  const config = rule.evaluator.config ?? {};
12
12
  const command = stringConfig(config, 'command');
13
13
  const args = arrayConfig(config, 'args', []);
14
- const result = await this.executor.run({ command, args, cwd: context.workdir, rejectOnError: false });
15
- if (result.exitCode === 0)
14
+ const successCode = numberConfig(config, 'successCode', 0);
15
+ const timeout = numberConfig(config, 'timeout', 60_000);
16
+ const result = await this.executor.run({
17
+ command,
18
+ args,
19
+ cwd: context.workdir,
20
+ timeout,
21
+ rejectOnError: false,
22
+ label: 'exit-code',
23
+ });
24
+ if (result.exitCode === successCode)
16
25
  return { findings: [], fixes: [] };
26
+ const template = stringConfig(config, 'message', `Command failed (exit {code}): ${command} ${args.join(' ')}`.trim());
27
+ const message = template.replaceAll('{code}', String(result.exitCode));
17
28
  return {
18
- findings: [
19
- createFinding(rule, `Command failed: ${command} ${args.join(' ')}`.trim(), null, {
20
- code: 'exit-code:failed',
21
- }),
22
- ],
29
+ findings: [createFinding(rule, message, null, { code: 'exit-code:failed' })],
23
30
  fixes: [],
24
31
  };
25
32
  }
26
33
  }
27
- function stringConfig(config, key) {
34
+ function numberConfig(config, key, fallback) {
35
+ const value = config[key];
36
+ return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
37
+ }
38
+ function stringConfig(config, key, fallback) {
28
39
  const value = config[key];
29
40
  if (typeof value === 'string')
30
41
  return value;
42
+ if (fallback !== undefined)
43
+ return fallback;
31
44
  throw new Error(`exit-code evaluator requires string config "${key}"`);
32
45
  }
33
46
  function arrayConfig(config, key, fallback) {
@@ -20,4 +20,12 @@ export declare function relativeToWorkdir(workdir: string, path: string): string
20
20
  export declare function relativeParent(path: string): string;
21
21
  /** Return true when a path matches any supplied fragment or suffix. */
22
22
  export declare function matchesAny(path: string, patterns: string[] | undefined): boolean;
23
+ /**
24
+ * Segment-aware glob matching with `**` (any depth) and `*` (single segment).
25
+ *
26
+ * Stricter than {@link matchesAny}: it anchors the whole path, so `apps/**` does
27
+ * not match `vendor/apps/x`. Used by evaluators that enforce path policies
28
+ * (coverage scoping, test-file location) where a loose match would change findings.
29
+ */
30
+ export declare function matchesGlob(path: string, pattern: string): boolean;
23
31
  //# sourceMappingURL=file-utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"file-utils.d.ts","sourceRoot":"","sources":["../../src/evaluators/file-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,cAAc,EAAW,MAAM,uBAAuB,CAAC;AAEjF,yCAAyC;AACzC,MAAM,WAAW,sBAAsB;IACnC,yBAAyB;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,0BAA0B;IAC1B,EAAE,CAAC,EAAE,UAAU,CAAC;CACnB;AAID,qFAAqF;AACrF,wBAAsB,aAAa,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAWtF;AAED,gDAAgD;AAChD,wBAAsB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,iBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAEnH;AAED,sDAAsD;AACtD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED,2DAA2D;AAC3D,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGnD;AAED,uEAAuE;AACvE,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,SAAS,GAAG,OAAO,CAMhF"}
1
+ {"version":3,"file":"file-utils.d.ts","sourceRoot":"","sources":["../../src/evaluators/file-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,cAAc,EAAW,MAAM,uBAAuB,CAAC;AAEjF,yCAAyC;AACzC,MAAM,WAAW,sBAAsB;IACnC,yBAAyB;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,0BAA0B;IAC1B,EAAE,CAAC,EAAE,UAAU,CAAC;CACnB;AAID,qFAAqF;AACrF,wBAAsB,aAAa,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAWtF;AAED,gDAAgD;AAChD,wBAAsB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,iBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAEnH;AAED,sDAAsD;AACtD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED,2DAA2D;AAC3D,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGnD;AAED,uEAAuE;AACvE,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,SAAS,GAAG,OAAO,CAMhF;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAUlE"}
@@ -33,3 +33,43 @@ export function matchesAny(path, patterns) {
33
33
  return clean.length === 0 || path.includes(clean) || path.endsWith(clean);
34
34
  });
35
35
  }
36
+ /**
37
+ * Segment-aware glob matching with `**` (any depth) and `*` (single segment).
38
+ *
39
+ * Stricter than {@link matchesAny}: it anchors the whole path, so `apps/**` does
40
+ * not match `vendor/apps/x`. Used by evaluators that enforce path policies
41
+ * (coverage scoping, test-file location) where a loose match would change findings.
42
+ */
43
+ export function matchesGlob(path, pattern) {
44
+ const normalized = path.replaceAll('\\', '/');
45
+ if (pattern.startsWith('**/')) {
46
+ const suffix = pattern.slice(3);
47
+ if (suffix.indexOf('*') === -1) {
48
+ return normalized.endsWith(suffix) || normalized.endsWith(`/${suffix}`);
49
+ }
50
+ }
51
+ if (pattern === normalized)
52
+ return true;
53
+ return matchSegments(normalized.split('/'), pattern.split('/'), 0, 0);
54
+ }
55
+ /** Recursive segment-level glob matcher backing {@link matchesGlob}. */
56
+ function matchSegments(file, pattern, fi, pi) {
57
+ if (pi >= pattern.length)
58
+ return fi >= file.length;
59
+ if (fi >= file.length)
60
+ return pattern.slice(pi).every((segment) => segment === '**');
61
+ const pat = pattern[pi] ?? '';
62
+ if (pat === '**') {
63
+ return matchSegments(file, pattern, fi, pi + 1) || matchSegments(file, pattern, fi + 1, pi);
64
+ }
65
+ if (!matchSegment(file[fi] ?? '', pat))
66
+ return false;
67
+ return matchSegments(file, pattern, fi + 1, pi + 1);
68
+ }
69
+ /** Match one path segment against a pattern segment where `*` matches any run of non-`/` chars. */
70
+ function matchSegment(segment, pattern) {
71
+ if (pattern.indexOf('*') === -1)
72
+ return segment === pattern;
73
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replaceAll('*', '[^/]*');
74
+ return new RegExp(`^${escaped}$`).test(segment);
75
+ }
@@ -1,8 +1,21 @@
1
1
  import { type ConstraintRule, type RuleContext, type RuleEvaluationResult, type RuleEvaluator } from '../types';
2
- /** Detects imports matching forbidden package or path prefixes. */
2
+ /**
3
+ * Detects forbidden imports / API usage.
4
+ *
5
+ * Two config shapes are supported:
6
+ * - Simple: `{ patterns: string[] }` — substring match against any import specifier,
7
+ * scoped by the rule's own `include` / `exclude`.
8
+ * - Structured: `{ forbidden: [...], scope: { include, exclude } }` — each forbidden
9
+ * entry is an exact `specifier` (also matching require/dynamic forms unless
10
+ * `includeRequire:false`) or a raw `pattern`, scoped by `scope.include` /
11
+ * `scope.exclude` globs.
12
+ */
3
13
  export declare class ForbiddenImportEvaluator implements RuleEvaluator {
4
- constructor();
5
- /** Evaluate import declarations against configured forbidden prefixes. */
14
+ /** Evaluate import/usage against the configured forbidden set. */
6
15
  evaluate(rule: ConstraintRule, context: RuleContext): Promise<RuleEvaluationResult>;
16
+ /** Legacy path: `{ patterns: string[] }`, substring-matched against import specifiers. */
17
+ private evaluateSimple;
18
+ /** Structured path: `{ forbidden: [...], scope: { include, exclude } }`. */
19
+ private evaluateStructured;
7
20
  }
8
21
  //# sourceMappingURL=forbidden-import-evaluator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"forbidden-import-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/forbidden-import-evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAGlB,mEAAmE;AACnE,qBAAa,wBAAyB,YAAW,aAAa;;IAG1D,0EAA0E;IACpE,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;CA4B5F"}
1
+ {"version":3,"file":"forbidden-import-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/forbidden-import-evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAclB;;;;;;;;;;GAUG;AACH,qBAAa,wBAAyB,YAAW,aAAa;IAC1D,kEAAkE;IAC5D,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAOzF,0FAA0F;YAC5E,cAAc;IA+B5B,4EAA4E;YAC9D,kBAAkB;CAoCnC"}
@@ -1,11 +1,26 @@
1
1
  import { createFinding, } from '../types.js';
2
- import { discoverFiles, readWorkdirFile } from './file-utils.js';
3
- /** Detects imports matching forbidden package or path prefixes. */
2
+ import { discoverFiles, matchesGlob, readWorkdirFile } from './file-utils.js';
3
+ /**
4
+ * Detects forbidden imports / API usage.
5
+ *
6
+ * Two config shapes are supported:
7
+ * - Simple: `{ patterns: string[] }` — substring match against any import specifier,
8
+ * scoped by the rule's own `include` / `exclude`.
9
+ * - Structured: `{ forbidden: [...], scope: { include, exclude } }` — each forbidden
10
+ * entry is an exact `specifier` (also matching require/dynamic forms unless
11
+ * `includeRequire:false`) or a raw `pattern`, scoped by `scope.include` /
12
+ * `scope.exclude` globs.
13
+ */
4
14
  export class ForbiddenImportEvaluator {
5
- constructor() { }
6
- /** Evaluate import declarations against configured forbidden prefixes. */
15
+ /** Evaluate import/usage against the configured forbidden set. */
7
16
  async evaluate(rule, context) {
8
17
  const config = rule.evaluator.config ?? {};
18
+ return Array.isArray(config.forbidden)
19
+ ? this.evaluateStructured(rule, context, config)
20
+ : this.evaluateSimple(rule, context, config);
21
+ }
22
+ /** Legacy path: `{ patterns: string[] }`, substring-matched against import specifiers. */
23
+ async evaluateSimple(rule, context, config) {
9
24
  const forbidden = arrayConfig(config, 'patterns');
10
25
  const files = await discoverFiles({
11
26
  workdir: context.workdir,
@@ -16,8 +31,7 @@ export class ForbiddenImportEvaluator {
16
31
  for (const file of files) {
17
32
  const lines = (await readWorkdirFile(context.workdir, file)).split('\n');
18
33
  for (const [index, line] of lines.entries()) {
19
- const imported = /(?:from\s+|import\s*\(|^\s*import\s*)['"](?<specifier>[^'"]+)['"]/.exec(line)?.groups
20
- ?.specifier;
34
+ const imported = importSpecifier(line);
21
35
  if (imported === undefined)
22
36
  continue;
23
37
  const matched = forbidden.find((pattern) => imported.includes(pattern));
@@ -31,6 +45,54 @@ export class ForbiddenImportEvaluator {
31
45
  }
32
46
  return { findings, fixes: [] };
33
47
  }
48
+ /** Structured path: `{ forbidden: [...], scope: { include, exclude } }`. */
49
+ async evaluateStructured(rule, context, config) {
50
+ const scope = config.scope;
51
+ const include = stringArray(scope?.include);
52
+ if (include === undefined) {
53
+ throw new Error('forbidden-import evaluator requires string[] config "scope.include"');
54
+ }
55
+ const exclude = stringArray(scope?.exclude) ?? [];
56
+ const entries = config.forbidden.map(compileEntry);
57
+ // Discover all source files, then apply scope globs precisely (discoverFiles'
58
+ // include matching is intentionally loose, so it cannot do `**`-anchored scoping).
59
+ const files = (await discoverFiles({ workdir: context.workdir }))
60
+ .filter((file) => include.some((glob) => matchesGlob(file, glob)))
61
+ .filter((file) => !exclude.some((glob) => matchesGlob(file, glob)));
62
+ const findings = [];
63
+ for (const file of files) {
64
+ const lines = (await readWorkdirFile(context.workdir, file)).split('\n');
65
+ for (const [index, line] of lines.entries()) {
66
+ const hit = entries.find((entry) => entry.regex.test(line));
67
+ if (hit !== undefined) {
68
+ findings.push(createFinding(rule, `Forbidden import/usage of "${hit.label}"`, file, {
69
+ line: index + 1,
70
+ code: 'import:forbidden',
71
+ }));
72
+ }
73
+ }
74
+ }
75
+ return { findings, fixes: [] };
76
+ }
77
+ }
78
+ /** Extract the specifier from an import/require/dynamic-import line, if any. */
79
+ function importSpecifier(line) {
80
+ return /(?:from\s+|import\s*\(|^\s*import\s*)['"](?<specifier>[^'"]+)['"]/.exec(line)?.groups?.specifier;
81
+ }
82
+ /** Compile a forbidden entry into a line-matching regex. */
83
+ function compileEntry(entry) {
84
+ if ('specifier' in entry) {
85
+ const spec = escapeRegExp(entry.specifier);
86
+ const boundary = `(?:/|['"])`;
87
+ const source = entry.includeRequire === false
88
+ ? `from\\s+['"]${spec}${boundary}`
89
+ : `(?:from\\s+|require\\(\\s*|import\\(\\s*)['"]${spec}${boundary}`;
90
+ return { regex: new RegExp(source), label: entry.specifier };
91
+ }
92
+ return { regex: new RegExp(entry.pattern), label: entry.pattern };
93
+ }
94
+ function escapeRegExp(value) {
95
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
34
96
  }
35
97
  function arrayConfig(config, key) {
36
98
  const value = config[key];
@@ -40,3 +102,6 @@ function arrayConfig(config, key) {
40
102
  return [value];
41
103
  throw new Error(`forbidden-import evaluator requires string[] config "${key}"`);
42
104
  }
105
+ function stringArray(value) {
106
+ return Array.isArray(value) && value.every((item) => typeof item === 'string') ? value : undefined;
107
+ }
@@ -0,0 +1,19 @@
1
+ import { type ConstraintRule, type RuleContext, type RuleEvaluationResult, type RuleEvaluator } from '../types';
2
+ /**
3
+ * Enforces architectural import boundaries without spawning a subprocess.
4
+ *
5
+ * Files matching a boundary's `scope` glob are scanned in-memory. Each forbidden
6
+ * entry is either a string (matched as an import-specifier substring) or an object
7
+ * with a `pattern` regex and an optional `mode` (`import` | `usage`).
8
+ *
9
+ * ## Options (in `evaluator.config`)
10
+ * - `boundaries` — non-empty array of boundary declarations:
11
+ * - `scope` — glob pattern selecting files this boundary applies to.
12
+ * - `forbidden` — array of strings or `{ pattern, mode?, syntax? }` objects.
13
+ * - `exclude` — optional globs within the scope to ignore.
14
+ */
15
+ export declare class ImportBoundaryEvaluator implements RuleEvaluator {
16
+ /** Evaluate import boundaries across all in-scope files. */
17
+ evaluate(rule: ConstraintRule, context: RuleContext): Promise<RuleEvaluationResult>;
18
+ }
19
+ //# sourceMappingURL=import-boundary-evaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"import-boundary-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/import-boundary-evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AA4BlB;;;;;;;;;;;;GAYG;AACH,qBAAa,uBAAwB,YAAW,aAAa;IACzD,4DAA4D;IACtD,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAuC5F"}
@@ -0,0 +1,85 @@
1
+ import { createFinding, } from '../types.js';
2
+ import { discoverFiles, matchesGlob, readWorkdirFile } from './file-utils.js';
3
+ /**
4
+ * Enforces architectural import boundaries without spawning a subprocess.
5
+ *
6
+ * Files matching a boundary's `scope` glob are scanned in-memory. Each forbidden
7
+ * entry is either a string (matched as an import-specifier substring) or an object
8
+ * with a `pattern` regex and an optional `mode` (`import` | `usage`).
9
+ *
10
+ * ## Options (in `evaluator.config`)
11
+ * - `boundaries` — non-empty array of boundary declarations:
12
+ * - `scope` — glob pattern selecting files this boundary applies to.
13
+ * - `forbidden` — array of strings or `{ pattern, mode?, syntax? }` objects.
14
+ * - `exclude` — optional globs within the scope to ignore.
15
+ */
16
+ export class ImportBoundaryEvaluator {
17
+ /** Evaluate import boundaries across all in-scope files. */
18
+ async evaluate(rule, context) {
19
+ const config = rule.evaluator.config ?? {};
20
+ const boundaries = config.boundaries;
21
+ if (!Array.isArray(boundaries) || boundaries.length === 0) {
22
+ throw new Error('import-boundary evaluator requires non-empty array config "boundaries"');
23
+ }
24
+ const compiled = boundaries.map((b) => compileBoundary(b));
25
+ // Discover all files once; filter per boundary below.
26
+ const allFiles = await discoverFiles({ workdir: context.workdir });
27
+ const findings = [];
28
+ for (const boundary of compiled) {
29
+ const inScope = allFiles
30
+ .filter((file) => matchesGlob(file, boundary.scope))
31
+ .filter((file) => !boundary.excludePatterns.some((ex) => matchesGlob(file, ex)));
32
+ for (const file of inScope) {
33
+ const content = await readWorkdirFile(context.workdir, file);
34
+ const lines = content.split('\n');
35
+ for (const [index, line] of lines.entries()) {
36
+ for (const entry of boundary.forbidden) {
37
+ if (entry.importOnly && !isImportLine(line))
38
+ continue;
39
+ if (entry.regex.test(line)) {
40
+ findings.push(createFinding(rule, `forbidden in boundary "${boundary.scope}": ${entry.label}`, file, {
41
+ line: index + 1,
42
+ code: 'import-boundary:violation',
43
+ }));
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ return { findings, fixes: [] };
50
+ }
51
+ }
52
+ /** Compile a raw boundary declaration into a scan-ready form. */
53
+ function compileBoundary(decl) {
54
+ return {
55
+ scope: decl.scope,
56
+ excludePatterns: decl.exclude ?? [],
57
+ forbidden: decl.forbidden.map((entry) => compileEntry(entry)),
58
+ };
59
+ }
60
+ /** Compile one forbidden entry into a regex + metadata. */
61
+ function compileEntry(entry) {
62
+ if (typeof entry === 'string') {
63
+ // String form: match as an import specifier substring.
64
+ const escaped = escapeRegExp(entry);
65
+ return {
66
+ regex: new RegExp(`(?:from\\s+|require\\(\\s*|import\\(\\s*)['"](?:[^'"]*)?${escaped}(?:[^'"]*)?['"]`),
67
+ label: entry,
68
+ importOnly: true,
69
+ };
70
+ }
71
+ // Object form with `pattern`.
72
+ const importOnly = (entry.mode ?? 'import') !== 'usage';
73
+ return {
74
+ regex: new RegExp(entry.pattern),
75
+ label: entry.pattern,
76
+ importOnly,
77
+ };
78
+ }
79
+ /** Return true when a source line is an import/export/require/dynamic-import statement. */
80
+ function isImportLine(line) {
81
+ return /(?:^\s*import\b|^\s*export\b.*\bfrom\b|(?:from|require|import)\s*\(?\s*['"])/.test(line);
82
+ }
83
+ function escapeRegExp(value) {
84
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
85
+ }
@@ -1,9 +1,22 @@
1
1
  import { type ConstraintRule, type RuleContext, type RuleEvaluationResult, type RuleEvaluator } from '../types';
2
- /** Evaluates file or directory existence constraints. */
2
+ /**
3
+ * Evaluates file/directory presence constraints.
4
+ *
5
+ * Two config shapes are supported:
6
+ * - Glob form: `{ must: 'present' | 'absent' }` scoped by the rule's `include` /
7
+ * `exclude` globs. `present` flags each include glob that matches zero files;
8
+ * `absent` flags each in-scope file that exists.
9
+ * - Explicit form: `{ paths: string[], mode?: 'require' | 'forbid' }` — checks the
10
+ * exact paths relative to the workdir (`require` = must exist, `forbid` = must not).
11
+ */
3
12
  export declare class PathEvaluator implements RuleEvaluator {
4
13
  private readonly fs;
5
14
  constructor();
6
- /** Evaluate required or forbidden paths. */
15
+ /** Evaluate required or forbidden paths in either config form. */
7
16
  evaluate(rule: ConstraintRule, context: RuleContext): Promise<RuleEvaluationResult>;
17
+ /** Glob form driven by `must` + the rule's include/exclude globs. */
18
+ private evaluateGlob;
19
+ /** Explicit form: exact `paths` checked with `mode` require/forbid. */
20
+ private evaluateExplicit;
8
21
  }
9
22
  //# sourceMappingURL=path-evaluator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"path-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/path-evaluator.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAElB,yDAAyD;AACzD,qBAAa,aAAc,YAAW,aAAa;IAC/C,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAiB;;IAMpC,4CAA4C;IACtC,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAgB5F"}
1
+ {"version":3,"file":"path-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/path-evaluator.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAGlB;;;;;;;;;GASG;AACH,qBAAa,aAAc,YAAW,aAAa;IAC/C,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAiB;;IAMpC,kEAAkE;IAC5D,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;IASzF,qEAAqE;YACvD,YAAY;IAuC1B,uEAAuE;YACzD,gBAAgB;CAmBjC"}
@@ -1,15 +1,61 @@
1
1
  import { resolve } from 'node:path';
2
2
  import { NodeFileSystem } from '@gobing-ai/ts-runtime';
3
3
  import { createFinding, } from '../types.js';
4
- /** Evaluates file or directory existence constraints. */
4
+ import { discoverFiles, matchesGlob } from './file-utils.js';
5
+ /**
6
+ * Evaluates file/directory presence constraints.
7
+ *
8
+ * Two config shapes are supported:
9
+ * - Glob form: `{ must: 'present' | 'absent' }` scoped by the rule's `include` /
10
+ * `exclude` globs. `present` flags each include glob that matches zero files;
11
+ * `absent` flags each in-scope file that exists.
12
+ * - Explicit form: `{ paths: string[], mode?: 'require' | 'forbid' }` — checks the
13
+ * exact paths relative to the workdir (`require` = must exist, `forbid` = must not).
14
+ */
5
15
  export class PathEvaluator {
6
16
  fs;
7
17
  constructor() {
8
18
  this.fs = new NodeFileSystem();
9
19
  }
10
- /** Evaluate required or forbidden paths. */
20
+ /** Evaluate required or forbidden paths in either config form. */
11
21
  async evaluate(rule, context) {
12
22
  const config = rule.evaluator.config ?? {};
23
+ const must = config.must;
24
+ if (must === 'present' || must === 'absent') {
25
+ return this.evaluateGlob(rule, context, must);
26
+ }
27
+ return this.evaluateExplicit(rule, context, config);
28
+ }
29
+ /** Glob form driven by `must` + the rule's include/exclude globs. */
30
+ async evaluateGlob(rule, context, must) {
31
+ const include = rule.include ?? ['**'];
32
+ const exclude = rule.exclude ?? [];
33
+ const files = await discoverFiles({ workdir: context.workdir });
34
+ const inScope = (file) => include.some((glob) => matchesGlob(file, glob)) && !exclude.some((glob) => matchesGlob(file, glob));
35
+ const findings = [];
36
+ if (must === 'present') {
37
+ for (const pattern of include) {
38
+ const present = files.some((file) => matchesGlob(file, pattern) && !exclude.some((g) => matchesGlob(file, g)));
39
+ if (!present) {
40
+ findings.push(createFinding(rule, `expected files matching "${pattern}", but none found`, pattern, {
41
+ code: 'path:missing',
42
+ }));
43
+ }
44
+ }
45
+ }
46
+ else {
47
+ for (const file of files) {
48
+ if (inScope(file)) {
49
+ findings.push(createFinding(rule, 'file should be absent (forbidden by rule)', file, {
50
+ code: 'path:forbidden',
51
+ }));
52
+ }
53
+ }
54
+ }
55
+ return { findings, fixes: [] };
56
+ }
57
+ /** Explicit form: exact `paths` checked with `mode` require/forbid. */
58
+ async evaluateExplicit(rule, context, config) {
13
59
  const paths = arrayConfig(config, 'paths');
14
60
  const mode = stringConfig(config, 'mode', 'require');
15
61
  const findings = [];
@@ -31,7 +77,7 @@ function arrayConfig(config, key) {
31
77
  return value;
32
78
  if (typeof value === 'string')
33
79
  return [value];
34
- throw new Error(`path evaluator requires string[] config "${key}"`);
80
+ throw new Error(`path evaluator requires config "${key}" (string[]) or "must" (present|absent)`);
35
81
  }
36
82
  function stringConfig(config, key, fallback) {
37
83
  const value = config[key];
@@ -1 +1 @@
1
- {"version":3,"file":"regex-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/regex-evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAGlB,qEAAqE;AACrE,qBAAa,cAAe,YAAW,aAAa;;IAGhD,4DAA4D;IACtD,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;CA2B5F"}
1
+ {"version":3,"file":"regex-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/regex-evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAGlB,qEAAqE;AACrE,qBAAa,cAAe,YAAW,aAAa;;IAGhD,4DAA4D;IACtD,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAuC5F"}
@@ -6,26 +6,61 @@ export class RegexEvaluator {
6
6
  /** Evaluate regex-based presence or absence constraints. */
7
7
  async evaluate(rule, context) {
8
8
  const config = rule.evaluator.config ?? {};
9
- const pattern = stringConfig(config, 'pattern');
9
+ const { pattern, flags } = normalizePattern(stringConfig(config, 'pattern'), stringConfig(config, 'flags', ''), config.multiline === true);
10
10
  const mode = stringConfig(config, 'mode', 'forbid');
11
- const flags = stringConfig(config, 'flags', 'm');
12
11
  const regex = new RegExp(pattern, flags);
13
12
  const files = await discoverFiles({ workdir: context.workdir, include: rule.include, exclude: rule.exclude });
14
13
  const findings = [];
15
14
  for (const file of files) {
16
15
  const content = await readWorkdirFile(context.workdir, file);
17
- regex.lastIndex = 0;
18
- const match = regex.exec(content);
19
- if (mode === 'require' && match === null) {
20
- findings.push(createFinding(rule, `Required pattern not found: ${pattern}`, file, { code: 'regex:missing' }));
16
+ if (mode === 'require') {
17
+ regex.lastIndex = 0;
18
+ if (!regex.test(content)) {
19
+ findings.push(createFinding(rule, `required pattern not found: ${pattern}`, file, { code: 'regex:missing' }));
20
+ }
21
+ continue;
21
22
  }
22
- if (mode !== 'require' && match !== null) {
23
- findings.push(createFinding(rule, `Forbidden pattern found: ${pattern}`, file, { code: 'regex:found' }));
23
+ // forbid: report each matching line so findings carry precise locations.
24
+ for (const [index, line] of content.split('\n').entries()) {
25
+ regex.lastIndex = 0;
26
+ if (regex.test(line)) {
27
+ findings.push(createFinding(rule, `forbidden pattern found: ${pattern}`, file, {
28
+ line: index + 1,
29
+ code: 'regex:found',
30
+ }));
31
+ }
24
32
  }
25
33
  }
26
34
  return { findings, fixes: [] };
27
35
  }
28
36
  }
37
+ /**
38
+ * Normalize a pattern + flags for JS `RegExp`.
39
+ *
40
+ * Accepts a leading `(?i)`/`(?im)` inline flag group (ripgrep/PCRE style) and
41
+ * folds it into the JS flags. `multiline` adds the `s` (dotAll) flag so `.`
42
+ * spans newlines, matching the old `--multiline` behavior. `m` is always set so
43
+ * `^`/`$` work per line.
44
+ */
45
+ function normalizePattern(rawPattern, rawFlags, multiline) {
46
+ const flagSet = new Set(['m']);
47
+ for (const flag of rawFlags) {
48
+ if ('gimsuy'.includes(flag))
49
+ flagSet.add(flag);
50
+ }
51
+ let pattern = rawPattern;
52
+ const inline = /^\(\?([a-z]+)\)/.exec(pattern);
53
+ if (inline) {
54
+ for (const flag of inline[1] ?? '') {
55
+ if ('imsu'.includes(flag))
56
+ flagSet.add(flag);
57
+ }
58
+ pattern = pattern.slice(inline[0].length);
59
+ }
60
+ if (multiline)
61
+ flagSet.add('s');
62
+ return { pattern, flags: [...flagSet].join('') };
63
+ }
29
64
  function stringConfig(config, key, fallback) {
30
65
  const value = config[key];
31
66
  if (typeof value === 'string')
@@ -0,0 +1,21 @@
1
+ import { type ConstraintRule, type RuleContext, type RuleEvaluationResult, type RuleEvaluator } from '../types';
2
+ /**
3
+ * Evaluates JSON schema artifact files for structural integrity.
4
+ *
5
+ * Pure JS — no subprocess. Validates JSON validity, title, required properties,
6
+ * `$defs` / `definitions` entries, and the presence of a top-level `required` array.
7
+ *
8
+ * ## Options (in `evaluator.config`)
9
+ * - `file` — path to the JSON file relative to the workdir (required).
10
+ * - `requiredTitle` — expected `title` value (optional).
11
+ * - `requiredProperties` — top-level `properties` keys that must exist (optional).
12
+ * - `requiredDefs` — `$defs` or `definitions` keys that must exist (optional).
13
+ * - `requireRequiredArray` — enforce that `required` is a non-empty array (default: `false`).
14
+ */
15
+ export declare class SchemaArtifactEvaluator implements RuleEvaluator {
16
+ private readonly fs;
17
+ constructor();
18
+ /** Evaluate the configured JSON schema artifact. */
19
+ evaluate(rule: ConstraintRule, context: RuleContext): Promise<RuleEvaluationResult>;
20
+ }
21
+ //# sourceMappingURL=schema-artifact-evaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-artifact-evaluator.d.ts","sourceRoot":"","sources":["../../src/evaluators/schema-artifact-evaluator.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,KAAK,cAAc,EAEnB,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EACrB,MAAM,UAAU,CAAC;AAElB;;;;;;;;;;;;GAYG;AACH,qBAAa,uBAAwB,YAAW,aAAa;IACzD,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAiB;;IAMpC,oDAAoD;IAC9C,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAiG5F"}