Gathering detailed insights and metrics for @ronilaukkarinen/stylelint-declaration-strict-value
Gathering detailed insights and metrics for @ronilaukkarinen/stylelint-declaration-strict-value
Gathering detailed insights and metrics for @ronilaukkarinen/stylelint-declaration-strict-value
Gathering detailed insights and metrics for @ronilaukkarinen/stylelint-declaration-strict-value
npm install @ronilaukkarinen/stylelint-declaration-strict-value
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
135 Stars
537 Commits
10 Forks
3 Watching
13 Branches
3 Contributors
Updated on 28 Nov 2024
JavaScript (63.24%)
TypeScript (36.58%)
Shell (0.18%)
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-66.7%
20
Compared to previous week
Last month
-41.6%
132
Compared to previous month
Last year
-27%
1,392
Compared to previous year
2
1
39
A stylelint plugin that enforces either variables ($sass
, namespace.$sass
, @less
, var(--cssnext)
, css-loader @value
), functions or custom CSS values, like keywords (inherit
, none
, currentColor
etc.), colors (#fff
, red
, etc.) or numbers incl. units (0
, 1px
, 100%
, etc.) for CSS longhand and experimental shorthand properties.
Table of Contents
1npm install stylelint-declaration-strict-value
Add it to your stylelint config plugins
array, then add "scale-unlimited/declaration-strict-value"
to your rules,
specifying the property for which you want to check the usage of variables, functions, keywords or values.
Like so:
1// .stylelintrc 2{ 3 "plugins": [ 4 "stylelint-declaration-strict-value" 5 ], 6 "rules": { 7 // ... 8 "scale-unlimited/declaration-strict-value": "color", 9 // ... 10 } 11}
The following patterns are considered warnings:
1a { color: #FFF; } 2 3a { color: inherit; } 4 5a { color: currentColor; }
The following patterns are not considered warnings:
1a { color: var(--color-white); } 2 3a { color: -var(--color-white); } 4 5a { color: color(red alpha(-10%)); }
1a { color: @color-white; } 2 3a { color: -@color-white; } 4 5a { color: darken(#fff, 10%); }
1a { color: $color-white; } 2 3a { color: namespace.$color-white; } 4 5a { color: -$color-white; } 6 7a { color: darken(#fff, 10%); }
1@value v-color-white: #fff; 2 3a { color: v-color-white; } 4 5a { color: -v-color-white; }
The config scheme looks as follows:
1[ 2 // primary options 3 "string" || "/RegExp/[gimsuy]" || ["string", "/RegExp/[gimsuy]" /* ... */], 4 5 // secondary options (optional) 6 { 7 ignoreVariables: true || false || { "color": true || false }, 8 ignoreFunctions: true || false || { "color": true || false }, 9 ignoreValues: "string" || "/RegExp/[gimsuy]" || 10 ["string", "/RegExp/[gimsuy]", /* ... */] || 11 { 12 // match all 13 "": "string" || "/RegExp/[gimsuy]" || ["string", "/RegExp/[gimsuy]", /* ... */], 14 15 // match specific prop 16 "color": "string" || "/RegExp/[gimsuy]" || ["string", "/RegExp/[gimsuy]", /* ... */], 17 18 // match pattern prop 19 "/RegExp/[gimsuy]": "string" || "/RegExp/[gimsuy]" || ["string", "/RegExp/[gimsuy]", /* ... */], 20 }, 21 // DEPRECATED (use ignoreValues) 22 ignoreKeywords: "string" || 23 ["string", "string", /* ... */] || 24 { 25 // match all 26 "": "string" || ["string", /* ... */], 27 28 // match specific prop 29 "color": "string" || ["string", /* ... */], 30 31 // match pattern prop 32 "/RegExp/[gimsuy]": "string" || ["string", /* ... */], 33 }, 34 expandShorthand: true || false, 35 recurseLonghand: true || false, 36 autoFixFunc: './auto-fix-func.js' || function() {}, 37 disableFix: true || false, 38 message: "Custom expected ${types} for \"${value}\" of \"${property}\"", 39 } 40]
Primary options represent either a single property or a list of multiple properties to check. Technically it's either a "string"
or an [array]
of simple strings or /RegExp/[gimsuy]
.
Multiple properties can be linted by passing as an array. Regex can also be used inside arrays.
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "z-index", "font-size"] 6 ], 7 // ... 8}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 z-index: 1; 4 font-size: 20px; 5 } 6 7a { 8 color: inherit; 9 z-index: auto; 10 font-size: inherit; 11 }
The following patterns are not considered warnings:
1a { 2 color: var(--color-white); 3 z-index: var(--a-z-index); 4 font-size: var(--a-font-size); 5}
1a { 2 color: @color-white; 3 z-index: @a-z-index; 4 font-size: @a-font-size; 5}
1a { 2 color: $color-white; 3 z-index: $a-z-index; 4 font-size: $a-font-size; 5 line-height: namespace.$line-height; 6}
1@value v-color-white: #fff; 2@value v-z-index: 123; 3@value v-font-size: 2em; 4 5a { 6 color: v-color-white; 7 z-index: v-z-index; 8 font-size: v-font-size; 9}
Note: Multiple Properties require you to use nested arrays [[]]
in your configuration.
Passing a regex will lint the variable usage for all matching properties, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": "/color$/", 5 // ... 6}
Note for JSON / YAML: Regex needs to be activated by surrounding /
slashes.
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5} 6 7a { 8 color: inherit; 9 background-color: inherit; 10 border-color: inherit; 11}
The following patterns are not considered warnings:
1a { 2 color: var(--color-white); 3 background-color: var(--color-white); 4 border-color: var(--color-white); 5}
1a { 2 color: @color-white; 3 background-color: @color-white; 4 border-color: @color-white; 5}
1a { 2 color: $color-white; 3 background-color: $color-white; 4 border-color: $color-white; 5} 6 7a { 8 color: namespace.$color-white; 9 background-color: namespace.$color-white; 10 border-color: namespace.$color-white; 11}
1@value v-color-white: #fff; 2 3a { 4 color: v-color-white; 5 background-color: v-color-white; 6 border-color: v-color-white; 7}
Additionally you can pass an optional second options hash to enable/disable variables, functions and custom keywords.
The default config is:
1// defaults 2{ 3 ignoreVariables: true, 4 ignoreFunctions: true, 5 ignoreValues: null, 6 // DEPRECATED (use ignoreValues) 7 ignoreKeywords: null, 8}
Variables can be enabled or disabled, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreVariables: false, 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { color: var(--color-white); }
1a { color: @color-white; }
1a { color: $color-white; } 2 3a { color: namespace.$color-white; }
1@value v-color-white: #fff; 2 3a { color: v-color-white; }
The following patterns are not considered warnings:
1a { color: color(red alpha(-10%)); }
1a { color: darken(#fff, 10%); }
1a { color: darken(#fff, 10%); }
Variables can be enabled for all props, except those listed in a hash, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [["/color$/", "margin"], { 5 ignoreVariables: { 6 "margin": false, 7 }, 8 }], 9 // ... 10}
The following patterns are considered warnings:
1a { margin: var(--margin); }
1a { margin: @margin; }
1a { margin: $margin; } 2 3a { margin: namespace.$margin; }
1@value v-margin: 10px; 2 3a { margin: v-margin; }
The following patterns are not considered warnings:
1a { color: var(--color-white); }
1a { color: @color-white; }
1a { color: $color-white; } 2 3a { color: namespace.$color-white; }
1@value v-color-white: #fff; 2 3a { color: v-color-white; }
Functions can be enabled or disabled, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreFunctions: false, 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { color: color(red alpha(-10%)); }
1a { color: darken(#fff, 10%); }
1a { color: darken(#fff, 10%); }
The following patterns are not considered warnings:
1a { color: var(--color-white); }
1a { color: @color-white; }
1a { color: $color-white; } 2 3a { color: namespace.$color-white; }
Functions can be enabled for all props, except those listed in a hash, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [["/color$/", "margin"], { 5 ignoreFunctions: { 6 "margin": false, 7 }, 8 }], 9 // ... 10}
The following patterns are considered warnings:
1a { margin: calc(10% - 5px); }
1a { margin: ceil(4.9); }
1a { margin: math.ceil(4.9); }
The following patterns are not considered warnings:
1a { color: color(red alpha(-10%)); }
1a { color: darken(#fff, 10%); }
1a { color: darken(#fff, 10%); }
This allows you to ignore any CSS value like:
currentColor
, inherit
, transparent
, etc.0
, 10px
, 1em
, 100%
, etc.#fff
, #FFFFFF
, red
, etc.This configuration can either be a simple "string"
, number
, "/RegExp/[gimsuy]"
, an [array]
of "strings"
, numbers
, "/RegExp/[gimsuy]"
or a complex hash of property/keyword mappings.
To ignore a single value
for all properties simply use a "string"
or "/RegExp/[gimsuy]"
, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreValues: "currentColor", 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5} 6 7a { 8 color: inherit; 9 background-color: inherit; 10 border-color: inherit; 11}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5}
Or with a "/RegExp/[gimsuy]"
for hex colors:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreValues: "/^#[0-9a-fA-F]{3,6}$/", 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5} 6 7a { 8 color: inherit; 9 background-color: inherit; 10 border-color: inherit; 11}
The following patterns are not considered warnings:
1a { 2 color: #fff; 3 background-color: #FFF; 4 border-color: #FFFFFF; 5}
Or with multiple properties:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreValues: "currentColor", 7 }], 8 // ... 9}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5 fill: #FFF; 6 stroke: #FFF; 7} 8 9a { 10 color: inherit; 11 background-color: inherit; 12 border-color: inherit; 13 fill: inherit; 14 stroke: inherit; 15}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5 fill: currentColor; 6 stroke: currentColor; 7}
To ignore a list of values
for all properties simply use an [array]
, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreValues: ["currentColor", "/^#[0-9a-fA-F]{3,6}$/", "inherit"], 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { 2 color: transparent; 3 background-color: transparent; 4 border-color: transparent; 5}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5} 6 7a { 8 color: #FFF; 9 background-color: #FFF; 10 border-color: #FFF; 11} 12 13a { 14 color: inherit; 15 background-color: inherit; 16 border-color: inherit; 17}
Or with multiple properties:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreValues: ["currentColor", "/^#[0-9a-fA-F]{3,6}$/", "inherit"], 7 }], 8 // ... 9}
The following patterns are considered warnings:
1a { 2 color: transparent; 3 background-color: transparent; 4 border-color: transparent; 5 fill: transparent; 6 stroke: transparent; 7}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5 fill: currentColor; 6 stroke: currentColor; 7} 8 9a { 10 color: #FFF; 11 background-color: #FFF; 12 border-color: #FFF; 13 fill: #FFF; 14 stroke: #FFF; 15} 16 17a { 18 color: inherit; 19 background-color: inherit; 20 border-color: inherit; 21 fill: inherit; 22 stroke: inherit; 23}
You may noticed that the above methods do count for all properties. In case you wish more sophisticated control {hash}
based configs is the right choice for you.
The basic principle works the same as above - you either have one value or a list of values. This time you can define them for each property separately, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreValues: { 7 "/color$/": ["currentColor", "/^#[0-9a-fA-F]{3,6}$/", "inherit"], 8 "fill": ["currentColor", "inherit"], 9 "stroke": "currentColor", 10 "z-index": "/^\\d+$/", 11 }, 12 }], 13 // ... 14}
The following patterns are considered warnings:
1a { 2 color: transparent; 3 background-color: transparent; 4 border-color: transparent; 5 fill: #FFF; 6 stroke: #FFF; 7 z-index: inherit; 8} 9 10a { 11 fill: transparent; 12 stroke: transparent; 13} 14 15a { 16 stroke: inherit; 17}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5 fill: currentColor; 6 stroke: currentColor; 7 z-index: 0; 8} 9 10a { 11 color: #FFF; 12 background-color: #FFF; 13 border-color: #FFF; 14 z-index: 1; 15} 16 17a { 18 color: inherit; 19 background-color: inherit; 20 border-color: inherit; 21 fill: inherit; 22 z-index: 1000; 23}
Note In case you still want to define a default list of allowed values, you can with the empty ""
string property name, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreValues: { 7 // default, for all 8 "": ["currentColor"], 9 10 // specific mapping 11 "/color$/": ["currentColor", "transparent", "inherit"], 12 "fill": ["currentColor", "inherit"], 13 }, 14 }], 15 // ... 16}
DEPRECATED: This allows you to ignore several CSS keywords like currentColor
, inherit
, transparent
, etc.
Note: for convenience also non-standard-keywords like 0
can be specified.
This configuration can either be a simple "string"
, number
, an [array]
of "strings"
, numbers
or a complex hash of property/keyword mappings.
To ignore a single keyword
for all properties simply use a "string"
, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreKeywords: "currentColor", 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5} 6 7a { 8 color: inherit; 9 background-color: inherit; 10 border-color: inherit; 11}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5}
Or with multiple properties:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreKeywords: "currentColor", 7 }], 8 // ... 9}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5 fill: #FFF; 6 stroke: #FFF; 7} 8 9a { 10 color: inherit; 11 background-color: inherit; 12 border-color: inherit; 13 fill: inherit; 14 stroke: inherit; 15}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5 fill: currentColor; 6 stroke: currentColor; 7}
To ignore a list of keywords
for all properties simply use an [array]
, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": ["/color$/", { 5 ignoreKeywords: ["currentColor", "transparent", "inherit"], 6 }], 7 // ... 8}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5} 6 7a { 8 color: transparent; 9 background-color: transparent; 10 border-color: transparent; 11} 12 13a { 14 color: inherit; 15 background-color: inherit; 16 border-color: inherit; 17}
Or with multiple properties:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreKeywords: ["currentColor", "transparent", "inherit"], 7 }], 8 // ... 9}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5 fill: #FFF; 6 stroke: #FFF; 7}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5 fill: currentColor; 6 stroke: currentColor; 7} 8 9a { 10 color: transparent; 11 background-color: transparent; 12 border-color: transparent; 13 fill: transparent; 14 stroke: transparent; 15} 16 17a { 18 color: inherit; 19 background-color: inherit; 20 border-color: inherit; 21 fill: inherit; 22 stroke: inherit; 23}
You may noticed that the above methods do count for all properties. In case you wish more sophisticated control {hash}
based configs is the right choice for you.
The basic principle works the same as above - you either have one keyword or a list of keywords. This time you can define them for each property separately, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreKeywords: { 7 "/color$/": ["currentColor", "transparent", "inherit"], 8 "fill": ["currentColor", "inherit"], 9 "stroke": "currentColor", 10 "z-index": 0, 11 }, 12 }], 13 // ... 14}
The following patterns are considered warnings:
1a { 2 color: #FFF; 3 background-color: #FFF; 4 border-color: #FFF; 5 fill: #FFF; 6 stroke: #FFF; 7 z-index: 1; 8} 9 10a { 11 fill: transparent; 12 stroke: transparent; 13} 14 15a { 16 stroke: inherit; 17}
The following patterns are not considered warnings:
1a { 2 color: currentColor; 3 background-color: currentColor; 4 border-color: currentColor; 5 fill: currentColor; 6 stroke: currentColor; 7 z-index: 0; 8} 9 10a { 11 color: transparent; 12 background-color: transparent; 13 border-color: transparent; 14} 15 16a { 17 color: inherit; 18 background-color: inherit; 19 border-color: inherit; 20 fill: inherit; 21}
Note In case you still want to define a default list of allowed keywords, you can with the empty ""
string property name, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 ignoreKeywords: { 7 // default, for all 8 "": ["currentColor"], 9 10 // specific mapping 11 "/color$/": ["currentColor", "transparent", "inherit"], 12 "fill": ["currentColor", "inherit"], 13 }, 14 }], 15 // ... 16}
CSS shorthand Syntax support is disabled by default.
This feature is baked by shortcss
and css-values
, a list of supported shorthand properties can be found at css-shorthand-properties
and a list of validated CSS property/value pairs at property-support
.
How this works is:
shortcss
is not 100% standard conform, i.e. assigning correct default values if you omit some longhands like text-decoration-color
.css-values
is performed for each expanded longhand property to avoid wrong linting errorsWarning: How do you determine, which longhand property is used by using a single shorthand? Like in this example:
1a { 2 background: white; // this is background-color 3 background: url('image.png'); // this is background-image 4 5 text-decoration: none; // this is text-decoration-style 6 text-decoration: #fff; // this is text-decoration-color 7}
Differentiating this would need a CSS value parser and a curated list of valid CSS values per prop.
This is beyond the scope of shortcss
.
All properties that are expanded right now, can be found here: https://github.com/gilmoreorless/css-shorthand-properties/blob/master/index.js
And it becomes even trickier if you consider dynamic values such as variables or functions, like:
1 background: get-bg(); // what does this function return? a longhand, or shorthand value? 2 3 text-decoration: $text-deco; // what is behind this var? is it a longhand, or shorthand value? 4 text-decoration: @text-deco; // what is behind this var? is it a longhand, or shorthand value? 5}
If true
and if no exact property match is found, shorthand CSS properties will be expanded and checked againtst your primary options.
Let's say you want to check border-color
.
You can either configure a direct longhand CSS property to match.
1// .stylelintrc 2{ 3 "plugins": [ 4 "stylelint-declaration-strict-value" 5 ], 6 "rules": { 7 // ... 8 "scale-unlimited/declaration-strict-value": ["border-color", { "expandShorthand": true }] 9 // ... 10 } 11}
The following patterns are considered warnings:
1a { border: 1px solid #FFF; } 2 3a { border: 1px solid inherit; } 4 5a { border: 1px solid currentColor; }
The following patterns are not considered warnings:
1a { border: 1px solid var(--color-white); } 2 3a { border: 1px solid -var(--color-white); } 4 5a { border: 1px solid color(red alpha(-10%)); }
1a { border: 1px solid @color-white; } 2 3a { border: 1px solid -@color-white; } 4 5a { border: 1px solid darken(#fff, 10%); }
1a { border: 1px solid $color-white; } 2 3a { border: 1px solid namespace.$color-white; } 4 5a { border: 1px solid -$color-white; } 6 7a { border: 1px solid darken(#fff, 10%); }
Or a simple regex for /color$/
.
1// .stylelintrc 2{ 3 "plugins": [ 4 "stylelint-declaration-strict-value" 5 ], 6 "rules": { 7 // ... 8 "scale-unlimited/declaration-strict-value": ["/color$/", { "expandShorthand": true }] 9 // ... 10 } 11}
The following patterns are considered warnings:
1a { border: 1px solid #FFF; } 2 3a { border: 1px solid inherit; } 4 5a { border: 1px solid currentColor; } 6 7a { color: #FFF; } 8 9a { color: inherit; } 10 11a { color: currentColor; }
The following patterns are not considered warnings:
1a { border: 1px solid var(--color-white); } 2 3a { border: 1px solid -var(--color-white); } 4 5a { border: 1px solid color(red alpha(-10%)); } 6 7a { color: var(--color-white); } 8 9a { color: -var(--color-white); } 10 11a { color: color(red alpha(-10%)); }
1a { border: 1px solid @color-white; } 2 3a { border: 1px solid -@color-white; } 4 5a { border: 1px solid darken(#fff, 10%); } 6 7a { color: @color-white; } 8 9a { color: -@color-white; } 10 11a { color: darken(#fff, 10%); }
1a { border: 1px solid $color-white; } 2 3a { border: 1px solid namespace.$color-white; } 4 5a { border: 1px solid -$color-white; } 6 7a { border: 1px solid darken(#fff, 10%); } 8 9a { color: $color-white; } 10 11a { color: namespace.$color-white; } 12 13a { color: -$color-white; } 14 15a { color: darken(#fff, 10%); }
If true
, each longhand property will also be expanded. This is only useful for the border
property.
You can provide your custom message
string, it will interpolate the ${types}
, ${value}
and ${property}
placeholders, like:
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/", "fill", "stroke"], { 6 "message": "Custom expected ${types} for \"${value}\" of \"${property}\"", 7 }], 8 // ... 9}
This plugin supports configurable autofixing enabled by --fix
option.
Important: it's up to you to specify how autofixing should take place, this is because this plugin has to deal with dynamic values not static ones (which are predictable and very easy to autofix).
So you have to supply an autoFixFunc
function and implement each fix you want by yourself. To help you with that this function receives the whole PostCSS API, all validations and configuration of this plugin, as follows node
, validation
, root
and config
.
validation
is a hash of { validVar, validFunc, validValue, validKeyword, longhandProp, longhandValue }
, which tells you which aspect of the rule failed validation.
You may throw
errors if autofixing is not possible.
Note: it's best you use a JavaScript based config file, which is easy because Stylelint utilizes cosmiconfig.
Alternatively you can specify a common JS module, which will be resolved by standard require
calls including support for CWD
.
You can also disable autofixing by setting disableFix
to true
;
1// .stylelintrc.js 2function autoFixFunc(node, validation, root, config) { 3 const { value, prop } = node 4 5 if (prop === 'color') { 6 switch (value) { 7 case '#fff': 8 // auto-fix by returned value 9 return '$color-white' 10 11 case 'red': 12 // auto-fix by PostCSS AST tranformation 13 node.value = '$color-red' 14 15 default: 16 // optional, you can throw your own error message if the value is not stated or handled, ex: color: blue 17 throw `Property ${prop} with value ${value} can't be autofixed!` 18 // or an Error object 19 throw new Error(`Property ${prop} with value ${value} can't be autofixed!`) 20 // or a falsy value to use the default error message 21 throw null; 22 } 23 } 24} 25 26module.exports = { 27 "rules": { 28 // ... 29 "scale-unlimited/declaration-strict-value": [ 30 ["/color$/"], { 31 autoFixFunc: autoFixFunc, 32 disableFix: true | false, 33 }], 34 // ... 35 } 36}
Or:
1// ./auto-fix-func.js 2function autoFixFunc(node, validation, root, config) { 3 const { value, prop } = node 4 5 if (prop === 'color') { 6 switch (value) { 7 case '#fff': 8 // auto-fix by returned value 9 return '$color-white' 10 11 case 'red': 12 // auto-fix by PostCSS AST tranformation 13 node.value = '$color-red' 14 } 15 } 16} 17 18module.exports = autoFixFunc
1// .stylelintrc 2"rules": { 3 // ... 4 "scale-unlimited/declaration-strict-value": [ 5 ["/color$/"], { 6 autoFixFunc: './auto-fix-func.js', 7 disableFix: true | false, 8 }], 9 // ... 10 }
Please find the API docs at ./docs/README.md
.
This package was mainly inspired by stylelint-declaration-use-variable
.
Originally I planned to contribute, but as I faced more issues I decided to write my own from scratch based on ES6
.
Proudly brought to you by <scale-unlimited>
The MIT License (MIT)
Copyright (c) 2016 Andreas Deuschlinger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
26 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More