-
-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add no-unmatchable-selectors rule
#301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TKDev7
wants to merge
1
commit into
eslint:main
Choose a base branch
from
TKDev7:no-unmatchable-selectors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+374
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # no-unmatchable-selectors | ||
|
|
||
| Disallow unmatchable selectors. | ||
|
|
||
| ## Background | ||
|
|
||
| An unmatchable selector is one that can never match any element in any document. These are effectively dead code and usually indicate mistakes. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule reports selectors that can never match any element. | ||
|
|
||
| It currently checks: | ||
|
|
||
| - `:nth-*()` pseudo-classes whose `An+B` formulas cannot produce a positive position (≥ 1). | ||
|
|
||
| Examples of **incorrect** code: | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| /* eslint css/no-unmatchable-selectors: "error" */ | ||
|
|
||
| a:nth-child(0) {} | ||
| a:nth-child(-n) {} | ||
| a:nth-last-child(0 of .active) {} | ||
| a:nth-of-type(0n) {} | ||
| a:nth-last-of-type(0n+0) {} | ||
| ``` | ||
|
|
||
| Examples of **correct** code: | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| /* eslint css/no-unmatchable-selectors: "error" */ | ||
|
|
||
| a:nth-child(1) {} | ||
| a:nth-child(even) {} | ||
| a:nth-child(odd) {} | ||
| a:nth-last-child(1 of .active) {} | ||
| a:nth-of-type(1n) {} | ||
| a:nth-last-of-type(1n+0) {} | ||
| ``` | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you intentionally use selectors that can never match (for example, as temporary placeholders during development), then you can safely disable this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| - [`selector-anb-no-unmatchable`](https://stylelint.io/user-guide/rules/selector-anb-no-unmatchable/) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @fileoverview Rule to disallow unmatchable selectors. | ||
| * @author TKDev7 | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Type Definitions | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * @import { CSSRuleDefinition } from "../types.js" | ||
| * @typedef {"unmatchableSelector"} NoUnmatchableSelectorsMessageIds | ||
| * @typedef {CSSRuleDefinition<{ RuleOptions: [], MessageIds: NoUnmatchableSelectorsMessageIds }>} NoUnmatchableSelectorsRuleDefinition | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** @type {NoUnmatchableSelectorsRuleDefinition} */ | ||
| export default { | ||
| meta: { | ||
| type: "problem", | ||
|
|
||
| docs: { | ||
| description: "Disallow unmatchable selectors", | ||
| recommended: true, | ||
| url: "https://github.com/eslint/css/blob/main/docs/rules/no-unmatchable-selectors.md", | ||
| }, | ||
|
|
||
| messages: { | ||
| unmatchableSelector: | ||
| "Unexpected unmatchable selector '{{selector}}'.", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const { sourceCode } = context; | ||
|
|
||
| return { | ||
| AnPlusB(node) { | ||
| const a = Number(node.a); | ||
| const b = Number(node.b); | ||
|
Comment on lines
+42
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment here describing what's happening? Either |
||
|
|
||
| if (a <= 0 && b <= 0) { | ||
| const pseudo = sourceCode.getParent( | ||
| sourceCode.getParent(node), | ||
| ); | ||
|
|
||
| context.report({ | ||
| loc: pseudo.loc, | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: sourceCode.getText(pseudo) }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,264 @@ | ||
| /** | ||
| * @fileoverview Tests for no-unmatchable-selectors rule. | ||
| * @author TKDev7 | ||
| */ | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Imports | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| import rule from "../../src/rules/no-unmatchable-selectors.js"; | ||
| import css from "../../src/index.js"; | ||
| import { RuleTester } from "eslint"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { css }, | ||
| language: "css/css", | ||
| }); | ||
|
|
||
| ruleTester.run("no-unmatchable-selectors", rule, { | ||
| valid: [ | ||
| "li:nth-child(1) {}", | ||
| "li:nth-child(n) {}", | ||
| "li:nth-child(-n+2) {}", | ||
| "li:nth-child(-2n+1) {}", | ||
| "li:nth-child(0n+1) {}", | ||
| "li:nth-child(2n) {}", | ||
| "li:nth-child(2n+0) {}", | ||
| "li:nth-child(2n-0) {}", | ||
| "li:nth-child(2n+2) {}", | ||
| "li:nth-child(1 of a) {}", | ||
| "li:nth-last-child(1) {}", | ||
| "li:nth-of-type(1) {}", | ||
| "li:nth-last-of-type(1) {}", | ||
| "li:nth-child(odd) {}", | ||
| "li:nth-child(even) {}", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "li:nth-child(0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 16, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(0n) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0n)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 17, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(+0n) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(+0n)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 18, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(-0n) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(-0n)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 18, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(0n+0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0n+0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 19, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(0n-0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0n-0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 19, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(-0n-0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(-0n-0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 20, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(0n-2) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0n-2)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 19, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(-n) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(-n)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 17, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(-2n) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(-2n)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 18, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(-3n+0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(-3n+0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 20, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(-1) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(-1)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 17, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(0 of a) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0 of a)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 21, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-last-child(0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-last-child(0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 21, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-of-type(0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-of-type(0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 18, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-last-of-type(0) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-last-of-type(0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 23, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "li:nth-child(0), li:nth-child(1) {}", | ||
| errors: [ | ||
| { | ||
| messageId: "unmatchableSelector", | ||
| data: { selector: ":nth-child(0)" }, | ||
| line: 1, | ||
| column: 3, | ||
| endLine: 1, | ||
| endColumn: 16, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's give a couple of examples here.