-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
6d036d6
commit 331579a
Showing
6 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import parse from '@commitlint/parse'; | ||
import {Commit} from '@commitlint/types'; | ||
import {headerTrim} from './header-trim'; | ||
|
||
const messages = { | ||
correct: 'test: subject', | ||
|
||
whitespaceStart: ' test: subject', | ||
whitespaceEnd: 'test: subject ', | ||
whitespaceSurround: ' test: subject ', | ||
|
||
tabStart: '\t\ttest: subject', | ||
tabEnd: 'test: subject\t\t', | ||
tabSurround: '\t\ttest: subject\t', | ||
|
||
mixStart: '\t\ttest: subject', | ||
mixEnd: 'test: subject\t\t', | ||
mixSurround: '\t \ttest: subject \t \t', | ||
}; | ||
|
||
const parsed = Object.entries(messages).reduce((_parsed, [key, message]) => { | ||
_parsed[key] = parse(message); | ||
return _parsed; | ||
}, {}) as Record<keyof typeof messages, Promise<Commit>>; | ||
|
||
test('should succeed when header is not surrounded by whitespace', async () => { | ||
const result = headerTrim(await parsed.correct); | ||
expect(result).toEqual(expect.arrayContaining([true])); | ||
}); | ||
|
||
( | ||
[ | ||
['mixed whitespace', parsed.mixStart], | ||
['whitespace', parsed.whitespaceStart], | ||
['tab', parsed.tabStart], | ||
] as const | ||
).forEach(([desc, commit]) => { | ||
test(`should fail with ${desc}`, async () => { | ||
const result = headerTrim(await commit); | ||
expect(result).toEqual( | ||
expect.arrayContaining([false, 'header must not start with whitespace']) | ||
); | ||
}); | ||
}); | ||
|
||
( | ||
[ | ||
['mixed whitespace', parsed.mixEnd], | ||
['whitespace', parsed.whitespaceEnd], | ||
['tab', parsed.tabEnd], | ||
] as const | ||
).forEach(([desc, commit]) => { | ||
test(`should fail when ends with ${desc}`, async () => { | ||
const result = headerTrim(await commit); | ||
expect(result).toEqual( | ||
expect.arrayContaining([false, 'header must not end with whitespace']) | ||
); | ||
}); | ||
}); | ||
|
||
( | ||
[ | ||
['mixed whitespace', parsed.mixSurround], | ||
['whitespace', parsed.whitespaceSurround], | ||
['tab', parsed.tabSurround], | ||
] as const | ||
).forEach(([desc, commit]) => { | ||
test(`should fail when surrounded by ${desc}`, async () => { | ||
const result = headerTrim(await commit); | ||
expect(result).toEqual( | ||
expect.arrayContaining([ | ||
false, | ||
'header must not be surrounded by whitespace', | ||
]) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import message from '@commitlint/message'; | ||
import {SyncRule} from '@commitlint/types'; | ||
|
||
export const headerTrim: SyncRule = (parsed) => { | ||
const {header} = parsed; | ||
|
||
const startsWithWhiteSpace = header !== header.trimStart(); | ||
const endsWithWhiteSpace = header !== header.trimEnd(); | ||
|
||
switch (true) { | ||
case startsWithWhiteSpace && endsWithWhiteSpace: | ||
return [ | ||
false, | ||
message(['header', 'must not be surrounded by whitespace']), | ||
]; | ||
|
||
case startsWithWhiteSpace: | ||
return [false, message(['header', 'must not start with whitespace'])]; | ||
|
||
case endsWithWhiteSpace: | ||
return [false, message(['header', 'must not end with whitespace'])]; | ||
|
||
default: | ||
return [true]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters