Skip to content

Commit d66b9fb

Browse files
authored
feat(stylelint-config): add rules for hover and scrollbar media, bare lengths and root margins (#221)
1 parent fee88c9 commit d66b9fb

13 files changed

Lines changed: 567 additions & 1 deletion

File tree

‎packages/stylelint-config/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"exports": {
2525
".": "./src/index.js",
26+
"./plugin": "./src/plugin/index.js",
2627
"./*": "./src/*.js",
2728
"./package.json": "./package.json"
2829
},
@@ -40,6 +41,7 @@
4041
},
4142
"dependencies": {
4243
"@stylistic/stylelint-plugin": "^6.0.1",
44+
"postcss-value-parser": "^4.2.0",
4345
"stylelint-declaration-strict-value": "^1.13.0",
4446
"stylelint-gamut": "^2.0.2",
4547
"stylelint-high-performance-animation": "^2.1.0",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import stylelint from 'stylelint'
2+
import {
3+
isInMedia,
4+
isString,
5+
toList
6+
} from './media.js'
7+
8+
const {
9+
createPlugin,
10+
utils: {
11+
report,
12+
ruleMessages,
13+
validateOptions
14+
}
15+
} = stylelint
16+
const ruleName = 'trigen/hover-in-media'
17+
const messages = ruleMessages(ruleName, {
18+
rejected: 'Put `:hover` styles in a hover media query, so a tap does not leave a control looking hovered.'
19+
})
20+
const HOVER_PATTERN = /:hover(?![\w-])/i
21+
const HOVER_FEATURE_PATTERN = /\(\s*(any-)?hover\s*:\s*hover\s*\)/i
22+
23+
function ruleFunction(primary, secondaryOptions) {
24+
return (root, result) => {
25+
const valid = validateOptions(
26+
result,
27+
ruleName,
28+
{
29+
actual: primary
30+
},
31+
{
32+
actual: secondaryOptions,
33+
possible: {
34+
customMedia: [isString]
35+
},
36+
optional: true
37+
}
38+
)
39+
40+
if (!valid) {
41+
return
42+
}
43+
44+
const customMedia = toList(secondaryOptions?.customMedia)
45+
46+
root.walkRules((rule) => {
47+
if (HOVER_PATTERN.test(rule.selector) && !isInMedia(rule, HOVER_FEATURE_PATTERN, customMedia)) {
48+
report({
49+
ruleName,
50+
result,
51+
node: rule,
52+
message: messages.rejected
53+
})
54+
}
55+
})
56+
}
57+
}
58+
59+
ruleFunction.ruleName = ruleName
60+
ruleFunction.messages = messages
61+
62+
export default createPlugin(ruleName, ruleFunction)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hoverInMediaRule from './hover-in-media.js'
2+
import noBareLengthRule from './no-bare-length.js'
3+
import noRootMarginRule from './no-root-margin.js'
4+
import thinScrollbarInMediaRule from './thin-scrollbar-in-media.js'
5+
import uikitModuleRootRule from './uikit-module-root.js'
6+
7+
export default [
8+
hoverInMediaRule,
9+
noBareLengthRule,
10+
noRootMarginRule,
11+
thinScrollbarInMediaRule,
12+
uikitModuleRootRule
13+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Check if a node stands in a media query that matches a feature or names one of the custom media.
3+
* @param {object} node - A rule or a declaration.
4+
* @param {RegExp} featurePattern - The standard feature, such as `(hover: hover)`.
5+
* @param {string[]} customMedia - The names of the custom media that mean the same.
6+
* @returns {boolean} Whether a media query around it matches.
7+
*/
8+
export function isInMedia(node, featurePattern, customMedia) {
9+
for (let parent = node.parent; parent; parent = parent.parent) {
10+
if (
11+
parent.type === 'atrule'
12+
&& parent.name.toLowerCase() === 'media'
13+
&& (
14+
featurePattern.test(parent.params)
15+
|| customMedia.some(name => parent.params.includes(`(${name})`))
16+
)
17+
) {
18+
return true
19+
}
20+
}
21+
22+
return false
23+
}
24+
25+
export function toList(value) {
26+
if (value === undefined) {
27+
return []
28+
}
29+
30+
return Array.isArray(value) ? value : [value]
31+
}
32+
33+
export function isString(value) {
34+
return typeof value === 'string'
35+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import stylelint from 'stylelint'
2+
import valueParser from 'postcss-value-parser'
3+
4+
const {
5+
createPlugin,
6+
utils: {
7+
report,
8+
ruleMessages,
9+
validateOptions
10+
}
11+
} = stylelint
12+
const ruleName = 'trigen/no-bare-length'
13+
const messages = ruleMessages(ruleName, {
14+
rejected: value => `Expected a token or a calc of tokens for "${value}": a value no scale owns is a custom property of the component.`
15+
})
16+
// Absolute and font-relative lengths and times; proportions (`%`, `fr`, `lh`) and viewport and
17+
// container units are not a scale's
18+
const UNITS = new Set([
19+
'px',
20+
'rem',
21+
'em',
22+
'ex',
23+
'ch',
24+
'cap',
25+
'ic',
26+
'rex',
27+
'rch',
28+
'rcap',
29+
'ric',
30+
'pt',
31+
'pc',
32+
'in',
33+
'cm',
34+
'mm',
35+
'q',
36+
'ms',
37+
's'
38+
])
39+
const SKIPPED_FUNCTIONS = new Set(['var', 'url', 'env'])
40+
41+
function ruleFunction(primary) {
42+
return (root, result) => {
43+
if (!validateOptions(result, ruleName, {
44+
actual: primary
45+
})) {
46+
return
47+
}
48+
49+
root.walkDecls((decl) => {
50+
// A custom property is where a value no scale owns is named
51+
if (decl.prop.startsWith('--')) {
52+
return
53+
}
54+
55+
valueParser(decl.value).walk((node) => {
56+
if (node.type === 'function' && SKIPPED_FUNCTIONS.has(node.value.toLowerCase())) {
57+
return false
58+
}
59+
60+
const dimension = node.type === 'word' ? valueParser.unit(node.value) : false
61+
62+
if (dimension && UNITS.has(dimension.unit.toLowerCase()) && Number(dimension.number) !== 0) {
63+
report({
64+
ruleName,
65+
result,
66+
node: decl,
67+
word: node.value,
68+
message: messages.rejected(node.value)
69+
})
70+
}
71+
72+
return undefined
73+
})
74+
})
75+
}
76+
}
77+
78+
ruleFunction.ruleName = ruleName
79+
ruleFunction.messages = messages
80+
81+
export default createPlugin(ruleName, ruleFunction)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import stylelint from 'stylelint'
2+
3+
const {
4+
createPlugin,
5+
utils: {
6+
report,
7+
ruleMessages,
8+
validateOptions
9+
}
10+
} = stylelint
11+
const ruleName = 'trigen/no-root-margin'
12+
const messages = ruleMessages(ruleName, {
13+
rejected: 'A block does not set its own outer margin: where it stands is its consumer\'s. Only `0` and `auto` reset or center it.'
14+
})
15+
// The block's root alone, with its states or modifiers, not an element inside it
16+
const ROOT_SELECTOR_PATTERN = /^\.root(?![\w-])[^\s>+~]*$/
17+
// A nested `&` with no combinator: the rule it stands in, in a state
18+
const NESTED_SAME_PATTERN = /^&[^\s>+~]*$/
19+
const MARGIN_PATTERN = /^margin(-|$)/i
20+
const NEUTRAL_VALUES = new Set(['0', 'auto'])
21+
22+
function isRootRule(rule) {
23+
return rule.selectors.some((selector) => {
24+
const trimmed = selector.trim()
25+
26+
return ROOT_SELECTOR_PATTERN.test(trimmed)
27+
|| (NESTED_SAME_PATTERN.test(trimmed) && rule.parent?.type === 'rule' && isRootRule(rule.parent))
28+
})
29+
}
30+
31+
function ruleFunction(primary) {
32+
return (root, result) => {
33+
if (!validateOptions(result, ruleName, {
34+
actual: primary
35+
})) {
36+
return
37+
}
38+
39+
root.walkRules((rule) => {
40+
if (!isRootRule(rule)) {
41+
return
42+
}
43+
44+
rule.each((node) => {
45+
if (
46+
node.type === 'decl'
47+
&& MARGIN_PATTERN.test(node.prop)
48+
&& !node.value.trim().split(/\s+/).every(value => NEUTRAL_VALUES.has(value.toLowerCase()))
49+
) {
50+
report({
51+
ruleName,
52+
result,
53+
node,
54+
message: messages.rejected
55+
})
56+
}
57+
})
58+
})
59+
}
60+
}
61+
62+
ruleFunction.ruleName = ruleName
63+
ruleFunction.messages = messages
64+
65+
export default createPlugin(ruleName, ruleFunction)

0 commit comments

Comments
 (0)