# no-incorrect-query-selector πŸ“ Disallow incorrect `querySelector()` and `querySelectorAll()` usage. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). This rule catches common incorrect or inefficient `querySelector()` and `querySelectorAll()` usage. ## Examples ```js // ❌ document.querySelectorAll('form')[0]; // βœ… document.querySelector('form'); ``` ```js // ❌ document.querySelectorAll('form').at(0); // βœ… document.querySelector('form'); ``` ```js // ❌ document.querySelectorAll('#foo'); // βœ… document.querySelector('#foo'); ``` ```js // ❌ if (document.querySelectorAll('.item')) {} // βœ… if (document.querySelectorAll('.item').length > 0) {} ``` ```js // ❌ const elements = document.querySelectorAll('.item'); if (elements) {} // βœ… const elements = document.querySelectorAll('.item'); if (elements.length > 0) {} ``` ```js // ❌ document.querySelectorAll('.item') === null; // βœ… // If you meant "no matches": document.querySelectorAll('.item').length === 0; ``` ```js // ❌ document.querySelector('.item') === undefined; // βœ… // If you meant "no match": document.querySelector('.item') === null; ``` ## Limitations This rule intentionally only checks simple, common cases. It does not validate CSS selectors, simplify selectors, or enforce `:scope`. When fixing first-match access, no-match results change from `undefined` to `null`, matching `querySelector()` behavior.