Skip to content

fix(types): allow context parameter in QualifiedRuleConfig functions#4636

Merged
escapedcat merged 1 commit intoconventional-changelog:masterfrom
Br1an67:fix/issue-4357-fix-4357
Mar 7, 2026
Merged

fix(types): allow context parameter in QualifiedRuleConfig functions#4636
escapedcat merged 1 commit intoconventional-changelog:masterfrom
Br1an67:fix/issue-4357-fix-4357

Conversation

@Br1an67
Copy link
Copy Markdown
Contributor

@Br1an67 Br1an67 commented Mar 7, 2026

Fixes #4357

Description

This PR updates the QualifiedRuleConfig type in @commitlint/types to allow rule configuration functions to accept an optional context parameter with a cwd property.

Previously, the type only supported functions with no parameters:

| (() => RuleConfigTuple<T>)
| (() => Promise<RuleConfigTuple<T>>)

Now it also supports functions with an optional context parameter:

| ((ctx?: RuleConfigContext) => RuleConfigTuple<T>)
| ((ctx?: RuleConfigContext) => Promise<RuleConfigTuple<T>>)

A new RuleConfigContext interface is introduced:

export interface RuleConfigContext {
  cwd?: string;
}

Motivation and Context

Users using configs like @commitlint/config-nx-scopes need 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:

import { utils } from '@commitlint/config-nx-scopes';
import type { UserConfig } from '@commitlint/types';
import { RuleConfigSeverity } from '@commitlint/types';

const config: UserConfig = {
  extends: ['@commitlint/config-conventional', '@commitlint/config-nx-scopes'],
  rules: {
    'scope-enum': async (ctx) => [
      RuleConfigSeverity.Error,
      'always',
      ['repo', 'deps', 'release', ...(await utils.getProjects(ctx))],
    ],
  },
};

export default config;

Usage examples

import type { UserConfig, RuleConfigSeverity } from '@commitlint/types';

const config: UserConfig = {
  rules: {
    // Sync function with context
    'scope-enum': (ctx) => [
      RuleConfigSeverity.Error,
      'always',
      ['foo', ctx?.cwd || 'default'],
    ],

    // Async function with context
    'scope-enum': async (ctx) => [
      RuleConfigSeverity.Error,
      'always',
      await getScopes(ctx?.cwd),
    ],

    // Without context (backward compatible)
    'scope-enum': () => [RuleConfigSeverity.Error, 'always', ['foo', 'bar']],
  },
};

How Has This Been Tested?

  • Added type tests in @commitlint/types/src/rules.test-d.ts to verify that:
    • Sync functions with context parameter are accepted
    • Async functions with context parameter are accepted
    • Functions without context parameter still work (backward compatibility)
  • All existing tests pass (npx vitest run @commitlint/types)
  • TypeScript build succeeds (npx tsc -b)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Diff stats:

 @commitlint/types/src/rules.test-d.ts | 32 ++++++++++++++++++++++++++
 @commitlint/types/src/rules.ts        |  8 +++++--
 2 files changed, 38 insertions(+), 2 deletions(-)

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
@qodo-code-review
Copy link
Copy Markdown

Review Summary by Qodo

Allow context parameter in QualifiedRuleConfig rule functions

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• 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
Diagram
flowchart 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"]
Loading

Grey Divider

File Changes

1. @commitlint/types/src/rules.ts ✨ Enhancement +6/-2

Add context parameter support to QualifiedRuleConfig

• Introduce RuleConfigContext interface with optional cwd property
• Update QualifiedRuleConfig type to accept functions with optional context parameter
• Maintain backward compatibility with functions without context

@commitlint/types/src/rules.ts


2. @commitlint/types/src/rules.test-d.ts 🧪 Tests +28/-0

Add type tests for context parameter support

• Add type tests for sync functions with context parameter
• Add type tests for async functions with context parameter
• Add type tests for functions without context (backward compatibility)
• Verify all variations compile correctly with TypeScript

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


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown

qodo-code-review Bot commented Mar 7, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented Mar 7, 2026

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RuleConfigContext interface (cwd?: string).
  • Extends QualifiedRuleConfig to allow sync/async rule config functions with an optional ctx?: RuleConfigContext parameter.
  • Adds .test-d.ts type 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.

@escapedcat
Copy link
Copy Markdown
Member

Thanks!

@escapedcat escapedcat merged commit 17537ae into conventional-changelog:master Mar 7, 2026
16 checks passed
This was referenced Mar 12, 2026
This was referenced Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Wrong @commitlint/types definitions for rules

3 participants