-
Notifications
You must be signed in to change notification settings - Fork 22
/
typography.js
173 lines (143 loc) · 4.67 KB
/
typography.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import stylelint from 'stylelint'
import {declarationValueIndex} from 'stylelint/lib/utils/nodeFieldIndices.cjs'
import {primitivesVariables} from './lib/utils.js'
const {
createPlugin,
utils: {report, ruleMessages, validateOptions},
} = stylelint
export const ruleName = 'primer/typography'
export const messages = ruleMessages(ruleName, {
rejected: (value, replacement) => {
// no possible replacement
if (!replacement) {
return `Please use a Primer typography variable instead of '${value}'. Consult the primer docs for a suitable replacement. https://primer.style/foundations/primitives/typography`
}
// multiple possible replacements
if (replacement.length) {
return `Please use one of the following Primer typography variables instead of '${value}': ${replacement.map(replacementObj => `'${replacementObj.name}'`).join(', ')}. https://primer.style/foundations/primitives/typography`
}
// one possible replacement
return `Please replace '${value}' with Primer typography variable '${replacement['name']}'. https://primer.style/foundations/primitives/typography`
},
})
const fontWeightKeywordMap = {
normal: 400,
bold: 700,
bolder: 600,
lighter: 300,
}
const getClosestFontWeight = (goalWeightNumber, fontWeightsTokens) => {
return fontWeightsTokens.reduce((prev, curr) =>
Math.abs(curr.values - goalWeightNumber) < Math.abs(prev.values - goalWeightNumber) ? curr : prev,
).values
}
const variables = primitivesVariables('typography')
const fontSizes = []
const fontWeights = []
const lineHeights = []
const fontStacks = []
const fontShorthands = []
// Props that we want to check for typography variables
const propList = ['font-size', 'font-weight', 'line-height', 'font-family', 'font']
for (const variable of variables) {
const name = variable['name']
if (name.includes('size')) {
fontSizes.push(variable)
}
if (name.includes('weight')) {
fontWeights.push(variable)
}
if (name.includes('lineHeight')) {
lineHeights.push(variable)
}
if (name.includes('fontStack')) {
fontStacks.push(variable)
}
if (name.includes('shorthand')) {
fontShorthands.push(variable)
}
}
/** @type {import('stylelint').Rule} */
const ruleFunction = primary => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [true],
})
let validValues = []
if (!validOptions) return
root.walkDecls(declNode => {
const {prop, value} = declNode
if (!propList.some(typographyProp => prop === typographyProp)) return
const checkForVariable = (vars, nodeValue) =>
vars.some(variable =>
new RegExp(`${variable['name'].replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(nodeValue),
)
// Exact values to ignore.
if (value === 'inherit') {
return
}
switch (prop) {
case 'font-size':
validValues = fontSizes
break
case 'font-weight':
validValues = fontWeights
break
case 'line-height':
validValues = lineHeights
break
case 'font-family':
validValues = fontStacks
break
case 'font':
validValues = fontShorthands
break
default:
validValues = []
}
if (checkForVariable(validValues, value)) {
return
}
const getReplacements = () => {
const replacementTokens = validValues.filter(variable => {
if (!(variable.values instanceof Array)) {
let nodeValue = value
if (prop === 'font-weight') {
nodeValue = getClosestFontWeight(fontWeightKeywordMap[value] || value, fontWeights)
}
return variable.values.toString() === nodeValue.toString()
}
return variable.values.includes(value.replace('-', ''))
})
if (!replacementTokens.length) {
return
}
return replacementTokens[0]
}
const replacement = getReplacements()
const fixable = replacement && !replacement.length
let fix = undefined
if (fixable) {
fix = () => {
declNode.value = value.replace(value, `var(${replacement['name']})`)
}
}
report({
index: declarationValueIndex(declNode),
endIndex: declarationValueIndex(declNode) + value.length,
message: messages.rejected(value, replacement, prop),
node: declNode,
result,
ruleName,
fix,
})
})
}
}
ruleFunction.ruleName = ruleName
ruleFunction.messages = messages
ruleFunction.meta = {
fixable: true,
}
export default createPlugin(ruleName, ruleFunction)