Skip to content

Commit

Permalink
Add useful error on empty presets
Browse files Browse the repository at this point in the history
Empty presets are most likely a mistake by the user. At best, they do nothing.
This improves the situation for humans that make mistakes, by throwing a useful error.

Closes GH-200.
Closes GH-202.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Remco Haszing <remcohaszing@gmail.com>
Reviewed-by: JounQin <admin@1stg.me>
  • Loading branch information
wooorm committed Aug 9, 2023
1 parent 4a94722 commit b35afe0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ function base() {
* @returns {void}
*/
function addPreset(result) {
if (!('plugins' in result) && !('settings' in result)) {
throw new Error(
'Expected usable value but received an empty preset, which is probably a mistake: presets typically come with `plugins` and sometimes with `settings`, but this has neither'
)
}

addList(result.plugins)

if (result.settings) {
Expand Down
11 changes: 7 additions & 4 deletions test/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,13 @@ test('use(preset)', async (t) => {
'should throw on invalid `plugins` (2)'
)

await t.test('should support empty presets', () => {
const processor = unified().use({}).freeze()
assert.equal(processor.attachers.length, 0)
})
assert.throws(
() => {
unified().use({}).freeze()
},
/Expected usable value but received an empty preset/,
'should throw on empty presets'
)

await t.test('should support presets with empty plugins', () => {
const processor = unified().use({plugins: []}).freeze()
Expand Down

0 comments on commit b35afe0

Please sign in to comment.