Skip to content

Commit 318a67f

Browse files
committed
refactor: move ignore wildcard matching to isIgnore method
* hide former wildcards inside new utility * remove related documentation * emit warning when wildcard configuration is passed BREAKING CHANGES * wildcards config is now ignored entirely
1 parent 702a2f7 commit 318a67f

File tree

5 files changed

+79
-30
lines changed

5 files changed

+79
-30
lines changed

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
]
6262
},
6363
"production": {
64-
"ignore": ["**/*.test.js"]
64+
"ignore": [
65+
"**/*.test.js"
66+
]
6567
}
6668
},
6769
"presets": [
@@ -174,6 +176,7 @@
174176
"meow": "3.7.0",
175177
"mz": "2.6.0",
176178
"pos": "0.4.2",
177-
"rc": "1.1.7"
179+
"rc": "1.1.7",
180+
"semver": "^5.3.0"
178181
}
179182
}

source/index.js

+9-28
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,28 @@ import format from './library/format';
33
import getConfiguration from './library/get-configuration';
44
import getMessages from './library/get-messages';
55
import getPreset from './library/get-preset';
6+
import isIgnored from './library/is-ignored';
67
import parse from './library/parse';
78

89
export {format, getConfiguration, getMessages, getPreset};
910

1011
export default async (message, options = {}) => {
11-
const {
12-
configuration: {
13-
rules,
14-
wildcards
15-
}
16-
} = options;
17-
18-
// Parse the commit message
19-
const parsed = parse(message);
20-
21-
// Wildcard matches skip the linting
22-
const bails = Object.entries(wildcards)
23-
.filter(entry => {
24-
const [, pattern] = entry;
25-
return Array.isArray(pattern);
26-
})
27-
.filter(entry => {
28-
const [, pattern] = entry;
29-
const expression = new RegExp(...pattern);
30-
return parsed.header.match(expression);
31-
})
32-
.map(entry => entry[0]);
12+
const {configuration} = options;
3313

3414
// Found a wildcard match, skip
35-
if (bails.length > 0) {
15+
if (isIgnored(message)) {
3616
return {
3717
valid: true,
38-
wildcards: bails,
39-
rules: [],
40-
warnings: [],
41-
errors: []
18+
errors: [],
19+
warnings: []
4220
};
4321
}
4422

23+
// Parse the commit message
24+
const parsed = parse(message);
25+
4526
// Validate against all rules
46-
const results = Object.entries(rules)
27+
const results = Object.entries(configuration.rules)
4728
.filter(entry => {
4829
const [, [level]] = entry;
4930
return level > 0;

source/library/is-ignored.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import semver from 'semver';
2+
3+
const WILDCARDS = [
4+
c => c.match(/^(Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?))$)/),
5+
c => c.match(/^(R|r)evert (.*)/),
6+
c => semver.valid(c)
7+
];
8+
9+
export default function isIgnored(commit = '') {
10+
return WILDCARDS.some(w => w(commit));
11+
}

source/library/is-ignored.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import test from 'ava';
2+
import isIgnored from './is-ignored';
3+
4+
test('should return false when called without arguments', t => {
5+
t.false(isIgnored());
6+
});
7+
8+
test('should return false when called with empty string', t => {
9+
t.false(isIgnored(''));
10+
});
11+
12+
test('should return false for normal commit', t => {
13+
t.false(isIgnored('initial commit'));
14+
});
15+
16+
test('should return false for branch merges', t => {
17+
t.true(isIgnored('Merge branch \'iss53\''));
18+
});
19+
20+
test('should return true for merged PRs', t => {
21+
t.true(isIgnored('Merge pull request #369'));
22+
});
23+
24+
test('should return true for revert commits', t => {
25+
t.true(isIgnored(`Revert "docs: add recipe for linting of all commits in a PR (#36)"\n\nThis reverts commit 1e69d542c16c2a32acfd139e32efa07a45f19111.`));
26+
t.true(isIgnored(`revert "docs: add recipe for linting of all commits in a PR (#36)"\n\nThis reverts commit 1e69d542c16c2a32acfd139e32efa07a45f19111.`));
27+
});
28+
29+
test('should return true for npm version commits', t => {
30+
t.true(isIgnored(`0.0.1`));
31+
t.true(isIgnored(`0.1.0`));
32+
t.true(isIgnored(`1.0.0`));
33+
t.true(isIgnored(`0.0.1-alpha`));
34+
t.true(isIgnored(`0.0.1-some-crazy-tag`));
35+
t.true(isIgnored(`0.0.1-0`));
36+
t.true(isIgnored(`0.0.1-999`));
37+
t.true(isIgnored(`0.0.1-alpha.0`));
38+
t.true(isIgnored(`0.0.1-alpha.999`));
39+
t.true(isIgnored(`0.0.1-some-crazy-tag.0`));
40+
t.true(isIgnored(`0.0.1-some-crazy-tag.999`));
41+
t.true(isIgnored(`0.0.1-1e69d54`));
42+
});

source/library/resolve-extends.js

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const cwd = importFrom.bind(null, process.cwd());
77
export default function resolveExtends(config = {}, prefix = '', key = 'extends', require = cwd) {
88
const extended = loadExtends(config, prefix, key, require)
99
.reduceRight((r, c) => merge(r, omit(c, [key])), config[key] ? {[key]: config[key]} : {});
10+
11+
// Remove deprecation warning in version 3
12+
if (typeof c === 'object' && 'wildcards' in config) {
13+
console.warn(`'wildcards' found in top-level configuration ignored. Remove them from your config to silence this warning.`);
14+
}
15+
1016
return merge({}, extended, config);
1117
}
1218

@@ -16,6 +22,12 @@ function loadExtends(config = {}, prefix = '', key = 'extends', require = cwd) {
1622
return toExtend.reduce((configs, raw) => {
1723
const id = [prefix, raw].filter(String).join('-');
1824
const c = require(id);
25+
26+
// Remove deprecation warning in version 3
27+
if (typeof c === 'object' && 'wildcards' in c) {
28+
console.warn(`'wildcards' found in '${id}' ignored. Raise an issue at 'npm repo ${id}' to remove the wildcards and silence this warning.`);
29+
}
30+
1931
return [...configs, c, ...loadExtends(c, prefix, key, require)];
2032
}, []);
2133
}

0 commit comments

Comments
 (0)