From b35afe0d9d4fdfc75ced40d945ebec19e5194166 Mon Sep 17 00:00:00 2001 From: Titus Date: Wed, 9 Aug 2023 15:30:52 +0200 Subject: [PATCH] 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 Reviewed-by: Remco Haszing Reviewed-by: JounQin --- lib/index.js | 6 ++++++ test/use.js | 11 +++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 925c4dfb..27cd4f0d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/test/use.js b/test/use.js index e3fa5913..ffb2968b 100644 --- a/test/use.js +++ b/test/use.js @@ -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()