Skip to content

feat(rules): make body-max-line-length ignore lines with URLs #4486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion @commitlint/ensure/src/max-line-length.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import ensure from "./max-length.js";

// Allow an exception for long lines which contain URLs.
//
// This is overly lenient, in order to avoid costly regexps which
// have to worry about all the many edge cases of valid URLs.
const URL_REGEX = /\bhttps?:\/\/\S+/;

export default (value: string, max: number): boolean =>
typeof value === "string" &&
value.split(/\r?\n/).every((line) => ensure(line, max));
value
.split(/\r?\n/)
.every((line) => URL_REGEX.test(line) || ensure(line, max));
60 changes: 53 additions & 7 deletions @commitlint/rules/src/body-max-line-length.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { test, expect } from "vitest";
import parse from "@commitlint/parse";
import type { Commit } from "conventional-commits-parser";
import { bodyMaxLineLength } from "./body-max-line-length.js";

const short = "a";
const long = "ab";
const url = "https://example.com/URL/with/a/very/long/path";

const value = short.length;

Expand All @@ -13,14 +15,30 @@ const messages = {
long: `test: subject\n${long}`,
shortMultipleLines: `test:subject\n${short}\n${short}\n${short}`,
longMultipleLines: `test:subject\n${short}\n${long}\n${short}`,
};
urlStandalone: `test:subject\n${short}\n${url}\n${short}`,
urlMarkdownLinkInline: `test:subject

This is a [link](${url}).`,
urlMarkdownLinkInList: `test:subject

Link in a list:

- ${url}`,
urlMarkdownLinkInFooter: `test:subject

const parsed = {
empty: parse(messages.empty),
short: parse(messages.short),
long: parse(messages.long),
Finally, [link][] via footer.

[link]: ${url}`,
};

const parsed = Object.entries(messages).reduce(
(_parsed, [key, message]) =>
Object.assign(_parsed, {
[key]: parse(message),
}),
{} as Record<keyof typeof messages, Promise<Commit>>,
);

test("with empty should succeed", async () => {
const [actual] = bodyMaxLineLength(await parsed.empty, undefined, value);
const expected = true;
Expand All @@ -40,13 +58,41 @@ test("with long should fail", async () => {
});

test("with short with multiple lines should succeed", async () => {
const [actual] = bodyMaxLineLength(await parsed.short, undefined, value);
const [actual] = bodyMaxLineLength(
await parsed.shortMultipleLines,
undefined,
value,
);
const expected = true;
expect(actual).toEqual(expected);
});

test("with long with multiple lines should fail", async () => {
const [actual] = bodyMaxLineLength(await parsed.long, undefined, value);
const [actual] = bodyMaxLineLength(
await parsed.longMultipleLines,
undefined,
value,
);
const expected = false;
expect(actual).toEqual(expected);
});

test("with multiple lines and standalone URL should succeed", async () => {
const [actual] = bodyMaxLineLength(
await parsed.urlStandalone,
undefined,
value,
);
const expected = true;
expect(actual).toEqual(expected);
});

test("with multiple lines and URL in inline Markdown link should succeed", async () => {
const [actual] = bodyMaxLineLength(
await parsed.urlMarkdownLinkInline,
undefined,
30,
);
const expected = true;
expect(actual).toEqual(expected);
});
4 changes: 2 additions & 2 deletions docs/reference/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

## body-max-line-length

- **condition**: `body` lines has `value` or less characters
- **condition**: `body` lines have `value` or less characters, or contain a URL
- **rule**: `always`
- **value**

Expand Down Expand Up @@ -97,7 +97,7 @@

## footer-max-line-length

- **condition**: `footer` lines has `value` or less characters
- **condition**: `footer` lines have `value` or less characters
- **rule**: `always`
- **value**

Expand Down