Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #59: Don't apply properties-alphabetical-order autofixing if there no violations #198

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 50 additions & 0 deletions rules/properties-alphabetical-order/isOrderCorrect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 isOrderCorrect(node) {
const allPropData = [];

return node.every(function isNodeCorrect(child) {
if (child.type !== 'decl') {
return true;
}

const { prop } = child;

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

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

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);
}

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

const 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 true;
}

return checkAlphabeticalOrder(previousPropData, propData);
});
}
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; padding: { bottom: 1em; top: 1em; } width: 25%;}',
},
{
// blocked by https://github.com/hudochenkov/stylelint-order/issues/78
skip: true,
Expand Down