From 7dd88c913cba9f444acc587c77210cb718c928c9 Mon Sep 17 00:00:00 2001 From: wtho Date: Thu, 28 Apr 2022 03:35:49 +0200 Subject: [PATCH] fix(rules): footer-leading-blank should work with body comments (#3139) --- @commitlint/rules/src/footer-leading-blank.test.ts | 14 ++++++++++++++ @commitlint/rules/src/footer-leading-blank.ts | 6 +++--- README.md | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/@commitlint/rules/src/footer-leading-blank.test.ts b/@commitlint/rules/src/footer-leading-blank.test.ts index 6af6e80117..dcfd0430ba 100644 --- a/@commitlint/rules/src/footer-leading-blank.test.ts +++ b/@commitlint/rules/src/footer-leading-blank.test.ts @@ -8,6 +8,8 @@ const messages = { without: 'test: subject\nbody\nBREAKING CHANGE: something important', withoutBody: 'feat(new-parser): introduces a new parsing library\n\nBREAKING CHANGE: new library does not support foo-construct', + withBodyWithComment: + 'feat(new-parser): introduces a new parsing library\n\nBody Line 1\n# comment\nBody Line 2\n\nBREAKING CHANGE: new library does not support foo-construct', with: 'test: subject\nbody\n\nBREAKING CHANGE: something important', withMulitLine: 'test: subject\nmulti\nline\nbody\n\nBREAKING CHANGE: something important', @@ -20,6 +22,9 @@ const parsed = { trailing: parse(messages.trailing), without: parse(messages.without), withoutBody: parse(messages.withoutBody), + withBodyWithComment: parse(messages.withBodyWithComment, undefined, { + commentChar: '#', + }), with: parse(messages.with), withMulitLine: parse(messages.withMulitLine), withDoubleNewLine: parse(messages.withDoubleNewLine), @@ -156,3 +161,12 @@ test('with double blank line before footer and double line in body should succee const expected = true; expect(actual).toEqual(expected); }); + +test('with body containing comments should succeed for "always"', async () => { + const [actual] = footerLeadingBlank( + await parsed.withBodyWithComment, + 'always' + ); + const expected = true; + expect(actual).toEqual(expected); +}); diff --git a/@commitlint/rules/src/footer-leading-blank.ts b/@commitlint/rules/src/footer-leading-blank.ts index c3aff43373..ad8e2756bc 100644 --- a/@commitlint/rules/src/footer-leading-blank.ts +++ b/@commitlint/rules/src/footer-leading-blank.ts @@ -10,9 +10,9 @@ export const footerLeadingBlank: SyncRule = (parsed, when = 'always') => { const negated = when === 'never'; const rawLines = toLines(parsed.raw); - const bodyLines = parsed.body ? toLines(parsed.body) : []; - const bodyOffset = bodyLines.length > 0 ? rawLines.indexOf(bodyLines[0]) : 1; - const [leading] = rawLines.slice(bodyLines.length + bodyOffset); + const footerLines = toLines(parsed.footer); + const footerOffset = rawLines.indexOf(footerLines[0]); + const [leading] = rawLines.slice(footerOffset - 1); // Check if the first line of footer is empty const succeeds = leading === ''; diff --git a/README.md b/README.md index 4323fe9b90..b5d2140f2a 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ Features will only be applied to the current main version. _Dates are subject to change._ -We're not a sponsored OSS project. Therefor we can't promise that we will release patch versions for older releases in a timley manner.\ +We're not a sponsored OSS project. Therefore we can't promise that we will release patch versions for older releases in a timely manner.\ If you are stuck on an older version and need a security patch we're happy if you can provide a PR. ## Related projects