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

Refactor utilities to unify parseSelector() and parseSelectorAST() #7647

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/rules/no-descending-specificity/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ const rule = (primary, secondaryOptions) => {
continue;
}

parseSelector(resolvedSelector, result, ruleNode, (s) => {
checkSelector(resolvedSelector, s, ruleNode, comparisonContext);
});
const selectorRoot = parseSelector(resolvedSelector, result, ruleNode);

if (selectorRoot) {
checkSelector(resolvedSelector, selectorRoot, ruleNode, comparisonContext);
}
}
}
});
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/no-descending-specificity/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ const rule = (primary, secondaryOptions) => {
continue;
}

parseSelector(resolvedSelector, result, ruleNode, (s) => {
checkSelector(resolvedSelector, s, ruleNode, comparisonContext);
});
const selectorRoot = parseSelector(resolvedSelector, result, ruleNode);

if (selectorRoot) {
checkSelector(resolvedSelector, selectorRoot, ruleNode, comparisonContext);
}
}
}
});
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/no-duplicate-selectors/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ const rule = (primary, secondaryOptions) => {
const selectorListParsed = [];

if (shouldDisallowDuplicateInList) {
parseSelector(sortedSelectorList, result, ruleNode, (selectors) => {
selectors.each((s) => {
parseSelector(sortedSelectorList, result, ruleNode)?.each((s) => {
const selector = String(s);

selectorListParsed.push(selector);
Expand All @@ -99,7 +98,6 @@ const rule = (primary, secondaryOptions) => {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
});
} else {
previousDuplicatePosition = contextSelectorSet.get(sortedSelectorList);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/no-duplicate-selectors/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ const rule = (primary, secondaryOptions) => {
const selectorListParsed = [];

if (shouldDisallowDuplicateInList) {
parseSelector(sortedSelectorList, result, ruleNode, (selectors) => {
selectors.each((s) => {
parseSelector(sortedSelectorList, result, ruleNode)?.each((s) => {
const selector = String(s);

selectorListParsed.push(selector);
Expand All @@ -96,7 +95,6 @@ const rule = (primary, secondaryOptions) => {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
});
} else {
previousDuplicatePosition = contextSelectorSet.get(sortedSelectorList);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-attribute-name-disallowed-list/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const rule = (primary) => {
return;
}

parseSelector(ruleNode.selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
parseSelector(ruleNode.selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const attributeName = attributeNode.qualifiedAttribute;

if (!matchesStringOrRegExp(attributeName, primary)) {
Expand All @@ -63,7 +62,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-attribute-name-disallowed-list/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const rule = (primary) => {
return;
}

parseSelector(ruleNode.selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
parseSelector(ruleNode.selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const attributeName = attributeNode.qualifiedAttribute;

if (!matchesStringOrRegExp(attributeName, primary)) {
Expand All @@ -59,7 +58,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-attribute-operator-allowed-list/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ const rule = (primary) => {
return;
}

parseSelector(selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
parseSelector(selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const { operator } = attributeNode;

if (!operator || primaryValues.has(operator)) {
Expand All @@ -66,7 +65,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-attribute-operator-allowed-list/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ const rule = (primary) => {
return;
}

parseSelector(selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
parseSelector(selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const { operator } = attributeNode;

if (!operator || primaryValues.has(operator)) {
Expand All @@ -62,7 +61,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ const rule = (primary) => {
return;
}

parseSelector(selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
parseSelector(selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const { operator } = attributeNode;

if (!operator || !primaryValues.has(operator)) {
Expand All @@ -66,7 +65,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ const rule = (primary) => {
return;
}

parseSelector(selector, result, ruleNode, (selectorTree) => {
selectorTree.walkAttributes((attributeNode) => {
parseSelector(selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const { operator } = attributeNode;

if (!operator || !primaryValues.has(operator)) {
Expand All @@ -62,7 +61,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
6 changes: 4 additions & 2 deletions lib/rules/selector-attribute-quotes/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const rule = (primary, _secondaryOptions, context) => {
return;
}

parseSelector(getRuleSelector(ruleNode), result, ruleNode, (selectorTree) => {
const selectorTree = parseSelector(getRuleSelector(ruleNode), result, ruleNode);

if (!selectorTree) return;

let selectorFixed = false;

selectorTree.walkAttributes((attributeNode) => {
Expand Down Expand Up @@ -90,7 +93,6 @@ const rule = (primary, _secondaryOptions, context) => {
if (selectorFixed) {
ruleNode.selector = selectorTree.toString();
}
});

/**
* @param {string} message
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/selector-attribute-quotes/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const rule = (primary, _secondaryOptions, context) => {
return;
}

parseSelector(getRuleSelector(ruleNode), result, ruleNode, (selectorTree) => {
const selectorTree = parseSelector(getRuleSelector(ruleNode), result, ruleNode);

if (!selectorTree) return;

let selectorFixed = false;

selectorTree.walkAttributes((attributeNode) => {
Expand Down Expand Up @@ -86,7 +89,6 @@ const rule = (primary, _secondaryOptions, context) => {
if (selectorFixed) {
ruleNode.selector = selectorTree.toString();
}
});

/**
* @param {string} message
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/selector-class-pattern/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ const rule = (primary, secondaryOptions) => {
continue;
}

parseSelector(nestedSelector, result, ruleNode, (s) => checkSelector(s, ruleNode));
const selectorRoot = parseSelector(nestedSelector, result, ruleNode);

if (selectorRoot) checkSelector(selectorRoot, ruleNode);
}
} else {
parseSelector(selector, result, ruleNode, (s) => checkSelector(s, ruleNode));
const selectorRoot = parseSelector(selector, result, ruleNode);

if (selectorRoot) checkSelector(selectorRoot, ruleNode);
}
});

Expand Down
8 changes: 6 additions & 2 deletions lib/rules/selector-class-pattern/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ const rule = (primary, secondaryOptions) => {
continue;
}

parseSelector(nestedSelector, result, ruleNode, (s) => checkSelector(s, ruleNode));
const selectorRoot = parseSelector(nestedSelector, result, ruleNode);

if (selectorRoot) checkSelector(selectorRoot, ruleNode);
}
} else {
parseSelector(selector, result, ruleNode, (s) => checkSelector(s, ruleNode));
const selectorRoot = parseSelector(selector, result, ruleNode);

if (selectorRoot) checkSelector(selectorRoot, ruleNode);
}
});

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-combinator-allowed-list/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ const rule = (primary) => {

const selector = ruleNode.selector;

parseSelector(selector, result, ruleNode, (fullSelector) => {
fullSelector.walkCombinators((combinatorNode) => {
parseSelector(selector, result, ruleNode)?.walkCombinators((combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}
Expand All @@ -66,7 +65,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-combinator-allowed-list/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const rule = (primary) => {

const selector = ruleNode.selector;

parseSelector(selector, result, ruleNode, (fullSelector) => {
fullSelector.walkCombinators((combinatorNode) => {
parseSelector(selector, result, ruleNode)?.walkCombinators((combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}
Expand All @@ -62,7 +61,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-combinator-disallowed-list/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ const rule = (primary) => {

const selector = ruleNode.selector;

parseSelector(selector, result, ruleNode, (fullSelector) => {
fullSelector.walkCombinators((combinatorNode) => {
parseSelector(selector, result, ruleNode)?.walkCombinators((combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}
Expand All @@ -66,7 +65,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-combinator-disallowed-list/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const rule = (primary) => {

const selector = ruleNode.selector;

parseSelector(selector, result, ruleNode, (fullSelector) => {
fullSelector.walkCombinators((combinatorNode) => {
parseSelector(selector, result, ruleNode)?.walkCombinators((combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}
Expand All @@ -62,7 +61,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-id-pattern/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const rule = (primary) => {
return;
}

parseSelector(ruleNode.selector, result, ruleNode, (fullSelector) => {
fullSelector.walkIds((selectorNode) => {
parseSelector(ruleNode.selector, result, ruleNode)?.walkIds((selectorNode) => {
if (normalizedPattern.test(selectorNode.value)) {
return;
}
Expand All @@ -56,7 +55,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
4 changes: 1 addition & 3 deletions lib/rules/selector-id-pattern/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const rule = (primary) => {
return;
}

parseSelector(ruleNode.selector, result, ruleNode, (fullSelector) => {
fullSelector.walkIds((selectorNode) => {
parseSelector(ruleNode.selector, result, ruleNode)?.walkIds((selectorNode) => {
if (normalizedPattern.test(selectorNode.value)) {
return;
}
Expand All @@ -52,7 +51,6 @@ const rule = (primary) => {
});
});
});
});
};
};

Expand Down
6 changes: 4 additions & 2 deletions lib/rules/selector-max-specificity/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ const rule = (primary, secondaryOptions) => {
continue;
}

parseSelector(resolvedSelector, result, ruleNode, (selectorTree) => {
const selectorTree = parseSelector(resolvedSelector, result, ruleNode);

if (!selectorTree) continue;

// Check if the selector specificity exceeds the allowed maximum
if (selectorSpecificity.compare(maxChildSpecificity(selectorTree), maxSpecificity) > 0) {
report({
Expand All @@ -257,7 +260,6 @@ const rule = (primary, secondaryOptions) => {
word: selector,
});
}
});
}
}
});
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/selector-max-specificity/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ const rule = (primary, secondaryOptions) => {
continue;
}

parseSelector(resolvedSelector, result, ruleNode, (selectorTree) => {
const selectorTree = parseSelector(resolvedSelector, result, ruleNode);
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved

if (!selectorTree) continue;

// Check if the selector specificity exceeds the allowed maximum
if (compare(maxChildSpecificity(selectorTree), maxSpecificity) > 0) {
report({
Expand All @@ -258,7 +261,6 @@ const rule = (primary, secondaryOptions) => {
word: selector,
});
}
});
}
}
});
Expand Down
Loading