@kong/design-tokens 1.18.3-pr.613.b9dd522.0 → 1.18.3-pr.613.d9d2a3d.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 +78 -24
- 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 +4 -4
|
@@ -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',
|
|
@@ -17,11 +17,6 @@ const getCssToken = (scssToken) => {
|
|
|
17
17
|
return `--${tokenName}`
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
// Create regex pattern to match var(--token, $token) format
|
|
21
|
-
const createProperUsagePattern = (cssToken, scssToken) => {
|
|
22
|
-
return new RegExp(`var\\(\\s*${cssToken}\\s*,\\s*${scssToken}\\s*\\)`)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
20
|
// Find all occurrences of a token in a value
|
|
26
21
|
const findAllTokenOccurrences = (value, token) => {
|
|
27
22
|
const occurrences = []
|
|
@@ -38,16 +33,32 @@ const findAllTokenOccurrences = (value, token) => {
|
|
|
38
33
|
}
|
|
39
34
|
|
|
40
35
|
// Check if token at given position is properly wrapped in var()
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
// Enforces exact format: var(--token, $token) with exactly one space before comma
|
|
37
|
+
const isTokenProperlyWrapped = (value, tokenIndex, token, cssToken) => {
|
|
38
|
+
// Use regex to find all var(--token, $token) patterns
|
|
39
|
+
// Escape regex metacharacters in token so it is treated literally in the pattern
|
|
40
|
+
const escapedToken = token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
41
|
+
const pattern = new RegExp(`var\\(${cssToken}, ${escapedToken}\\)`, 'g')
|
|
42
|
+
|
|
43
|
+
// Find all matches
|
|
44
|
+
let match
|
|
45
|
+
while ((match = pattern.exec(value)) !== null) {
|
|
46
|
+
const matchStart = match.index
|
|
47
|
+
const matchEnd = matchStart + match[0].length
|
|
48
|
+
|
|
49
|
+
// Check if our token at tokenIndex is inside this match
|
|
50
|
+
const tokenEndIndex = tokenIndex + token.length
|
|
51
|
+
if (tokenIndex >= matchStart && tokenEndIndex <= matchEnd) {
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return false
|
|
46
57
|
}
|
|
47
58
|
|
|
48
59
|
const ruleFunction = () => {
|
|
49
60
|
return (postcssRoot, postcssResult) => {
|
|
50
|
-
const validOptions = validateOptions(
|
|
61
|
+
const validOptions = validateOptions(postcssResult, ruleName, {})
|
|
51
62
|
|
|
52
63
|
if (!validOptions) {
|
|
53
64
|
return
|
|
@@ -74,12 +85,11 @@ const ruleFunction = () => {
|
|
|
74
85
|
|
|
75
86
|
uniqueScssTokens.forEach((scssToken) => {
|
|
76
87
|
const cssToken = getCssToken(scssToken)
|
|
77
|
-
const properUsagePattern = createProperUsagePattern(cssToken, scssToken)
|
|
78
88
|
const tokenOccurrences = findAllTokenOccurrences(declValue, scssToken)
|
|
79
89
|
|
|
80
90
|
// Check each occurrence to see if it's properly wrapped
|
|
81
91
|
const hasImproperUsage = tokenOccurrences.some((tokenIndex) => {
|
|
82
|
-
return !isTokenProperlyWrapped(declValue, tokenIndex, scssToken,
|
|
92
|
+
return !isTokenProperlyWrapped(declValue, tokenIndex, scssToken, cssToken)
|
|
83
93
|
})
|
|
84
94
|
|
|
85
95
|
if (hasImproperUsage) {
|
|
@@ -96,19 +106,53 @@ const ruleFunction = () => {
|
|
|
96
106
|
// First, identify all positions that need to be replaced
|
|
97
107
|
improperlyUsedTokens.forEach((scssToken) => {
|
|
98
108
|
const cssToken = getCssToken(scssToken)
|
|
99
|
-
const properUsagePattern = createProperUsagePattern(cssToken, scssToken)
|
|
100
109
|
const tokenOccurrences = findAllTokenOccurrences(fixedValue, scssToken)
|
|
101
110
|
const properFormat = `var(${cssToken}, ${scssToken})`
|
|
102
111
|
|
|
103
|
-
tokenOccurrences.forEach((
|
|
112
|
+
tokenOccurrences.forEach((tokenIndex) => {
|
|
104
113
|
// Check if this occurrence is in proper format
|
|
105
|
-
if (!isTokenProperlyWrapped(fixedValue,
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
if (!isTokenProperlyWrapped(fixedValue, tokenIndex, scssToken, cssToken)) {
|
|
115
|
+
// Find the var() expression containing this token to replace the whole thing
|
|
116
|
+
const beforeToken = fixedValue.substring(0, tokenIndex)
|
|
117
|
+
const varStartIndex = beforeToken.lastIndexOf('var(')
|
|
118
|
+
|
|
119
|
+
if (varStartIndex === -1) {
|
|
120
|
+
// No var( found, just replace the token with proper format
|
|
121
|
+
replacements.push({
|
|
122
|
+
start: tokenIndex,
|
|
123
|
+
end: tokenIndex + scssToken.length,
|
|
124
|
+
replacement: properFormat,
|
|
125
|
+
})
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Find the matching closing parenthesis
|
|
130
|
+
let parenCount = 0
|
|
131
|
+
let searchIndex = varStartIndex
|
|
132
|
+
let varEndIndex = -1
|
|
133
|
+
|
|
134
|
+
while (searchIndex < fixedValue.length) {
|
|
135
|
+
const char = fixedValue[searchIndex]
|
|
136
|
+
if (char === '(') {
|
|
137
|
+
parenCount++
|
|
138
|
+
} else if (char === ')') {
|
|
139
|
+
parenCount--
|
|
140
|
+
if (parenCount === 0) {
|
|
141
|
+
varEndIndex = searchIndex
|
|
142
|
+
break
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
searchIndex++
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (varEndIndex !== -1) {
|
|
149
|
+
// Replace the entire var() expression
|
|
150
|
+
replacements.push({
|
|
151
|
+
start: varStartIndex,
|
|
152
|
+
end: varEndIndex + 1,
|
|
153
|
+
replacement: properFormat,
|
|
154
|
+
})
|
|
155
|
+
}
|
|
112
156
|
}
|
|
113
157
|
})
|
|
114
158
|
})
|
|
@@ -116,8 +160,18 @@ const ruleFunction = () => {
|
|
|
116
160
|
// Sort replacements by position (descending) to avoid offset issues
|
|
117
161
|
replacements.sort((a, b) => b.start - a.start)
|
|
118
162
|
|
|
163
|
+
// Remove duplicates (same start position)
|
|
164
|
+
const uniqueReplacements = []
|
|
165
|
+
const seenStarts = new Set()
|
|
166
|
+
replacements.forEach((replacement) => {
|
|
167
|
+
if (!seenStarts.has(replacement.start)) {
|
|
168
|
+
seenStarts.add(replacement.start)
|
|
169
|
+
uniqueReplacements.push(replacement)
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
119
173
|
// Apply replacements from end to start
|
|
120
|
-
|
|
174
|
+
uniqueReplacements.forEach(({ start, end, replacement }) => {
|
|
121
175
|
fixedValue = fixedValue.substring(0, start) + replacement + fixedValue.substring(end)
|
|
122
176
|
})
|
|
123
177
|
|
package/dist/tokens/js/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kong/design-tokens",
|
|
3
|
-
"version": "1.18.3-pr.613.
|
|
3
|
+
"version": "1.18.3-pr.613.d9d2a3d.0",
|
|
4
4
|
"description": "Kong UI Design Tokens and style dictionary",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"url": "https://github.com/Kong/design-tokens/issues"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@commitlint/cli": "^20.
|
|
57
|
-
"@commitlint/config-conventional": "^20.
|
|
56
|
+
"@commitlint/cli": "^20.3.0",
|
|
57
|
+
"@commitlint/config-conventional": "^20.3.0",
|
|
58
58
|
"@digitalroute/cz-conventional-changelog-for-jira": "^8.0.1",
|
|
59
59
|
"@evilmartians/lefthook": "^2.0.13",
|
|
60
60
|
"@kong/eslint-config-kong-ui": "^1.6.0",
|
|
@@ -126,7 +126,7 @@
|
|
|
126
126
|
"jiraAppend": "]"
|
|
127
127
|
}
|
|
128
128
|
},
|
|
129
|
-
"packageManager": "pnpm@10.
|
|
129
|
+
"packageManager": "pnpm@10.27.0",
|
|
130
130
|
"engines": {
|
|
131
131
|
"node": ">=20.19.5",
|
|
132
132
|
"pnpm": ">=9.14.4 || >=10.1.0"
|