Skip to content

Commit 2c32314

Browse files
committed
fix: optimize loadParserOpts
1 parent f1bf2d2 commit 2c32314

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ function isObjectLike(obj: unknown): obj is Record<string, unknown> {
44
return Boolean(obj) && typeof obj === 'object'; // typeof null === 'object'
55
}
66

7-
function isPromiseLike(obj: unknown): obj is Promise<unknown> {
8-
return isObjectLike(obj) && typeof (obj as any).then === 'function';
9-
}
10-
117
function isParserOptsFunction<T extends ParserPreset>(
128
obj: T
139
): obj is T & {
14-
parserOpts: (
15-
cb: (_: never, parserOpts: Record<string, unknown>) => unknown
16-
) => Record<string, unknown> | undefined;
10+
parserOpts: (...args: any[]) => any;
1711
} {
1812
return typeof obj.parserOpts === 'function';
1913
}
@@ -27,9 +21,21 @@ export async function loadParserOpts(
2721
// Await for the module, loaded with require
2822
const parser = await pendingParser;
2923

30-
// Await parser opts if applicable
31-
if (isPromiseLike(parser.parserOpts)) {
32-
parser.parserOpts = ((await parser.parserOpts) as any).parserOpts;
24+
// exit early, no opts to resolve
25+
if (!parser.parserOpts) {
26+
return parser;
27+
}
28+
29+
// Pull nested parserOpts, might happen if overwritten with a module in main config
30+
if (typeof parser.parserOpts === 'object') {
31+
// Await parser opts if applicable
32+
parser.parserOpts = await parser.parserOpts;
33+
if (
34+
isObjectLike(parser.parserOpts) &&
35+
isObjectLike(parser.parserOpts.parserOpts)
36+
) {
37+
parser.parserOpts = parser.parserOpts.parserOpts;
38+
}
3339
return parser;
3440
}
3541

@@ -40,10 +46,10 @@ export async function loadParserOpts(
4046
parser.name.startsWith('conventional-changelog-')
4147
) {
4248
return new Promise((resolve) => {
43-
const result = parser.parserOpts((_: never, opts) => {
49+
const result = parser.parserOpts((_: never, opts: any) => {
4450
resolve({
4551
...parser,
46-
parserOpts: opts.parserOpts,
52+
parserOpts: opts?.parserOpts,
4753
});
4854
});
4955

@@ -53,20 +59,13 @@ export async function loadParserOpts(
5359
Promise.resolve(result).then((opts) => {
5460
resolve({
5561
...parser,
56-
parserOpts: opts.parserOpts,
62+
parserOpts: opts?.parserOpts,
5763
});
5864
});
5965
}
6066
return;
6167
});
6268
}
6369

64-
// Pull nested parserOpts, might happen if overwritten with a module in main config
65-
if (
66-
isObjectLike(parser.parserOpts) &&
67-
typeof parser.parserOpts.parserOpts === 'object'
68-
) {
69-
parser.parserOpts = parser.parserOpts.parserOpts;
70-
}
7170
return parser;
7271
}

0 commit comments

Comments
 (0)