Skip to content

Commit 17537ae

Browse files
authored
fix(types): allow context parameter in QualifiedRuleConfig functions (#4636)
Add RuleConfigContext interface and update QualifiedRuleConfig type to accept functions with an optional context parameter, enabling rules like scope-enum to receive context with cwd property. Fixes #4357
1 parent a746981 commit 17537ae

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

@commitlint/types/src/rules.test-d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,31 @@ const _scopeCaseSimpleCheck: Partial<RulesConfig> = {
4444
"scope-case": _scopeCaseSimple,
4545
};
4646
void _scopeCaseSimpleCheck;
47+
48+
// Tests for context parameter support:
49+
// https://github.com/conventional-changelog/commitlint/issues/4357
50+
// Rule functions should accept an optional context parameter with cwd.
51+
52+
// Sync function with context
53+
const _syncWithCtx: Partial<RulesConfig> = {
54+
"scope-enum": (ctx) => [ERROR, "always", ["foo", ctx?.cwd || "bar"]],
55+
};
56+
void _syncWithCtx;
57+
58+
// Async function with context
59+
const _asyncWithCtx: Partial<RulesConfig> = {
60+
"scope-enum": async (ctx) => [ERROR, "always", ["foo", ctx?.cwd || "bar"]],
61+
};
62+
void _asyncWithCtx;
63+
64+
// Function without context (backward compatibility)
65+
const _funcNoCtx: Partial<RulesConfig> = {
66+
"scope-enum": () => [ERROR, "always", ["foo", "bar"]],
67+
};
68+
void _funcNoCtx;
69+
70+
// Async function without context (backward compatibility)
71+
const _asyncNoCtx: Partial<RulesConfig> = {
72+
"scope-enum": async () => [ERROR, "always", ["foo", "bar"]],
73+
};
74+
void _asyncNoCtx;

@commitlint/types/src/rules.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ export enum RuleConfigQuality {
6464
Qualified,
6565
}
6666

67+
export interface RuleConfigContext {
68+
cwd?: string;
69+
}
70+
6771
export type QualifiedRuleConfig<T> =
68-
| (() => RuleConfigTuple<T>)
69-
| (() => Promise<RuleConfigTuple<T>>)
72+
| ((ctx?: RuleConfigContext) => RuleConfigTuple<T>)
73+
| ((ctx?: RuleConfigContext) => Promise<RuleConfigTuple<T>>)
7074
| RuleConfigTuple<T>;
7175

7276
export type RuleConfig<

0 commit comments

Comments
 (0)