fix(types): allow context parameter in QualifiedRuleConfig functions#4636
Conversation
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 conventional-changelog#4357
Review Summary by QodoAllow context parameter in QualifiedRuleConfig rule functions
WalkthroughsDescription• Add RuleConfigContext interface with optional cwd property • Update QualifiedRuleConfig type to accept optional context parameter • Enable rule functions to receive context for dynamic configuration • Add comprehensive type tests for sync/async functions with/without context Diagramflowchart LR
A["QualifiedRuleConfig Type"] -->|"Add context support"| B["Accept ctx?: RuleConfigContext"]
C["RuleConfigContext Interface"] -->|"Define context shape"| D["cwd?: string property"]
B -->|"Enable usage"| E["Rule functions with context"]
E -->|"Example"| F["scope-enum with dynamic scopes"]
File Changes1. @commitlint/types/src/rules.ts
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewⓘ The new review experience is currently in Beta. Learn more |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Pull request overview
Updates @commitlint/types rule configuration typing to support rule config functions that accept an optional context object (currently with cwd?: string), aligning TypeScript types with existing runtime behavior and unblocking configs like @commitlint/config-nx-scopes (Fixes #4357).
Changes:
- Introduces a new exported
RuleConfigContextinterface (cwd?: string). - Extends
QualifiedRuleConfigto allow sync/async rule config functions with an optionalctx?: RuleConfigContextparameter. - Adds
.test-d.tstype tests to assert both context-accepting and contextless functions remain assignable.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| @commitlint/types/src/rules.ts | Expands QualifiedRuleConfig function signatures and exports RuleConfigContext. |
| @commitlint/types/src/rules.test-d.ts | Adds compile-time type tests covering the new context parameter and backward compatibility. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks! |
Fixes #4357
Description
This PR updates the
QualifiedRuleConfigtype in@commitlint/typesto allow rule configuration functions to accept an optional context parameter with acwdproperty.Previously, the type only supported functions with no parameters:
Now it also supports functions with an optional context parameter:
A new
RuleConfigContextinterface is introduced:Motivation and Context
Users using configs like
@commitlint/config-nx-scopesneed to pass a context parameter to rule functions. The JavaScript implementation already supports this pattern (context is optional and defaults to empty object), but the TypeScript types were preventing this usage.This allows users to write configs like:
Usage examples
How Has This Been Tested?
@commitlint/types/src/rules.test-d.tsto verify that:npx vitest run @commitlint/types)npx tsc -b)Types of changes
Checklist:
Diff stats: