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.
  • Loading branch information
wooorm committed Oct 12, 2022
1 parent b8d42cb commit 7e5bfaf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 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
12 changes: 7 additions & 5 deletions test/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ test('use(preset)', (t) => {
'should throw on invalid `plugins` (2)'
)

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

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

0 comments on commit 7e5bfaf

Please sign in to comment.