Skip to content

Commit 6c3cc1a

Browse files
committed
Add support for postcss-styled-syntax
1 parent 149d0f6 commit 6c3cc1a

File tree

9 files changed

+167
-83
lines changed

9 files changed

+167
-83
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Lint and autofix stylesheet order with [stylelint-order].
1212
* Sorts properties.
1313
* Sorts at-rules by different options.
1414
* Groups properties, custom properties, dollar variables, nested rules, nested at-rules.
15-
* Supports CSS, SCSS (using [postcss-scss]), CSS-in-JS (with [@stylelint/postcss-css-in-js]), HTML (with [postcss-html]), and most likely any other syntax added by other PostCSS plugins.
15+
* Supports CSS, SCSS (using [postcss-scss]), CSS-in-JS (with [postcss-styled-syntax]), HTML (with [postcss-html]), and most likely any other syntax added by other PostCSS plugins.
1616

1717
## Installation
1818

@@ -167,7 +167,7 @@ I recommend [Prettier] for formatting stylesheets.
167167
[gulp-postcss]: https://github.com/postcss/gulp-postcss
168168
[postcss-scss]: https://github.com/postcss/postcss-scss
169169
[postcss-html]: https://github.com/gucong3000/postcss-html
170-
[@stylelint/postcss-css-in-js]: https://github.com/stylelint/postcss-css-in-js
170+
[postcss-styled-syntax]: https://github.com/hudochenkov/postcss-styled-syntax
171171
[Prettier]: https://prettier.io/
172172
[stylelint]: https://stylelint.io/
173173
[stylelint-order]: https://github.com/hudochenkov/stylelint-order

jest-setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const plugin = require('.');
44
const postcss = require('postcss');
5-
const cssInJS = require('@stylelint/postcss-css-in-js'); // eslint-disable-line import/no-extraneous-dependencies
5+
const cssInJS = require('postcss-styled-syntax'); // eslint-disable-line import/no-extraneous-dependencies
66
const html = require('postcss-html'); // eslint-disable-line import/no-extraneous-dependencies
77

88
global.groupTest = function groupTest(testGroups) {

lib/getContainingNode.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
module.exports = function getContainingNode(node) {
2-
// For styled-components declarations are children of Root node
3-
if (
4-
node.type !== 'rule' &&
5-
node.type !== 'atrule' &&
6-
node.parent.document &&
7-
node.parent.document.nodes &&
8-
node.parent.document.nodes.some((item) => item.type === 'root')
9-
) {
2+
// For styled-components: declarations are children of Root node
3+
if (node.type !== 'rule' && node.type !== 'atrule' && node.parent.type === 'root') {
104
return node.parent;
115
}
126

lib/isAllowedToProcess.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const atRuleExcludes = ['function', 'if', 'else', 'for', 'each', 'while'];
2+
3+
module.exports = function isAllowedToProcess(node) {
4+
if (node.type === 'atrule' && atRuleExcludes.includes(node.name)) {
5+
return false;
6+
}
7+
8+
if (!node?.nodes?.length) {
9+
return false;
10+
}
11+
12+
// postcss-styled-syntax: Interpolations at the end of node
13+
if (node.raws.after.includes('${')) {
14+
return false;
15+
}
16+
17+
// postcss-styled-syntax: Interpolations among children of a node
18+
if (node.nodes.some((item) => item.raws.before.includes('${'))) {
19+
return false;
20+
}
21+
22+
// @stylelint/postcss-css-in-js only
23+
if (node.nodes.some((item) => item.type === 'literal')) {
24+
return false;
25+
}
26+
27+
return true;
28+
};

lib/isRuleWithNodes.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/order/sortNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const createExpectedOrder = require('./createExpectedOrder');
2-
const isRuleWithNodes = require('../isRuleWithNodes');
2+
const isAllowedToProcess = require('../isAllowedToProcess');
33
const processLastComments = require('./processLastComments');
44
const processMostNodes = require('./processMostNodes');
55
const sortByIndexes = require('./sortByIndexes');
66

77
module.exports = function sortNode(node, optsOrder) {
8-
if (!isRuleWithNodes(node)) {
8+
if (!isAllowedToProcess(node)) {
99
return;
1010
}
1111

lib/properties-order/sortNodeProperties.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ let createExpectedPropertiesOrder = require('./createExpectedPropertiesOrder');
22
let getComments = require('../getComments');
33
let getPropertiesOrderData = require('./getPropertiesOrderData');
44
let isCustomProperty = require('../isCustomProperty');
5-
let isRuleWithNodes = require('../isRuleWithNodes');
5+
let isAllowedToProcess = require('../isAllowedToProcess');
66
let isStandardSyntaxProperty = require('../isStandardSyntaxProperty');
77
let sortDeclarations = require('./sortDeclarations');
88
let sortDeclarationsAlphabetically = require('./sortDeclarationsAlphabetically');
99
let vendor = require('./vendor');
1010

1111
module.exports = function sortNodeProperties(node, { order, unspecifiedPropertiesPosition }) {
12-
if (!isRuleWithNodes(node)) {
12+
if (!isAllowedToProcess(node)) {
1313
return;
1414
}
1515

0 commit comments

Comments
 (0)