Skip to content

Commit

Permalink
made use of prev method on childNode + added check descendant combina…
Browse files Browse the repository at this point in the history
…tor type
  • Loading branch information
fpetrakov committed Aug 17, 2022
1 parent 7244516 commit ef7630f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
8 changes: 1 addition & 7 deletions lib/rules/selector-max-universal/__tests__/index.js
Expand Up @@ -321,18 +321,12 @@ testRule({

testRule({
ruleName,
config: [0, { ignoreAfterCombinators: ['~', '+', '>', 'h1', 'article'] }],
config: [0, { ignoreAfterCombinators: ['~', '+', '>'] }],

accept: [
{
code: '.foo {}',
},
{
code: 'article * {}',
},
{
code: 'h1 * {}',
},
{
code: '.foo ~ * {}',
},
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/selector-max-universal/index.js
Expand Up @@ -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;
}
Expand Down

0 comments on commit ef7630f

Please sign in to comment.