@kong/design-tokens 1.18.3-pr.613.5525a1e.0 → 1.18.3-pr.613.857d684.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/stylelint-plugin/rules/use-css-token/index.mjs +71 -49
- package/dist/tokens/css/custom-properties.css +1 -1
- package/dist/tokens/js/cjs/index.d.ts +1 -1
- package/dist/tokens/js/cjs/index.js +1 -1
- package/dist/tokens/js/index.d.ts +1 -1
- package/dist/tokens/js/index.mjs +1 -1
- package/dist/tokens/less/variables.less +1 -1
- package/dist/tokens/scss/_map.scss +1 -1
- package/dist/tokens/scss/_variables.scss +1 -1
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@ import { KONG_TOKEN_PREFIX, RULE_NAME_PREFIX } from '../../utilities/index.mjs'
|
|
|
4
4
|
const { ruleMessages, validateOptions, report } = stylelint.utils
|
|
5
5
|
const ruleName = `${RULE_NAME_PREFIX}/use-css-token`
|
|
6
6
|
const messages = ruleMessages(ruleName, {
|
|
7
|
-
expected: 'SCSS tokens must be used as fallback values in CSS custom properties. Use format: var(--kui-design-token, $kui-design-token)',
|
|
7
|
+
expected: 'SCSS tokens must be used as fallback values in CSS custom properties. Use format: var(--kui-design-token, $kui-design-token) with exactly one space before the comma and no other spaces.',
|
|
8
8
|
})
|
|
9
9
|
const meta = {
|
|
10
10
|
url: 'https://github.com/Kong/design-tokens/blob/main/stylelint-plugin/README.md',
|
|
@@ -33,49 +33,26 @@ const findAllTokenOccurrences = (value, token) => {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// Check if token at given position is properly wrapped in var()
|
|
36
|
+
// Enforces exact format: var(--token, $token) with exactly one space before comma
|
|
36
37
|
const isTokenProperlyWrapped = (value, tokenIndex, token, cssToken) => {
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
while (searchIndex < value.length) {
|
|
52
|
-
const char = value[searchIndex]
|
|
53
|
-
if (char === '(') {
|
|
54
|
-
parenCount++
|
|
55
|
-
} else if (char === ')') {
|
|
56
|
-
parenCount--
|
|
57
|
-
if (parenCount === 0) {
|
|
58
|
-
varEndIndex = searchIndex
|
|
59
|
-
break
|
|
60
|
-
}
|
|
38
|
+
// Use regex to find all var(--token, $token) patterns
|
|
39
|
+
// Escape special regex characters in tokens (though they should only have letters/digits/hyphens)
|
|
40
|
+
const pattern = new RegExp(`var\\(${cssToken}, ${token}\\)`, 'g')
|
|
41
|
+
|
|
42
|
+
// Find all matches
|
|
43
|
+
let match
|
|
44
|
+
while ((match = pattern.exec(value)) !== null) {
|
|
45
|
+
const matchStart = match.index
|
|
46
|
+
const matchEnd = matchStart + match[0].length
|
|
47
|
+
|
|
48
|
+
// Check if our token at tokenIndex is inside this match
|
|
49
|
+
const tokenEndIndex = tokenIndex + token.length
|
|
50
|
+
if (tokenIndex >= matchStart && tokenEndIndex <= matchEnd) {
|
|
51
|
+
return true
|
|
61
52
|
}
|
|
62
|
-
searchIndex++
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (varEndIndex === -1) {
|
|
66
|
-
return false
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Verify the token is actually inside this var() expression
|
|
70
|
-
const tokenEndIndex = tokenIndex + token.length
|
|
71
|
-
if (tokenIndex < varStartIndex || tokenEndIndex > varEndIndex) {
|
|
72
|
-
return false
|
|
73
53
|
}
|
|
74
54
|
|
|
75
|
-
|
|
76
|
-
const varExpression = value.substring(varStartIndex, varEndIndex + 1)
|
|
77
|
-
const expectedPattern = new RegExp(`^var\\(\\s*${cssToken}\\s*,\\s*${token}\\s*\\)$`)
|
|
78
|
-
return expectedPattern.test(varExpression)
|
|
55
|
+
return false
|
|
79
56
|
}
|
|
80
57
|
|
|
81
58
|
const ruleFunction = () => {
|
|
@@ -131,15 +108,50 @@ const ruleFunction = () => {
|
|
|
131
108
|
const tokenOccurrences = findAllTokenOccurrences(fixedValue, scssToken)
|
|
132
109
|
const properFormat = `var(${cssToken}, ${scssToken})`
|
|
133
110
|
|
|
134
|
-
tokenOccurrences.forEach((
|
|
111
|
+
tokenOccurrences.forEach((tokenIndex) => {
|
|
135
112
|
// Check if this occurrence is in proper format
|
|
136
|
-
if (!isTokenProperlyWrapped(fixedValue,
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
113
|
+
if (!isTokenProperlyWrapped(fixedValue, tokenIndex, scssToken, cssToken)) {
|
|
114
|
+
// Find the var() expression containing this token to replace the whole thing
|
|
115
|
+
const beforeToken = fixedValue.substring(0, tokenIndex)
|
|
116
|
+
const varStartIndex = beforeToken.lastIndexOf('var(')
|
|
117
|
+
|
|
118
|
+
if (varStartIndex === -1) {
|
|
119
|
+
// No var( found, just replace the token with proper format
|
|
120
|
+
replacements.push({
|
|
121
|
+
start: tokenIndex,
|
|
122
|
+
end: tokenIndex + scssToken.length,
|
|
123
|
+
replacement: properFormat,
|
|
124
|
+
})
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Find the matching closing parenthesis
|
|
129
|
+
let parenCount = 0
|
|
130
|
+
let searchIndex = varStartIndex
|
|
131
|
+
let varEndIndex = -1
|
|
132
|
+
|
|
133
|
+
while (searchIndex < fixedValue.length) {
|
|
134
|
+
const char = fixedValue[searchIndex]
|
|
135
|
+
if (char === '(') {
|
|
136
|
+
parenCount++
|
|
137
|
+
} else if (char === ')') {
|
|
138
|
+
parenCount--
|
|
139
|
+
if (parenCount === 0) {
|
|
140
|
+
varEndIndex = searchIndex
|
|
141
|
+
break
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
searchIndex++
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (varEndIndex !== -1) {
|
|
148
|
+
// Replace the entire var() expression
|
|
149
|
+
replacements.push({
|
|
150
|
+
start: varStartIndex,
|
|
151
|
+
end: varEndIndex + 1,
|
|
152
|
+
replacement: properFormat,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
143
155
|
}
|
|
144
156
|
})
|
|
145
157
|
})
|
|
@@ -147,8 +159,18 @@ const ruleFunction = () => {
|
|
|
147
159
|
// Sort replacements by position (descending) to avoid offset issues
|
|
148
160
|
replacements.sort((a, b) => b.start - a.start)
|
|
149
161
|
|
|
162
|
+
// Remove duplicates (same start position)
|
|
163
|
+
const uniqueReplacements = []
|
|
164
|
+
const seenStarts = new Set()
|
|
165
|
+
replacements.forEach((replacement) => {
|
|
166
|
+
if (!seenStarts.has(replacement.start)) {
|
|
167
|
+
seenStarts.add(replacement.start)
|
|
168
|
+
uniqueReplacements.push(replacement)
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
|
|
150
172
|
// Apply replacements from end to start
|
|
151
|
-
|
|
173
|
+
uniqueReplacements.forEach(({ start, end, replacement }) => {
|
|
152
174
|
fixedValue = fixedValue.substring(0, start) + replacement + fixedValue.substring(end)
|
|
153
175
|
})
|
|
154
176
|
|
package/dist/tokens/js/index.mjs
CHANGED