forked from primer/stylelint-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsive-widths.js
98 lines (82 loc) · 2.63 KB
/
responsive-widths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const stylelint = require('stylelint')
const declarationValueIndex = require('stylelint/lib/utils/declarationValueIndex')
const valueParser = require('postcss-value-parser')
const ruleName = 'primer/responsive-widths'
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: value => {
return `A value larger than the smallest viewport could break responsive pages. Use a width value smaller than ${value}. https://primer.style/css/support/breakpoints`
}
})
// 320px is the smallest viewport size that we support
const walkGroups = (root, validate) => {
for (const node of root.nodes) {
if (node.type === 'function') {
walkGroups(node, validate)
} else {
validate(node)
}
}
return root
}
// eslint-disable-next-line no-unused-vars
module.exports = stylelint.createPlugin(ruleName, (enabled, options = {}, context) => {
if (!enabled) {
return noop
}
const lintResult = (root, result) => {
root.walk(decl => {
// Ignore things inside of breakpoints
if (decl.type === 'atrule' && decl.name === 'include' && decl.params.includes('breakpoint')) {
return false
}
if (decl.type !== 'decl' || !decl.prop.match(/^(min-width|width)/)) {
return noop
}
const problems = []
walkGroups(valueParser(decl.value), node => {
// Only check word types. https://github.com/TrySound/postcss-value-parser#word
if (node.type !== 'word') {
return
}
// Exact values to ignore.
if (['*', '+', '-', '/', '0', 'auto', 'inherit', 'initial'].includes(node.value)) {
return
}
const valueUnit = valueParser.unit(node.value)
switch (valueUnit.unit) {
case 'px':
if (parseInt(valueUnit.number) > 320) {
problems.push({
index: declarationValueIndex(decl) + node.sourceIndex,
message: messages.rejected(node.value)
})
}
break
case 'vw':
if (parseInt(valueUnit.number) > 100) {
problems.push({
index: declarationValueIndex(decl) + node.sourceIndex,
message: messages.rejected(node.value)
})
}
break
}
})
if (problems.length) {
for (const err of problems) {
stylelint.utils.report({
index: err.index,
message: err.message,
node: decl,
result,
ruleName
})
}
}
})
}
return lintResult
})
function noop() {}
module.exports.ruleName = ruleName
module.exports.messages = messages