Initial commit

This commit is contained in:
Patrick Marsceill
2017-03-09 13:16:08 -05:00
commit b7b0d0d7bf
4147 changed files with 401224 additions and 0 deletions
@@ -0,0 +1,52 @@
# selector-max-compound-selectors
Limit the number of compound selectors in a selector.
```css
div .bar[data-val] > a.baz + .boom > #lorem {}
/* ↑ ↑ ↑ ↑ ↑
| | | | |
Lv1 Lv2 Lv3 Lv4 Lv5 -- these are compound selectors */
```
A [compound selector](https://www.w3.org/TR/selectors4/#compound) is a chain of one or more simple (tag, class, id, universal, attribute) selectors. If there is more than one compound selector in a complete selector, they will be separated by combinators (e.g. ` `, `+`, `>`). One reason why you might want to limit the number of compound selectors is described in the [SMACSS book](https://smacss.com/book/applicability).
This rule resolves nested selectors before calculating the depth of a selector.
`:not()` is considered one compound selector irrespective to the complexity of the selector inside it. The rule *does* process that inner selector, but does so separately, independent of the main selector.
## Options
`int`: Maximum compound selectors allowed.
For example, with `3`:
The following patterns are considered warnings:
```css
.foo .bar .baz .lorem {}
```
```css
.foo .baz {
& > .bar .lorem {}
}
```
The following patterns are *not* considered warnings:
```css
div {}
```
```css
.foo div {}
```
```css
#foo #bar > #baz {}
```
```css
.foo + div :not (a b ~ c) {} /* `a b ~ c` is inside :not() and is processed separately */
```
@@ -0,0 +1,85 @@
"use strict"
const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule")
const isStandardSyntaxSelector = require("../../utils/isStandardSyntaxSelector")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const resolvedNestedSelector = require("postcss-resolve-nested-selector")
const selectorParser = require("postcss-selector-parser")
const ruleName = "selector-max-compound-selectors"
const messages = ruleMessages(ruleName, {
expected: (selector, max) => `Expected "${selector}" to have no more than ${max} compound selectors`,
})
const rule = function (max) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: max,
possible: [
function (max) {
return typeof max === "number" && max > 0
},
],
})
if (!validOptions) {
return
}
// Finds actual selectors in selectorNode object and checks them
function checkSelector(selectorNode, rule) {
let compoundCount = 1
selectorNode.each(childNode => {
// Only traverse inside actual selectors and :not()
if (childNode.type === "selector" || childNode.value === ":not") {
checkSelector(childNode, rule)
}
// Compound selectors are separated by combinators, so increase count when meeting one
if (childNode.type === "combinator") {
compoundCount++
}
})
if (selectorNode.type !== "root" && selectorNode.type !== "pseudo" && compoundCount > max) {
report({
ruleName,
result,
node: rule,
message: messages.expected(selectorNode, max),
word: selectorNode,
})
}
}
root.walkRules(rule => {
if (!isStandardSyntaxRule(rule)) {
return
}
if (!isStandardSyntaxSelector(rule.selector)) {
return
}
// Nested selectors are processed in steps, as nesting levels are resolved.
// Here we skip processing the intermediate parts of selectors (to process only fully resolved selectors)
if (rule.nodes.some(node => node.type === "rule" || node.type === "atrule")) {
return
}
// Using `rule.selectors` gets us each selector if there is a comma separated set
rule.selectors.forEach(selector => {
resolvedNestedSelector(selector, rule).forEach(resolvedSelector => {
// Process each resolved selector with `checkSelector` via postcss-selector-parser
selectorParser(s => checkSelector(s, rule)).process(resolvedSelector)
})
})
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule