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

Lines changed: 5 additions & 6 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 30 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 7 additions & 8 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 3 deletions
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 */

0 commit comments

Comments
 (0)