Skip to content

Commit

Permalink
fix: allow [0] shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Jan 9, 2018
1 parent edf7187 commit 84cf938
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 12 additions & 8 deletions @commitlint/core/src/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ export default async (message, rules = {}, opts = {}) => {
);
}

if (config.length !== 2 && config.length !== 3) {
return new Error(
`config for rule ${name} must be 2 or 3 items long, received ${util.inspect(
config
)} of length ${config.length}`
);
}

const [level, when] = config;

if (typeof level !== 'number' || isNaN(level)) {
Expand All @@ -59,6 +51,18 @@ export default async (message, rules = {}, opts = {}) => {
);
}

if (level === 0 && config.length === 1) {
return null;
}

if (config.length !== 2 && config.length !== 3) {
return new Error(
`config for rule ${name} must be 2 or 3 items long, received ${util.inspect(
config
)} of length ${config.length}`
);
}

if (level < 0 || level > 2) {
return new RangeError(
`level for rule ${name} must be between 0 and 2, received ${util.inspect(
Expand Down
7 changes: 5 additions & 2 deletions @commitlint/core/src/lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ test('throws for invalid rule config', async t => {
t.true(error.message.indexOf('scope-enum must be array') > -1);
});

test('allows disable shorthand', async t => {
await t.notThrows(lint('foo', {'type-enum': [0], 'scope-enum': [0]}));
});

test('throws for rule with invalid length', async t => {
const error = await t.throws(
lint('type(scope): foo', {'type-enum': [], 'scope-enum': [1, 2, 3, 4]})
lint('type(scope): foo', {'scope-enum': [1, 2, 3, 4]})
);

t.true(error.message.indexOf('type-enum must be 2 or 3 items long') > -1);
t.true(error.message.indexOf('scope-enum must be 2 or 3 items long') > -1);
});

Expand Down

0 comments on commit 84cf938

Please sign in to comment.