Skip to content

Commit e3d2091

Browse files
authored
refactor: use enum instead of hardcoding (#3864)
Same energy as b3007ee.
1 parent e6be814 commit e3d2091

File tree

9 files changed

+106
-102
lines changed

9 files changed

+106
-102
lines changed

@commitlint/config-nx-scopes/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {RuleConfigSeverity} from '@commitlint/types';
12
const {
23
getProjects: getNXProjects,
34
} = require('nx/src/generators/utils/project-configuration');
@@ -6,7 +7,7 @@ const {FsTree} = require('nx/src/generators/tree');
67
module.exports = {
78
utils: {getProjects},
89
rules: {
9-
'scope-enum': (ctx) => Promise.resolve([2, 'always', getProjects(ctx)]),
10+
'scope-enum': (ctx) => Promise.resolve([RuleConfigSeverity.Error, 'always', getProjects(ctx)]),
1011
},
1112
};
1213

@commitlint/config-validator/src/validate.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {validateConfig} from './validate';
2-
import {UserConfig} from '@commitlint/types';
2+
import {RuleConfigSeverity, UserConfig} from '@commitlint/types';
33

44
const validSchemas: Record<string, UserConfig> = {
55
empty: {},
@@ -9,7 +9,7 @@ const validSchemas: Record<string, UserConfig> = {
99
withMultipleExtends: {extends: ['test', 'test2']},
1010
withFormatter: {formatter: ''},
1111
withHelpUrl: {helpUrl: ''},
12-
withRules: {rules: {a: [0], b: [1, 'never'], c: [2, 'never', true]}},
12+
withRules: {rules: {a: [RuleConfigSeverity.Disabled], b: [RuleConfigSeverity.Warning, 'never'], c: [RuleConfigSeverity.Error, 'never', true]}},
1313
withParserPresetString: {parserPreset: 'test'},
1414
withParserPresetObject: {parserPreset: {}},
1515
withParserPresetObject2: {parserPreset: {name: 'string', path: 'string'}},

@commitlint/cz-commitlint/src/Process.test.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {QualifiedRules, UserPromptConfig} from '@commitlint/types';
1+
import {QualifiedRules, RuleConfigSeverity, UserPromptConfig} from '@commitlint/types';
22
import {Answers, DistinctQuestion} from 'inquirer';
33
import isFunction from 'lodash.isfunction';
44
import process from './Process';
@@ -68,22 +68,22 @@ afterEach(() => {
6868
describe('conventional-changlog', () => {
6969
beforeEach(() => {
7070
rules = {
71-
'body-leading-blank': [1, 'always'],
72-
'body-max-line-length': [2, 'always', 100],
73-
'footer-leading-blank': [1, 'always'],
74-
'footer-max-line-length': [2, 'always', 100],
75-
'header-max-length': [2, 'always', 100],
71+
'body-leading-blank': [RuleConfigSeverity.Warning, 'always'],
72+
'body-max-line-length': [RuleConfigSeverity.Error, 'always', 100],
73+
'footer-leading-blank': [RuleConfigSeverity.Warning, 'always'],
74+
'footer-max-line-length': [RuleConfigSeverity.Error, 'always', 100],
75+
'header-max-length': [RuleConfigSeverity.Error, 'always', 100],
7676
'subject-case': [
77-
2,
77+
RuleConfigSeverity.Error,
7878
'never',
7979
['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
8080
],
81-
'subject-empty': [2, 'never'],
82-
'subject-full-stop': [2, 'never', '.'],
83-
'type-case': [2, 'always', 'lower-case'],
84-
'type-empty': [2, 'never'],
81+
'subject-empty': [RuleConfigSeverity.Error, 'never'],
82+
'subject-full-stop': [RuleConfigSeverity.Error, 'never', '.'],
83+
'type-case': [RuleConfigSeverity.Error, 'always', 'lower-case'],
84+
'type-empty': [RuleConfigSeverity.Error, 'never'],
8585
'type-enum': [
86-
2,
86+
RuleConfigSeverity.Error,
8787
'always',
8888
[
8989
'build',

@commitlint/cz-commitlint/src/store/rules.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {QualifiedRules} from '@commitlint/types';
1+
import {QualifiedRules, RuleConfigSeverity} from '@commitlint/types';
22
import {GetRuleMethod, SetRulesMethod} from './rules';
33

44
let getRule: GetRuleMethod;
@@ -11,9 +11,9 @@ beforeEach(() => {
1111
describe('getRule', () => {
1212
test('should get rule when prefix and property strict match', () => {
1313
const rules: QualifiedRules = {
14-
'body-max-length': [2, 'always', 100],
15-
'footer-max-line-length': [2, 'always', 100],
16-
'subject-empty': [2, 'never'],
14+
'body-max-length': [RuleConfigSeverity.Error, 'always', 100],
15+
'footer-max-line-length': [RuleConfigSeverity.Error, 'always', 100],
16+
'subject-empty': [RuleConfigSeverity.Error, 'never'],
1717
};
1818
setRules(rules);
1919

@@ -26,7 +26,7 @@ describe('getRule', () => {
2626

2727
test('should not get rule when prefix is invalid', () => {
2828
const rules: QualifiedRules = {
29-
'body-max-length': [2, 'always', 100],
29+
'body-max-length': [RuleConfigSeverity.Error, 'always', 100],
3030
};
3131
setRules(rules);
3232

@@ -37,8 +37,8 @@ describe('getRule', () => {
3737

3838
test('should not get rule when property is partial match', () => {
3939
const rules: QualifiedRules = {
40-
'body-max-length': [2, 'always', 100],
41-
'body-leading-blank': [1, 'always'],
40+
'body-max-length': [RuleConfigSeverity.Error, 'always', 100],
41+
'body-leading-blank': [RuleConfigSeverity.Warning, 'always'],
4242
};
4343
setRules(rules);
4444

@@ -52,13 +52,13 @@ describe('setRule', () => {
5252
expect(getRule('body', 'max-length')).toBeUndefined();
5353

5454
let rules: QualifiedRules = {
55-
'body-max-length': [2, 'always', 100],
55+
'body-max-length': [RuleConfigSeverity.Error, 'always', 100],
5656
};
5757
setRules(rules);
5858
expect(getRule('body', 'max-length')).toBe(rules['body-max-length']);
5959

6060
rules = {
61-
'footer-max-length': [2, 'always', 100],
61+
'footer-max-length': [RuleConfigSeverity.Error, 'always', 100],
6262
};
6363
setRules(rules);
6464
expect(getRule('body', 'max-length')).toBeUndefined();

@commitlint/cz-commitlint/src/utils/rules.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ test('getMaxLength', () => {
5959
expect(getMaxLength([RuleConfigSeverity.Error] as any)).toBe(Infinity);
6060

6161
const rules: any = {
62-
'body-max-line-length': [2, 'always', 100],
63-
'header-max-length': [2, 'always', 100],
62+
'body-max-line-length': [RuleConfigSeverity.Error, 'always', 100],
63+
'header-max-length': [RuleConfigSeverity.Error, 'always', 100],
6464
'test-max-length': [RuleConfigSeverity.Disabled, 'always', 100],
6565
};
6666
let lengthRule = rules['header-max-length'];
@@ -83,8 +83,8 @@ test('getMinLength', () => {
8383
expect(getMinLength([RuleConfigSeverity.Error] as any)).toBe(0);
8484

8585
const rules: any = {
86-
'body-min-length': [2, 'always', 10],
87-
'footer-min-length': [2, 'always', 20],
86+
'body-min-length': [RuleConfigSeverity.Error, 'always', 10],
87+
'footer-min-length': [RuleConfigSeverity.Error, 'always', 20],
8888
'test-min-length': [RuleConfigSeverity.Disabled, 'always', 100],
8989
};
9090
let lengthRule = rules['header-min-length'];

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

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import lint from './lint';
2+
import {RuleConfigSeverity} from '@commitlint/types';
23

34
test('throws without params', async () => {
45
const error = (lint as any)();
@@ -20,21 +21,21 @@ test('positive on stub message and no rule', async () => {
2021

2122
test('positive on stub message and adhered rule', async () => {
2223
const actual = await lint('foo: bar', {
23-
'type-enum': [2, 'always', ['foo']],
24+
'type-enum': [RuleConfigSeverity.Error, 'always', ['foo']],
2425
});
2526
expect(actual.valid).toBe(true);
2627
});
2728

2829
test('negative on stub message and broken rule', async () => {
2930
const actual = await lint('foo: bar', {
30-
'type-enum': [2, 'never', ['foo']],
31+
'type-enum': [RuleConfigSeverity.Error, 'never', ['foo']],
3132
});
3233
expect(actual.valid).toBe(false);
3334
});
3435

3536
test('positive on ignored message and broken rule', async () => {
3637
const actual = await lint('Revert "some bogus commit"', {
37-
'type-empty': [2, 'never'],
38+
'type-empty': [RuleConfigSeverity.Error, 'never'],
3839
});
3940
expect(actual.valid).toBe(true);
4041
expect(actual.input).toBe('Revert "some bogus commit"');
@@ -44,7 +45,7 @@ test('negative on ignored message, disabled ignored messages and broken rule', a
4445
const actual = await lint(
4546
'Revert "some bogus commit"',
4647
{
47-
'type-empty': [2, 'never'],
48+
'type-empty': [RuleConfigSeverity.Error, 'never'],
4849
},
4950
{
5051
defaultIgnores: false,
@@ -58,7 +59,7 @@ test('positive on custom ignored message and broken rule', async () => {
5859
const actual = await lint(
5960
ignoredMessage,
6061
{
61-
'type-empty': [2, 'never'],
62+
'type-empty': [RuleConfigSeverity.Error, 'never'],
6263
},
6364
{
6465
ignores: [(c) => c === ignoredMessage],
@@ -72,8 +73,8 @@ test('positive on stub message and opts', async () => {
7273
const actual = await lint(
7374
'foo-bar',
7475
{
75-
'type-enum': [2, 'always', ['foo']],
76-
'type-empty': [2, 'never'],
76+
'type-enum': [RuleConfigSeverity.Error, 'always', ['foo']],
77+
'type-empty': [RuleConfigSeverity.Error, 'never'],
7778
},
7879
{
7980
parserOpts: {
@@ -85,7 +86,7 @@ test('positive on stub message and opts', async () => {
8586
});
8687

8788
test('throws for invalid rule names', async () => {
88-
const error = lint('foo', {foo: [2, 'always'], bar: [1, 'never']});
89+
const error = lint('foo', {foo: [RuleConfigSeverity.Error, 'always'], bar: [RuleConfigSeverity.Warning, 'never']});
8990

9091
await expect(error).rejects.toThrow(/^Found invalid rule names: foo, bar/);
9192
});
@@ -150,8 +151,8 @@ test('throws for rule with invalid condition', async () => {
150151

151152
test('throws for rule with out of range condition', async () => {
152153
const error = lint('type(scope): foo', {
153-
'type-enum': [1, 'foo'] as any,
154-
'header-max-length': [1, 'bar'] as any,
154+
'type-enum': [RuleConfigSeverity.Warning, 'foo'] as any,
155+
'header-max-length': [RuleConfigSeverity.Warning, 'bar'] as any,
155156
});
156157

157158
await expect(error).rejects.toThrow('type-enum must be "always" or "never"');
@@ -162,15 +163,15 @@ test('throws for rule with out of range condition', async () => {
162163

163164
test('succeds for issue', async () => {
164165
const report = await lint('somehting #1', {
165-
'references-empty': [2, 'never'],
166+
'references-empty': [RuleConfigSeverity.Error, 'never'],
166167
});
167168

168169
expect(report.valid).toBe(true);
169170
});
170171

171172
test('fails for issue', async () => {
172173
const report = await lint('somehting #1', {
173-
'references-empty': [2, 'always'],
174+
'references-empty': [RuleConfigSeverity.Error, 'always'],
174175
});
175176

176177
expect(report.valid).toBe(false);
@@ -180,7 +181,7 @@ test('succeds for custom issue prefix', async () => {
180181
const report = await lint(
181182
'somehting REF-1',
182183
{
183-
'references-empty': [2, 'never'],
184+
'references-empty': [RuleConfigSeverity.Error, 'never'],
184185
},
185186
{
186187
parserOpts: {
@@ -196,7 +197,7 @@ test('fails for custom issue prefix', async () => {
196197
const report = await lint(
197198
'somehting #1',
198199
{
199-
'references-empty': [2, 'never'],
200+
'references-empty': [RuleConfigSeverity.Error, 'never'],
200201
},
201202
{
202203
parserOpts: {
@@ -212,7 +213,7 @@ test('fails for custom plugin rule', async () => {
212213
const report = await lint(
213214
'somehting #1',
214215
{
215-
'plugin-rule': [2, 'never'],
216+
'plugin-rule': [RuleConfigSeverity.Error, 'never'],
216217
},
217218
{
218219
plugins: {
@@ -232,7 +233,7 @@ test('passes for custom plugin rule', async () => {
232233
const report = await lint(
233234
'somehting #1',
234235
{
235-
'plugin-rule': [2, 'never'],
236+
'plugin-rule': [RuleConfigSeverity.Error, 'never'],
236237
},
237238
{
238239
plugins: {
@@ -275,7 +276,7 @@ test('returns original message with commit header, body and footer, parsing comm
275276
const report = await lint(
276277
message,
277278
{
278-
'references-empty': [2, 'never'],
279+
'references-empty': [RuleConfigSeverity.Error, 'never'],
279280
},
280281
{
281282
parserOpts: {
@@ -291,7 +292,7 @@ test('passes for async rule', async () => {
291292
const report = await lint(
292293
'somehting #1',
293294
{
294-
'async-rule': [2, 'never'],
295+
'async-rule': [RuleConfigSeverity.Error, 'never'],
295296
},
296297
{
297298
plugins: {

0 commit comments

Comments
 (0)