mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-07 04:26:32 +08:00
R=jacktfranklin@chromium.org Bug: none Change-Id: I961be18530d3ccbc4efed0c2cda9bd603b14e27b Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3372931 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Tim Van der Lippe <tvanderlippe@chromium.org>
75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
// @ts-nocheck
|
|
|
|
'use strict';
|
|
|
|
const isNonNegativeInteger = require('../../utils/isNonNegativeInteger');
|
|
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
|
|
const parseSelector = require('../../utils/parseSelector');
|
|
const report = require('../../utils/report');
|
|
const resolvedNestedSelector = require('postcss-resolve-nested-selector');
|
|
const ruleMessages = require('../../utils/ruleMessages');
|
|
const validateOptions = require('../../utils/validateOptions');
|
|
|
|
const ruleName = 'selector-max-combinators';
|
|
|
|
const messages = ruleMessages(ruleName, {
|
|
expected: (selector, max) =>
|
|
`Expected "${selector}" to have no more than ${max} ${
|
|
max === 1 ? 'combinator' : 'combinators'
|
|
}`,
|
|
});
|
|
|
|
function rule(max) {
|
|
return (root, result) => {
|
|
const validOptions = validateOptions(result, ruleName, {
|
|
actual: max,
|
|
possible: isNonNegativeInteger,
|
|
});
|
|
|
|
if (!validOptions) {
|
|
return;
|
|
}
|
|
|
|
function checkSelector(selectorNode, ruleNode) {
|
|
const count = selectorNode.reduce((total, childNode) => {
|
|
// Only traverse inside actual selectors
|
|
if (childNode.type === 'selector') {
|
|
checkSelector(childNode, ruleNode);
|
|
}
|
|
|
|
if (childNode.type === 'combinator') total += 1;
|
|
|
|
return total;
|
|
}, 0);
|
|
|
|
if (selectorNode.type !== 'root' && selectorNode.type !== 'pseudo' && count > max) {
|
|
report({
|
|
ruleName,
|
|
result,
|
|
node: ruleNode,
|
|
message: messages.expected(selectorNode, max),
|
|
word: selectorNode,
|
|
});
|
|
}
|
|
}
|
|
|
|
root.walkRules((ruleNode) => {
|
|
if (!isStandardSyntaxRule(ruleNode)) {
|
|
return;
|
|
}
|
|
|
|
for (const selector of ruleNode.selectors) {
|
|
for (const resolvedSelector of resolvedNestedSelector(selector, ruleNode)) {
|
|
parseSelector(resolvedSelector, result, ruleNode, (container) =>
|
|
checkSelector(container, ruleNode),
|
|
);
|
|
}
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
rule.ruleName = ruleName;
|
|
rule.messages = messages;
|
|
module.exports = rule;
|