Skip to content

Commit 226cf37

Browse files
committed
fix: address PR review comments
- Fix type position detection to use startsWith instead of indexOf - Show position indicator for warnings too (not just errors) - Fix tilde length calculation for body/footer multi-line content - Remove redundant filter check in lint.ts - Fix TypeScript syntax in docs (use optional property)
1 parent 6a56853 commit 226cf37

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

@commitlint/format/src/format.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function formatInput(
5656
}
5757

5858
const positionIndicator = showPosition
59-
? getPositionIndicator(errors, input)
59+
? getPositionIndicator([...errors, ...warnings], input)
6060
: undefined;
6161

6262
const lines: string[] = [`${decoration} input: ${decoratedInput}`];
@@ -91,16 +91,15 @@ function getPositionIndicator(
9191
const headerEndIndex = input.indexOf("\n\n");
9292
if (headerEndIndex === -1) return undefined;
9393

94-
const bodyLineStart = headerEndIndex + 2;
95-
const charsOnLine = input.slice(bodyLineStart).indexOf("\n");
96-
const lineLength =
97-
charsOnLine === -1 ? input.length - bodyLineStart : charsOnLine;
94+
const bodyText = input.slice(headerEndIndex + 2);
95+
const firstBodyLine = bodyText.split("\n")[0];
96+
const lineLength = firstBodyLine.length;
9897

99-
if (start.column <= lineLength) {
98+
if (start.column <= lineLength + 1) {
10099
const spacesBefore = Math.max(0, start.column - 1);
101100
const tildeLength = Math.max(
102101
1,
103-
Math.min(end.column, lineLength) - start.column,
102+
Math.min(end.column - start.column, lineLength - (start.column - 1)),
104103
);
105104
indicator =
106105
padding + " ".repeat(spacesBefore) + tilde.repeat(tildeLength);
@@ -109,12 +108,16 @@ function getPositionIndicator(
109108
const footerStartIndex = input.lastIndexOf("\n\n");
110109
if (footerStartIndex === -1) return undefined;
111110

112-
const footerLineStart = footerStartIndex + 2;
113-
const lineLength = input.length - footerLineStart;
111+
const footerText = input.slice(footerStartIndex + 2);
112+
const firstFooterLine = footerText.split("\n")[0];
113+
const lineLength = firstFooterLine.length;
114114

115-
if (start.column <= lineLength) {
115+
if (start.column <= lineLength + 1) {
116116
const spacesBefore = Math.max(0, start.column - 1);
117-
const tildeLength = Math.max(1, end.column - start.column);
117+
const tildeLength = Math.max(
118+
1,
119+
Math.min(end.column - start.column, lineLength - (start.column - 1)),
120+
);
118121
indicator =
119122
padding + " ".repeat(spacesBefore) + tilde.repeat(tildeLength);
120123
}

@commitlint/lint/src/lint.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ function getRulePosition(
4545
case "type-min-length":
4646
case "type-max-length": {
4747
if (!parsed.type) return undefined;
48-
const offset = raw.indexOf(parsed.type);
49-
if (offset === -1) return undefined;
48+
if (!raw.startsWith(parsed.type)) return undefined;
49+
const offset = 0;
5050
return {
5151
start: { line: 1, column: offset + 1, offset },
5252
end: {
@@ -315,8 +315,7 @@ export default async function lint(
315315
});
316316

317317
const results = (await Promise.all(pendingResults)).filter(
318-
(result): result is LintRuleOutcome =>
319-
result !== null && result.message !== undefined,
318+
(result): result is LintRuleOutcome => result !== null,
320319
);
321320

322321
const errors = results.filter(

docs/api/format.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type formatOptions = {
6464
/**
6565
* Show position indicator (~~~) for errors in the input line
6666
**/
67-
showPosition: boolean = false;
67+
showPosition?: boolean;
6868
}
6969

7070
format(report?: Report = {}, options?: formatOptions = {}) => string[];

0 commit comments

Comments
 (0)