Skip to content

Commit

Permalink
Compatibility with newer postcss-sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Oct 16, 2021
1 parent 4597116 commit 6e7330a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 77 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"main": "index.js",
"dependencies": {
"postcss": "^8.3.9",
"postcss-sorting": "^7.0.0"
"postcss-sorting": "^7.0.1"
},
"peerDependencies": {
"stylelint": "^14.0.0"
Expand Down
14 changes: 2 additions & 12 deletions rules/order/checkNode.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const stylelint = require('stylelint');
const postcss = require('postcss');
const postcssSorting = require('postcss-sorting');
const sortNode = require('postcss-sorting/lib/order/sortNode');
const checkOrder = require('./checkOrder');
const getOrderData = require('./getOrderData');
const ruleName = require('./ruleName');
const messages = require('./messages');

module.exports = function checkNode({
node,
originalNode,
isFixEnabled,
orderInfo,
primaryOption,
Expand Down Expand Up @@ -37,15 +35,7 @@ module.exports = function checkNode({
});

if (shouldFix) {
let sortingOptions = {
order: primaryOption,
};

// creating PostCSS Root node with current node as a child,
// so PostCSS Sorting can process it
let tempRoot = postcss.root({ nodes: [originalNode] });

postcssSorting(sortingOptions)(tempRoot);
sortNode(node, primaryOption);
}
}

Expand Down
1 change: 0 additions & 1 deletion rules/order/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function rule(primaryOption, options = {}, context = {}) {
if (isRuleWithNodes(node)) {
checkNode({
node,
originalNode,
isFixEnabled,
orderInfo,
primaryOption,
Expand Down
14 changes: 6 additions & 8 deletions rules/properties-alphabetical-order/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let stylelint = require('stylelint');
let postcssSorting = require('postcss-sorting');
let sortNodeProperties = require('postcss-sorting/lib/properties-order/sortNodeProperties');
let { namespace, getContainingNode, isRuleWithNodes } = require('../../utils');
let checkNode = require('./checkNode');

Expand Down Expand Up @@ -33,12 +33,6 @@ function rule(actual, options = {}, context = {}) {

let disableFix = options.disableFix || false;

if (context.fix && !disableFix) {
postcssSorting({ 'properties-order': 'alphabetical' })(root);

return;
}

let processedParents = [];

root.walk(function processRulesAndAtrules(input) {
Expand All @@ -52,7 +46,11 @@ function rule(actual, options = {}, context = {}) {
processedParents.push(node);

if (isRuleWithNodes(node)) {
checkNode(node, result, ruleName, messages);
if (context.fix && !disableFix) {
sortNodeProperties(node, { order: 'alphabetical' });
} else {
checkNode(node, result, ruleName, messages);
}
}
});
};
Expand Down
28 changes: 6 additions & 22 deletions rules/properties-order/checkNodeForOrder.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const stylelint = require('stylelint');
const postcss = require('postcss');
const postcssSorting = require('postcss-sorting');
const sortNodeProperties = require('postcss-sorting/lib/properties-order/sortNodeProperties');
const { isProperty } = require('../../utils');
const checkOrder = require('./checkOrder');
const getNodeData = require('./getNodeData');
const createFlatOrder = require('./createFlatOrder');
const ruleName = require('./ruleName');
const messages = require('./messages');

module.exports = async function checkNodeForOrder({
module.exports = function checkNodeForOrder({
node,
originalNode,
isFixEnabled,
primaryOption,
unspecified,
Expand Down Expand Up @@ -48,24 +46,10 @@ module.exports = async function checkNodeForOrder({
});

if (shouldFixOrder) {
let sortingOptions = {
'properties-order': createFlatOrder(primaryOption),
'unspecified-properties-position':
unspecified === 'ignore' ? 'bottom' : unspecified,
};

// creating PostCSS Root node with current node as a child,
// so PostCSS Sorting can process it
let tempRoot = postcss.root({ nodes: [originalNode] });

try {
await postcss([postcssSorting(sortingOptions)]).process(tempRoot, {
from: result.from,
});
} catch (e) {
// With CSS-in-JS syntax PostCSS throws stringify error for node.type `literal`. Why it's even tries to stringify?!
// If syntax is provided to both `tempRoot` and to `postcss().process()`, then CSS-in-JS syntax fails with exceeded call stack
}
sortNodeProperties(node, {
order: createFlatOrder(primaryOption),
unspecifiedPropertiesPosition: unspecified === 'ignore' ? 'bottom' : unspecified,
});
}
}

Expand Down
5 changes: 2 additions & 3 deletions rules/properties-order/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function rule(primaryOption, options = {}, context = {}) {
let processedParents = [];

// Check all rules and at-rules recursively
root.walk(async function processRulesAndAtrules(input) {
root.walk(function processRulesAndAtrules(input) {
let node = getContainingNode(input);

// Avoid warnings duplication, caused by interfering in `root.walk()` algorigthm with `getContainingNode()`
Expand All @@ -51,9 +51,8 @@ function rule(primaryOption, options = {}, context = {}) {
processedParents.push(node);

if (isRuleWithNodes(node)) {
await checkNodeForOrder({
checkNodeForOrder({
node,
originalNode: input,
isFixEnabled,
primaryOption,
unspecified: options.unspecified || 'ignore',
Expand Down
23 changes: 0 additions & 23 deletions rules/properties-order/tests/custom-properties.js

This file was deleted.

0 comments on commit 6e7330a

Please sign in to comment.