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 ignore: ["custom-elements"] to selector-max-type #6588

Merged
merged 6 commits into from Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 14 additions & 1 deletion lib/rules/selector-max-type/README.md
Expand Up @@ -77,7 +77,7 @@ div a .foo:not(span) {}

## Optional secondary options

### `ignore: ["child", "compounded", "descendant", "next-sibling"]`
### `ignore: ["child", "compounded", "custom-elements", "descendant", "next-sibling"]`

#### `"child"`

Expand Down Expand Up @@ -115,6 +115,19 @@ div span a.foo {}
div span a#bar {}
```

#### `"custom-elements"`

Discount custom elements.

For example, with `2`:

The following pattern is _not_ considered a problem:

<!-- prettier-ignore -->
```css
div a foo-bar {}
```

#### `"descendant"`

Discount descendant type selectors.
Expand Down
37 changes: 37 additions & 0 deletions lib/rules/selector-max-type/__tests__/index.js
Expand Up @@ -489,6 +489,43 @@ testRule({
],
});

testRule({
ruleName,
config: [0, { ignore: ['custom-elements'] }],

accept: [
{
code: 'x-foo {}',
description: 'custom element',
},
{
code: '.foo x-foo {}',
description: 'class and custom element',
},
{
code: 'custom-element::part(foo) {}',
description: 'shadow parts',
},
],

reject: [
{
code: 'x-Foo {}',
description: 'invalid custom element',
message: messages.expected('x-Foo', 0),
line: 1,
column: 1,
},
{
code: 'x-foo div {}',
description: 'custom element and type',
message: messages.expected('x-foo div', 0),
line: 1,
column: 1,
},
],
});

testRule({
ruleName,
config: [0, { ignore: ['compounded', 'descendant'] }],
Expand Down
8 changes: 7 additions & 1 deletion lib/rules/selector-max-type/index.js
Expand Up @@ -14,6 +14,7 @@ const resolvedNestedSelector = require('postcss-resolve-nested-selector');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isRegExp, isString } = require('../../utils/validateTypes');
const isCustomElement = require('../../utils/isCustomElement');

const ruleName = 'selector-max-type';

Expand Down Expand Up @@ -41,7 +42,7 @@ const rule = (primary, secondaryOptions) => {
{
actual: secondaryOptions,
possible: {
ignore: ['descendant', 'child', 'compounded', 'next-sibling'],
ignore: ['descendant', 'child', 'compounded', 'next-sibling', 'custom-elements'],
ignoreTypes: [isString, isRegExp],
},
optional: true,
Expand All @@ -56,6 +57,7 @@ const rule = (primary, secondaryOptions) => {
const ignoreChild = optionsMatches(secondaryOptions, 'ignore', 'child');
const ignoreCompounded = optionsMatches(secondaryOptions, 'ignore', 'compounded');
const ignoreNextSibling = optionsMatches(secondaryOptions, 'ignore', 'next-sibling');
const ignoreCustomElements = optionsMatches(secondaryOptions, 'ignore', 'custom-elements');

/**
* @param {import('postcss-selector-parser').Container<unknown>} selectorNode
Expand Down Expand Up @@ -88,6 +90,10 @@ const rule = (primary, secondaryOptions) => {
return total;
}

if (ignoreCustomElements && childNode.value && isCustomElement(childNode.value)) {
return total;
}

if (childNode.type === 'tag' && !isStandardSyntaxTypeSelector(childNode)) {
return total;
}
Expand Down