Skip to content

Commit

Permalink
order: Apply fix if there violations only #51
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Nov 2, 2017
1 parent bccb7af commit 6b0c036
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
7 changes: 7 additions & 0 deletions rules/order/checkNode.js
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions rules/order/checkOrder.js
Expand Up @@ -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,
Expand Down
22 changes: 14 additions & 8 deletions rules/order/index.js
Expand Up @@ -37,33 +37,39 @@ 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,
unspecified,
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);
}
};
}

Expand Down
31 changes: 31 additions & 0 deletions rules/order/tests/index.js
Expand Up @@ -346,6 +346,7 @@ testRule(rule, {
},
],
],
fix: true,

accept: [
{
Expand Down Expand Up @@ -504,6 +505,7 @@ testRule(rule, {
'declarations',
],
],
fix: true,

accept: [
{
Expand Down Expand Up @@ -669,6 +671,7 @@ testRule(rule, {
unspecified: 'top',
},
],
fix: true,

accept: [
{
Expand Down Expand Up @@ -1044,6 +1047,7 @@ testRule(rule, {
],
});

// Doesn't has fix, because postcss-sorting doesn't know about at-variables
testRule(rule, {
ruleName,
syntax: 'less',
Expand Down Expand Up @@ -1091,6 +1095,7 @@ testRule(rule, {
],
});

// Doesn't has fix, because postcss-sorting doesn't know about less-mixins
testRule(rule, {
ruleName,
syntax: 'less',
Expand Down Expand Up @@ -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;
}
`,
},
],
});

0 comments on commit 6b0c036

Please sign in to comment.