Skip to content

Commit

Permalink
Don't apply properties-alphabetical-order autofixing if there are n…
Browse files Browse the repository at this point in the history
…o violations. Fixes #59 (#198)
  • Loading branch information
Astrael1 authored Oct 14, 2024
1 parent 2453a16 commit a024635
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 54 deletions.
55 changes: 55 additions & 0 deletions rules/properties-alphabetical-order/checkChild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { isStandardSyntaxProperty } from '../../utils/isStandardSyntaxProperty.js';
import { isCustomProperty } from '../../utils/isCustomProperty.js';
import * as vendor from '../../utils/vendor.js';
import { checkAlphabeticalOrder } from '../checkAlphabeticalOrder.js';

export function checkChild(child, allPropData) {
if (child.type !== 'decl') {
return null;
}

let { prop } = child;

if (!isStandardSyntaxProperty(prop)) {
return null;
}

if (isCustomProperty(prop)) {
return null;
}

let unprefixedPropName = vendor.unprefixed(prop);

// Hack to allow -moz-osx-font-smoothing to be understood
// just like -webkit-font-smoothing
if (unprefixedPropName.startsWith('osx-')) {
unprefixedPropName = unprefixedPropName.slice(4);
}

let propData = {
name: prop,
unprefixedName: unprefixedPropName,
index: allPropData.length,
node: child,
};

let previousPropData = allPropData[allPropData.length - 1]; // eslint-disable-line unicorn/prefer-at -- Need to support older Node.js

allPropData.push(propData);

// Skip first decl
if (!previousPropData) {
return null;
}

let isCorrectOrder = checkAlphabeticalOrder(previousPropData, propData);

if (isCorrectOrder) {
return null;
}

return {
expectedFirst: propData.name,
expectedSecond: previousPropData.name,
};
}
63 changes: 10 additions & 53 deletions rules/properties-alphabetical-order/checkNode.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,20 @@
import stylelint from 'stylelint';
import { checkAlphabeticalOrder } from '../checkAlphabeticalOrder.js';
import { isCustomProperty } from '../../utils/isCustomProperty.js';
import { isStandardSyntaxProperty } from '../../utils/isStandardSyntaxProperty.js';
import * as vendor from '../../utils/vendor.js';
import { checkChild } from './checkChild.js';

// eslint-disable-next-line max-params
export function checkNode(node, result, ruleName, messages) {
let allPropData = [];

node.each(function processEveryNode(child) {
if (child.type !== 'decl') {
return;
const problem = checkChild(child, allPropData);

if (problem) {
stylelint.utils.report({
message: messages.expected(problem.expectedFirst, problem.expectedSecond),
node: child,
result,
ruleName,
});
}

let { prop } = child;

if (!isStandardSyntaxProperty(prop)) {
return;
}

if (isCustomProperty(prop)) {
return;
}

let unprefixedPropName = vendor.unprefixed(prop);

// Hack to allow -moz-osx-font-smoothing to be understood
// just like -webkit-font-smoothing
if (unprefixedPropName.startsWith('osx-')) {
unprefixedPropName = unprefixedPropName.slice(4);
}

let propData = {
name: prop,
unprefixedName: unprefixedPropName,
index: allPropData.length,
node: child,
};

let previousPropData = allPropData[allPropData.length - 1]; // eslint-disable-line unicorn/prefer-at -- Need to support older Node.js

allPropData.push(propData);

// Skip first decl
if (!previousPropData) {
return;
}

let isCorrectOrder = checkAlphabeticalOrder(previousPropData, propData);

if (isCorrectOrder) {
return;
}

stylelint.utils.report({
message: messages.expected(propData.name, previousPropData.name),
node: child,
result,
ruleName,
});
});
}
3 changes: 2 additions & 1 deletion rules/properties-alphabetical-order/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { checkNode } from './checkNode.js';
import { namespace } from '../../utils/namespace.js';
import { getContainingNode } from '../../utils/getContainingNode.js';
import { isRuleWithNodes } from '../../utils/isRuleWithNodes.js';
import { isOrderCorrect } from './isOrderCorrect.js';

let ruleName = namespace('properties-alphabetical-order');

Expand Down Expand Up @@ -35,7 +36,7 @@ export function rule(actual, options, context = {}) {
processedParents.push(node);

if (isRuleWithNodes(node)) {
if (context.fix) {
if (context.fix && !isOrderCorrect(node)) {
sortNodeProperties(node, { order: 'alphabetical' });

// log warnings if any problems weren't fixed
Expand Down
9 changes: 9 additions & 0 deletions rules/properties-alphabetical-order/isOrderCorrect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { checkChild } from './checkChild.js';

export function isOrderCorrect(node) {
const allPropData = [];

return node.every(function isChildCorrect(child) {
return !checkChild(child, allPropData);
});
}
3 changes: 3 additions & 0 deletions rules/properties-alphabetical-order/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ testRule({
{
code: 'a { font-size: 1px; -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialised; font-weight: bold; }',
},
{
code: 'a { color: #000; span { bottom: 1em; top: 1em; } width: 25%;}',
},
{
// blocked by https://github.com/hudochenkov/stylelint-order/issues/78
skip: true,
Expand Down

0 comments on commit a024635

Please sign in to comment.