Skip to content

Commit 720e751

Browse files
committed
feat!: enable position indicator by default
- Change position indicator from ~ (multiple) to ^ (single caret) - Enable showPosition by default (breaking change) - Aligns with TypeScript/Rust error formatting - Keep --show-position flag to allow disabling BREAKING CHANGE: position indicator is now shown by default. Output format changed from multiple ~ characters to single ^ caret. Users can disable with --show-position=false.
1 parent 21b4b53 commit 720e751

File tree

6 files changed

+26
-30
lines changed

6 files changed

+26
-30
lines changed

@commitlint/cli/src/cli.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ test("should print help", async () => {
606606
-q, --quiet toggle console output [boolean] [default: false]
607607
-t, --to upper end of the commit range to lint; applies if edit=false [string]
608608
-V, --verbose enable verbose output for reports without problems [boolean]
609-
--show-position show position of error in output [boolean]
609+
--show-position show position of error in output [boolean] [default: true]
610610
-s, --strict enable strict mode; result code 2 for warnings, 3 for errors [boolean]
611611
--options path to a JSON file or Common.js module containing CLI options
612612
-v, --version display version information [boolean]

@commitlint/cli/src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ const cli = yargs(process.argv.slice(2))
136136
},
137137
"show-position": {
138138
type: "boolean",
139+
default: true,
139140
description: "show position of error in output",
140141
},
141142
strict: {

@commitlint/format/src/format.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ test("shows position indicator when showPosition is true and error has position"
328328
},
329329
);
330330

331-
expect(actual).toContain("~~~");
331+
expect(actual).toContain("^");
332332
});
333333

334334
test("does not show position indicator when showPosition is false", () => {
@@ -355,10 +355,10 @@ test("does not show position indicator when showPosition is false", () => {
355355
},
356356
);
357357

358-
expect(actual).not.toContain("~~~");
358+
expect(actual).not.toContain("^");
359359
});
360360

361-
test("does not show position indicator when showPosition is not provided", () => {
361+
test("shows position indicator when showPosition is not provided (default)", () => {
362362
const actual = format(
363363
{
364364
results: [
@@ -381,7 +381,7 @@ test("does not show position indicator when showPosition is not provided", () =>
381381
},
382382
);
383383

384-
expect(actual).not.toContain("~~~");
384+
expect(actual).toContain("^");
385385
});
386386

387387
test("does not show position indicator when error has no position", () => {
@@ -406,7 +406,7 @@ test("does not show position indicator when error has no position", () => {
406406
},
407407
);
408408

409-
expect(actual).not.toContain("~~~");
409+
expect(actual).not.toContain("^");
410410
});
411411

412412
test("shows correct position for subject error", () => {
@@ -434,10 +434,10 @@ test("shows correct position for subject error", () => {
434434
},
435435
);
436436

437-
expect(actual).toContain("~~~~~~~~");
437+
expect(actual).toContain("^");
438438
});
439439

440-
test("shows position indicator with multiple tildes for longer errors", () => {
440+
test("shows position indicator with single caret for longer errors", () => {
441441
const actual = format(
442442
{
443443
results: [
@@ -462,7 +462,5 @@ test("shows position indicator with multiple tildes for longer errors", () => {
462462
},
463463
);
464464

465-
expect(actual).toContain(
466-
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
467-
);
465+
expect(actual).toContain("^");
468466
});

@commitlint/format/src/format.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function formatInput(
3838
result: FormattableResult & WithInput,
3939
options: FormatOptions = {},
4040
): string[] {
41-
const { color: enabled = true, showPosition = false } = options;
41+
const { color: enabled = true, showPosition = true } = options;
4242
const { errors = [], warnings = [], input = "" } = result;
4343

4444
if (!input) {
@@ -47,19 +47,20 @@ function formatInput(
4747

4848
const sign = "⧗";
4949
const decoration = enabled ? pc.gray(sign) : sign;
50+
const prefix = `${decoration} input: `;
5051

5152
const decoratedInput = enabled ? pc.bold(input) : input;
5253
const hasProblems = errors.length > 0 || warnings.length > 0;
5354

5455
if (!hasProblems) {
55-
return options.verbose ? [`${decoration} input: ${decoratedInput}`] : [];
56+
return options.verbose ? [`${prefix}${decoratedInput}`] : [];
5657
}
5758

5859
const positionIndicator = showPosition
59-
? getPositionIndicator([...errors, ...warnings], input)
60+
? getPositionIndicator([...errors, ...warnings], input, prefix.length)
6061
: undefined;
6162

62-
const lines: string[] = [`${decoration} input: ${decoratedInput}`];
63+
const lines: string[] = [`${prefix}${decoratedInput}`];
6364

6465
if (positionIndicator) {
6566
lines.push(positionIndicator);
@@ -71,6 +72,7 @@ function formatInput(
7172
function getPositionIndicator(
7273
problems: FormattableProblem[],
7374
input: string,
75+
prefixLength: number,
7476
): string | undefined {
7577
const problemWithPosition = problems.find(
7678
(problem) => problem?.start !== undefined && problem?.end !== undefined,
@@ -79,28 +81,21 @@ function getPositionIndicator(
7981
return undefined;
8082
}
8183

82-
const { start, end } = problemWithPosition;
83-
const padding = " ";
84+
const padding = " ".repeat(prefixLength);
8485

85-
const tilde = "~";
86+
const caret = "^";
8687

8788
const normalizedInput = input.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
8889
const lines = normalizedInput.split("\n");
89-
const targetLine = lines[start.line - 1];
90+
const targetLine = lines[problemWithPosition.start.line - 1];
9091

9192
if (!targetLine) {
9293
return undefined;
9394
}
9495

95-
const lineLength = targetLine.length;
96-
const spacesBefore = Math.max(0, start.column - 1);
97-
const tildeLength = Math.max(
98-
1,
99-
Math.min(end.column - start.column, lineLength - (start.column - 1)),
100-
);
96+
const spacesBefore = Math.max(0, problemWithPosition.start.column - 1);
10197

102-
const indicator =
103-
padding + " ".repeat(spacesBefore) + tilde.repeat(tildeLength);
98+
const indicator = padding + " ".repeat(spacesBefore) + caret;
10499

105100
return indicator;
106101
}

@commitlint/lint/src/lint.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ function getRulePosition(
107107
}
108108
return undefined;
109109
}
110-
const subjectStart = raw.indexOf(parsed.subject);
111-
if (subjectStart === -1) return undefined;
110+
const typeEnd = parsed.type ? parsed.type.length : 0;
111+
const hasScope = parsed.scope ? parsed.scope.length + 3 : 0;
112+
const separator = ": ".length;
113+
const subjectStart = typeEnd + hasScope + separator;
112114
return {
113115
start: { line: 1, column: subjectStart + 1, offset: subjectStart },
114116
end: {

docs/api/format.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type formatOptions = {
6262
helpUrl: string;
6363

6464
/**
65-
* Show position indicator (~~~) for errors in the input line
65+
* Show position indicator (^) for errors in the input line
6666
**/
6767
showPosition?: boolean;
6868
}

0 commit comments

Comments
 (0)