From bf7055cc8b6b30c453914f859f2e4bdbe6c3893a Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Wed, 9 Apr 2025 20:31:40 -0300 Subject: [PATCH 1/3] perf(rules): optimize header-trim --- @commitlint/rules/src/header-trim.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/@commitlint/rules/src/header-trim.ts b/@commitlint/rules/src/header-trim.ts index 289c1881a0..6e14d36bc7 100644 --- a/@commitlint/rules/src/header-trim.ts +++ b/@commitlint/rules/src/header-trim.ts @@ -8,23 +8,20 @@ export const headerTrim: SyncRule = (parsed) => { return [true]; } - const startsWithWhiteSpace = header !== header.trimStart(); - const endsWithWhiteSpace = header !== header.trimEnd(); + const startsWithWhiteSpace = header.length > header.trimStart().length; + const endsWithWhiteSpace = header.length > header.trimEnd().length; - switch (true) { - case startsWithWhiteSpace && endsWithWhiteSpace: - return [ - false, - message(["header", "must not be surrounded by whitespace"]), - ]; + if (startsWithWhiteSpace && endsWithWhiteSpace) + return [ + false, + message(["header", "must not be surrounded by whitespace"]), + ]; - case startsWithWhiteSpace: - return [false, message(["header", "must not start with whitespace"])]; + if (startsWithWhiteSpace) + return [false, message(["header", "must not start with whitespace"])]; - case endsWithWhiteSpace: - return [false, message(["header", "must not end with whitespace"])]; + if (endsWithWhiteSpace) + return [false, message(["header", "must not end with whitespace"])]; - default: - return [true]; - } + return [true]; }; From a9f78cecae1916a0a62d85fb0f005b0c2617a07f Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Wed, 9 Apr 2025 20:48:34 -0300 Subject: [PATCH 2/3] Simplify other lines --- @commitlint/rules/src/header-trim.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/@commitlint/rules/src/header-trim.ts b/@commitlint/rules/src/header-trim.ts index 6e14d36bc7..293d458223 100644 --- a/@commitlint/rules/src/header-trim.ts +++ b/@commitlint/rules/src/header-trim.ts @@ -4,18 +4,14 @@ import { SyncRule } from "@commitlint/types"; export const headerTrim: SyncRule = (parsed) => { const { header } = parsed; - if (!header) { + if (!header) return [true]; - } const startsWithWhiteSpace = header.length > header.trimStart().length; const endsWithWhiteSpace = header.length > header.trimEnd().length; if (startsWithWhiteSpace && endsWithWhiteSpace) - return [ - false, - message(["header", "must not be surrounded by whitespace"]), - ]; + return [false, message(["header", "must not be surrounded by whitespace"])]; if (startsWithWhiteSpace) return [false, message(["header", "must not start with whitespace"])]; From 5bc14eed5f02cd62ce6fbf8cf580dcb88ac00534 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Thu, 10 Apr 2025 11:39:38 -0300 Subject: [PATCH 3/3] chore: fix formatting --- @commitlint/rules/src/header-trim.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/@commitlint/rules/src/header-trim.ts b/@commitlint/rules/src/header-trim.ts index 293d458223..05edeab268 100644 --- a/@commitlint/rules/src/header-trim.ts +++ b/@commitlint/rules/src/header-trim.ts @@ -4,8 +4,7 @@ import { SyncRule } from "@commitlint/types"; export const headerTrim: SyncRule = (parsed) => { const { header } = parsed; - if (!header) - return [true]; + if (!header) return [true]; const startsWithWhiteSpace = header.length > header.trimStart().length; const endsWithWhiteSpace = header.length > header.trimEnd().length;