From ef88b79017313013aa3a6381734f1fc3c59ef675 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 26 Nov 2026 16:07:50 +0530 Subject: [PATCH] fix: report a parser error for a `(` with nothing after it --- src/__tests__/exceptions.mjs | 9 +++++++++ src/__tests__/property/known.mjs | 6 ------ src/parser.js | 4 ++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/__tests__/exceptions.mjs b/src/__tests__/exceptions.mjs index f4b839a..e763cec 100644 --- a/src/__tests__/exceptions.mjs +++ b/src/__tests__/exceptions.mjs @@ -35,6 +35,15 @@ throws( "a(", "Expected a closing parenthesis.", ); +// A `(` with nothing after it, at the start or right after a pseudo, still +// threw a raw TypeError: there was no token to start the new node at. +for (const selector of ["(", ":not(", "a:hover(", "a, ("]) { + throws( + `unclosed parenthesis with nothing after it in ${selector}`, + selector, + "Expected a closing parenthesis.", + ); +} // An attribute selector whose last token before `]` is one of `* $ ^ ~ |`. // The `attribute` token loop looked ahead to `next` without checking it diff --git a/src/__tests__/property/known.mjs b/src/__tests__/property/known.mjs index 3f7a129..112250e 100644 --- a/src/__tests__/property/known.mjs +++ b/src/__tests__/property/known.mjs @@ -45,10 +45,4 @@ export const KNOWN = [ note: "an attribute namespace of `&` prints as `[&|x]`, which does not parse", match: (f) => f.case === "attribute namespace setter" && f.value === "&", }, - { - property: "invalid input throws a parser error", - tracking: "not filed yet", - note: "an unclosed `(` at the start or after a pseudo throws a TypeError", - match: (f) => f.error.startsWith("TypeError") && f.input.includes("("), - }, ]; diff --git a/src/parser.js b/src/parser.js index 7eb118d..456abc0 100644 --- a/src/parser.js +++ b/src/parser.js @@ -918,6 +918,10 @@ export default class Parser { let unbalanced = 1; let openingToken = this.currToken; this.position++; + if (!this.currToken) { + // Nothing follows the `(`, so there is no token to start a node at. + return this.expected("closing parenthesis", openingToken[TOKEN.START_POS]); + } if (last && last.type === types.PSEUDO) { const selector = new Selector({ source: { start: tokenStart(this.tokens[this.position]) },