stylelint-declaration-strict-value
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -11,3 +11,5 @@ 'use strict'; | ||
severity: 'error', | ||
message: null | ||
message: null, | ||
disableFix: null, | ||
autoFixFunc: null | ||
}; |
33
index.js
@@ -32,2 +32,3 @@ 'use strict'; | ||
var rule = function rule(properties, options) { | ||
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return function (root, result) { | ||
@@ -56,4 +57,7 @@ // validate stylelint plugin options | ||
ignoreKeywords = config.ignoreKeywords, | ||
message = config.message; | ||
message = config.message, | ||
disableFix = config.disableFix, | ||
autoFixFunc = config.autoFixFunc; | ||
var autoFixFuncNormalized = (0, _validation.getAutoFixFunc)(autoFixFunc); | ||
var reKeywords = ignoreKeywords ? {} : null; | ||
@@ -126,11 +130,22 @@ | ||
// support auto fixing | ||
utils.report({ | ||
ruleName: ruleName, | ||
result: result, | ||
node: node, | ||
line: start.line, | ||
column: start.column + prop.length + raws.between.length, | ||
message: messages.expected(types, value, prop, message) | ||
}); | ||
if (context.fix && !disableFix) { | ||
var fixedValue = autoFixFuncNormalized(node, { validVar: validVar, validFunc: validFunc, validKeyword: validKeyword }, root, config); | ||
// apply fixed value if returned | ||
if (fixedValue) { | ||
// eslint-disable-next-line no-param-reassign | ||
node.value = fixedValue; | ||
} | ||
} else { | ||
utils.report({ | ||
ruleName: ruleName, | ||
result: result, | ||
node: node, | ||
line: start.line, | ||
column: start.column + prop.length + raws.between.length, | ||
message: messages.expected(types, value, prop, message) | ||
}); | ||
} | ||
} | ||
@@ -137,0 +152,0 @@ } |
@@ -6,6 +6,11 @@ 'use strict'; | ||
}); | ||
exports.getIgnoredKeywords = exports.getTypes = exports.expected = exports.validOptions = exports.validProperties = undefined; | ||
exports.getAutoFixFunc = exports.getIgnoredKeywords = exports.getTypes = exports.expected = exports.validOptions = exports.validProperties = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /* globals process */ | ||
var _path = require('path'); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _defaults = require('../defaults'); | ||
@@ -73,2 +78,6 @@ | ||
if ('disableFix' in actual && typeof actual.disableFix !== 'boolean' && actual.disableFix !== null) return false; | ||
if ('autoFixFunc' in actual && typeof actual.autoFixFunc !== 'function' && typeof actual.autoFixFunc !== 'string' && actual.autoFixFunc !== null) return false; | ||
return true; | ||
@@ -169,2 +178,28 @@ } | ||
/** | ||
* | ||
* @param autoFixFunc | ||
* @returns {*} | ||
*/ | ||
function getAutoFixFunc(autoFixFunc) { | ||
var type = typeof autoFixFunc === 'undefined' ? 'undefined' : _typeof(autoFixFunc); | ||
if (type === 'function') { | ||
return autoFixFunc; | ||
} else if (type === 'string') { | ||
var resolveAutoFixfunc = void 0; | ||
try { | ||
resolveAutoFixfunc = require.resolve(autoFixFunc); | ||
} catch (error) { | ||
resolveAutoFixfunc = require.resolve(_path2.default.join(process.cwd(), autoFixFunc)); | ||
} | ||
// eslint-disable-next-line import/no-dynamic-require, global-require | ||
return require(resolveAutoFixfunc); | ||
} | ||
return null; | ||
} | ||
exports.validProperties = validProperties; | ||
@@ -174,2 +209,3 @@ exports.validOptions = validOptions; | ||
exports.getTypes = getTypes; | ||
exports.getIgnoredKeywords = getIgnoredKeywords; | ||
exports.getIgnoredKeywords = getIgnoredKeywords; | ||
exports.getAutoFixFunc = getAutoFixFunc; |
{ | ||
"name": "stylelint-declaration-strict-value", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Specify properties for which a variable, function, keyword must be used", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,3 @@ <!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
- [message](#message) | ||
- [Autofix support](#autofix-support) | ||
- [Scheme](#scheme) | ||
@@ -623,2 +624,83 @@ - [Credit / Inspiration](#credit--inspiration) | ||
#### Autofix support | ||
This plugin supports **configurable** [autofixing enabled by `--fix` option](https://stylelint.io/user-guide/cli/#autofixing-errors). | ||
**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](http://api.postcss.org/postcss.html), all validations and configuration of this plugin, as follows [`node`](http://api.postcss.org/Node.html), `validation`, [`root`](http://api.postcss.org/Declaration.html#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](https://github.com/davidtheclark/cosmiconfig). | ||
Alternatively you can specify a common JS module, which will be resolves by [standard `require`](https://nodejs.org/api/modules.html#modules_file_modules) calls including support for `CDW`. | ||
You can also disable autofixing by setting [`disableFix`](https://github.com/stylelint/stylelint/blob/master/docs/developer-guide/plugins.md#the-anatomy-of-a-plugin) to `true`; | ||
```js | ||
// .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:** | ||
```js | ||
// ./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 | ||
``` | ||
```js | ||
// .stylelintrc | ||
"rules": { | ||
// ... | ||
"scale-unlimited/declaration-strict-value": [ | ||
["/color/"], { | ||
autoFixFunc: './auto-fix-func.js', | ||
disableFix: true | false, | ||
}], | ||
// ... | ||
} | ||
``` | ||
### Scheme | ||
@@ -625,0 +707,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84590
321
748
3