The linting system provides types for executing lint operations on commit messages and processing the results. This includes configuration options, outcome reporting, and integration with the parser and rule systems.
interface LintOptions {
/** If it should ignore the default commit messages (defaults to `true`) */
defaultIgnores?: IsIgnoredOptions["defaults"];
/** Additional commits to ignore, defined by ignore matchers */
ignores?: IsIgnoredOptions["ignores"];
/** The parser configuration to use when linting the commit */
parserOpts?: Options;
plugins?: PluginRecords;
helpUrl?: string;
}Configuration options for lint operations:
type Matcher = (commit: string) => boolean;
interface IsIgnoredOptions {
ignores?: Matcher[];
defaults?: boolean;
}System for defining which commits should be ignored during linting:
type LintRuleConfig = Record<
string,
| Readonly<[RuleConfigSeverity.Disabled]>
| RuleConfigTuple<void>
| RuleConfigTuple<unknown>
>;Mapping of rule names to their configuration tuples, used specifically in linting contexts.
interface LintOutcome {
/** The linted commit, as string */
input: string;
/** If the linted commit is considered valid */
valid: boolean;
/** All errors, per rule, for the commit */
errors: LintRuleOutcome[];
/** All warnings, per rule, for the commit */
warnings: LintRuleOutcome[];
}Complete result of linting a single commit message:
interface LintRuleOutcome {
/** If the commit is considered valid for the rule */
valid: boolean;
/** The "severity" of the rule (1 = warning, 2 = error) */
level: RuleConfigSeverity;
/** The name of the rule */
name: string;
/** The message returned from the rule, if invalid */
message: string;
}Result of applying a single rule to a commit:
import { LintOptions, Matcher } from "@commitlint/types";
// Custom ignore patterns
const customIgnores: Matcher[] = [
(commit) => commit.startsWith("WIP:"),
(commit) => commit.includes("[skip ci]"),
(commit) => /^Merge branch/.test(commit)
];
const lintOptions: LintOptions = {
defaultIgnores: true,
ignores: customIgnores,
parserOpts: {
headerPattern: /^(\w*)(?:\(([\w$.\-* ]*)\))?: (.*)$/,
headerCorrespondence: ["type", "scope", "subject"]
}
};import { LintOutcome, RuleConfigSeverity } from "@commitlint/types";
function processLintResult(result: LintOutcome): void {
console.log(`Linting: "${result.input}"`);
if (result.valid) {
console.log("✅ Commit message is valid");
return;
}
// Process errors (severity 2)
if (result.errors.length > 0) {
console.log("❌ Errors:");
result.errors.forEach(error => {
console.log(` - ${error.name}: ${error.message}`);
});
}
// Process warnings (severity 1)
if (result.warnings.length > 0) {
console.log("⚠️ Warnings:");
result.warnings.forEach(warning => {
console.log(` - ${warning.name}: ${warning.message}`);
});
}
}import { Matcher, IsIgnoredOptions } from "@commitlint/types";
// Ignore commits from dependabot
const ignoreDependabot: Matcher = (commit: string) => {
return commit.includes("dependabot") || commit.startsWith("Bump ");
};
// Ignore revert commits
const ignoreReverts: Matcher = (commit: string) => {
return commit.startsWith("Revert ");
};
// Ignore commits with specific prefixes
const ignorePrefix: Matcher = (commit: string) => {
const ignoredPrefixes = ["chore(release):", "docs(readme):", "test:"];
return ignoredPrefixes.some(prefix => commit.startsWith(prefix));
};
const ignoreOptions: IsIgnoredOptions = {
defaults: true, // Keep default ignore patterns
ignores: [ignoreDependabot, ignoreReverts, ignorePrefix]
};import { LintRuleConfig, RuleConfigSeverity } from "@commitlint/types";
const lintRules: LintRuleConfig = {
"type-enum": [RuleConfigSeverity.Error, "always", ["feat", "fix", "docs"]],
"type-case": [RuleConfigSeverity.Error, "always", "lower-case"],
"subject-case": [RuleConfigSeverity.Warning, "always", "sentence-case"],
"subject-max-length": [RuleConfigSeverity.Error, "always", 50],
"body-leading-blank": [RuleConfigSeverity.Warning, "always"],
"footer-leading-blank": [RuleConfigSeverity.Warning, "always"]
};import { LintRuleOutcome, RuleConfigSeverity } from "@commitlint/types";
function analyzeRuleViolations(outcomes: LintRuleOutcome[]): void {
const errorsByRule = new Map<string, number>();
const warningsByRule = new Map<string, number>();
outcomes.forEach(outcome => {
if (!outcome.valid) {
const map = outcome.level === RuleConfigSeverity.Error ? errorsByRule : warningsByRule;
const count = map.get(outcome.name) || 0;
map.set(outcome.name, count + 1);
}
});
console.log("Most common rule violations:");
console.log("Errors:", Array.from(errorsByRule.entries()));
console.log("Warnings:", Array.from(warningsByRule.entries()));
}