From 5c37078323aef648f02840fb6ffd3e3c2f84c86e Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 6 Nov 2023 15:53:23 +0100 Subject: [PATCH] Refactor error for unsupported feature --- lib/pseudo.js | 10 ++++++---- test/select-all.js | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/pseudo.js b/lib/pseudo.js index e040b4b..f365185 100644 --- a/lib/pseudo.js +++ b/lib/pseudo.js @@ -6,7 +6,7 @@ * @typedef {import('./types.js').SelectState} SelectState */ -import {unreachable} from 'devlop' +import {ok as assert, unreachable} from 'devlop' import fauxEsmNthCheck from 'nth-check' import {zwitch} from 'zwitch' import {parent} from './util.js' @@ -409,10 +409,12 @@ function getCachedNthCheck(query) { if (!fn) { const value = query.argument + assert(value, 'expected `argument`') - /* c8 ignore next 3 -- never happens with our config */ - if (!value || value.type !== 'Formula') { - unreachable('`:nth` has a formula') + if (value.type !== 'Formula') { + throw new Error( + 'Expected `nth` formula, such as `even` or `2n+1` (`of` is not yet supported)' + ) } fn = nthCheck(value.a + 'n+' + value.b) diff --git a/test/select-all.js b/test/select-all.js index 70a53b3..a2a01c6 100644 --- a/test/select-all.js +++ b/test/select-all.js @@ -438,6 +438,15 @@ test('select.selectAll()', async function (t) { [u('b', 'Bravo'), u('b', 'Delta'), u('b', 'Foxtrot')] ) }) + + await t.test( + 'should throw on unsupported `of` syntax', + async function () { + assert.throws(function () { + selectAll(':nth-child(odd of a)', u('a')) + }, /Expected `nth` formula, such as `even` or `2n\+1` \(`of` is not yet supported\)/) + } + ) }) await t.test(':nth-last-child', async function (t) {