-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,8 @@ | |
"parserOpts": {} | ||
}, | ||
"additionalProperties": true | ||
} | ||
}, | ||
{"typeof": "function"} | ||
] | ||
}, | ||
"helpUrl": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {loadParserOpts} from './load-parser-opts'; | ||
|
||
test('handles a plain preset', async () => { | ||
const preset = { | ||
parserOpts: {}, | ||
}; | ||
|
||
expect(await loadParserOpts(preset)).toEqual(preset); | ||
}); | ||
|
||
test('handles primitive values', async () => { | ||
expect(await loadParserOpts('')).toEqual(undefined); | ||
expect(await loadParserOpts(undefined)).toEqual(undefined); | ||
}); | ||
|
||
test('handles an object without any parserOpts', async () => { | ||
const preset = {}; | ||
expect(await loadParserOpts(preset)).toEqual(preset); | ||
}); | ||
|
||
test('handles nested parserOpts', async () => { | ||
const opts = {a: 4}; | ||
|
||
// plain nested parserOpts | ||
let loaded = await loadParserOpts({ | ||
parserOpts: { | ||
parserOpts: opts, | ||
}, | ||
}); | ||
expect(loaded).toHaveProperty('parserOpts', opts); | ||
|
||
// async nested parserOpts | ||
loaded = await loadParserOpts({ | ||
parserOpts: Promise.resolve({ | ||
parserOpts: opts, | ||
}), | ||
}); | ||
expect(loaded).toHaveProperty('parserOpts', opts); | ||
}); | ||
|
||
test('runs a sync function which returns the preset', async () => { | ||
const preset = {}; | ||
const fn = () => preset; | ||
const opts = await loadParserOpts(fn); | ||
|
||
expect(opts).toEqual(preset); | ||
}); | ||
|
||
test('runs an async function which returns the preset', async () => { | ||
const preset = {}; | ||
const fn = async () => preset; | ||
const opts = await loadParserOpts(fn); | ||
|
||
expect(opts).toEqual(preset); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters