@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
@@ -1,12 +1,24 @@
1
1
  import type { ProcessExecutor } from '@gobing-ai/ts-runtime';
2
2
  import { AgentDetectionEvaluator } from '../evaluators/agent-detection-evaluator';
3
+ import { CoverageGateEvaluator } from '../evaluators/coverage-gate-evaluator';
3
4
  import { ExitCodeEvaluator } from '../evaluators/exit-code-evaluator';
4
5
  import { ForbiddenImportEvaluator } from '../evaluators/forbidden-import-evaluator';
6
+ import { ImportBoundaryEvaluator } from '../evaluators/import-boundary-evaluator';
5
7
  import { PathEvaluator } from '../evaluators/path-evaluator';
6
8
  import { RegexEvaluator } from '../evaluators/regex-evaluator';
9
+ import { SchemaArtifactEvaluator } from '../evaluators/schema-artifact-evaluator';
7
10
  import { SecretsScannerEvaluator } from '../evaluators/secrets-scanner-evaluator';
11
+ import { SgEvaluator } from '../evaluators/sg-evaluator';
12
+ import { TestLocationEvaluator } from '../evaluators/test-location-evaluator';
13
+ import { TsdocExportEvaluator } from '../evaluators/tsdoc-export-evaluator';
8
14
  import { JsonFormatter } from '../formatters/json';
9
15
  import { TextFormatter } from '../formatters/text';
16
+ import {
17
+ GoTestPathResolver,
18
+ PythonTestPathResolver,
19
+ RustTestPathResolver,
20
+ TypeScriptTestPathResolver,
21
+ } from '../resolvers/test-path-resolver';
10
22
  import type { RuleEngineHost } from './rule-engine-host';
11
23
 
12
24
  /** Register bundled evaluators and formatters on a host. */
@@ -21,6 +33,16 @@ export function registerBuiltins(host: RuleEngineHost, executor?: ProcessExecuto
21
33
  host.evaluators.register('exit-code', new ExitCodeEvaluator(executor), 'builtin');
22
34
  host.evaluators.register('secrets-scanner', new SecretsScannerEvaluator(), 'builtin');
23
35
  host.evaluators.register('agent-detection', new AgentDetectionEvaluator(), 'builtin');
36
+ host.evaluators.register('coverage-gate', new CoverageGateEvaluator(), 'builtin');
37
+ host.evaluators.register('tsdoc-export', new TsdocExportEvaluator(), 'builtin');
38
+ host.resolvers.register('typescript', new TypeScriptTestPathResolver(), 'builtin');
39
+ host.resolvers.register('python', new PythonTestPathResolver(), 'builtin');
40
+ host.resolvers.register('go', new GoTestPathResolver(), 'builtin');
41
+ host.resolvers.register('rust', new RustTestPathResolver(), 'builtin');
42
+ host.evaluators.register('test-location', new TestLocationEvaluator(host.resolvers), 'builtin');
43
+ host.evaluators.register('sg', new SgEvaluator(executor), 'builtin');
44
+ host.evaluators.register('schema-artifact', new SchemaArtifactEvaluator(), 'builtin');
45
+ host.evaluators.register('import-boundary', new ImportBoundaryEvaluator(), 'builtin');
24
46
  host.formatters.register('text', new TextFormatter(), 'builtin');
25
47
  host.formatters.register('json', new JsonFormatter(), 'builtin');
26
48
  }
@@ -1,3 +1,4 @@
1
+ import type { TestPathResolver } from '../resolvers/test-path-resolver';
1
2
  import type { ResultFormatter, RuleEvaluator } from '../types';
2
3
  import { CapabilityRegistry } from './capability-registry';
3
4
 
@@ -7,9 +8,12 @@ export class RuleEngineHost {
7
8
  readonly evaluators: CapabilityRegistry<RuleEvaluator>;
8
9
  /** Formatter registry keyed by formatter name. */
9
10
  readonly formatters: CapabilityRegistry<ResultFormatter>;
11
+ /** Test-path resolver registry keyed by language name. */
12
+ readonly resolvers: CapabilityRegistry<TestPathResolver>;
10
13
 
11
14
  constructor() {
12
15
  this.evaluators = new CapabilityRegistry<RuleEvaluator>('evaluator');
13
16
  this.formatters = new CapabilityRegistry<ResultFormatter>('formatter');
17
+ this.resolvers = new CapabilityRegistry<TestPathResolver>('resolver');
14
18
  }
15
19
  }
package/src/index.ts CHANGED
@@ -1,7 +1,11 @@
1
+ export * from './config/extensions';
1
2
  export * from './config/loader';
2
3
  export * from './engine';
4
+ export * from './fixers/fixers';
5
+ export * from './fixers/test-stub-fixer';
3
6
  export * from './formatters/json';
4
7
  export * from './formatters/text';
5
8
  export * from './host/capability-registry';
6
9
  export * from './host/rule-engine-host';
10
+ export * from './resolvers/test-path-resolver';
7
11
  export * from './types';
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Pluggable test-path resolution for the test-location evaluator.
3
+ *
4
+ * Each implementation maps a source file path to its conventional test file path
5
+ * for one language, so the same evaluator works across TypeScript, Python, Go, and
6
+ * Rust projects. Resolvers are registered on the host and selected per rule via
7
+ * `evaluator.config.resolver`.
8
+ */
9
+
10
+ /** Metadata about an exported symbol, used when generating a test skeleton. */
11
+ export interface ExportInfo {
12
+ /** Symbol name. */
13
+ readonly name: string;
14
+ /** Declaration kind. */
15
+ readonly kind: 'function' | 'class' | 'const' | 'type' | 'interface' | 'unknown';
16
+ /** One-based source line, when known. */
17
+ readonly line?: number;
18
+ }
19
+
20
+ /** Maps source files to expected test files for a project type. */
21
+ export interface TestPathResolver {
22
+ /** Language name this resolver handles (registry key). */
23
+ readonly name: string;
24
+ /** Compute the expected test file path for a source file. */
25
+ resolveTestPath(srcRelPath: string): string;
26
+ /** Optionally generate a test skeleton for a source file. */
27
+ generateSkeleton?(srcRelPath: string, exports: ExportInfo[]): string;
28
+ }
29
+
30
+ /**
31
+ * TypeScript conventions:
32
+ * src/foo/bar.ts → tests/foo/bar.test.ts
33
+ * packages/core/src/foo/bar.ts → packages/core/tests/foo/bar.test.ts
34
+ */
35
+ export class TypeScriptTestPathResolver implements TestPathResolver {
36
+ /** Registry key. */
37
+ readonly name = 'typescript';
38
+
39
+ constructor() {}
40
+
41
+ /** Map a TS/JS source path to its `tests/…test.ts` counterpart. */
42
+ resolveTestPath(srcRelPath: string): string {
43
+ if (srcRelPath.includes('.test.') || srcRelPath.includes('.spec.')) return srcRelPath;
44
+ const srcIdx = srcRelPath.indexOf('/src/');
45
+ if (srcIdx !== -1) {
46
+ const pkg = srcRelPath.slice(0, srcIdx);
47
+ const rel = srcRelPath.slice(srcIdx + '/src/'.length).replace(/\.(ts|tsx|js|jsx)$/, '.test.ts');
48
+ return `${pkg}/tests/${rel}`;
49
+ }
50
+ const withoutExt = srcRelPath.replace(/\.(ts|tsx|js|jsx)$/, '');
51
+ return `tests/${withoutExt.replace(/^src\//, '')}.test.ts`;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Python conventions (pytest):
57
+ * src/foo/bar.py → tests/foo/test_bar.py
58
+ */
59
+ export class PythonTestPathResolver implements TestPathResolver {
60
+ /** Registry key. */
61
+ readonly name = 'python';
62
+
63
+ constructor() {}
64
+
65
+ /** Map a Python source path to its `tests/…/test_*.py` counterpart. */
66
+ resolveTestPath(srcRelPath: string): string {
67
+ if (!srcRelPath) throw new Error('empty source path');
68
+ if (srcRelPath.endsWith('_test.py') || srcRelPath.includes('/test_') || srcRelPath.startsWith('test_')) {
69
+ return srcRelPath;
70
+ }
71
+ if (srcRelPath.startsWith('tests/')) return srcRelPath;
72
+ if (!srcRelPath.endsWith('.py')) throw new Error(`unsupported extension for python resolver: ${srcRelPath}`);
73
+ const srcIdx = srcRelPath.indexOf('/src/');
74
+ if (srcIdx !== -1) {
75
+ const pkg = srcRelPath.slice(0, srcIdx);
76
+ return `${pkg}/tests/${testify(srcRelPath.slice(srcIdx + '/src/'.length))}`;
77
+ }
78
+ if (srcRelPath.startsWith('src/')) return `tests/${testify(srcRelPath.slice(4))}`;
79
+ return `tests/${testify(srcRelPath)}`;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Go conventions:
85
+ * foo/bar.go → foo/bar_test.go (sibling)
86
+ */
87
+ export class GoTestPathResolver implements TestPathResolver {
88
+ /** Registry key. */
89
+ readonly name = 'go';
90
+
91
+ constructor() {}
92
+
93
+ /** Map a Go source path to its sibling `_test.go` file. */
94
+ resolveTestPath(srcRelPath: string): string {
95
+ if (!srcRelPath) throw new Error('empty source path');
96
+ if (srcRelPath.endsWith('_test.go')) return srcRelPath;
97
+ if (!srcRelPath.endsWith('.go')) throw new Error(`unsupported extension for go resolver: ${srcRelPath}`);
98
+ return srcRelPath.replace(/\.go$/, '_test.go');
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Rust conventions (Cargo integration tests):
104
+ * crate/src/foo.rs → crate/tests/foo.rs
105
+ */
106
+ export class RustTestPathResolver implements TestPathResolver {
107
+ /** Registry key. */
108
+ readonly name = 'rust';
109
+
110
+ constructor() {}
111
+
112
+ /** Map a Rust source path to its `tests/` integration-test counterpart. */
113
+ resolveTestPath(srcRelPath: string): string {
114
+ if (!srcRelPath) throw new Error('empty source path');
115
+ if (srcRelPath.startsWith('tests/') || srcRelPath.includes('/tests/')) return srcRelPath;
116
+ if (!srcRelPath.endsWith('.rs')) throw new Error(`unsupported extension for rust resolver: ${srcRelPath}`);
117
+ const srcIdx = srcRelPath.indexOf('/src/');
118
+ if (srcIdx !== -1) {
119
+ const crate = srcRelPath.slice(0, srcIdx);
120
+ return `${crate}/tests/${srcRelPath.slice(srcIdx + '/src/'.length)}`;
121
+ }
122
+ if (srcRelPath.startsWith('src/')) return `tests/${srcRelPath.slice(4)}`;
123
+ return `tests/${srcRelPath}`;
124
+ }
125
+ }
126
+
127
+ /** Prefix the basename of a Python source path with `test_`. */
128
+ function testify(rel: string): string {
129
+ const lastSlash = rel.lastIndexOf('/');
130
+ const dir = lastSlash >= 0 ? rel.slice(0, lastSlash + 1) : '';
131
+ const base = lastSlash >= 0 ? rel.slice(lastSlash + 1) : rel;
132
+ return `${dir}test_${base}`;
133
+ }
package/src/types.ts CHANGED
@@ -56,6 +56,18 @@ export interface ConstraintRuleFile {
56
56
  rules: ConstraintRule[];
57
57
  }
58
58
 
59
+ /** Relative module paths a preset contributes per capability kind. */
60
+ export interface PresetExtensions {
61
+ /** Test-path resolver module paths. */
62
+ resolvers?: string[];
63
+ /** Evaluator module paths. */
64
+ evaluators?: string[];
65
+ /** Fixer module paths. */
66
+ fixers?: string[];
67
+ /** Formatter module paths. */
68
+ formatters?: string[];
69
+ }
70
+
59
71
  /** Preset definition that composes category folders or other presets. */
60
72
  export interface PresetDefinition {
61
73
  /** Preset name. */
@@ -66,6 +78,8 @@ export interface PresetDefinition {
66
78
  disable?: string[];
67
79
  /** Per-rule overrides. */
68
80
  overrides?: Record<string, { fix?: { mode: FixMode } }>;
81
+ /** Custom capability modules contributed by this preset (opt-in to load). */
82
+ extensions?: PresetExtensions;
69
83
  }
70
84
 
71
85
  /** Candidate fix emitted by an evaluator or fixer. */
@@ -84,6 +98,16 @@ export interface Fix {
84
98
  mode: Exclude<FixMode, 'none'>;
85
99
  }
86
100
 
101
+ /**
102
+ * What a finding represents.
103
+ *
104
+ * - `violation`: the rule ran and the project breached its policy (the default).
105
+ * - `error`: the rule could not run — a misconfiguration or runtime fault in the
106
+ * evaluator itself. These are not policy breaches and callers may surface them
107
+ * separately (e.g. "rule misconfigured") rather than as project violations.
108
+ */
109
+ export type FindingKind = 'violation' | 'error';
110
+
87
111
  /** Finding emitted by a constraint rule. */
88
112
  export interface ConstraintFinding {
89
113
  /** Rule identifier. */
@@ -100,6 +124,8 @@ export interface ConstraintFinding {
100
124
  column?: number;
101
125
  /** Machine-readable evaluator/source code. */
102
126
  code?: string;
127
+ /** Whether this is a policy violation or an evaluator error. Absent means `violation`. */
128
+ kind?: FindingKind;
103
129
  }
104
130
 
105
131
  /** Aggregate result returned by a rule evaluator. */
@@ -194,4 +220,12 @@ export const PresetDefinitionSchema = z.object({
194
220
  overrides: z
195
221
  .record(z.string(), z.object({ fix: z.object({ mode: z.enum(['none', 'suggest', 'auto']) }).optional() }))
196
222
  .optional(),
223
+ extensions: z
224
+ .object({
225
+ resolvers: z.array(z.string()).optional(),
226
+ evaluators: z.array(z.string()).optional(),
227
+ fixers: z.array(z.string()).optional(),
228
+ formatters: z.array(z.string()).optional(),
229
+ })
230
+ .optional(),
197
231
  });