diff --git a/rules/order/checkNode.js b/rules/order/checkNode.js index 9c7f541..931a35b 100644 --- a/rules/order/checkNode.js +++ b/rules/order/checkNode.js @@ -44,6 +44,13 @@ module.exports = function checkNode(node, sharedInfo) { return; } + if (sharedInfo.isFixEnabled) { + sharedInfo.shouldFix = true; + + // Don't go further, fix will be applied + return; + } + stylelint.utils.report({ message: sharedInfo.messages.expected(nodeData.description, previousNodeData.description), node: child, diff --git a/rules/order/checkOrder.js b/rules/order/checkOrder.js index b97312d..4d4ff26 100644 --- a/rules/order/checkOrder.js +++ b/rules/order/checkOrder.js @@ -22,6 +22,13 @@ module.exports = function checkOrder(firstNodeData, secondNodeData, allNodesData priorSpecifiedNodeData.expectedPosition && priorSpecifiedNodeData.expectedPosition > secondNodeData.expectedPosition ) { + if (sharedInfo.isFixEnabled) { + sharedInfo.shouldFix = true; + + // Don't go further, fix will be applied + return; + } + stylelint.utils.report({ message: sharedInfo.messages.expected( secondNodeData.description, diff --git a/rules/order/index.js b/rules/order/index.js index fa96c4a..b7c8c9d 100644 --- a/rules/order/index.js +++ b/rules/order/index.js @@ -37,18 +37,13 @@ function rule(expectation, options, context) { return; } - const disableFix = _.get(options, ['disableFix'], false); - - if (context.fix && !disableFix) { - postcssSorting({ order: expectation })(root); - - return; - } + const disableFix = _.get(options, 'disableFix', false); + const isFixEnabled = context.fix && !disableFix; const expectedOrder = createExpectedOrder(expectation); // By default, ignore unspecified properties - const unspecified = _.get(options, ['unspecified'], 'ignore'); + const unspecified = _.get(options, 'unspecified', 'ignore'); const sharedInfo = { expectedOrder, @@ -56,14 +51,25 @@ function rule(expectation, options, context) { messages, ruleName, result, + isFixEnabled, + shouldFix: false, }; // Check all rules and at-rules recursively root.walk(function processRulesAndAtrules(node) { + // return early if we know there is a violation and auto fix should be applied + if (isFixEnabled && sharedInfo.shouldFix) { + return; + } + if (node.type === 'rule' || node.type === 'atrule') { checkNode(node, sharedInfo); } }); + + if (sharedInfo.shouldFix) { + postcssSorting({ order: expectation })(root); + } }; } diff --git a/rules/order/tests/index.js b/rules/order/tests/index.js index c94231e..e9d9283 100644 --- a/rules/order/tests/index.js +++ b/rules/order/tests/index.js @@ -346,6 +346,7 @@ testRule(rule, { }, ], ], + fix: true, accept: [ { @@ -504,6 +505,7 @@ testRule(rule, { 'declarations', ], ], + fix: true, accept: [ { @@ -669,6 +671,7 @@ testRule(rule, { unspecified: 'top', }, ], + fix: true, accept: [ { @@ -1044,6 +1047,7 @@ testRule(rule, { ], }); +// Doesn't has fix, because postcss-sorting doesn't know about at-variables testRule(rule, { ruleName, syntax: 'less', @@ -1091,6 +1095,7 @@ testRule(rule, { ], }); +// Doesn't has fix, because postcss-sorting doesn't know about less-mixins testRule(rule, { ruleName, syntax: 'less', @@ -1208,3 +1213,29 @@ testRule(rule, { }, ], }); + +testRule(rule, { + ruleName, + config: [['custom-properties']], + fix: true, + + accept: [ + { + code: ` + a { + --width: 10px; + display: none; + } + `, + }, + { + description: 'should not fix if there no violation', + code: ` + a { + display: none; + --width: 10px; + } + `, + }, + ], +});