Skip to content
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

Add ignoreContextFunctionalPseudoClasses to selector-max-id #4835

Merged
merged 7 commits into from Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/rules/selector-max-id/README.md
Expand Up @@ -74,3 +74,32 @@ The following patterns are _not_ considered violations:
/* `#bar` is inside `:not()`, so it is evaluated separately */
#foo #bar:not(#baz) {}
```

### `ignoreContextFunctionalPseudoClasses: ["/regex/", /regex/, "non-regex"]`

Ignore selectors inside of specified [functional pseudo-classes](https://drafts.csswg.org/selectors-4/#pseudo-classes) that provide [evaluation contexts](https://drafts.csswg.org/selectors-4/#specificity-rules).

Given:

```js
[":not", /^:(h|H)as$/];
```

The following patterns are considered violations:

<!-- prettier-ignore -->
```css
a:matches(#foo) {}
```

While the following patters are _not_ considered violations:

<!-- prettier-ignore -->
```css
a:not(#foo) {}
```

<!-- prettier-ignore -->
```css
a:has(#foo) {}
```
25 changes: 25 additions & 0 deletions lib/rules/selector-max-id/__tests__/index.js
Expand Up @@ -459,3 +459,28 @@ testRule({
},
],
});

testRule({
ruleName,
config: [0, { ignoreContextFunctionalPseudoClasses: [':not', /^:(h|H)as$/] }],

accept: [
{
code: 'a:not(#foo) {}',
description: 'selector within ignored pseudo-class (string input)',
},
{
code: 'a:has(#foo) {}',
description: 'selector within ignored pseudo-class (regex input)',
},
],
reject: [
{
code: 'a:matches(#foo) {}',
description: 'selector within non-ignored pseudo class',
message: messages.expected('#foo', 0),
line: 1,
column: 11,
},
],
});
44 changes: 34 additions & 10 deletions lib/rules/selector-max-id/index.js
Expand Up @@ -2,8 +2,10 @@

'use strict';

const _ = require('lodash');
const isLogicalCombination = require('../../utils/isLogicalCombination');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const optionsMatches = require('../../utils/optionsMatches');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const resolvedNestedSelector = require('postcss-resolve-nested-selector');
Expand All @@ -17,25 +19,40 @@ const messages = ruleMessages(ruleName, {
`Expected "${selector}" to have no more than ${max} ID ${max === 1 ? 'selector' : 'selectors'}`,
});

function rule(max) {
function rule(max, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: max,
possible: [
function (max) {
return typeof max === 'number' && max >= 0;
const validOptions = validateOptions(
result,
ruleName,
{
actual: max,
possible: [
function (max) {
return typeof max === 'number' && max >= 0;
},
],
},
{
actual: options,
possible: {
ignoreContextFunctionalPseudoClasses: [_.isString, _.isRegExp],
},
],
});
optional: true,
},
);

if (!validOptions) {
return;
}

function checkSelector(selectorNode, ruleNode) {
const count = selectorNode.reduce((total, childNode) => {
// Only traverse inside actual selectors and logical combinations
if (childNode.type === 'selector' || isLogicalCombination(childNode)) {
// Only traverse inside actual selectors and logical combinations that are not part of ignored functional pseudo-classes
if (
childNode.type === 'selector' ||
(isLogicalCombination(childNode) &&
!isIgnoredContextFunctionalPseudoClass(childNode, options))
) {
checkSelector(childNode, ruleNode);
}

Expand All @@ -53,6 +70,13 @@ function rule(max) {
}
}

function isIgnoredContextFunctionalPseudoClass(node, options) {
return (
node.type === 'pseudo' &&
optionsMatches(options, 'ignoreContextFunctionalPseudoClasses', node.value)
);
}

root.walkRules((ruleNode) => {
if (!isStandardSyntaxRule(ruleNode)) {
return;
Expand Down