sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-scoped-selector`

Closed

#1,429 opened on Jul 16, 2021

 (5 comments) (2 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Many people don't know the Element.querySelector() and Element.querySelectorAll() behaves differently than most common JavaScript DOM libraries, details.

Got the idea here

Fail

const inner = element.querySelectorAll('.outer .inner');

Pass

const inner = document.querySelectorAll('.outer .inner');
const inner = element.querySelectorAll(':scope .outer .inner');

I'm not sure about these, they are never going to get wrong result.

const inner = element.querySelectorAll('.a');
const inner = element.querySelectorAll('.a + .b');

Should we enforce them consistently, or only check descendant combinator and child combinator?

Contributor guide