Skip to content

Commit

Permalink
refactor: use enum instead of hardcoding (#3864)
Browse files Browse the repository at this point in the history
Same energy as b3007ee.
  • Loading branch information
knocte authored Jan 21, 2024
1 parent e6be814 commit e3d2091
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 102 deletions.
3 changes: 2 additions & 1 deletion @commitlint/config-nx-scopes/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {RuleConfigSeverity} from '@commitlint/types';
const {
getProjects: getNXProjects,
} = require('nx/src/generators/utils/project-configuration');
Expand All @@ -6,7 +7,7 @@ const {FsTree} = require('nx/src/generators/tree');
module.exports = {
utils: {getProjects},
rules: {
'scope-enum': (ctx) => Promise.resolve([2, 'always', getProjects(ctx)]),
'scope-enum': (ctx) => Promise.resolve([RuleConfigSeverity.Error, 'always', getProjects(ctx)]),
},
};

Expand Down
4 changes: 2 additions & 2 deletions @commitlint/config-validator/src/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {validateConfig} from './validate';
import {UserConfig} from '@commitlint/types';
import {RuleConfigSeverity, UserConfig} from '@commitlint/types';

const validSchemas: Record<string, UserConfig> = {
empty: {},
Expand All @@ -9,7 +9,7 @@ const validSchemas: Record<string, UserConfig> = {
withMultipleExtends: {extends: ['test', 'test2']},
withFormatter: {formatter: ''},
withHelpUrl: {helpUrl: ''},
withRules: {rules: {a: [0], b: [1, 'never'], c: [2, 'never', true]}},
withRules: {rules: {a: [RuleConfigSeverity.Disabled], b: [RuleConfigSeverity.Warning, 'never'], c: [RuleConfigSeverity.Error, 'never', true]}},
withParserPresetString: {parserPreset: 'test'},
withParserPresetObject: {parserPreset: {}},
withParserPresetObject2: {parserPreset: {name: 'string', path: 'string'}},
Expand Down
24 changes: 12 additions & 12 deletions @commitlint/cz-commitlint/src/Process.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {QualifiedRules, UserPromptConfig} from '@commitlint/types';
import {QualifiedRules, RuleConfigSeverity, UserPromptConfig} from '@commitlint/types';
import {Answers, DistinctQuestion} from 'inquirer';
import isFunction from 'lodash.isfunction';
import process from './Process';
Expand Down Expand Up @@ -68,22 +68,22 @@ afterEach(() => {
describe('conventional-changlog', () => {
beforeEach(() => {
rules = {
'body-leading-blank': [1, 'always'],
'body-max-line-length': [2, 'always', 100],
'footer-leading-blank': [1, 'always'],
'footer-max-line-length': [2, 'always', 100],
'header-max-length': [2, 'always', 100],
'body-leading-blank': [RuleConfigSeverity.Warning, 'always'],
'body-max-line-length': [RuleConfigSeverity.Error, 'always', 100],
'footer-leading-blank': [RuleConfigSeverity.Warning, 'always'],
'footer-max-line-length': [RuleConfigSeverity.Error, 'always', 100],
'header-max-length': [RuleConfigSeverity.Error, 'always', 100],
'subject-case': [
2,
RuleConfigSeverity.Error,
'never',
['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'subject-empty': [RuleConfigSeverity.Error, 'never'],
'subject-full-stop': [RuleConfigSeverity.Error, 'never', '.'],
'type-case': [RuleConfigSeverity.Error, 'always', 'lower-case'],
'type-empty': [RuleConfigSeverity.Error, 'never'],
'type-enum': [
2,
RuleConfigSeverity.Error,
'always',
[
'build',
Expand Down
18 changes: 9 additions & 9 deletions @commitlint/cz-commitlint/src/store/rules.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {QualifiedRules} from '@commitlint/types';
import {QualifiedRules, RuleConfigSeverity} from '@commitlint/types';
import {GetRuleMethod, SetRulesMethod} from './rules';

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

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

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

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

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

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

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

rules = {
'footer-max-length': [2, 'always', 100],
'footer-max-length': [RuleConfigSeverity.Error, 'always', 100],
};
setRules(rules);
expect(getRule('body', 'max-length')).toBeUndefined();
Expand Down
8 changes: 4 additions & 4 deletions @commitlint/cz-commitlint/src/utils/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ test('getMaxLength', () => {
expect(getMaxLength([RuleConfigSeverity.Error] as any)).toBe(Infinity);

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

const rules: any = {
'body-min-length': [2, 'always', 10],
'footer-min-length': [2, 'always', 20],
'body-min-length': [RuleConfigSeverity.Error, 'always', 10],
'footer-min-length': [RuleConfigSeverity.Error, 'always', 20],
'test-min-length': [RuleConfigSeverity.Disabled, 'always', 100],
};
let lengthRule = rules['header-min-length'];
Expand Down
37 changes: 19 additions & 18 deletions @commitlint/lint/src/lint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import lint from './lint';
import {RuleConfigSeverity} from '@commitlint/types';

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

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

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

test('positive on ignored message and broken rule', async () => {
const actual = await lint('Revert "some bogus commit"', {
'type-empty': [2, 'never'],
'type-empty': [RuleConfigSeverity.Error, 'never'],
});
expect(actual.valid).toBe(true);
expect(actual.input).toBe('Revert "some bogus commit"');
Expand All @@ -44,7 +45,7 @@ test('negative on ignored message, disabled ignored messages and broken rule', a
const actual = await lint(
'Revert "some bogus commit"',
{
'type-empty': [2, 'never'],
'type-empty': [RuleConfigSeverity.Error, 'never'],
},
{
defaultIgnores: false,
Expand All @@ -58,7 +59,7 @@ test('positive on custom ignored message and broken rule', async () => {
const actual = await lint(
ignoredMessage,
{
'type-empty': [2, 'never'],
'type-empty': [RuleConfigSeverity.Error, 'never'],
},
{
ignores: [(c) => c === ignoredMessage],
Expand All @@ -72,8 +73,8 @@ test('positive on stub message and opts', async () => {
const actual = await lint(
'foo-bar',
{
'type-enum': [2, 'always', ['foo']],
'type-empty': [2, 'never'],
'type-enum': [RuleConfigSeverity.Error, 'always', ['foo']],
'type-empty': [RuleConfigSeverity.Error, 'never'],
},
{
parserOpts: {
Expand All @@ -85,7 +86,7 @@ test('positive on stub message and opts', async () => {
});

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

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

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

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

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

expect(report.valid).toBe(true);
});

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

expect(report.valid).toBe(false);
Expand All @@ -180,7 +181,7 @@ test('succeds for custom issue prefix', async () => {
const report = await lint(
'somehting REF-1',
{
'references-empty': [2, 'never'],
'references-empty': [RuleConfigSeverity.Error, 'never'],
},
{
parserOpts: {
Expand All @@ -196,7 +197,7 @@ test('fails for custom issue prefix', async () => {
const report = await lint(
'somehting #1',
{
'references-empty': [2, 'never'],
'references-empty': [RuleConfigSeverity.Error, 'never'],
},
{
parserOpts: {
Expand All @@ -212,7 +213,7 @@ test('fails for custom plugin rule', async () => {
const report = await lint(
'somehting #1',
{
'plugin-rule': [2, 'never'],
'plugin-rule': [RuleConfigSeverity.Error, 'never'],
},
{
plugins: {
Expand All @@ -232,7 +233,7 @@ test('passes for custom plugin rule', async () => {
const report = await lint(
'somehting #1',
{
'plugin-rule': [2, 'never'],
'plugin-rule': [RuleConfigSeverity.Error, 'never'],
},
{
plugins: {
Expand Down Expand Up @@ -275,7 +276,7 @@ test('returns original message with commit header, body and footer, parsing comm
const report = await lint(
message,
{
'references-empty': [2, 'never'],
'references-empty': [RuleConfigSeverity.Error, 'never'],
},
{
parserOpts: {
Expand All @@ -291,7 +292,7 @@ test('passes for async rule', async () => {
const report = await lint(
'somehting #1',
{
'async-rule': [2, 'never'],
'async-rule': [RuleConfigSeverity.Error, 'never'],
},
{
plugins: {
Expand Down
Loading

0 comments on commit e3d2091

Please sign in to comment.