Skip to content

Commit

Permalink
Refactor utilities to unify parseSelector() and parseSelectorAST() (
Browse files Browse the repository at this point in the history
#7647)

This unifies similar utilities using `postcss-selector-parser` into one.
The new utility returns a root object for selectors.
  • Loading branch information
ybiquitous committed Apr 25, 2024
1 parent eddd1e6 commit 8c23fce
Show file tree
Hide file tree
Showing 55 changed files with 1,181 additions and 1,234 deletions.
8 changes: 5 additions & 3 deletions lib/rules/no-descending-specificity/index.cjs
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
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
14 changes: 6 additions & 8 deletions lib/rules/no-duplicate-selectors/index.cjs
Expand Up @@ -89,16 +89,14 @@ const rule = (primary, secondaryOptions) => {
const selectorListParsed = [];

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

selectorListParsed.push(selector);
selectorListParsed.push(selector);

if (contextSelectorSet.get(selector)) {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
if (contextSelectorSet.get(selector)) {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
} else {
previousDuplicatePosition = contextSelectorSet.get(sortedSelectorList);
Expand Down
14 changes: 6 additions & 8 deletions lib/rules/no-duplicate-selectors/index.mjs
Expand Up @@ -86,16 +86,14 @@ const rule = (primary, secondaryOptions) => {
const selectorListParsed = [];

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

selectorListParsed.push(selector);
selectorListParsed.push(selector);

if (contextSelectorSet.get(selector)) {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
if (contextSelectorSet.get(selector)) {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
} else {
previousDuplicatePosition = contextSelectorSet.get(sortedSelectorList);
Expand Down
32 changes: 15 additions & 17 deletions lib/rules/selector-attribute-name-disallowed-list/index.cjs
Expand Up @@ -41,26 +41,24 @@ const rule = (primary) => {
return;
}

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

if (!matchesStringOrRegExp(attributeName, primary)) {
return;
}
if (!matchesStringOrRegExp(attributeName, primary)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('attribute');
const endIndex = index + attributeName.length;
const index = attributeNode.sourceIndex + attributeNode.offsetOf('attribute');
const endIndex = index + attributeName.length;

report({
message: messages.rejected,
messageArgs: [attributeName],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
report({
message: messages.rejected,
messageArgs: [attributeName],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
});
});
Expand Down
32 changes: 15 additions & 17 deletions lib/rules/selector-attribute-name-disallowed-list/index.mjs
Expand Up @@ -37,26 +37,24 @@ const rule = (primary) => {
return;
}

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

if (!matchesStringOrRegExp(attributeName, primary)) {
return;
}
if (!matchesStringOrRegExp(attributeName, primary)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('attribute');
const endIndex = index + attributeName.length;
const index = attributeNode.sourceIndex + attributeNode.offsetOf('attribute');
const endIndex = index + attributeName.length;

report({
message: messages.rejected,
messageArgs: [attributeName],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
report({
message: messages.rejected,
messageArgs: [attributeName],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
});
});
Expand Down
38 changes: 18 additions & 20 deletions lib/rules/selector-attribute-operator-allowed-list/index.cjs
Expand Up @@ -44,26 +44,24 @@ const rule = (primary) => {
return;
}

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

if (!operator || primaryValues.has(operator)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;

report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
parseSelector(selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const { operator } = attributeNode;

if (!operator || primaryValues.has(operator)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;

report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
});
});
Expand Down
32 changes: 15 additions & 17 deletions lib/rules/selector-attribute-operator-allowed-list/index.mjs
Expand Up @@ -40,26 +40,24 @@ const rule = (primary) => {
return;
}

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

if (!operator || primaryValues.has(operator)) {
return;
}
if (!operator || primaryValues.has(operator)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;
const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;

report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
});
});
Expand Down
38 changes: 18 additions & 20 deletions lib/rules/selector-attribute-operator-disallowed-list/index.cjs
Expand Up @@ -44,26 +44,24 @@ const rule = (primary) => {
return;
}

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

if (!operator || !primaryValues.has(operator)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;

report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
parseSelector(selector, result, ruleNode)?.walkAttributes((attributeNode) => {
const { operator } = attributeNode;

if (!operator || !primaryValues.has(operator)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;

report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
});
});
Expand Down
32 changes: 15 additions & 17 deletions lib/rules/selector-attribute-operator-disallowed-list/index.mjs
Expand Up @@ -40,26 +40,24 @@ const rule = (primary) => {
return;
}

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

if (!operator || !primaryValues.has(operator)) {
return;
}
if (!operator || !primaryValues.has(operator)) {
return;
}

const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;
const index = attributeNode.sourceIndex + attributeNode.offsetOf('operator');
const endIndex = index + operator.length;

report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
report({
message: messages.rejected,
messageArgs: [operator],
node: ruleNode,
index,
endIndex,
result,
ruleName,
});
});
});
Expand Down

0 comments on commit 8c23fce

Please sign in to comment.