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 1 commit
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
20 changes: 19 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", "descendant", "next-sibling", "custom-elements"]`
muddv marked this conversation as resolved.
Show resolved Hide resolved

#### `"child"`

Expand Down Expand Up @@ -151,6 +151,24 @@ div a + span {}
#bar + div + span + a + span {}
```

#### `"custom-elements"`

Allow custom elements.
muddv marked this conversation as resolved.
Show resolved Hide resolved

The following patterns are considered problems:

<!-- prettier-ignore -->
```css
x-Foo {}
```

muddv marked this conversation as resolved.
Show resolved Hide resolved
The following patterns are _not_ considered problems:
muddv marked this conversation as resolved.
Show resolved Hide resolved

<!-- prettier-ignore -->
```css
x-foo {}
muddv marked this conversation as resolved.
Show resolved Hide resolved
```

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

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

testRule({
//TODO
muddv marked this conversation as resolved.
Show resolved Hide resolved
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