From ef7630f2f0eac23395db7268bd6b3b75e8e0987c Mon Sep 17 00:00:00 2001 From: fpetrakov Date: Wed, 17 Aug 2022 17:31:54 +0300 Subject: [PATCH] made use of prev method on childNode + added check descendant combinator type --- lib/rules/selector-max-universal/__tests__/index.js | 8 +------- lib/rules/selector-max-universal/index.js | 8 ++++---- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/rules/selector-max-universal/__tests__/index.js b/lib/rules/selector-max-universal/__tests__/index.js index 78e588a0b6..99439e99f1 100644 --- a/lib/rules/selector-max-universal/__tests__/index.js +++ b/lib/rules/selector-max-universal/__tests__/index.js @@ -321,18 +321,12 @@ testRule({ testRule({ ruleName, - config: [0, { ignoreAfterCombinators: ['~', '+', '>', 'h1', 'article'] }], + config: [0, { ignoreAfterCombinators: ['~', '+', '>'] }], accept: [ { code: '.foo {}', }, - { - code: 'article * {}', - }, - { - code: 'h1 * {}', - }, { code: '.foo ~ * {}', }, diff --git a/lib/rules/selector-max-universal/index.js b/lib/rules/selector-max-universal/index.js index 2987286a1b..2a8bda7b83 100644 --- a/lib/rules/selector-max-universal/index.js +++ b/lib/rules/selector-max-universal/index.js @@ -52,19 +52,19 @@ const rule = (primary, secondaryOptions) => { * @param {import('postcss').Rule} ruleNode */ function checkSelector(selectorNode, ruleNode) { - const count = selectorNode.reduce((total, childNode, index, nodes) => { + const count = selectorNode.reduce((total, childNode) => { // Only traverse inside actual selectors // All logical combinations will be resolved as nested selector in `postcss-resolve-nested-selector` if (childNode.type === 'selector') { checkSelector(childNode, ruleNode); } - let prevChildNode = nodes[index - 1]; + let prevChildNode = childNode.prev(); let prevChildNodeValue = prevChildNode && prevChildNode.value; // checks if previous child node is descendant combinator - if (prevChildNodeValue === ' ') { - prevChildNode = nodes[index - 2]; + if (prevChildNode && prevChildNode.type === 'combinator' && prevChildNodeValue === ' ') { + prevChildNode = prevChildNode.prev(); prevChildNodeValue = prevChildNode && prevChildNode.value; }