Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 420e8d6

Browse files
authored
feat(load): accept functions as parser presets (#2982)
Previously, `load` could accept `parserPresets` as both values and promises of values. Per #2964 however, there was expectations for it to also support functions returning said values. This commit enables to specify `parserPresets` in all the same ways as rules. That is, both commitlint rules and `parserPresets` can be specified as: - Plain values - Promises - Functions returning plain values - Functions returning promises Solves #2964.
1 parent 718984a commit 420e8d6

File tree

4 files changed

+69
-2
lines changed
  • @commitlint
    • config-validator/src
      • __snapshots__
        • validate.test.ts.snap
      • commitlint.schema.json
    • load/src/utils
      • load-parser-opts.test.ts
      • load-parser-opts.ts

4 files changed

+69
-2
lines changed

@commitlint/config-validator/src/__snapshots__/validate.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ exports[`validation should fail for parserPreset 1`] = `
5656
"Commitlint configuration in parserPreset.js is invalid:
5757
- Property \\"parserPreset\\" has the wrong type - should be string.
5858
- Property \\"parserPreset\\" has the wrong type - should be object.
59+
- \\"parserPreset\\" should be a function. Value: [].
5960
- \\"parserPreset\\" should match exactly one schema in oneOf. Value: [].
6061
"
6162
`;

@commitlint/config-validator/src/commitlint.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
"parserOpts": {}
5858
},
5959
"additionalProperties": true
60-
}
60+
},
61+
{"typeof": "function"}
6162
]
6263
},
6364
"helpUrl": {

@commitlint/load/src/utils/load-parser-opts.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {loadParserOpts} from './load-parser-opts';
2+
3+
test('handles a plain preset', async () => {
4+
const preset = {
5+
parserOpts: {},
6+
};
7+
8+
expect(await loadParserOpts(preset)).toEqual(preset);
9+
});
10+
11+
test('handles primitive values', async () => {
12+
expect(await loadParserOpts('')).toEqual(undefined);
13+
expect(await loadParserOpts(undefined)).toEqual(undefined);
14+
});
15+
16+
test('handles an object without any parserOpts', async () => {
17+
const preset = {};
18+
expect(await loadParserOpts(preset)).toEqual(preset);
19+
});
20+
21+
test('handles nested parserOpts', async () => {
22+
const opts = {a: 4};
23+
24+
// plain nested parserOpts
25+
let loaded = await loadParserOpts({
26+
parserOpts: {
27+
parserOpts: opts,
28+
},
29+
});
30+
expect(loaded).toHaveProperty('parserOpts', opts);
31+
32+
// async nested parserOpts
33+
loaded = await loadParserOpts({
34+
parserOpts: Promise.resolve({
35+
parserOpts: opts,
36+
}),
37+
});
38+
expect(loaded).toHaveProperty('parserOpts', opts);
39+
});
40+
41+
test('runs a sync function which returns the preset', async () => {
42+
const preset = {};
43+
const fn = () => preset;
44+
const opts = await loadParserOpts(fn);
45+
46+
expect(opts).toEqual(preset);
47+
});
48+
49+
test('runs an async function which returns the preset', async () => {
50+
const preset = {};
51+
const fn = async () => preset;
52+
const opts = await loadParserOpts(fn);
53+
54+
expect(opts).toEqual(preset);
55+
});

@commitlint/load/src/utils/load-parser-opts.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {ParserPreset} from '@commitlint/types';
22

3+
type Awaitable<T> = T | PromiseLike<T>;
4+
35
function isObjectLike(obj: unknown): obj is Record<string, unknown> {
46
return Boolean(obj) && typeof obj === 'object'; // typeof null === 'object'
57
}
@@ -15,8 +17,16 @@ function isParserOptsFunction(
1517
}
1618

1719
export async function loadParserOpts(
18-
pendingParser: string | ParserPreset | Promise<ParserPreset> | undefined
20+
pendingParser:
21+
| string
22+
| Awaitable<ParserPreset>
23+
| (() => Awaitable<ParserPreset>)
24+
| undefined
1925
): Promise<ParserPreset | undefined> {
26+
if (typeof pendingParser === 'function') {
27+
return loadParserOpts(pendingParser());
28+
}
29+
2030
if (!pendingParser || typeof pendingParser !== 'object') {
2131
return undefined;
2232
}

0 commit comments

Comments
(0)