Skip to content

Commit 6fea4cf

Browse files
committed
fix: unify rule config types
1 parent a0c4bb3 commit 6fea4cf

File tree

13 files changed

+178
-139
lines changed

13 files changed

+178
-139
lines changed

@commitlint/cli/cli.js

100644100755
File mode changed.

@commitlint/cli/src/cli.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
LintOutcome,
1414
ParserOptions,
1515
ParserPreset,
16-
QualifiedConfig
16+
QualifiedConfig,
17+
Formatter
1718
} from '@commitlint/types';
1819
import {CliError} from './cli-error';
1920

@@ -177,7 +178,7 @@ async function main(options: CliFlags) {
177178

178179
const results = await Promise.all(
179180
// TODO: validate why those types do not match
180-
messages.map(message => lint(message, loaded.rules as any, opts))
181+
messages.map(message => lint(message, loaded.rules, opts))
181182
);
182183

183184
if (Object.keys(loaded.rules).length === 0) {
@@ -338,10 +339,8 @@ function selectParserOpts(parserPreset: ParserPreset) {
338339
return parserPreset.parserOpts;
339340
}
340341

341-
function loadFormatter(config: QualifiedConfig, flags: CliFlags) {
342-
// TODO: validate why formatter is unknown????
343-
const moduleName: string =
344-
flags.format || (config.formatter as any) || '@commitlint/format';
342+
function loadFormatter(config: QualifiedConfig, flags: CliFlags): Formatter {
343+
const moduleName = flags.format || config.formatter || '@commitlint/format';
345344
const modulePath =
346345
resolveFrom.silent(__dirname, moduleName) ||
347346
resolveFrom.silent(flags.cwd, moduleName) ||

@commitlint/format/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@commitlint/utils": "^8.3.4"
3838
},
3939
"dependencies": {
40+
"@commitlint/types": "^8.3.5",
4041
"chalk": "^3.0.0"
4142
}
4243
}

@commitlint/format/src/format.ts

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
import chalk from 'chalk';
2+
import {
3+
ChalkColor,
4+
FormattableReport,
5+
FormatOptions,
6+
FormattableResult,
7+
WithInput
8+
} from '@commitlint/types';
29

310
const DEFAULT_SIGNS = [' ', '⚠', '✖'] as const;
411
const DEFAULT_COLORS = ['white', 'yellow', 'red'] as const;
512

6-
export interface FormattableProblem {
7-
level: 0 | 1 | 2;
8-
name: string;
9-
message: string;
10-
}
11-
12-
export interface FormattableResult {
13-
errors?: FormattableProblem[];
14-
warnings?: FormattableProblem[];
15-
}
16-
17-
export interface WithInput {
18-
input?: string;
19-
}
20-
21-
export interface FormattableReport {
22-
results?: (FormattableResult & WithInput)[];
23-
}
24-
25-
export type ChalkColor = keyof typeof chalk;
26-
27-
export interface FormatOptions {
28-
color?: boolean;
29-
signs?: readonly [string, string, string];
30-
colors?: readonly [ChalkColor, ChalkColor, ChalkColor];
31-
verbose?: boolean;
32-
helpUrl?: string;
33-
}
34-
3513
export function format(
3614
report: FormattableReport = {},
3715
options: FormatOptions = {}
38-
) {
16+
): string {
3917
const {results = []} = report;
4018
const fi = (result: FormattableResult & WithInput) =>
4119
formatInput(result, options);

@commitlint/format/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"outDir": "./lib"
77
},
88
"include": ["./src"],
9-
"exclude": ["./src/**/*.test.ts", "./lib/**/*"]
9+
"exclude": ["./src/**/*.test.ts", "./lib/**/*"],
10+
"references": [{"path": "../types"}]
1011
}

@commitlint/lint/src/lint.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ test('throws for rule with invalid level', async () => {
125125

126126
test('throws for rule with out of range level', async () => {
127127
const error = lint('type(scope): foo', {
128-
'type-enum': [-1, 'always'],
129-
'header-max-length': [3, 'always']
128+
'type-enum': [-1, 'always'] as any,
129+
'header-max-length': [3, 'always'] as any
130130
});
131131

132132
await expect(error).rejects.toThrow('rule type-enum must be between 0 and 2');

@commitlint/lint/src/lint.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import parse from '@commitlint/parse';
44
import defaultRules from '@commitlint/rules';
55
import {buildCommitMesage} from './commit-message';
66
import {
7-
LintRuleConfig,
87
LintOptions,
98
LintOutcome,
109
LintRuleOutcome,
1110
Rule,
12-
RuleSeverity
11+
RuleConfigSeverity,
12+
QualifiedRules
1313
} from '@commitlint/types';
1414

1515
export default async function lint(
1616
message: string,
17-
rawRulesConfig?: LintRuleConfig,
17+
rawRulesConfig?: QualifiedRules,
1818
rawOpts?: LintOptions
1919
): Promise<LintOutcome> {
2020
const opts = rawOpts
@@ -76,7 +76,7 @@ export default async function lint(
7676

7777
const [level] = config;
7878

79-
if (level === RuleSeverity.Disabled && config.length === 1) {
79+
if (level === RuleConfigSeverity.Disabled && config.length === 1) {
8080
return null;
8181
}
8282

@@ -132,10 +132,10 @@ export default async function lint(
132132

133133
// Validate against all rules
134134
const results = Object.entries(rulesConfig)
135-
.filter(([, [level]]) => level > 0)
135+
.filter(([, config]) => typeof config !== 'undefined' && config[0] > 0)
136136
.map(entry => {
137137
const [name, config] = entry;
138-
const [level, when, value] = config;
138+
const [level, when, value] = config!; //
139139

140140
// Level 0 rules are ignored
141141
if (level === 0) {
@@ -148,8 +148,7 @@ export default async function lint(
148148
throw new Error(`Could not find rule implementation for ${name}`);
149149
}
150150

151-
const executableRule = rule as Rule<unknown>;
152-
const [valid, message] = executableRule(parsed, when, value);
151+
const [valid, message] = rule(parsed, when, value as any);
153152

154153
return {
155154
level,

@commitlint/types/src/format.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import chalk from 'chalk';
2+
import {QualifiedRules} from './load';
3+
import {RuleConfigSeverity} from './rules';
4+
5+
export type Formatter = (
6+
report: FormattableReport,
7+
options: FormatOptions
8+
) => string;
9+
10+
export interface FormattableProblem {
11+
level: RuleConfigSeverity;
12+
name: keyof QualifiedRules;
13+
message: string;
14+
}
15+
16+
export interface FormattableResult {
17+
errors?: FormattableProblem[];
18+
warnings?: FormattableProblem[];
19+
}
20+
21+
export interface WithInput {
22+
input?: string;
23+
}
24+
25+
export interface FormattableReport {
26+
results?: (FormattableResult & WithInput)[];
27+
}
28+
29+
export type ChalkColor = keyof typeof chalk;
30+
31+
export interface FormatOptions {
32+
color?: boolean;
33+
signs?: readonly [string, string, string];
34+
colors?: readonly [ChalkColor, ChalkColor, ChalkColor];
35+
verbose?: boolean;
36+
helpUrl?: string;
37+
}

@commitlint/types/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './ensure';
2+
export * from './format';
23
export * from './is-ignored';
34
export * from './rules';
45
export * from './lint';

@commitlint/types/src/lint.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {IsIgnoredOptions} from './is-ignored';
2-
import {RuleConfigTuple, PluginRecords, RuleSeverity} from './load';
2+
import {PluginRecords} from './load';
33
import {ParserOptions} from './parse';
4+
import {RuleConfigSeverity, RuleConfigTuple} from './rules';
45

56
export type LintRuleConfig = Record<
67
string,
7-
| Readonly<[RuleSeverity.Disabled]>
8+
| Readonly<[RuleConfigSeverity.Disabled]>
89
| RuleConfigTuple<void>
910
| RuleConfigTuple<unknown>
1011
>;
@@ -35,7 +36,7 @@ export interface LintRuleOutcome {
3536
/** If the commit is considered valid for the rule */
3637
valid: boolean;
3738
/** The "severity" of the rule (1 = warning, 2 = error) */
38-
level: RuleSeverity;
39+
level: RuleConfigSeverity;
3940
/** The name of the rule */
4041
name: string;
4142
/** The message returned from the rule, if invalid */

@commitlint/types/src/load.ts

+4-80
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {TargetCaseType} from './ensure';
2-
import {Rule, RuleCondition} from './rules';
1+
import {Rule, RulesConfig, RuleConfigQuality} from './rules';
32

43
export type PluginRecords = Record<string, Plugin>;
54

@@ -14,84 +13,9 @@ export interface LoadOptions {
1413
file?: string;
1514
}
1615

17-
export enum RuleSeverity {
18-
Disabled = 0,
19-
Warning = 1,
20-
Error = 2
21-
}
22-
23-
export type RuleConfigTuple<T> = T extends void
24-
? Readonly<[RuleSeverity, RuleCondition]>
25-
: Readonly<[RuleSeverity, RuleCondition, T]>;
26-
27-
export enum RuleConfigQuality {
28-
User,
29-
Qualified
30-
}
31-
32-
export type QualifiedRuleConfig<T> =
33-
| (() => RuleConfigTuple<T>)
34-
| (() => RuleConfigTuple<Promise<T>>)
35-
| RuleConfigTuple<T>;
36-
37-
export type RuleConfig<
38-
V = RuleConfigQuality.Qualified,
39-
T = void
40-
> = V extends RuleConfigQuality.Qualified
41-
? RuleConfigTuple<T>
42-
: QualifiedRuleConfig<T>;
43-
44-
export type CaseRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
45-
V,
46-
TargetCaseType
47-
>;
48-
export type LengthRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
49-
V,
50-
number
51-
>;
52-
export type EnumRuleConfig<V = RuleConfigQuality.User> = RuleConfig<
53-
V,
54-
string[]
55-
>;
56-
57-
export type RulesConfig<V = RuleConfigQuality.User> = {
58-
'body-case': CaseRuleConfig<V>;
59-
'body-empty': RuleConfig<V>;
60-
'body-leading-blank': RuleConfig<V>;
61-
'body-max-length': LengthRuleConfig<V>;
62-
'body-max-line-length': LengthRuleConfig<V>;
63-
'body-min-length': LengthRuleConfig<V>;
64-
'footer-empty': RuleConfig<V>;
65-
'footer-leading-blank': RuleConfig<V>;
66-
'footer-max-length': LengthRuleConfig<V>;
67-
'footer-max-line-length': LengthRuleConfig<V>;
68-
'footer-min-length': LengthRuleConfig<V>;
69-
'header-case': CaseRuleConfig<V>;
70-
'header-full-stop': RuleConfig<V>;
71-
'header-max-length': LengthRuleConfig<V>;
72-
'header-min-length': LengthRuleConfig<V>;
73-
'references-empty': RuleConfig<V>;
74-
'scope-case': CaseRuleConfig<V>;
75-
'scope-empty': RuleConfig<V>;
76-
'scope-enum': EnumRuleConfig<V>;
77-
'scope-max-length': LengthRuleConfig<V>;
78-
'scope-min-length': LengthRuleConfig<V>;
79-
'signed-off-by': RuleConfig<V>;
80-
'subject-case': CaseRuleConfig<V>;
81-
'subject-empty': RuleConfig<V>;
82-
'subject-full-stop': RuleConfig<V>;
83-
'subject-max-length': LengthRuleConfig<V>;
84-
'subject-min-length': LengthRuleConfig<V>;
85-
'type-case': CaseRuleConfig<V>;
86-
'type-empty': RuleConfig<V>;
87-
'type-enum': EnumRuleConfig<V>;
88-
'type-max-length': LengthRuleConfig<V>;
89-
'type-min-length': LengthRuleConfig<V>;
90-
};
91-
9216
export interface UserConfig {
9317
extends?: string[];
94-
formatter?: unknown;
18+
formatter?: string;
9519
rules?: Partial<RulesConfig>;
9620
parserPreset?: string | ParserPreset;
9721
ignores?: ((commit: string) => boolean)[];
@@ -101,7 +25,7 @@ export interface UserConfig {
10125

10226
export interface UserPreset {
10327
extends?: string[];
104-
formatter?: unknown;
28+
formatter?: string;
10529
rules?: Partial<RulesConfig>;
10630
parserPreset?: string | ParserPreset;
10731
ignores?: ((commit: string) => boolean)[];
@@ -113,7 +37,7 @@ export type QualifiedRules = Partial<RulesConfig<RuleConfigQuality.Qualified>>;
11337

11438
export interface QualifiedConfig {
11539
extends: string[];
116-
formatter: unknown;
40+
formatter: string;
11741
rules: QualifiedRules;
11842
parserPreset: ParserPreset;
11943
ignores: ((commit: string) => boolean)[];

0 commit comments

Comments
 (0)