Skip to content

Commit 982357f

Browse files
feat(rules): add async promise based rules methods into lint
1 parent 9075844 commit 982357f

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

@commitlint/lint/src/index.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default async (message, rules = {}, opts = {}) => {
119119
}
120120

121121
// Validate against all rules
122-
const results = toPairs(rules)
122+
const results = await Promise.all(toPairs(rules)
123123
.filter(entry => {
124124
const [, [level]] = toPairs(entry);
125125
return level > 0;
@@ -134,17 +134,25 @@ export default async (message, rules = {}, opts = {}) => {
134134
}
135135

136136
const rule = mergedImplementations[name];
137-
138-
const [valid, message] = rule(parsed, when, value);
139-
140-
return {
141-
level,
142-
valid,
143-
name,
144-
message
145-
};
137+
const ruleOutput = rule(parsed, when, value);
138+
if (ruleOutput instanceof Promise) {
139+
return ruleOutput.then(([valid, message]) => ({
140+
level,
141+
valid,
142+
name,
143+
message
144+
}));
145+
} else {
146+
const [valid, message] = ruleOutput;
147+
return Promise.resolve({
148+
level,
149+
valid,
150+
name,
151+
message
152+
});
153+
}
146154
})
147-
.filter(Boolean);
155+
.filter(Boolean));
148156

149157
const errors = results.filter(result => result.level === 2 && !result.valid);
150158
const warnings = results.filter(

@commitlint/lint/src/index.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,26 @@ test('passes for custom plugin rule', async t => {
252252
t.true(report.valid);
253253
});
254254

255+
test('passes for custom plugin rule if rule is a Promise', async t => {
256+
const report = await lint(
257+
'somehting #1',
258+
{
259+
'plugin-rule': [2, 'never']
260+
},
261+
{
262+
plugins: {
263+
'plugin-example': {
264+
rules: {
265+
'plugin-rule': () => Promise.resolve([true])
266+
}
267+
}
268+
}
269+
}
270+
);
271+
272+
t.true(report.valid);
273+
});
274+
255275
test('returns original message only with commit header', async t => {
256276
const message = 'foo: bar';
257277
const report = await lint(message);

0 commit comments

Comments
 (0)