Skip to content

Commit a2b627d

Browse files
committed
refactor: abstract lintoutcome construction
1 parent 9079ae4 commit a2b627d

File tree

2 files changed

+67
-31
lines changed

2 files changed

+67
-31
lines changed

@commitlint/lint/src/lint-outcome.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
LintOutcome as LintOutcomeData,
3+
LintRuleOutcome,
4+
Commit
5+
} from '@commitlint/types';
6+
import {buildCommitMesage} from './commit-message';
7+
8+
export interface EmptyInit {
9+
message: string;
10+
}
11+
12+
export interface ResultsInit {
13+
parsed: Commit;
14+
results: LintRuleOutcome[];
15+
}
16+
17+
export class LintOutcome implements LintOutcomeData {
18+
public readonly input: string;
19+
public readonly valid: boolean;
20+
public readonly errors: LintRuleOutcome[];
21+
public readonly warnings: LintRuleOutcome[];
22+
23+
public static empty(init: EmptyInit): LintOutcome {
24+
return new LintOutcome({
25+
valid: true,
26+
errors: [],
27+
warnings: [],
28+
input: init.message
29+
});
30+
}
31+
32+
public static fromResults(init: ResultsInit): LintOutcome {
33+
const errors = init.results.filter(
34+
result => result.level === 2 && !result.valid
35+
);
36+
const warnings = init.results.filter(
37+
result => result.level === 1 && !result.valid
38+
);
39+
40+
const valid = errors.length === 0;
41+
42+
return {
43+
valid,
44+
errors,
45+
warnings,
46+
input: buildCommitMesage(init.parsed)
47+
};
48+
}
49+
50+
private constructor(init: LintOutcomeData) {
51+
this.input = init.input;
52+
this.valid = init.valid;
53+
this.errors = init.errors;
54+
this.warnings = init.warnings;
55+
}
56+
}

@commitlint/lint/src/lint.ts

+11-31
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import util from 'util';
22
import isIgnored from '@commitlint/is-ignored';
33
import parse from '@commitlint/parse';
44
import defaultRules from '@commitlint/rules';
5-
import toPairs from 'lodash/toPairs';
6-
import {buildCommitMesage} from './commit-message';
75
import {
86
LintRuleConfig,
97
LintOptions,
108
LintRuleOutcome,
119
Rule,
12-
RuleSeverity,
13-
LintOutcome
10+
RuleSeverity
1411
} from '@commitlint/types';
12+
import {LintOutcome} from './lint-outcome';
1513

1614
export default async function lint(
1715
message: string,
@@ -27,28 +25,19 @@ export default async function lint(
2725
if (
2826
isIgnored(message, {defaults: opts.defaultIgnores, ignores: opts.ignores})
2927
) {
30-
return {
31-
valid: true,
32-
errors: [],
33-
warnings: [],
34-
input: message
35-
};
28+
return LintOutcome.empty({message});
3629
}
3730

3831
// Parse the commit message
3932
const parsed = await parse(message, undefined, opts.parserOpts);
33+
4034
if (
4135
parsed.header === null &&
4236
parsed.body === null &&
4337
parsed.footer === null
4438
) {
4539
// Commit is empty, skip
46-
return {
47-
valid: true,
48-
errors: [],
49-
warnings: [],
50-
input: message
51-
};
40+
return LintOutcome.empty({message});
5241
}
5342

5443
const allRules: Map<string, Rule<unknown> | Rule<never>> = new Map(
@@ -79,7 +68,7 @@ export default async function lint(
7968
);
8069
}
8170

82-
const invalid = toPairs(rulesConfig)
71+
const invalid = Object.entries(rulesConfig)
8372
.map(([name, config]) => {
8473
if (!Array.isArray(config)) {
8574
return new Error(
@@ -146,7 +135,7 @@ export default async function lint(
146135
}
147136

148137
// Validate against all rules
149-
const results = toPairs(rulesConfig)
138+
const results = Object.entries(rulesConfig)
150139
.filter(([, [level]]) => level > 0)
151140
.map(entry => {
152141
const [name, config] = entry;
@@ -175,17 +164,8 @@ export default async function lint(
175164
})
176165
.filter((result): result is LintRuleOutcome => result !== null);
177166

178-
const errors = results.filter(result => result.level === 2 && !result.valid);
179-
const warnings = results.filter(
180-
result => result.level === 1 && !result.valid
181-
);
182-
183-
const valid = errors.length === 0;
184-
185-
return {
186-
valid,
187-
errors,
188-
warnings,
189-
input: buildCommitMesage(parsed)
190-
};
167+
return LintOutcome.fromResults({
168+
results,
169+
parsed
170+
});
191171
}

0 commit comments

Comments
 (0)