-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathindex.js
85 lines (72 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import ruleFunctions from './rules';
import format from './library/format';
import getConfiguration from './library/get-configuration';
import getMessages from './library/get-messages';
import getPreset from './library/get-preset';
import parse from './library/parse';
export {format, getConfiguration, getMessages, getPreset};
export default async (message, options = {}) => {
const {
configuration: {
rules,
wildcards
}
} = options;
// Parse the commit message
const parsed = parse(message);
// Wildcard matches skip the linting
const bails = Object.entries(wildcards)
.filter(entry => {
const [, pattern] = entry;
return Array.isArray(pattern);
})
.filter(entry => {
const [, pattern] = entry;
const expression = new RegExp(...pattern);
return parsed.header.match(expression);
})
.map(entry => entry[0]);
// Found a wildcard match, skip
if (bails.length > 0) {
return {
valid: true,
wildcards: bails,
rules: [],
warnings: [],
errors: []
};
}
// Validate against all rules
const results = Object.entries(rules)
.filter(entry => {
const [, [level]] = entry;
return level > 0;
})
.map(entry => {
const [name, config] = entry;
const [level, when, value] = config;
// Level 0 rules are ignored
if (level === 0) {
return null;
}
const rule = ruleFunctions[name];
const [valid, message] = rule(parsed, when, value);
return {
level,
valid,
name,
message
};
})
.filter(Boolean);
const errors = results.filter(result =>
result.level > 1 && !result.valid);
const warnings = results.filter(result =>
result.level < 2 && !result.valid);
const valid = errors.length === 0;
return {
valid,
errors,
warnings
};
};