GeezerVison is showing github.com — Change `ignore*` for `value-type-custom-property-required-list` (#9626) · stylelint/stylelint@9040322
File treeExpand file treeCollapse file tree Open diff view settings declaration-property-value-no-unknown value-type-custom-property-required-list
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .changeset/pre/public-birds-join.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | "stylelint": minor | 3 | 3 | --- | 4 | 4 |
| 5 | | -Added: `value-type-custom-property-required-list` rule | | 5 | +Added: experimental `value-type-custom-property-required-list` rule |
Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ This is a prerelease. Help us test the next major version: `npm i -D stylelint@n | 23 | 23 | - Added: `result.stylelint.postcss` property for plugins ([#9594](https://github.com/stylelint/stylelint/pull/9594)) ([@jeddy3](https://github.com/jeddy3)). | 24 | 24 | - Added: `stylelint.version` property ([#9595](https://github.com/stylelint/stylelint/pull/9595)) ([@ybiquitous](https://github.com/ybiquitous)). | 25 | 25 | - Added: `value-no-invalid` rule ([#9507](https://github.com/stylelint/stylelint/pull/9507)) ([@jeddy3](https://github.com/jeddy3)). | 26 | | -- Added: `value-type-custom-property-required-list` rule ([#9572](https://github.com/stylelint/stylelint/pull/9572)) ([@jeddy3](https://github.com/jeddy3)). | | 26 | +- Added: experimental `value-type-custom-property-required-list` rule ([#9572](https://github.com/stylelint/stylelint/pull/9572)) ([@jeddy3](https://github.com/jeddy3)). | 27 | 27 | - Added: stable support for `processors` configuration property ([#9543](https://github.com/stylelint/stylelint/pull/9543)) ([@jeddy3](https://github.com/jeddy3)). | 28 | 28 | - Added: stable support for `referenceFiles` configuration property ([#9598](https://github.com/stylelint/stylelint/pull/9598)) ([@jeddy3](https://github.com/jeddy3)). | 29 | 29 | - Fixed: `--fix` for files where the only change is the removal of a byte order mark ([#9512](https://github.com/stylelint/stylelint/pull/9512)) ([@firefoxic](https://github.com/firefoxic)). | |
Collapse file Copy file name to clipboardExpand all lines: docs/about/semantic-versioning.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,7 @@ Might break your lint build: | 25 | 25 | - a new public API capability is created | 26 | 26 | - a new formatter is created | 27 | 27 | - a new deprecation is introduced | | 28 | +- a feature documented as experimental is changed in an incompatible way | 28 | 29 |
| 29 | 30 | ## Major release | 30 | 31 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/migration-guide/to-18.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,7 @@ You can use the [`referenceFiles`](../user-guide/configure.md#referencefiles) co | 59 | 59 |
| 60 | 60 | ### Added `declaration-property-custom-property-allowed-list` and `value-type-custom-property-required-list` rules | 61 | 61 |
| 62 | | -We've added the [`declaration-property-custom-property-allowed-list`](../../lib/rules/declaration-property-custom-property-allowed-list/README.md) rule to specify which custom properties are allowed in which properties, and the [`value-type-custom-property-required-list`](../../lib/rules/value-type-custom-property-required-list/README.md) rule to specify which value types require custom properties. | | 62 | +We've added the [`declaration-property-custom-property-allowed-list`](../../lib/rules/declaration-property-custom-property-allowed-list/README.md) rule to specify which custom properties are allowed in which properties, and the experimental [`value-type-custom-property-required-list`](../../lib/rules/value-type-custom-property-required-list/README.md) rule to specify which value types require custom properties. | 63 | 63 |
| 64 | 64 | For example, to allow only `--spacing-*` custom properties in `margin` and `padding` properties and only `--radii-*` in `border-radius`, and to require custom properties for `<length>` values: | 65 | 65 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/user-guide/customize.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,7 @@ You can combine the [`referenceFiles`](configure.md#referencefiles) configuratio | 59 | 59 | + }, | 60 | 60 | + "value-type-custom-property-required-list": [ | 61 | 61 | + ["color", "length"], | 62 | | -+ { "ignoreProperties": { "/.+/": ["0", "transparent", "currentcolor"] } } | | 62 | ++ { "ignoreValues": ["0", "transparent", "currentcolor"] } | 63 | 63 | + ] | 64 | 64 | + } | 65 | 65 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/rules/declaration-property-value-no-unknown/index.mjs +1-10Lines changed: 1 addition & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,6 +27,7 @@ import isDescriptorDeclaration from '../../utils/isDescriptorDeclaration.mjs'; | 27 | 27 | import isStandardSyntaxDeclaration from '../../utils/isStandardSyntaxDeclaration.mjs'; | 28 | 28 | import isStandardSyntaxProperty from '../../utils/isStandardSyntaxProperty.mjs'; | 29 | 29 | import isStandardSyntaxValue from '../../utils/isStandardSyntaxValue.mjs'; | | 30 | +import isWithinRange from '../../utils/isWithinRange.mjs'; | 30 | 31 | import memoize from '../../utils/memoize.mjs'; | 31 | 32 | import memoizeForReferenceRoots from '../../utils/memoizeForReferenceRoots.mjs'; | 32 | 33 | import optionsMatchesEntry from '../../utils/optionsMatchesEntry.mjs'; | @@ -701,16 +702,6 @@ function getPrefixedPropName(name) { | 701 | 702 | return `-stylelint${name}`; | 702 | 703 | } | 703 | 704 |
| 704 | | -/** | 705 | | - * @param {VarFunctionSubstitution} substitution | 706 | | - * @param {number} start | 707 | | - * @param {number} end | 708 | | - * @returns {boolean} | 709 | | - */ | 710 | | -function isWithinRange(substitution, start, end) { | 711 | | - return start <= substitution.start && substitution.end <= end; | 712 | | -} | 713 | | - | 714 | 705 | /** | 715 | 706 | * @param {import('css-tree').LexerMatchResult['error']} error | 716 | 707 | * @returns {error is import('css-tree').SyntaxMatchError} | |
Collapse file Copy file name to clipboardExpand all lines: lib/rules/value-type-custom-property-required-list/README.md +78-13Lines changed: 78 additions & 13 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,9 @@ a { border: 1px solid red; } | 9 | 9 | * These <length> and <color> values */ | 10 | 10 | ``` | 11 | 11 |
| | 12 | +> [!WARNING] | | 13 | +> This rule is **experimental**, as it's our first whose options take value types. To give feedback on them, [open an issue](https://github.com/stylelint/stylelint/issues/new/choose). | | 14 | +
| 12 | 15 | Use this rule to enforce a convention. For example, require custom properties for: | 13 | 16 |
| 14 | 17 | - `color`, so that colors come from your palette | | 70 | 73 |
| 71 | 74 | ## Optional secondary options | 72 | 75 |
| 73 | | -### `ignoreProperties` | | 76 | +### `ignoreFunctions` | | 77 | + | | 78 | +```json | | 79 | +{ "ignoreFunctions": { "type": ["array", "of", "functions", "/regex/"] } } | | 80 | +``` | | 81 | + | | 82 | +Ignore the parts of a type that are within the specified functions. | | 83 | + | | 84 | +Given: | 74 | 85 |
| 75 | 86 | ```json | 76 | 87 | { | 77 | | - "ignoreProperties": { "property-name": ["array", "of", "values", "/regex/"] } | | 88 | + "value-type-custom-property-required-list": [ | | 89 | + ["color", "number", "angle", "percentage"], | | 90 | + { | | 91 | + "ignoreFunctions": { | | 92 | + "number": ["/.+/"], | | 93 | + "color": ["color-mix"], | | 94 | + "/^(angle|percentage)$/": ["/-gradient$/"] | | 95 | + } | | 96 | + } | | 97 | + ] | 78 | 98 | } | 79 | 99 | ``` | 80 | 100 |
| 81 | | -Ignore the specified property and value pairs. | | 101 | +The following patterns are _not_ considered problems: | | 102 | + | | 103 | +<!-- prettier-ignore --> | | 104 | +```css | | 105 | +a { transform: scale(1.5); } | | 106 | +``` | | 107 | + | | 108 | +<!-- prettier-ignore --> | | 109 | +```css | | 110 | +a { color: color-mix(in srgb, red, blue); } | | 111 | +``` | 82 | 112 |
| 83 | | -Keys in the object indicate property names and values indicate the parts of a value. | | 113 | +<!-- prettier-ignore --> | | 114 | +```css | | 115 | +a { background: linear-gradient(45deg, var(--foo) 0%, var(--bar) 100%); } | | 116 | +``` | | 117 | + | | 118 | +### `ignoreProperties` | | 119 | + | | 120 | +```json | | 121 | +{ "ignoreProperties": { "type": ["array", "of", "properties", "/regex/"] } } | | 122 | +``` | 84 | 123 |
| 85 | | -You can specify a regex for a property name, such as `{ "/.+/": ["transparent"] }`, and for a value, such as `{ "transform": ["/.+/"] }`. | | 124 | +Ignore the parts of a type in the values of the specified properties. | 86 | 125 |
| 87 | 126 | Given: | 88 | 127 |
| 89 | 128 | ```json | 90 | 129 | { | 91 | 130 | "value-type-custom-property-required-list": [ | 92 | 131 | ["color", "length"], | 93 | | - { | 94 | | - "ignoreProperties": { | 95 | | - "/.+/": ["transparent"], | 96 | | - "transform": ["/.+/"] | 97 | | - } | 98 | | - } | | 132 | + { "ignoreProperties": { "/.+/": ["transform"], "length": ["/^border/"] } } | 99 | 133 | ] | 100 | 134 | } | 101 | 135 | ``` | @@ -104,10 +138,41 @@ The following patterns are _not_ considered problems: | 104 | 138 |
| 105 | 139 | <!-- prettier-ignore --> | 106 | 140 | ```css | 107 | | -a { color: transparent; } | | 141 | +a { transform: translateX(1px); } | 108 | 142 | ``` | 109 | 143 |
| 110 | 144 | <!-- prettier-ignore --> | 111 | 145 | ```css | 112 | | -a { transform: translateX(4px); } | | 146 | +a { border-width: 1px; } | | 147 | +``` | | 148 | + | | 149 | +### `ignoreValues` | | 150 | + | | 151 | +```json | | 152 | +{ "ignoreValues": ["array", "of", "values", "/regex/"] } | | 153 | +``` | | 154 | + | | 155 | +Ignore the specified parts of a value. | | 156 | + | | 157 | +Given: | | 158 | + | | 159 | +```json | | 160 | +{ | | 161 | + "value-type-custom-property-required-list": [ | | 162 | + ["color", "number"], | | 163 | + { "ignoreValues": ["0", "transparent"] } | | 164 | + ] | | 165 | +} | | 166 | +``` | | 167 | + | | 168 | +The following patterns are _not_ considered problems: | | 169 | + | | 170 | +<!-- prettier-ignore --> | | 171 | +```css | | 172 | +a { flex: 0; } | | 173 | +``` | | 174 | + | | 175 | +<!-- prettier-ignore --> | | 176 | +```css | | 177 | +a { color: transparent; } | 113 | 178 | ``` |
Collapse file Copy file name to clipboardExpand all lines: lib/rules/value-type-custom-property-required-list/__tests__/index.mjs +127-10Lines changed: 127 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -146,39 +146,142 @@ testRule({ | 146 | 146 | ], | 147 | 147 | }); | 148 | 148 |
| | 149 | +testRule({ | | 150 | + ruleName, | | 151 | + config: [['color', 'number'], { ignoreValues: ['0', 'transparent', '/^oklch\\(/'] }], | | 152 | + | | 153 | + accept: [ | | 154 | + { | | 155 | + code: 'a { flex: 0; }', | | 156 | + }, | | 157 | + { | | 158 | + code: 'a { color: transparent; }', | | 159 | + }, | | 160 | + { | | 161 | + code: 'a { color: oklch(20% 0.1 0deg); }', | | 162 | + }, | | 163 | + ], | | 164 | + | | 165 | + reject: [ | | 166 | + { | | 167 | + code: 'a { color: rgb(0 0 0); }', | | 168 | + message: messages.expected('rgb(0 0 0)', 'color'), | | 169 | + line: 1, | | 170 | + column: 12, | | 171 | + endLine: 1, | | 172 | + endColumn: 22, | | 173 | + }, | | 174 | + { | | 175 | + code: 'a { color: color-mix(in srgb, transparent, red); }', | | 176 | + message: messages.expected('red', 'color'), | | 177 | + line: 1, | | 178 | + column: 44, | | 179 | + endLine: 1, | | 180 | + endColumn: 47, | | 181 | + }, | | 182 | + ], | | 183 | +}); | | 184 | + | 149 | 185 | testRule({ | 150 | 186 | ruleName, | 151 | 187 | config: [ | 152 | | - ['color', 'length'], | 153 | | - { ignoreProperties: { '/.+/': ['transparent'], transform: ['/.+/'] } }, | | 188 | + ['color', 'number', 'angle', 'percentage'], | | 189 | + { | | 190 | + ignoreFunctions: { | | 191 | + number: ['/.+/'], | | 192 | + color: ['color-mix'], | | 193 | + '/^(angle|percentage)$/': ['/-gradient$/'], | | 194 | + }, | | 195 | + }, | 154 | 196 | ], | 155 | 197 |
| 156 | 198 | accept: [ | 157 | 199 | { | 158 | | - code: 'a { color: transparent; }', | | 200 | + code: 'a { transform: scale(1.5); }', | 159 | 201 | }, | | 202 | + { | | 203 | + code: 'a { color: color-mix(in srgb, red, blue); }', | | 204 | + }, | | 205 | + { | | 206 | + code: 'a { --foo: 0; color: rgb(var(--foo) 0 0); }', | | 207 | + description: 'color function with a custom property argument', | | 208 | + }, | | 209 | + { | | 210 | + code: 'a { --foo: red; --bar: blue; background: linear-gradient(45deg, var(--foo) 0%, var(--bar) 100%); }', | | 211 | + }, | | 212 | + ], | | 213 | + | | 214 | + reject: [ | | 215 | + { | | 216 | + code: 'a { opacity: 0.5; }', | | 217 | + message: messages.expected('0.5', 'number'), | | 218 | + line: 1, | | 219 | + column: 14, | | 220 | + endLine: 1, | | 221 | + endColumn: 17, | | 222 | + }, | | 223 | + { | | 224 | + code: 'a { color: rgb(0 0 0); }', | | 225 | + message: messages.expected('rgb(0 0 0)', 'color'), | | 226 | + line: 1, | | 227 | + column: 12, | | 228 | + endLine: 1, | | 229 | + endColumn: 22, | | 230 | + }, | | 231 | + { | | 232 | + code: 'a { background: linear-gradient(45deg, red 0%, blue 100%); }', | | 233 | + warnings: [ | | 234 | + { | | 235 | + message: messages.expected('red', 'color'), | | 236 | + line: 1, | | 237 | + column: 40, | | 238 | + endLine: 1, | | 239 | + endColumn: 43, | | 240 | + }, | | 241 | + { | | 242 | + message: messages.expected('blue', 'color'), | | 243 | + line: 1, | | 244 | + column: 48, | | 245 | + endLine: 1, | | 246 | + endColumn: 52, | | 247 | + }, | | 248 | + ], | | 249 | + }, | | 250 | + ], | | 251 | +}); | | 252 | + | | 253 | +testRule({ | | 254 | + ruleName, | | 255 | + config: [ | | 256 | + ['color', 'length'], | | 257 | + { ignoreProperties: { '/.+/': ['transform'], length: ['/^border/'] } }, | | 258 | + ], | | 259 | + | | 260 | + accept: [ | 160 | 261 | { | 161 | 262 | code: 'a { transform: translateX(1px); }', | 162 | 263 | }, | | 264 | + { | | 265 | + code: 'a { border-width: 1px; }', | | 266 | + }, | 163 | 267 | ], | 164 | 268 |
| 165 | 269 | reject: [ | 166 | 270 | { | 167 | | - code: 'a { border: 1px solid transparent; }', | | 271 | + code: 'a { margin: 1px; }', | 168 | 272 | message: messages.expected('1px', 'length'), | 169 | 273 | line: 1, | 170 | 274 | column: 13, | 171 | 275 | endLine: 1, | 172 | 276 | endColumn: 16, | 173 | 277 | }, | 174 | 278 | { | 175 | | - code: 'a { color: color-mix(in srgb, transparent, red); }', | | 279 | + code: 'a { border: 1px solid red; }', | 176 | 280 | message: messages.expected('red', 'color'), | 177 | 281 | line: 1, | 178 | | - column: 44, | | 282 | + column: 23, | 179 | 283 | endLine: 1, | 180 | | - endColumn: 47, | 181 | | - description: 'color function with an ignored argument', | | 284 | + endColumn: 26, | 182 | 285 | }, | 183 | 286 | ], | 184 | 287 | }); | @@ -191,7 +294,13 @@ testRuleConfigs({ | 191 | 294 | config: 'color', | 192 | 295 | }, | 193 | 296 | { | 194 | | - config: [['color'], { ignoreProperties: { color: ['transparent', /^calc\(/] } }], | | 297 | + config: [['color'], { ignoreValues: ['transparent', /^calc\(/] }], | | 298 | + }, | | 299 | + { | | 300 | + config: [['color'], { ignoreFunctions: { color: ['rgb', /^color-/] } }], | | 301 | + }, | | 302 | + { | | 303 | + config: [['color'], { ignoreProperties: { color: ['border', /^background/] } }], | 195 | 304 | }, | 196 | 305 | ], | 197 | 306 |
| @@ -205,7 +314,15 @@ testRuleConfigs({ | 205 | 314 | description: 'invalid primary option type', | 206 | 315 | }, | 207 | 316 | { | 208 | | - config: [['color'], { ignoreProperties: 1 }], | | 317 | + config: [['color'], { ignoreValues: { color: ['transparent'] } }], | | 318 | + description: 'invalid ignoreValues option type', | | 319 | + }, | | 320 | + { | | 321 | + config: [['color'], { ignoreFunctions: ['rgb'] }], | | 322 | + description: 'invalid ignoreFunctions option type', | | 323 | + }, | | 324 | + { | | 325 | + config: [['color'], { ignoreProperties: ['border'] }], | 209 | 326 | description: 'invalid ignoreProperties option type', | 210 | 327 | }, | 211 | 328 | ], | |
|