Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f1f44d

Browse files
fix(prompt): prompt does not respect [body-leading-blank] setting (conventional-changelog#4066)
* fix(prompt): prompt does not respect [body-leading-blank] setting * test(prompt): add unit tests
1 parent 0394629 commit 3f1f44d

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

β€Ž@commitlint/prompt/src/input.test.tsβ€Ž

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
/// <reference path="./inquirer/inquirer.d.ts" />
22

3-
import {test,expect, vi} from 'vitest';
3+
import {expect,test, vi} from 'vitest';
44
// @ts-expect-error -- no typings
55
import config from '@commitlint/config-angular';
66
import chalk from 'chalk';
7-
import {Answers, DistinctQuestion, PromptModule} from 'inquirer';
7+
import {
8+
Answers,
9+
DistinctQuestion,
10+
InputCustomOptions,
11+
PromptModule,
12+
} from 'inquirer';
813

914
import {input} from './input.js';
1015

16+
const testConfig = {
17+
parserPreset: config.parserPreset,
18+
rules: {
19+
...config.rules,
20+
},
21+
};
22+
1123
vi.mock('@commitlint/load', () => ({
12-
default: () => config,
24+
default: () => testConfig,
1325
}));
1426

1527
test('should work with all fields filled', async () => {
28+
const prompt = stub({
29+
'input-custom': {
30+
type: 'fix',
31+
scope: 'test',
32+
subject: 'subject',
33+
body: 'body',
34+
footer: 'footer',
35+
},
36+
});
37+
const message = await input(prompt);
38+
expect(message).toEqual('fix(test): subject\n' + '\nbody\n' + '\nfooter');
39+
});
40+
41+
test('should not add leading blank line to body and footer if rules are disabled', async () => {
42+
testConfig.rules['body-leading-blank'] = ['1', 'never'];
43+
testConfig.rules['footer-leading-blank'] = ['1', 'never'];
1644
const prompt = stub({
1745
'input-custom': {
1846
type: 'fix',
@@ -24,6 +52,10 @@ test('should work with all fields filled', async () => {
2452
});
2553
const message = await input(prompt);
2654
expect(message).toEqual('fix(test): subject\n' + 'body\n' + 'footer');
55+
// reset config mock
56+
testConfig.rules['body-leading-blank'] = config.rules['body-leading-blank'];
57+
testConfig.rules['footer-leading-blank'] =
58+
config.rules['footer-leading-blank'];
2759
});
2860

2961
test('should work without scope', async () => {
@@ -37,7 +69,7 @@ test('should work without scope', async () => {
3769
},
3870
});
3971
const message = await input(prompt);
40-
expect(message).toEqual('fix: subject\n' + 'body\n' + 'footer');
72+
expect(message).toEqual('fix: subject\n' + '\nbody\n' + '\nfooter');
4173
});
4274

4375
test('should fail without type', async () => {
@@ -72,7 +104,7 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
72104
if (!questions) {
73105
throw new Error(`Unexpected config type: ${configType}`);
74106
}
75-
const answer = questions[promptConfig.name!];
107+
let answer = questions[promptConfig.name!];
76108
if (answer == null) {
77109
throw new Error(`Unexpected config name: ${promptConfig.name}`);
78110
}
@@ -83,7 +115,11 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
83115
throw new Error(validationResult || undefined);
84116
}
85117
}
86-
118+
const forceLeadingBlankFn = (promptConfig as InputCustomOptions)
119+
.forceLeadingBlankFn;
120+
if (forceLeadingBlankFn) {
121+
answer = forceLeadingBlankFn(answer as string);
122+
}
87123
result[promptConfig.name!] = answer;
88124
}
89125
return result;

β€Ž@commitlint/prompt/src/inquirer/InputCustomPrompt.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export default class InputCustomPrompt<
4646

4747
onEnd(state: SuccessfulPromptStateData): void {
4848
this.lineSubscription.unsubscribe();
49+
// Add or remove leading blank if rule is active.
50+
state.value = this.opt.forceLeadingBlankFn(state.value);
4951
super.onEnd(state);
5052
}
5153

β€Ž@commitlint/prompt/src/inquirer/inquirer.d.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare module 'inquirer' {
1515
log?(answers?: T): string;
1616
tabCompletion?: InputCustomCompletionOption[];
1717
maxLength(answers?: T): number;
18+
forceLeadingBlankFn(input: string): string;
1819
}
1920

2021
interface QuestionMap<T extends Answers = Answers> {

β€Ž@commitlint/prompt/src/library/get-prompt.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,6 @@ export default function getPrompt(
119119
transformer(value: string) {
120120
return forceCaseFn(value);
121121
},
122+
forceLeadingBlankFn,
122123
};
123124
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /