@kong/stylelint-plugin-design-tokens 0.0.1
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/LICENSE +201 -0
- package/README.md +126 -0
- package/index.mjs +4 -0
- package/package.json +35 -0
- package/rules/token-var-usage/index.mjs +341 -0
- package/rules/use-proper-token/index.mjs +73 -0
- package/rules/use-proper-token/token-map.mjs +81 -0
- package/utilities/constants.mjs +5 -0
- package/utilities/extract-tokens-from-value.mjs +15 -0
- package/utilities/index.mjs +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023 Kong, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @kong/stylelint-plugin-design-tokens
|
|
2
|
+
|
|
3
|
+
[Stylelint](https://github.com/stylelint/stylelint) plugin for linting design tokens.
|
|
4
|
+
|
|
5
|
+
- [Usage](#usage)
|
|
6
|
+
- [Rules](#rules)
|
|
7
|
+
- [`use-proper-token`](#use-proper-token)
|
|
8
|
+
- [`token-var-usage`](#token-var-usage)
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
Install `@kong/stylelint-plugin-design-tokens` and `stylelint` packages as a `devDependency` in your project
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add -D @kong/stylelint-plugin-design-tokens stylelint
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
In your stylelint config file, add the plugin and enable rules that you want to use:
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
plugins: [
|
|
22
|
+
'@kong/stylelint-plugin-design-tokens'
|
|
23
|
+
],
|
|
24
|
+
rules: {
|
|
25
|
+
'@kong/design-tokens/use-proper-token': [true, {
|
|
26
|
+
disableFix: true,
|
|
27
|
+
severity: 'error' // You can also configure as `warning`
|
|
28
|
+
}]
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Rules
|
|
33
|
+
|
|
34
|
+
### `use-proper-token`
|
|
35
|
+
|
|
36
|
+
Rule that parses CSS properties for inappropriate tokens being referenced.
|
|
37
|
+
|
|
38
|
+
For example, the `kui-color-text-primary` token **should** be used as a value for `color`, but **should not** be used for `background-color`.
|
|
39
|
+
|
|
40
|
+
#### :red_circle: Incorrect usage
|
|
41
|
+
|
|
42
|
+
```scss
|
|
43
|
+
.foo {
|
|
44
|
+
// This **will** trigger an error, text color token used for background-color property
|
|
45
|
+
background-color: $kui-color-text-primary;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### :green_circle: Correct usage
|
|
50
|
+
|
|
51
|
+
```scss
|
|
52
|
+
.foo {
|
|
53
|
+
// This **will NOT** trigger an error, appropriate token for the property
|
|
54
|
+
color: $kui-color-text-primary;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `token-var-usage`
|
|
59
|
+
|
|
60
|
+
Rule that ensures SCSS design tokens are properly used as fallback values in CSS custom properties.
|
|
61
|
+
|
|
62
|
+
SCSS tokens (e.g., `$kui-color-text-primary`) **must** be wrapped in the `var()` function with the corresponding CSS custom property as the first argument and the SCSS token as the fallback. The format must be exact: `var(--kui-token-name, $kui-token-name)` with exactly one space after the comma and no other spaces.
|
|
63
|
+
|
|
64
|
+
For cases where SCSS interpolation is needed (e.g., when defining CSS custom properties), the interpolated format is also valid: `var(--kui-token-name, #{$kui-token-name})`.
|
|
65
|
+
|
|
66
|
+
This approach provides progressive enhancement: browsers use the CSS custom property (which can be overridden), with the SCSS token as a compile-time fallback.
|
|
67
|
+
|
|
68
|
+
#### :red_circle: Incorrect usage
|
|
69
|
+
|
|
70
|
+
```scss
|
|
71
|
+
.foo {
|
|
72
|
+
// Direct SCSS token usage without var()
|
|
73
|
+
color: $kui-color-text-primary;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.bar {
|
|
77
|
+
// Missing space after the comma
|
|
78
|
+
color: var(--kui-color-text-primary,$kui-color-text-primary);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.baz {
|
|
82
|
+
// Extra spaces
|
|
83
|
+
color: var(--kui-color-text-primary, $kui-color-text-primary);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.qux {
|
|
87
|
+
// Standalone interpolation without var() - no auto-fix provided
|
|
88
|
+
// Developer must determine correct usage based on context
|
|
89
|
+
color: #{$kui-color-text-primary};
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### :green_circle: Correct usage
|
|
94
|
+
|
|
95
|
+
```scss
|
|
96
|
+
.foo {
|
|
97
|
+
// Proper format with CSS custom property and SCSS fallback
|
|
98
|
+
color: var(--kui-color-text-primary, $kui-color-text-primary);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.bar {
|
|
102
|
+
// Interpolated format (useful when defining CSS custom properties)
|
|
103
|
+
--vc-white: var(--kui-color-text-inverse, #{$kui-color-text-inverse});
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### Limitations
|
|
108
|
+
|
|
109
|
+
The `token-var-usage` rule has some known limitations:
|
|
110
|
+
|
|
111
|
+
1. **Multi-line `var()` declarations**: The rule currently does not handle `var()` function calls that span multiple lines. To ensure proper linting and auto-fixing, keep your `var()` declarations on a single line:
|
|
112
|
+
|
|
113
|
+
```scss
|
|
114
|
+
// ❌ May produce unexpected behavior
|
|
115
|
+
font-size: var(
|
|
116
|
+
--kui-font-size-40,
|
|
117
|
+
$kui-font-size-40
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
// ✅ Works correctly
|
|
121
|
+
font-size: var(--kui-font-size-40, $kui-font-size-40);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
2. **Standalone interpolation**: SCSS tokens wrapped only in interpolation syntax (e.g., `#{$kui-color-text-primary}`) without `var()` cannot be auto-fixed. The rule will flag these cases, but you must manually determine the correct usage based on your context—whether to use the standard `var()` format or the interpolated `var()` format.
|
|
125
|
+
|
|
126
|
+
3. **Complex SCSS expressions**: The rule is designed to work with direct token references. Complex SCSS expressions involving tokens (e.g., calculations, functions) may not be properly detected or auto-fixed.
|
package/index.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kong/stylelint-plugin-design-tokens",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Stylelint plugin for Kong UI design tokens",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.mjs",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"index.mjs",
|
|
12
|
+
"rules",
|
|
13
|
+
"utilities",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"stylelint": ">=14.0.0"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/Kong/design-tokens.git"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/Kong/design-tokens/packages/stylelint-plugin-design-tokens/",
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=20.19.5",
|
|
30
|
+
"pnpm": ">=10.28.2"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"lint": "eslint ."
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import stylelint from 'stylelint'
|
|
2
|
+
import { KONG_TOKEN_PREFIX, RULE_NAME_PREFIX } from '../../utilities/index.mjs'
|
|
3
|
+
|
|
4
|
+
const { ruleMessages, validateOptions, report } = stylelint.utils
|
|
5
|
+
const ruleName = `${RULE_NAME_PREFIX}/token-var-usage`
|
|
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) or var(--kui-design-token, #{$kui-design-token}) for interpolation, with exactly one space after the comma and no other spaces.',
|
|
8
|
+
})
|
|
9
|
+
const meta = {
|
|
10
|
+
url: 'https://github.com/Kong/design-tokens/blob/main/stylelint-plugin/README.md',
|
|
11
|
+
fixable: true,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Convert SCSS token ($kui-token) to CSS token (--kui-token)
|
|
15
|
+
const getCssToken = (scssToken) => {
|
|
16
|
+
const tokenName = scssToken.substring(1) // Remove $
|
|
17
|
+
return `--${tokenName}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Find all occurrences of a token in a value
|
|
21
|
+
const findAllTokenOccurrences = (value, token) => {
|
|
22
|
+
const occurrences = []
|
|
23
|
+
let searchIndex = 0
|
|
24
|
+
while (true) {
|
|
25
|
+
const index = value.indexOf(token, searchIndex)
|
|
26
|
+
if (index === -1) {
|
|
27
|
+
break
|
|
28
|
+
}
|
|
29
|
+
occurrences.push(index)
|
|
30
|
+
searchIndex = index + 1
|
|
31
|
+
}
|
|
32
|
+
return occurrences
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Check if token is inside standalone interpolation (#{$token}) without var() wrapper
|
|
36
|
+
const isTokenInStandaloneInterpolation = (value, tokenIndex, token) => {
|
|
37
|
+
// Check if token is inside #{...}
|
|
38
|
+
const beforeToken = value.substring(0, tokenIndex)
|
|
39
|
+
const afterTokenStart = tokenIndex + token.length
|
|
40
|
+
|
|
41
|
+
// Look for #{ before the token
|
|
42
|
+
const interpolationStartIndex = beforeToken.lastIndexOf('#{')
|
|
43
|
+
if (interpolationStartIndex === -1) {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Check if there's anything between #{ and the token (should be nothing or whitespace)
|
|
48
|
+
const betweenInterpolationAndToken = value.substring(interpolationStartIndex + 2, tokenIndex)
|
|
49
|
+
if (betweenInterpolationAndToken.trim() !== '') {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Look for } after the token
|
|
54
|
+
const afterToken = value.substring(afterTokenStart)
|
|
55
|
+
const interpolationEndMatch = afterToken.match(/^\s*}/)
|
|
56
|
+
if (!interpolationEndMatch) {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Now check if this #{$token} is inside a var() function
|
|
61
|
+
// If it is, it's not "standalone" interpolation
|
|
62
|
+
const fullBeforeToken = value.substring(0, interpolationStartIndex)
|
|
63
|
+
const varStartIndex = fullBeforeToken.lastIndexOf('var(')
|
|
64
|
+
|
|
65
|
+
if (varStartIndex !== -1) {
|
|
66
|
+
// Check if the var() contains this interpolation
|
|
67
|
+
// Find the closing paren of the var()
|
|
68
|
+
let parenCount = 0
|
|
69
|
+
let searchIndex = varStartIndex
|
|
70
|
+
|
|
71
|
+
while (searchIndex < value.length) {
|
|
72
|
+
const char = value[searchIndex]
|
|
73
|
+
if (char === '(') {
|
|
74
|
+
parenCount++
|
|
75
|
+
} else if (char === ')') {
|
|
76
|
+
parenCount--
|
|
77
|
+
if (parenCount === 0) {
|
|
78
|
+
// Found the closing paren
|
|
79
|
+
// Check if our token is inside this var()
|
|
80
|
+
if (tokenIndex < searchIndex) {
|
|
81
|
+
// Token is inside the var(), so this is NOT standalone interpolation
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
break
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
searchIndex++
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Check if token at given position is properly wrapped in var()
|
|
95
|
+
// Accepts both formats:
|
|
96
|
+
// - var(--token, $token) with exactly one space after comma
|
|
97
|
+
// - var(--token, #{$token}) with exactly one space after comma (interpolated)
|
|
98
|
+
const isTokenProperlyWrapped = (value, tokenIndex, token, cssToken) => {
|
|
99
|
+
// Use regex to find all var(--token, $token) or var(--token, #{$token}) patterns
|
|
100
|
+
// Escape regex metacharacters in token so it is treated literally in the pattern
|
|
101
|
+
const escapedToken = token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
102
|
+
|
|
103
|
+
// Pattern 1: var(--token, $token)
|
|
104
|
+
const pattern1 = new RegExp(`var\\(${cssToken}, ${escapedToken}\\)`, 'g')
|
|
105
|
+
|
|
106
|
+
// Pattern 2: var(--token, #{$token})
|
|
107
|
+
const pattern2 = new RegExp(`var\\(${cssToken}, #{${escapedToken}}\\)`, 'g')
|
|
108
|
+
|
|
109
|
+
// Check both patterns
|
|
110
|
+
const patterns = [pattern1, pattern2]
|
|
111
|
+
|
|
112
|
+
for (const pattern of patterns) {
|
|
113
|
+
// Reset lastIndex for each pattern
|
|
114
|
+
pattern.lastIndex = 0
|
|
115
|
+
|
|
116
|
+
let match
|
|
117
|
+
while ((match = pattern.exec(value)) !== null) {
|
|
118
|
+
const matchStart = match.index
|
|
119
|
+
const matchEnd = matchStart + match[0].length
|
|
120
|
+
|
|
121
|
+
// Check if our token at tokenIndex is inside this match
|
|
122
|
+
const tokenEndIndex = tokenIndex + token.length
|
|
123
|
+
if (tokenIndex >= matchStart && tokenEndIndex <= matchEnd) {
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return false
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const ruleFunction = () => {
|
|
133
|
+
return (postcssRoot, postcssResult) => {
|
|
134
|
+
const validOptions = validateOptions(postcssResult, ruleName, {})
|
|
135
|
+
|
|
136
|
+
if (!validOptions) {
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
postcssRoot.walkDecls((decl) => {
|
|
141
|
+
const declValue = decl.value
|
|
142
|
+
|
|
143
|
+
// Extract all SCSS tokens (starting with $kui-)
|
|
144
|
+
const scssTokenRegex = new RegExp(`\\$${KONG_TOKEN_PREFIX}[a-z0-9-]+`, 'g')
|
|
145
|
+
const scssTokens = declValue.match(scssTokenRegex)
|
|
146
|
+
|
|
147
|
+
// If no SCSS tokens are used, skip validation
|
|
148
|
+
if (!scssTokens || scssTokens.length === 0) {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Get unique SCSS tokens
|
|
153
|
+
const uniqueScssTokens = Array.from(new Set(scssTokens))
|
|
154
|
+
|
|
155
|
+
// Check if each SCSS token is properly wrapped in var()
|
|
156
|
+
// Pattern: var(--kui-token-name, $kui-token-name)
|
|
157
|
+
const improperlyUsedTokens = []
|
|
158
|
+
|
|
159
|
+
uniqueScssTokens.forEach((scssToken) => {
|
|
160
|
+
const cssToken = getCssToken(scssToken)
|
|
161
|
+
const tokenOccurrences = findAllTokenOccurrences(declValue, scssToken)
|
|
162
|
+
|
|
163
|
+
// Check each occurrence to see if it's properly wrapped
|
|
164
|
+
const hasImproperUsage = tokenOccurrences.some((tokenIndex) => {
|
|
165
|
+
return !isTokenProperlyWrapped(declValue, tokenIndex, scssToken, cssToken)
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
if (hasImproperUsage) {
|
|
169
|
+
improperlyUsedTokens.push(scssToken)
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
if (improperlyUsedTokens.length > 0) {
|
|
174
|
+
// Create fix function
|
|
175
|
+
const fix = () => {
|
|
176
|
+
let fixedValue = declValue
|
|
177
|
+
const replacements = []
|
|
178
|
+
|
|
179
|
+
// First, identify all positions that need to be replaced
|
|
180
|
+
improperlyUsedTokens.forEach((scssToken) => {
|
|
181
|
+
const cssToken = getCssToken(scssToken)
|
|
182
|
+
const tokenOccurrences = findAllTokenOccurrences(declValue, scssToken)
|
|
183
|
+
const properFormat = `var(${cssToken}, ${scssToken})`
|
|
184
|
+
|
|
185
|
+
tokenOccurrences.forEach((tokenIndex) => {
|
|
186
|
+
// Check if this occurrence is in proper format
|
|
187
|
+
if (!isTokenProperlyWrapped(declValue, tokenIndex, scssToken, cssToken)) {
|
|
188
|
+
// Skip auto-fix for tokens in standalone interpolation (e.g., #{$token})
|
|
189
|
+
// Let the developer decide the appropriate fix
|
|
190
|
+
if (isTokenInStandaloneInterpolation(declValue, tokenIndex, scssToken)) {
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Find the var() expression containing this token
|
|
195
|
+
const beforeToken = declValue.substring(0, tokenIndex)
|
|
196
|
+
const varStartIndex = beforeToken.lastIndexOf('var(')
|
|
197
|
+
const lastClosingParenBeforeToken = beforeToken.lastIndexOf(')')
|
|
198
|
+
|
|
199
|
+
if (varStartIndex === -1 || lastClosingParenBeforeToken > varStartIndex) {
|
|
200
|
+
// No enclosing var( found for this token (either no var( at all, or the nearest
|
|
201
|
+
// var( was already closed before the token, e.g. `var(--x) $kui-space-40`)
|
|
202
|
+
replacements.push({
|
|
203
|
+
start: tokenIndex,
|
|
204
|
+
end: tokenIndex + scssToken.length,
|
|
205
|
+
replacement: properFormat,
|
|
206
|
+
})
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Find the matching closing parenthesis
|
|
211
|
+
let parenCount = 0
|
|
212
|
+
let searchIndex = varStartIndex
|
|
213
|
+
let varEndIndex = -1
|
|
214
|
+
|
|
215
|
+
while (searchIndex < declValue.length) {
|
|
216
|
+
const char = declValue[searchIndex]
|
|
217
|
+
if (char === '(') {
|
|
218
|
+
parenCount++
|
|
219
|
+
} else if (char === ')') {
|
|
220
|
+
parenCount--
|
|
221
|
+
if (parenCount === 0) {
|
|
222
|
+
varEndIndex = searchIndex
|
|
223
|
+
break
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
searchIndex++
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (varEndIndex !== -1) {
|
|
230
|
+
// Extract the var() content
|
|
231
|
+
const varContent = declValue.substring(varStartIndex + 4, varEndIndex) // +4 to skip 'var('
|
|
232
|
+
|
|
233
|
+
// Check if there's a comma (indicating primary property + fallback)
|
|
234
|
+
const commaIndex = varContent.indexOf(',')
|
|
235
|
+
|
|
236
|
+
if (commaIndex !== -1) {
|
|
237
|
+
// Extract primary property and fallback
|
|
238
|
+
const primaryProperty = varContent.substring(0, commaIndex).trim()
|
|
239
|
+
const fallbackValue = varContent.substring(commaIndex + 1).trim()
|
|
240
|
+
|
|
241
|
+
// Check if primary property is a CSS custom property (not a kui- token reference)
|
|
242
|
+
// and if the token is in the fallback part
|
|
243
|
+
const fallbackStartInOriginal = varStartIndex + 4 + commaIndex + 1
|
|
244
|
+
|
|
245
|
+
if (tokenIndex >= fallbackStartInOriginal && primaryProperty.startsWith('--')) {
|
|
246
|
+
// Token is in fallback position and there's a valid CSS property
|
|
247
|
+
// Replace all $kui- tokens in the fallback with var(--kui-token)
|
|
248
|
+
let newFallback = fallbackValue
|
|
249
|
+
|
|
250
|
+
// Find all $kui- tokens in the fallback
|
|
251
|
+
const fallbackTokenRegex = new RegExp(`\\$${KONG_TOKEN_PREFIX}[a-z0-9-]+`, 'g')
|
|
252
|
+
const fallbackTokens = fallbackValue.match(fallbackTokenRegex)
|
|
253
|
+
|
|
254
|
+
if (fallbackTokens) {
|
|
255
|
+
// Replace each token with var() wrapper, from end to start to avoid offset issues
|
|
256
|
+
const fallbackReplacements = []
|
|
257
|
+
fallbackTokens.forEach((fbToken) => {
|
|
258
|
+
const fbCssToken = getCssToken(fbToken)
|
|
259
|
+
const fbTokenOccurrences = findAllTokenOccurrences(fallbackValue, fbToken)
|
|
260
|
+
|
|
261
|
+
fbTokenOccurrences.forEach((fbTokenIndex) => {
|
|
262
|
+
fallbackReplacements.push({
|
|
263
|
+
start: fbTokenIndex,
|
|
264
|
+
end: fbTokenIndex + fbToken.length,
|
|
265
|
+
token: fbToken,
|
|
266
|
+
cssToken: fbCssToken,
|
|
267
|
+
})
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
// Sort by position descending
|
|
272
|
+
fallbackReplacements.sort((a, b) => b.start - a.start)
|
|
273
|
+
|
|
274
|
+
// Apply replacements
|
|
275
|
+
fallbackReplacements.forEach(({ start, end, token, cssToken }) => {
|
|
276
|
+
newFallback = newFallback.substring(0, start) + `var(${cssToken}, ${token})` + newFallback.substring(end)
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Construct the new var() with preserved primary property
|
|
281
|
+
const newVarExpression = `var(${primaryProperty}, ${newFallback})`
|
|
282
|
+
|
|
283
|
+
replacements.push({
|
|
284
|
+
start: varStartIndex,
|
|
285
|
+
end: varEndIndex + 1,
|
|
286
|
+
replacement: newVarExpression,
|
|
287
|
+
})
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Default case: replace the entire var() expression with proper format
|
|
293
|
+
replacements.push({
|
|
294
|
+
start: varStartIndex,
|
|
295
|
+
end: varEndIndex + 1,
|
|
296
|
+
replacement: properFormat,
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
})
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
// Sort replacements by position (descending) to avoid offset issues
|
|
304
|
+
replacements.sort((a, b) => b.start - a.start)
|
|
305
|
+
|
|
306
|
+
// Remove duplicates (same start position)
|
|
307
|
+
const uniqueReplacements = []
|
|
308
|
+
const seenStarts = new Set()
|
|
309
|
+
replacements.forEach((replacement) => {
|
|
310
|
+
if (!seenStarts.has(replacement.start)) {
|
|
311
|
+
seenStarts.add(replacement.start)
|
|
312
|
+
uniqueReplacements.push(replacement)
|
|
313
|
+
}
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
// Apply replacements from end to start
|
|
317
|
+
uniqueReplacements.forEach(({ start, end, replacement }) => {
|
|
318
|
+
fixedValue = fixedValue.substring(0, start) + replacement + fixedValue.substring(end)
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
decl.value = fixedValue
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
report({
|
|
325
|
+
message: messages.expected,
|
|
326
|
+
node: decl,
|
|
327
|
+
result: postcssResult,
|
|
328
|
+
ruleName,
|
|
329
|
+
fix,
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
ruleFunction.ruleName = ruleName
|
|
337
|
+
ruleFunction.messages = messages
|
|
338
|
+
ruleFunction.meta = meta
|
|
339
|
+
|
|
340
|
+
export default stylelint.createPlugin(ruleName, ruleFunction)
|
|
341
|
+
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import stylelint from 'stylelint'
|
|
2
|
+
import { PROPERTY_TOKEN_MAP } from './token-map.mjs'
|
|
3
|
+
import { KONG_TOKEN_PREFIX, extractTokensFromValue, RULE_NAME_PREFIX } from '../../utilities/index.mjs'
|
|
4
|
+
|
|
5
|
+
const { ruleMessages, validateOptions, report } = stylelint.utils
|
|
6
|
+
const ruleName = `${RULE_NAME_PREFIX}/use-proper-token`
|
|
7
|
+
const messages = ruleMessages(ruleName, {
|
|
8
|
+
unexpected: (token, property) => `Unexpected usage of '${token}' token in '${property}' property.`,
|
|
9
|
+
})
|
|
10
|
+
const meta = {
|
|
11
|
+
url: 'https://github.com/Kong/design-tokens/blob/main/stylelint-plugin/README.md',
|
|
12
|
+
fixable: false,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ruleFunction = () => {
|
|
16
|
+
return (postcssRoot, postcssResult) => {
|
|
17
|
+
const validOptions = validateOptions(postcssResult, ruleName, {})
|
|
18
|
+
|
|
19
|
+
if (!validOptions) {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
postcssRoot.walkDecls((decl) => {
|
|
24
|
+
/**
|
|
25
|
+
* PostCSS AST declaration node
|
|
26
|
+
* Docs: https://postcss.org/api/#declaration
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const declProp = decl.prop
|
|
30
|
+
const declValue = decl.value
|
|
31
|
+
// check if the value contains a token as CSS or SCSS variable
|
|
32
|
+
const hasToken = [`--${KONG_TOKEN_PREFIX}`, `$${KONG_TOKEN_PREFIX}`].some(pattern => declValue.includes(pattern))
|
|
33
|
+
if (!hasToken) {
|
|
34
|
+
// skip validating if the value does not contain a token
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const tokenProperty = Object.keys(PROPERTY_TOKEN_MAP).find(key => key.split(',').some(prop => prop === declProp))
|
|
39
|
+
|
|
40
|
+
// check if the property is in the property map
|
|
41
|
+
const isEnforcedProp = !!tokenProperty
|
|
42
|
+
if (!isEnforcedProp) {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const valueTokens = extractTokensFromValue(declValue)
|
|
47
|
+
// get the appropriate tokens for the property and create regex for each
|
|
48
|
+
// regex pattern: /kui-(?:[a-z0-9-]+-)?token(?:-[a-z0-9-]+)?$/
|
|
49
|
+
// this allows to match both regular and component tokens
|
|
50
|
+
const appropriateTokens = PROPERTY_TOKEN_MAP[tokenProperty].map(token => new RegExp(KONG_TOKEN_PREFIX + '(?:[a-z0-9-]+-)?' + token + '(?:-[a-z0-9-]+)?$'))
|
|
51
|
+
// filter out tokens that are not appropriate for the property
|
|
52
|
+
const inappropriateTokens = valueTokens.filter(vToken => !appropriateTokens.some(aTokenRegex => aTokenRegex.test(vToken)))
|
|
53
|
+
|
|
54
|
+
if (inappropriateTokens.length) {
|
|
55
|
+
// report the error for each inappropriate token
|
|
56
|
+
inappropriateTokens.forEach((token) => {
|
|
57
|
+
report({
|
|
58
|
+
message: messages.unexpected(token, declProp),
|
|
59
|
+
node: decl,
|
|
60
|
+
result: postcssResult,
|
|
61
|
+
ruleName,
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
ruleFunction.ruleName = ruleName
|
|
70
|
+
ruleFunction.messages = messages
|
|
71
|
+
ruleFunction.meta = meta
|
|
72
|
+
|
|
73
|
+
export default stylelint.createPlugin(ruleName, ruleFunction)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map of CSS properties and their corresponding allowed design tokens
|
|
3
|
+
* Example: for `background-color` CSS property allowed tokens are `color-background` and `method-color-background`
|
|
4
|
+
*
|
|
5
|
+
* As long as tokens follow this pattern, they don't need to be added to the arrays in the map:
|
|
6
|
+
* - starts with a prefix `kui-`
|
|
7
|
+
* - optionally followed by a component name in kebab-case
|
|
8
|
+
* - followed by a token name in kebab-case
|
|
9
|
+
* - optionally followed by a variant name in kebab-case
|
|
10
|
+
* Examples: `kui-analytics-chart-background-color-inverse`, `kui-color-background`, `kui-color-background-inverse`
|
|
11
|
+
*
|
|
12
|
+
* key: CSS Property name
|
|
13
|
+
* value: Array of valid Kong Design Tokens, without the `kui-` prefix
|
|
14
|
+
*
|
|
15
|
+
* ! To enforce no token should be used for a CSS property, set the value to an empty array.
|
|
16
|
+
*/
|
|
17
|
+
export const PROPERTY_TOKEN_MAP = {
|
|
18
|
+
background: ['color-background', 'status-color'],
|
|
19
|
+
'background-color': ['color-background', 'status-color'],
|
|
20
|
+
'background-size': [],
|
|
21
|
+
border: ['border-radius', 'border-width', 'color-border'],
|
|
22
|
+
'border-bottom': ['border-radius', 'border-width', 'color-border'],
|
|
23
|
+
'border-bottom-color': ['color-border'],
|
|
24
|
+
'border-bottom-left-radius': ['border-radius'],
|
|
25
|
+
'border-bottom-right-radius': ['border-radius'],
|
|
26
|
+
'border-bottom-width': ['border-width'],
|
|
27
|
+
'border-color': ['color-border'],
|
|
28
|
+
'border-left': ['border-radius', 'border-width', 'color-border'],
|
|
29
|
+
'border-left-color': ['color-border'],
|
|
30
|
+
'border-left-width': ['border-width'],
|
|
31
|
+
'border-radius': ['border-radius'],
|
|
32
|
+
'border-right': ['border-radius', 'border-width', 'color-border'],
|
|
33
|
+
'border-right-color': ['color-border'],
|
|
34
|
+
'border-right-width': ['border-width'],
|
|
35
|
+
'border-spacing': ['space'],
|
|
36
|
+
'border-top': ['border-radius', 'border-width', 'color-border'],
|
|
37
|
+
'border-top-color': ['color-border'],
|
|
38
|
+
'border-top-left-radius': ['border-radius'],
|
|
39
|
+
'border-top-right-radius': ['border-radius'],
|
|
40
|
+
'border-top-width': ['border-width'],
|
|
41
|
+
'border-width': ['border-width'],
|
|
42
|
+
bottom: [],
|
|
43
|
+
'box-shadow': ['border-width', 'color-border', 'shadow'],
|
|
44
|
+
color: ['color-text', 'icon-color', 'status-color'],
|
|
45
|
+
'column-gap': ['space'],
|
|
46
|
+
'column-width': [],
|
|
47
|
+
fill: ['color-text', 'status-color'],
|
|
48
|
+
font: ['font-family', 'font-size', 'font-weight'],
|
|
49
|
+
'font-family': ['font-family'],
|
|
50
|
+
'font-size': ['font-size'],
|
|
51
|
+
'font-weight': ['font-weight'],
|
|
52
|
+
gap: ['space'],
|
|
53
|
+
height: ['icon-size'],
|
|
54
|
+
inset: [],
|
|
55
|
+
left: [],
|
|
56
|
+
'letter-spacing': ['letter-spacing'],
|
|
57
|
+
'line-height': ['line-height'],
|
|
58
|
+
margin: ['space'],
|
|
59
|
+
'margin-bottom': ['space'],
|
|
60
|
+
'margin-left': ['space'],
|
|
61
|
+
'margin-right': ['space'],
|
|
62
|
+
'margin-top': ['space'],
|
|
63
|
+
'max-height': ['icon-size'],
|
|
64
|
+
'max-width': ['icon-size', 'breakpoint'],
|
|
65
|
+
'min-height': ['icon-size'],
|
|
66
|
+
'min-width': ['icon-size', 'breakpoint'],
|
|
67
|
+
outline: ['border-width', 'color-border'],
|
|
68
|
+
'outline-color': ['color-border'],
|
|
69
|
+
'outline-width': ['border-width'],
|
|
70
|
+
padding: ['space', 'padding'],
|
|
71
|
+
'padding-bottom': ['space', 'padding'],
|
|
72
|
+
'padding-left': ['space', 'padding'],
|
|
73
|
+
'padding-right': ['space', 'padding'],
|
|
74
|
+
'padding-top': ['space', 'padding'],
|
|
75
|
+
right: [],
|
|
76
|
+
'row-gap': ['space'],
|
|
77
|
+
stroke: ['color-text', 'status-color'],
|
|
78
|
+
'text-decoration-color': ['color-text'],
|
|
79
|
+
top: [],
|
|
80
|
+
width: ['icon-size', 'breakpoint'],
|
|
81
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { KONG_TOKEN_PREFIX } from './constants.mjs'
|
|
2
|
+
|
|
3
|
+
// returns an array of unique design tokens found in a CSS value
|
|
4
|
+
export default function(cssValue) {
|
|
5
|
+
const regex = new RegExp(`${KONG_TOKEN_PREFIX}[a-z0-9-]+`, 'gi')
|
|
6
|
+
const matches = cssValue.match(regex)
|
|
7
|
+
|
|
8
|
+
if (!matches) {
|
|
9
|
+
return []
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const uniqueTokens = [...new Set(matches)]
|
|
13
|
+
|
|
14
|
+
return uniqueTokens
|
|
15
|
+
}
|