Root — commitlint.config.js

Module: root-commitlint-config-js Cohesion: 0.80 Members: 0

Root — commitlint.config.js

This document provides a comprehensive overview of the commitlint.config.js module, which defines the rules for commit message linting within the project.

commitlint.config.js

This file serves as the central configuration for commitlint, a tool that helps your team adhere to a consistent commit message convention. By enforcing a structured format, it improves the readability of the Git history, facilitates automated changelog generation, and enables better understanding of changes at a glance.

Purpose

The primary purpose of commitlint.config.js is to:

How it Works

commitlint.config.js is a Node.js module that exports an object containing configuration options for the commitlint CLI tool. When a developer attempts to commit changes, a Git hook (typically managed by husky) invokes commitlint. commitlint then reads this configuration file to determine the rules against which the commit message should be validated. If the commit message violates any of the defined rules, the commit operation is aborted, and an error message is displayed.

graph TD
    A[Developer Commits Code] --> B{Git Hook (e.g., Husky)};
    B --> C[commitlint CLI];
    C --> D[commitlint.config.js];
    D -- Defines Rules --> C;
    C -- Validates Commit Message --> B;
    B -- Success/Failure --> A;

Key Configuration Details

The commitlint.config.js file extends a base configuration and then customizes specific rules.

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      2,
      'always',
      [
        'feat',     class="hl-cmt">// New feature
        'fix',      class="hl-cmt">// Bug fix
        'docs',     class="hl-cmt">// Documentation only changes
        'style',    class="hl-cmt">// Code style changes (formatting, etc)
        'refactor', class="hl-cmt">// Code refactoring
        'perf',     class="hl-cmt">// Performance improvements
        'test',     class="hl-cmt">// Adding or updating tests
        'chore',    class="hl-cmt">// Maintenance tasks
        'ci',       class="hl-cmt">// CI/CD changes
        'build',    class="hl-cmt">// Build system changes
        'revert',   class="hl-cmt">// Revert a previous commit
      ],
    ],
    'subject-case': [0], class="hl-cmt">// Allow any case for subject
    'subject-max-length': [2, 'always', 100],
  },
};

extends: ['@commitlint/config-conventional']

This line is crucial. It tells commitlint to inherit the rules defined by the @commitlint/config-conventional package. This package provides a robust baseline for the Conventional Commits specification, including rules for type, scope, subject, body, and footer. By extending this, we leverage a well-established standard and only need to override or add specific rules.

rules Object

This object defines or overrides specific linting rules. Each rule is an array with the following structure: [level, applicable, value].

Let's break down the configured rules:

  1. type-enum:

  1. subject-case:

  1. subject-max-length:

Contributing and Modifying

Developers needing to adjust the commit message rules should modify commitlint.config.js.

After any changes, ensure to test them by attempting a commit that would either pass or fail based on your modifications.