Package Exports
- stylelint-declaration-strict-value
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stylelint-declaration-strict-value) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Table of Contents
stylelint-declaration-strict-value
A stylelint plugin that enforces either variables ($sass, namespace.$sass, @less, var(--cssnext)), functions or custom CSS keywords (inherit, none, etc.) for property's values.
Installation
npm install stylelint-declaration-strict-valueUsage
Add it to your stylelint config plugins array, then add "declaration-strict-value" to your rules,
specifying the property for which you want to check the usage of variables, functions or keywords.
Like so:
// .stylelintrc
{
"plugins": [
"stylelint-declaration-strict-value"
],
"rules": {
// ...
"scale-unlimited/declaration-strict-value": "color",
// ...
}
}The following patterns are considered warnings:
a { color: #FFF; }
a { color: inherit; }
a { color: currentColor; }The following patterns are not considered warnings:
a { color: var(--color-white); }
a { color: -var(--color-white); }
a { color: color(red alpha(-10%)); }a { color: @color-white; }
a { color: -@color-white; }
a { color: darken(#fff, 10%); }a { color: $color-white; }
a { color: namespace.$color-white; }
a { color: -$color-white; }
a { color: darken(#fff, 10%); }Scheme
The config scheme looks as follows:
[
// primary options
"string" || "/RegExp/" || ["string", "/RegExp/" /* ... */],
// secondary options (optional)
{
ignoreVariables: true || false,
ignoreFunctions: true || false,
ignoreKeywords: "string" ||
["string", "string", /* ... */] ||
{
// match all
"": "string" || ["string", /* ... */],
// match specific prop
"color": "string" || ["string", /* ... */],
},
autoFixFunc: './auto-fix-func.js' || function() {},
disableFix: true || false,
message: "Custom expected ${types} for \"${value}\" of \"${property}\"",
}
]Primary Options
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/.
Multiple Properties
Multiple properties can be linted by passing as an array. Regex can also be used inside arrays.
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/", "z-index", "font-size"]
],
// ...
}The following patterns are considered warnings:
a {
color: #FFF;
z-index: 1;
font-size: 20px;
}
a {
color: inherit;
z-index: auto;
font-size: inherit;
}The following patterns are not considered warnings:
a {
color: var(--color-white);
z-index: var(--a-z-index);
font-size: var(--a-font-size);
}a {
color: @color-white;
z-index: @a-z-index;
font-size: @a-font-size;
}a {
color: $color-white;
z-index: $a-z-index;
font-size: $a-font-size;
line-height: namespace.$line-height;
}Note: Multiple Properties require you to use nested arrays [[]] in your configuration.
Regex support
Passing a regex will lint the variable usage for all matching properties, like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": "/color/",
// ...
}Note for JSON / YAML: Regex needs to be activated by surrounding / slashes.
The following patterns are considered warnings:
a {
color: #FFF;
background-color: #FFF;
border-color: #FFF;
}
a {
color: inherit;
background-color: inherit;
border-color: inherit;
}The following patterns are not considered warnings:
a {
color: var(--color-white);
background-color: var(--color-white);
border-color: var(--color-white);
}a {
color: @color-white;
background-color: @color-white;
border-color: @color-white;
}a {
color: $color-white;
background-color: $color-white;
border-color: $color-white;
}
a {
color: namespace.$color-white;
background-color: namespace.$color-white;
border-color: namespace.$color-white;
}Secondary Options
Additionally you can pass an optional second options hash to enable/disable variables, functions and custom keywords.
The default config is:
// defaults
{
ignoreVariables: true,
ignoreFunctions: true,
ignoreKeywords: null,
}ignoreVariables
Variables can be enabled or disabled, like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": ["/color/", {
ignoreVariables: false,
}],
// ...
}The following patterns are considered warnings:
a { color: var(--color-white); }a { color: @color-white; }a { color: $color-white; }
a { color: namespace.$color-white; }The following patterns are not considered warnings:
a { color: color(red alpha(-10%)); }a { color: darken(#fff, 10%); }a { color: darken(#fff, 10%); }ignoreFunctions
Functions can be enabled or disabled, like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": ["/color/", {
ignoreFunctions: false,
}],
// ...
}The following patterns are considered warnings:
a { color: color(red alpha(-10%)); }a { color: darken(#fff, 10%); }a { color: darken(#fff, 10%); }The following patterns are not considered warnings:
a { color: var(--color-white); }a { color: @color-white; }a { color: $color-white; }
a { color: namespace.$color-white; }ignoreKeywords
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.
Simple single keyword
To ignore a single keyword for all properties simply use a "string", like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": ["/color/", {
ignoreKeywords: "currentColor",
}],
// ...
}The following patterns are considered warnings:
a {
color: #FFF;
background-color: #FFF;
border-color: #FFF;
}
a {
color: inherit;
background-color: inherit;
border-color: inherit;
}The following patterns are not considered warnings:
a {
color: currentColor;
background-color: currentColor;
border-color: currentColor;
}Or with multiple properties:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/", "fill", "stroke"], {
ignoreKeywords: "currentColor",
}],
// ...
}The following patterns are considered warnings:
a {
color: #FFF;
background-color: #FFF;
border-color: #FFF;
fill: #FFF;
stroke: #FFF;
}
a {
color: inherit;
background-color: inherit;
border-color: inherit;
fill: inherit;
stroke: inherit;
}The following patterns are not considered warnings:
a {
color: currentColor;
background-color: currentColor;
border-color: currentColor;
fill: currentColor;
stroke: currentColor;
}List of keywords
To ignore a list of keywords for all properties simply use an [array], like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": ["/color/", {
ignoreKeywords: ["currentColor", "transparent", "inherit"],
}],
// ...
}The following patterns are considered warnings:
a {
color: #FFF;
background-color: #FFF;
border-color: #FFF;
}The following patterns are not considered warnings:
a {
color: currentColor;
background-color: currentColor;
border-color: currentColor;
}
a {
color: transparent;
background-color: transparent;
border-color: transparent;
}
a {
color: inherit;
background-color: inherit;
border-color: inherit;
}Or with multiple properties:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/", "fill", "stroke"], {
ignoreKeywords: ["currentColor", "transparent", "inherit"],
}],
// ...
}The following patterns are considered warnings:
a {
color: #FFF;
background-color: #FFF;
border-color: #FFF;
fill: #FFF;
stroke: #FFF;
}The following patterns are not considered warnings:
a {
color: currentColor;
background-color: currentColor;
border-color: currentColor;
fill: currentColor;
stroke: currentColor;
}
a {
color: transparent;
background-color: transparent;
border-color: transparent;
fill: transparent;
stroke: transparent;
}
a {
color: inherit;
background-color: inherit;
border-color: inherit;
fill: inherit;
stroke: inherit;
}Complex Mighty Hash Mapping
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:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/", "fill", "stroke"], {
ignoreKeywords: {
"/color/": ["currentColor", "transparent", "inherit"],
"fill": ["currentColor", "inherit"],
"stroke": "currentColor",
"z-index": 0,
},
}],
// ...
}The following patterns are considered warnings:
a {
color: #FFF;
background-color: #FFF;
border-color: #FFF;
fill: #FFF;
stroke: #FFF;
z-index: 1;
}
a {
fill: transparent;
stroke: transparent;
}
a {
stroke: inherit;
}The following patterns are not considered warnings:
a {
color: currentColor;
background-color: currentColor;
border-color: currentColor;
fill: currentColor;
stroke: currentColor;
z-index: 0;
}
a {
color: transparent;
background-color: transparent;
border-color: transparent;
}
a {
color: inherit;
background-color: inherit;
border-color: inherit;
fill: inherit;
}Note In case you still want to define a default list of allowed keywords, you can with the empty "" string property name, like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/", "fill", "stroke"], {
ignoreKeywords: {
// default, for all
"": ["currentColor"],
// specific mapping
"/color/": ["currentColor", "transparent", "inherit"],
"fill": ["currentColor", "inherit"],
},
}],
// ...
}message
You can provide your custom message string, it will interpolate the ${types}, ${value} and ${property} placeholders, like:
// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/", "fill", "stroke"], {
message: "Custom expected ${types} for \"${value}\" of \"${property}\"",
}],
// ...
}Autofix support
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, validKeyword }, which tells you which aspect of the rule failed validation.
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;
// .stylelintrc.js
function autoFixFunc(node, validation, root, config) {
const { value, prop } = node
if (prop === 'color') {
switch (value) {
case '#fff':
// auto-fix by returned value
return '$color-white'
case 'red':
// auto-fix by PostCSS AST tranformation
node.value = '$color-red'
}
}
}
module.exports = {
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/"], {
autoFixFunc: autoFixFunc,
disableFix: true | false,
}],
// ...
}
}Or:
// ./auto-fix-func.js
function autoFixFunc(node, validation, root, config) {
const { value, prop } = node
if (prop === 'color') {
switch (value) {
case '#fff':
// auto-fix by returned value
return '$color-white'
case 'red':
// auto-fix by PostCSS AST tranformation
node.value = '$color-red'
}
}
}
module.exports = autoFixFunc// .stylelintrc
"rules": {
// ...
"scale-unlimited/declaration-strict-value": [
["/color/"], {
autoFixFunc: './auto-fix-func.js',
disableFix: true | false,
}],
// ...
}Credit / Inspiration
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>
License
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.