From cabdfa8c2dd21ad022975dc3777f7de514ceb17d Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 8 Jan 2025 00:21:50 +0100 Subject: [PATCH] fix(lint): fix single char selectors being ignored (#27576) The selector splitting code that's used for JS linting plugins didn't properly account for selectors being a single character. This can happen in the case of `*`. Instead of comparing against the length, we'll now check if the remaining string portion is not empty, which is more robust. It also allows us to detect trailing whitespace, which we didn't before. --- cli/js/40_lint_selector.js | 5 +++-- tests/unit/lint_selectors_test.ts | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/js/40_lint_selector.js b/cli/js/40_lint_selector.js index 7b94c4f960..c4cd523f9f 100644 --- a/cli/js/40_lint_selector.js +++ b/cli/js/40_lint_selector.js @@ -406,8 +406,9 @@ export function splitSelectors(input) { } } - if (last < input.length - 1) { - out.push(input.slice(last).trim()); + const remaining = input.slice(last).trim(); + if (remaining.length > 0) { + out.push(remaining); } return out; diff --git a/tests/unit/lint_selectors_test.ts b/tests/unit/lint_selectors_test.ts index c116871318..e9e4dacb4e 100644 --- a/tests/unit/lint_selectors_test.ts +++ b/tests/unit/lint_selectors_test.ts @@ -20,6 +20,9 @@ import { import { assertThrows } from "@std/assert"; Deno.test("splitSelectors", () => { + assertEquals(splitSelectors("*"), ["*"]); + assertEquals(splitSelectors("*,*"), ["*", "*"]); + assertEquals(splitSelectors("*,* "), ["*", "*"]); assertEquals(splitSelectors("foo"), ["foo"]); assertEquals(splitSelectors("foo, bar"), ["foo", "bar"]); assertEquals(splitSelectors("foo:f(bar, baz)"), ["foo:f(bar, baz)"]);