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 all commits
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
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