Skip to content

Commit af6d426

Browse files
committed
fix: correctly check for RegExp
1 parent 1b2c039 commit af6d426

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

.husky/commit-msg

100644100755
File mode changed.

.husky/pre-commit

100644100755
File mode changed.

__tests__/plugin.test.js

+118
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
const commitlintPluginSelectiveScope = require('../index')
2+
const commitlintPluginSelectiveScopeResolver =
3+
commitlintPluginSelectiveScope.rules['selective-scope']
4+
// Wrap the resolver so we don't have to pass in "always"
5+
const commitlintPluginSelectiveScopeResolverWrapped = (ctx, rules) =>
6+
commitlintPluginSelectiveScopeResolver(ctx, 'always', rules)
7+
8+
const TEST_RULES = {
9+
feat: [/^frontend\/[^/]+$/, 'backend'],
10+
perf: [],
11+
ci: [null, 'codebuild', 'jenkins']
12+
}
213

314
describe('commitlintPluginSelectiveScope', () => {
415
it('should return a valid config', () => {
@@ -7,4 +18,111 @@ describe('commitlintPluginSelectiveScope', () => {
718
Object.keys(commitlintPluginSelectiveScope.rules).length
819
).toBeGreaterThan(0)
920
})
21+
22+
it('should not enforce scope if the type does not appear in the rules', () => {
23+
expect(
24+
commitlintPluginSelectiveScopeResolverWrapped(
25+
{ scope: 'frontend/web', type: 'fix' },
26+
TEST_RULES
27+
)[0]
28+
).toBe(true)
29+
30+
expect(
31+
commitlintPluginSelectiveScopeResolverWrapped(
32+
{ scope: 'anything', type: 'fix' },
33+
TEST_RULES
34+
)[0]
35+
).toBe(true)
36+
37+
expect(
38+
commitlintPluginSelectiveScopeResolverWrapped(
39+
{ scope: null, type: 'fix' },
40+
TEST_RULES
41+
)[0]
42+
).toBe(true)
43+
})
44+
45+
it('should not allow scope if the type appears in the rules with an empty array', () => {
46+
expect(
47+
commitlintPluginSelectiveScopeResolverWrapped(
48+
{ scope: null, type: 'perf' },
49+
TEST_RULES
50+
)[0]
51+
).toBe(true)
52+
53+
expect(
54+
commitlintPluginSelectiveScopeResolverWrapped(
55+
{ scope: 'something', type: 'perf' },
56+
TEST_RULES
57+
)[0]
58+
).toBe(false)
59+
})
60+
61+
describe('should only allow scopes defined if the type appears in the rule with a non-empty array', () => {
62+
it('should properly match a string literal', () => {
63+
expect(
64+
commitlintPluginSelectiveScopeResolverWrapped(
65+
{ scope: 'backend', type: 'feat' },
66+
TEST_RULES
67+
)[0]
68+
).toBe(true)
69+
70+
expect(
71+
commitlintPluginSelectiveScopeResolverWrapped(
72+
{ scope: 'test', type: 'feat' },
73+
TEST_RULES
74+
)[0]
75+
).toBe(false)
76+
})
77+
78+
it('should properly match a RegExp', () => {
79+
expect(
80+
commitlintPluginSelectiveScopeResolverWrapped(
81+
{ scope: 'frontend/web', type: 'feat' },
82+
TEST_RULES
83+
)[0]
84+
).toBe(true)
85+
86+
expect(
87+
commitlintPluginSelectiveScopeResolverWrapped(
88+
{ scope: 'frontend', type: 'feat' },
89+
TEST_RULES
90+
)[0]
91+
).toBe(false)
92+
})
93+
})
94+
95+
describe('optional scope', () => {
96+
it('should allow scope to be optional if the type appears in the rules with a null in the array', () => {
97+
expect(
98+
commitlintPluginSelectiveScopeResolverWrapped(
99+
{ scope: null, type: 'ci' },
100+
TEST_RULES
101+
)[0]
102+
).toBe(true)
103+
})
104+
105+
it('should match scope if provided', () => {
106+
expect(
107+
commitlintPluginSelectiveScopeResolverWrapped(
108+
{ scope: 'codebuild', type: 'ci' },
109+
TEST_RULES
110+
)[0]
111+
).toBe(true)
112+
113+
expect(
114+
commitlintPluginSelectiveScopeResolverWrapped(
115+
{ scope: 'jenkins', type: 'ci' },
116+
TEST_RULES
117+
)[0]
118+
).toBe(true)
119+
120+
expect(
121+
commitlintPluginSelectiveScopeResolverWrapped(
122+
{ scope: 'github', type: 'ci' },
123+
TEST_RULES
124+
)[0]
125+
).toBe(false)
126+
})
127+
})
10128
})

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
allowedScopes.findIndex(s => {
3131
if (
3232
typeof ctx.scope === 'string' &&
33-
Object.prototype.toString.call() === '[object RegExp]'
33+
Object.prototype.toString.call(s) === '[object RegExp]'
3434
) {
3535
return ctx.scope.match(s)
3636
}

0 commit comments

Comments
 (0)