Skip to content

feat(rules): add regex support for [subject|header]-full-stop rules #818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion @commitlint/rules/src/header-full-stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import message from '@commitlint/message';
export default (parsed, when, value) => {
const {header} = parsed;
const negated = when === 'never';
const hasStop = header[header.length - 1] === value;
const stop = header[header.length - 1];
const hasStop =
value.length > 1 ? new RegExp(value, 'u').test(stop) : stop === value;

return [
negated ? !hasStop : hasStop,
Expand Down
24 changes: 24 additions & 0 deletions @commitlint/rules/src/header-full-stop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ test('without against "never ." should succeed', async t => {
const expected = true;
t.is(actual, expected);
});

test('with against "always [\\.0-9]" should succeed', async t => {
const [actual] = check(await parsed.with, 'always', '[\\.0-9]');
const expected = true;
t.is(actual, expected);
});

test('with against "never [\\.0-9]" should fail', async t => {
const [actual] = check(await parsed.with, 'never', '[\\.0-9]');
const expected = false;
t.is(actual, expected);
});

test('without against "always [\\.0-9]" should fail', async t => {
const [actual] = check(await parsed.without, 'always', '[\\.0-9]');
const expected = false;
t.is(actual, expected);
});

test('without against "never [\\.0-9]" should succeed', async t => {
const [actual] = check(await parsed.without, 'never', '[\\.0-9]');
const expected = true;
t.is(actual, expected);
});
4 changes: 3 additions & 1 deletion @commitlint/rules/src/subject-full-stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default (parsed, when, value) => {
}

const negated = when === 'never';
const hasStop = input[input.length - 1] === value;
const stop = input[input.length - 1];
const hasStop =
value.length > 1 ? new RegExp(value, 'u').test(stop) : stop === value;

return [
negated ? !hasStop : hasStop,
Expand Down
24 changes: 24 additions & 0 deletions @commitlint/rules/src/subject-full-stop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ test('without against "never ." should succeed', async t => {
const expected = true;
t.is(actual, expected);
});

test('with against "always [\\.0-9]" should succeed', async t => {
const [actual] = check(await parsed.with, 'always', '[\\.0-9]');
const expected = true;
t.is(actual, expected);
});

test('with against "never [\\.0-9]" should fail', async t => {
const [actual] = check(await parsed.with, 'never', '[\\.0-9]');
const expected = false;
t.is(actual, expected);
});

test('without against "always [\\.0-9]" should fail', async t => {
const [actual] = check(await parsed.without, 'always', '[\\.0-9]');
const expected = false;
t.is(actual, expected);
});

test('without against "never [\\.0-9]" should succeed', async t => {
const [actual] = check(await parsed.without, 'never', '[\\.0-9]');
const expected = true;
t.is(actual, expected);
});
14 changes: 8 additions & 6 deletions docs/reference-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ Rule configurations are either of type `array` residing on a key with the rule's
```

#### header-full-stop
* **condition**: `header` ends with `value`
* **condition**: `header` ends with `value` or `header`'s last character matches `value` string regexp
* **rule**: `never`
* **value**
* **possible values**
```js
'.'
'.' // default
'[\\.0-9]' // any string regexp to match with last character
```

#### header-max-length
Expand Down Expand Up @@ -211,11 +212,12 @@ Rule configurations are either of type `array` residing on a key with the rule's
* **rule**: `never`

#### subject-full-stop
* **condition**: `subject` ends with `value`
* **condition**: `subject` ends with `value` or `subject`'s last character matches `value` string regexp
* **rule**: `never`
* **value**
* **possible values**
```js
'.'
'.' // default
'[\\.0-9]' // any string regexp to match with last character
```

#### subject-max-length
Expand Down