From cd5bc15c23046b255b28dbd24854c182b3e8ea7e Mon Sep 17 00:00:00 2001 From: misha Date: Tue, 24 Jan 2023 18:25:33 +0300 Subject: [PATCH 1/5] Add ignore: [custom-elements] to selector-max-type --- lib/rules/selector-max-type/README.md | 20 +++++++++- .../selector-max-type/__tests__/index.js | 38 +++++++++++++++++++ lib/rules/selector-max-type/index.js | 8 +++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/rules/selector-max-type/README.md b/lib/rules/selector-max-type/README.md index ab890d00a8..7215f5fa18 100644 --- a/lib/rules/selector-max-type/README.md +++ b/lib/rules/selector-max-type/README.md @@ -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"]` #### `"child"` @@ -151,6 +151,24 @@ div a + span {} #bar + div + span + a + span {} ``` +#### `"custom-elements"` + +Allow custom elements. + +The following patterns are considered problems: + + +```css +x-Foo {} +``` + +The following patterns are _not_ considered problems: + + +```css +x-foo {} +``` + ### `ignoreTypes: ["/regex/", /regex/, "non-regex"]` Given: diff --git a/lib/rules/selector-max-type/__tests__/index.js b/lib/rules/selector-max-type/__tests__/index.js index 7177a7bbc2..a8b4094859 100644 --- a/lib/rules/selector-max-type/__tests__/index.js +++ b/lib/rules/selector-max-type/__tests__/index.js @@ -489,6 +489,44 @@ testRule({ ], }); +testRule({ + //TODO + 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'] }], diff --git a/lib/rules/selector-max-type/index.js b/lib/rules/selector-max-type/index.js index b48ff5fe22..e9c331c562 100644 --- a/lib/rules/selector-max-type/index.js +++ b/lib/rules/selector-max-type/index.js @@ -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'; @@ -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, @@ -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} selectorNode @@ -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; } From bb31f0e4292e4ceca00721544c90d938b2c148f3 Mon Sep 17 00:00:00 2001 From: muddv Date: Tue, 24 Jan 2023 21:08:03 +0300 Subject: [PATCH 2/5] Remove unnecessary line Co-authored-by: Richard Hallows --- lib/rules/selector-max-type/__tests__/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/rules/selector-max-type/__tests__/index.js b/lib/rules/selector-max-type/__tests__/index.js index a8b4094859..6d13b85ca4 100644 --- a/lib/rules/selector-max-type/__tests__/index.js +++ b/lib/rules/selector-max-type/__tests__/index.js @@ -490,7 +490,6 @@ testRule({ }); testRule({ - //TODO ruleName, config: [0, { ignore: ['custom-elements'] }], From 3f5bccfef535fe0ed6343dd90543e951265df6bc Mon Sep 17 00:00:00 2001 From: misha Date: Tue, 24 Jan 2023 21:29:25 +0300 Subject: [PATCH 3/5] Readme fixes for ignore custom-elements --- lib/rules/selector-max-type/README.md | 33 ++++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/rules/selector-max-type/README.md b/lib/rules/selector-max-type/README.md index 7215f5fa18..76b0f67139 100644 --- a/lib/rules/selector-max-type/README.md +++ b/lib/rules/selector-max-type/README.md @@ -77,7 +77,7 @@ div a .foo:not(span) {} ## Optional secondary options -### `ignore: ["child", "compounded", "descendant", "next-sibling", "custom-elements"]` +### `ignore: ["child", "compounded", "custom-elements", "descendant", "next-sibling"]` #### `"child"` @@ -115,6 +115,19 @@ div span a.foo {} div span a#bar {} ``` +#### `"custom-elements"` + +Discount custom elements. + +For example, with `2`: + +The following patterns is _not_ considered a problem: + + +```css +div a foo-bar {} +``` + #### `"descendant"` Discount descendant type selectors. @@ -151,24 +164,6 @@ div a + span {} #bar + div + span + a + span {} ``` -#### `"custom-elements"` - -Allow custom elements. - -The following patterns are considered problems: - - -```css -x-Foo {} -``` - -The following patterns are _not_ considered problems: - - -```css -x-foo {} -``` - ### `ignoreTypes: ["/regex/", /regex/, "non-regex"]` Given: From 414c5a749a570825d548ff765b219d56ea8ace90 Mon Sep 17 00:00:00 2001 From: misha Date: Wed, 25 Jan 2023 15:55:47 +0300 Subject: [PATCH 4/5] Fix typo --- lib/rules/selector-max-type/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/selector-max-type/README.md b/lib/rules/selector-max-type/README.md index 76b0f67139..610b554e4a 100644 --- a/lib/rules/selector-max-type/README.md +++ b/lib/rules/selector-max-type/README.md @@ -121,7 +121,7 @@ Discount custom elements. For example, with `2`: -The following patterns is _not_ considered a problem: +The following pattern is _not_ considered a problem: ```css From 164f42ed1b36e17531403bf79defe14ec5ec24d4 Mon Sep 17 00:00:00 2001 From: Richard Hallows Date: Wed, 25 Jan 2023 14:22:51 +0000 Subject: [PATCH 5/5] Create smart-lobsters-roll.md --- .changeset/smart-lobsters-roll.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smart-lobsters-roll.md diff --git a/.changeset/smart-lobsters-roll.md b/.changeset/smart-lobsters-roll.md new file mode 100644 index 0000000000..bd6f2812d0 --- /dev/null +++ b/.changeset/smart-lobsters-roll.md @@ -0,0 +1,5 @@ +--- +"stylelint": patch +--- + +Added: `ignore: ["custom-elements"]` to `selector-max-type`