Skip to content

Commit 8d56e4a

Browse files
authored
Merge pull request #160 from sbonasu/add-enforceAllErrors
feat: add lint-every-commit flag to commitlint function
2 parents 445c9b3 + 958b552 commit 8d56e4a

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

bin/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ const { argv } = require('yargs')
1515
default: defaultBranch,
1616
description: `Use a different branch point other than ${defaultBranch}`,
1717
},
18+
'lint-every-commit': {
19+
type: 'boolean',
20+
default: false,
21+
description: 'lint every commit including normally ignored errors',
22+
},
1823
});
1924

2025
(async () => {
21-
let formatted = await commitlint(argv);
26+
let formatted = await commitlint({
27+
...argv,
28+
shouldLintEveryCommit: argv['lint-every-commit'],
29+
});
2230

2331
console.log(formatted);
2432
})();

src/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function doesReportMatchErrorSchema(report, schema) {
1818
&& report.errors.every(error => schema.includes(error.name));
1919
}
2020

21-
async function runCommitLint(commit) {
21+
async function runCommitLint(commit, { shouldLintEveryCommit }) {
2222
const { default: load } = await import('@commitlint/load');
2323
const { default: read } = await import('@commitlint/read');
2424
const { default: lint } = await import('@commitlint/lint');
@@ -46,7 +46,7 @@ async function runCommitLint(commit) {
4646
if (report.valid) {
4747
hasValidReports = true;
4848
orderedValidAndErrorReports.push(report);
49-
} else if (!doesReportMatchErrorSchema(report, IGNORED_ERROR_REPORT_SCHEMA)) {
49+
} else if (shouldLintEveryCommit || !doesReportMatchErrorSchema(report, IGNORED_ERROR_REPORT_SCHEMA)) {
5050
hasErrorReports = true;
5151
orderedValidAndErrorReports.push(report);
5252
errorCount++;
@@ -102,9 +102,7 @@ async function succeedWithLatestCommit() {
102102
});
103103
}
104104

105-
async function commitlint({
106-
defaultBranch,
107-
}) {
105+
async function commitlint({ defaultBranch, shouldLintEveryCommit } = {}) {
108106
let currentBranch = await getCurrentBranch();
109107
if (currentBranch === defaultBranch) {
110108
return await succeedWithLatestCommit();
@@ -125,8 +123,7 @@ async function commitlint({
125123
return await succeedWithLatestCommit();
126124
}
127125

128-
let formatted = await runCommitLint(commitSinceBranchPoint);
129-
126+
let formatted = await runCommitLint(commitSinceBranchPoint, { shouldLintEveryCommit });
130127
return formatted;
131128
}
132129

test/index-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,40 @@ describe(function() {
7171
expect(err.exitCode).to.equal(1);
7272
});
7373

74+
it('succeeds when one good commit and one unconventional commit on branch', async function() {
75+
await execa('git', ['branch', 'foo'], { cwd: tmpPath });
76+
await execa('git', ['checkout', 'foo'], { cwd: tmpPath });
77+
await execa('git', ['commit', '--allow-empty', '-m', 'foo'], { cwd: tmpPath });
78+
await execa('git', ['commit', '--allow-empty', '-m', 'chore: bar'], { cwd: tmpPath });
79+
80+
let { stdout } = await execa(bin, { cwd: tmpPath });
81+
82+
expect(stdout).to.equal(`⧗ input: chore: bar
83+
✔ found 0 problems, 0 warnings`);
84+
});
85+
86+
it('fails when lint-every-commit is enabled and branch has one good commit and one unconventional commit', async function() {
87+
await execa('git', ['branch', 'foo'], { cwd: tmpPath });
88+
await execa('git', ['checkout', 'foo'], { cwd: tmpPath });
89+
await execa('git', ['commit', '--allow-empty', '-m', 'foo'], { cwd: tmpPath });
90+
await execa('git', ['commit', '--allow-empty', '-m', 'chore: bar'], { cwd: tmpPath });
91+
92+
let err = await execa(bin, ['--lint-every-commit'], {
93+
cwd: tmpPath,
94+
reject: false,
95+
});
96+
97+
expect(err.stdout.trim()).to.equal(`⧗ input: chore: bar
98+
✔ found 0 problems, 0 warnings
99+
⧗ input: foo
100+
✖ subject may not be empty [subject-empty]
101+
✖ type may not be empty [type-empty]
102+
103+
✖ found 2 problems, 0 warnings`);
104+
105+
expect(err.exitCode).to.equal(1);
106+
});
107+
74108
// There is no way to actually generate just a `subject-empty` error.
75109
// `chore:` is always recognized as missing a type with a `type-empty` as well.
76110
// eslint-disable-next-line mocha/no-skipped-tests

0 commit comments

Comments
 (0)