-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (64 loc) · 2.04 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
module.exports = {
rules: {
'selective-scope': (ctx, applicable, rule) => {
if (applicable === 'never') {
return [false, 'the "allowed-scopes" rule does not support "never"']
}
const allowedScopes = rule[ctx.type]
// If the type does not appear in the rule config, allow any scope or no scope
if (allowedScopes === undefined) {
return [true]
}
if (Array.isArray(allowedScopes)) {
// If the type maps to an empty array in the rule config, scope it not allowed
if (allowedScopes.length === 0) {
if (ctx.scope != null) {
return [
false,
`commit messages with type "${ctx.type}" must not specify a scope`
]
}
return [true]
}
// Otherwise attempt to match against either null, a string literal, or a RegExp
if (
allowedScopes.findIndex(s => {
if (
typeof ctx.scope === 'string' &&
Object.prototype.toString.call(s) === '[object RegExp]'
) {
return ctx.scope.match(s)
}
// Equality comparison works for both strings and null
return s === ctx.scope
}) !== -1
) {
return [true]
} else if (allowedScopes.includes(null)) {
return [
false,
`commit message with type "${
ctx.type
}" may specify a scope, but if specified, it must be one of the following: ${allowedScopes
.filter(s => s !== null)
.map(s => `"${s}"`)
.join(', ')}`
]
} else {
return [
false,
`commit message with type "${
ctx.type
}" must specify one of the following scopes: ${allowedScopes
.map(s => `"${s}"`)
.join(', ')}`
]
}
}
return [
false,
`invalid rule entry: { ${ctx.type}: ${JSON.stringify(allowedScopes)} }`
]
}
}
}