Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useful error on empty presets #202

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the types to reflect this?

interface PresetWithPlugins {
  plugins: PluggableList
  settings?: Record<string, unknown>
}

interface PresetWithSettings {
  plugins?: PluggableList
  settings: Record<string, unknown>
}

export type Preset = PresetWithPlugins | PresetWithSettings

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d say that makes errors harder to read, when something is wrong, TS will try to match both, and show both.
For a small gain?

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