Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: simplify configurable is-ignored
  • Loading branch information
marionebl committed Apr 26, 2019
commit 9cd95ca38be82cbe56d09f88f1948aede57dd627
45 changes: 19 additions & 26 deletions @commitlint/is-ignored/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,24 @@ export const WILDCARDS = [
];

export default function isIgnored(commit = '', opts = {}) {
let wildcards = [];
if (opts.ignoredMessages) {
if (opts.disableDefaultIgnoredMessages) {
wildcards = wildcards.concat(WILDCARDS);
}
if (!Array.isArray(opts.ignoredMessages)) {
throw new Error('ignoredMessages must be an Array');
}
opts.ignoredMessages.forEach(ignoreConfig => {
if (typeof ignoreConfig === 'function') {
wildcards.push(ignoreConfig);
} else if (ignoreConfig instanceof RegExp) {
wildcards.push(c => c.match(ignoreConfig));
} else if (typeof ignoreConfig === 'string') {
wildcards.push(c => c.match(new RegExp(ignoreConfig)));
} else {
throw new Error(
'ignoredMessage element must be a function, string or RegExp'
);
}
});
} else if (opts.disableDefaultIgnoredMessages) {
return false;
} else {
wildcards = WILDCARDS;
const ignores = typeof opts.ignores === 'undefined' ? [] : opts.ignores;

if (!Array.isArray(ignores)) {
throw new Error(
`ignores must be of type array, received ${ignores} of type ${typeof ignores}`
);
}
return wildcards.some(w => w(commit));

const invalids = ignores.filter(c => typeof c !== 'function');

if (invalids.length > 0) {
throw new Error(
`ignores must be array of type function, received items of type: ${invalids
.map(i => typeof i)
.join(', ')}`
);
}

const base = opts.defaults === false ? [] : WILDCARDS;
return [...base, ...ignores].some(w => w(commit));
}
47 changes: 11 additions & 36 deletions @commitlint/is-ignored/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,61 +118,36 @@ test('should return false for commits containing, but not starting, with merge b
t.false(isIgnored('foo bar Merge branch xxx'));
});

test('should return false for ignored message if disableDefaultIgnoredMessages is true', t => {
test('should return false for ignored message if defaults is false', t => {
t.false(
isIgnored('Auto-merged develop into master', {
disableDefaultIgnoredMessages: true
defaults: false
})
);
});

test('should return false for ignored message if custom ignoredMessages and disableDefaultIgnoredMessages is true', t => {
test('should return false for ignored message if custom ignores and defaults is false', t => {
t.false(
isIgnored('Auto-merged develop into master', {
disableDefaultIgnoredMessages: true,
ignoredMessages: []
defaults: false
})
);
});

test('should throw error if ignoredMessages is not an array', t => {
test('should throw error if ignores is not an array', t => {
const ignoredString = 'this should be ignored';
t.throws(
t.throws(() => {
isIgnored(ignoredString, {
ignoredMessages: 'throws error'
})
);
ignores: 'throws error'
});
});
});

test('should return true for custom ignoredMessages as function', t => {
test('should return true for custom ignores as function', t => {
const ignoredString = 'this should be ignored';
t.true(
isIgnored(ignoredString, {
ignoredMessages: [c => c === ignoredString]
})
);
});

test('should return true for custom ignoredMessages as RegExp', t => {
t.true(
isIgnored('Should Ignore', {
ignoredMessages: [/^should ignore$/i]
})
);
});

test('should return true for custom ignoredMessages as string', t => {
t.true(
isIgnored('Should Ignore', {
ignoredMessages: ['[sS]hould [iI]gnore']
})
);
});

test('should throw error if ignoredMessage value is not an RegExp, string or function', t => {
t.throws(
isIgnored('some commit message', {
ignoredMessages: [true]
ignores: [c => c === ignoredString]
})
);
});
4 changes: 3 additions & 1 deletion @commitlint/lint/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const buildCommitMesage = ({header, body, footer}) => {

export default async (message, rules = {}, opts = {}) => {
// Found a wildcard match, skip
if (isIgnored(message, opts)) {
if (
isIgnored(message, {defaults: opts.defaultIgnores, ignores: opts.ignores})
) {
return {
valid: true,
errors: [],
Expand Down
4 changes: 2 additions & 2 deletions @commitlint/lint/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('negative on ignored message, disabled ignored messages and broken rule', a
'type-empty': [2, 'never']
},
{
disableDefaultIgnoredMessages: true
defaultIgnores: false
}
);
t.false(actual.valid);
Expand All @@ -59,7 +59,7 @@ test('positive on custom ignored message and broken rule', async t => {
'type-empty': [2, 'never']
},
{
ignoredMessages: [c => c === ignoredMessage]
ignores: [c => c === ignoredMessage]
}
);
t.true(actual.valid);
Expand Down
12 changes: 3 additions & 9 deletions @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const valid = input =>
'plugins',
'parserPreset',
'formatter',
'ignoredMessages',
'disableDefaultIgnoredMessages'
'ignores',
'defaultIgnores'
);

export default async (seed = {}, options = {cwd: process.cwd()}) => {
Expand All @@ -27,13 +27,7 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
const config = valid(merge(loaded.config, seed));
const opts = merge(
{extends: [], rules: {}, formatter: '@commitlint/format'},
pick(
config,
'extends',
'plugins',
'ignoredMessages',
'disableDefaultIgnoredMessages'
)
pick(config, 'extends', 'plugins', 'ignores', 'defaultIgnores')
);

// Resolve parserPreset key
Expand Down