Skip to content

Commit b35afe0

Browse files
authored
Add useful error on empty presets
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>
1 parent 4a94722 commit b35afe0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ function base() {
197197
* @returns {void}
198198
*/
199199
function addPreset(result) {
200+
if (!('plugins' in result) && !('settings' in result)) {
201+
throw new Error(
202+
'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'
203+
)
204+
}
205+
200206
addList(result.plugins)
201207

202208
if (result.settings) {

test/use.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,13 @@ test('use(preset)', async (t) => {
291291
'should throw on invalid `plugins` (2)'
292292
)
293293

294-
await t.test('should support empty presets', () => {
295-
const processor = unified().use({}).freeze()
296-
assert.equal(processor.attachers.length, 0)
297-
})
294+
assert.throws(
295+
() => {
296+
unified().use({}).freeze()
297+
},
298+
/Expected usable value but received an empty preset/,
299+
'should throw on empty presets'
300+
)
298301

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

0 commit comments

Comments
 (0)