Skip to content

Commit b2348fc

Browse files
committed
Allow limited sorting for properties in CSS-in-JS rules containing interpolation. Closes #99
1 parent 6319a0c commit b2348fc

11 files changed

Lines changed: 260 additions & 84 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ Some at-rules, like [control](https://sass-lang.com/documentation/file.SASS_REFE
5050

5151
### CSS-in-JS
5252

53-
Plugin will ignore rules, which have template literal interpolation, to avoid breaking the logic:
53+
To avoid breaking the logic if the rule has a template literal interpolation, properties will be sorted only among neighbouring properties before or after the interpolation:
5454

5555
```js
5656
const Component = styled.div`
57-
/* The following properties WILL NOT be sorted, because interpolation is on properties level */
57+
/* 'z-index' and 'top' will be sorted as a single group. 'position' and 'display' will be sorted as a second group. Interpolation separates properties into groups. */
5858
z-index: 1;
5959
top: 1px;
6060
${props => props.great && 'color: red'};
6161
position: absolute;
6262
display: block;
6363
6464
div {
65-
/* The following properties WILL be sorted, because interpolation for property value only */
65+
/* The following properties WILL be sorted together, because interpolation is for property value only */
6666
z-index: 2;
6767
position: static;
6868
top: ${2 + 10}px;

lib/getComments.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ function beforeDeclaration(comments, previousNode, nodeData, currentInitialIndex
6767

6868
commentData.initialIndex = initialIndex - 0.0001;
6969

70-
// add a marker
71-
previousNode.sortProperty = true;
72-
7370
const newComments = [commentData, ...comments];
7471

7572
return beforeDeclaration(newComments, previousNode.prev(), nodeData, commentData.initialIndex);
@@ -96,9 +93,6 @@ function afterDeclaration(comments, nextNode, nodeData, currentInitialIndex) {
9693

9794
commentData.initialIndex = initialIndex + 0.0001;
9895

99-
// add a marker
100-
nextNode.sortProperty = true;
101-
10296
return afterDeclaration(
10397
[...comments, commentData],
10498
nextNode.next(),

lib/isAllowedToProcess.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const atRuleExcludes = ['function', 'if', 'else', 'for', 'each', 'while'];
22

3-
module.exports = function isAllowedToProcess(node) {
3+
module.exports = function isAllowedToProcess(node, options) {
44
if (node.type === 'atrule' && atRuleExcludes.includes(node.name)) {
55
return false;
66
}
@@ -9,14 +9,18 @@ module.exports = function isAllowedToProcess(node) {
99
return false;
1010
}
1111

12-
// postcss-styled-syntax: Interpolations at the end of node
13-
if (node.raws.after?.includes('${')) {
14-
return false;
15-
}
12+
const ignoreInterpolations = options && options.ignoreInterpolations;
1613

17-
// postcss-styled-syntax: Interpolations among children of a node
18-
if (node.nodes.some((item) => item.raws.before.includes('${'))) {
19-
return false;
14+
if (!ignoreInterpolations) {
15+
// postcss-styled-syntax: Interpolations at the end of node
16+
if (node.raws.after?.includes('${')) {
17+
return false;
18+
}
19+
20+
// postcss-styled-syntax: Interpolations among children of a node
21+
if (node.nodes.some((item) => item.raws.before.includes('${'))) {
22+
return false;
23+
}
2024
}
2125

2226
// @stylelint/postcss-css-in-js only

lib/properties-order/__tests__/fixtures/ignore-template-literals-flat.expected.js

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

lib/properties-order/__tests__/fixtures/ignore-template-literals-flat.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Only first group is unordered
2+
const Component = styled.div`
3+
top: 0;
4+
z-index: 2;
5+
${props => props.great && 'color: red'};
6+
position: absolute;
7+
display: block;
8+
`;
9+
10+
// Only second group is unordered
11+
const Component2 = styled.div`
12+
top: 0;
13+
z-index: 2;
14+
${props => props.great && 'color: red'};
15+
position: absolute;
16+
display: block;
17+
`;
18+
19+
// Both groups are unordered
20+
const Component3 = styled.div`
21+
top: 0;
22+
z-index: 2;
23+
${props => props.great && 'color: red'};
24+
position: absolute;
25+
display: block;
26+
`;
27+
28+
// Interpolation at the top
29+
const Component4 = styled.div`
30+
${props => props.great && 'color: red'}
31+
position: absolute;
32+
top: 0;
33+
`;
34+
35+
// Interpolation at the bottom
36+
const Component5 = styled.div`
37+
position: absolute;
38+
top: 0;
39+
${props => props.great && 'color: red'}
40+
`;
41+
42+
// Three groups
43+
const Component6 = styled.div`
44+
top: 0;
45+
z-index: 2;
46+
${props => props.great && 'color: red'}
47+
position: absolute;
48+
display: block;
49+
${props => props.great && 'color: blue'}
50+
top: 0;
51+
display: block;
52+
`;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Only first group is unordered
2+
const Component = styled.div`
3+
z-index: 2;
4+
top: 0;
5+
${props => props.great && 'color: red'};
6+
position: absolute;
7+
display: block;
8+
`;
9+
10+
// Only second group is unordered
11+
const Component2 = styled.div`
12+
top: 0;
13+
z-index: 2;
14+
${props => props.great && 'color: red'};
15+
display: block;
16+
position: absolute;
17+
`;
18+
19+
// Both groups are unordered
20+
const Component3 = styled.div`
21+
z-index: 2;
22+
top: 0;
23+
${props => props.great && 'color: red'};
24+
display: block;
25+
position: absolute;
26+
`;
27+
28+
// Interpolation at the top
29+
const Component4 = styled.div`
30+
${props => props.great && 'color: red'}
31+
top: 0;
32+
position: absolute;
33+
`;
34+
35+
// Interpolation at the bottom
36+
const Component5 = styled.div`
37+
top: 0;
38+
position: absolute;
39+
${props => props.great && 'color: red'}
40+
`;
41+
42+
// Three groups
43+
const Component6 = styled.div`
44+
top: 0;
45+
z-index: 2;
46+
${props => props.great && 'color: red'}
47+
display: block;
48+
position: absolute;
49+
${props => props.great && 'color: blue'}
50+
display: block;
51+
top: 0;
52+
`;

lib/properties-order/__tests__/fixtures/ignore-template-literals-nested.expected.js renamed to lib/properties-order/__tests__/fixtures/template-literals-nested.expected.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Component = styled.div`
2-
z-index: 1;
32
top: 1px;
3+
z-index: 1;
44
${props => props.great && 'color: red'};
55
position: absolute;
66
display: block;
@@ -55,9 +55,9 @@ const Component3 = styled.div`
5555
z-index: 2;
5656
5757
span {
58-
z-index: 3;
5958
top: 3px;
6059
display: flex;
60+
z-index: 3;
6161
${props => props.great && 'color: red'};
6262
position: relative;
6363
}

lib/properties-order/__tests__/fixtures/ignore-template-literals-nested.js renamed to lib/properties-order/__tests__/fixtures/template-literals-nested.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const Component2 = styled.div`
2929
div {
3030
z-index: 2;
3131
${props => props.great && 'color: red'};
32-
position: static;
3332
top: 2px;
33+
position: static;
3434
display: inline-block;
3535
3636
span {

lib/properties-order/__tests__/properties-order.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,18 @@ test('Should sort properties in css helper (styled)', () =>
236236
__dirname,
237237
));
238238

239-
test('Ignore template literals in flat components (styled)', () =>
239+
test('Template literals in flat components (styled)', () =>
240240
runTest(
241-
'ignore-template-literals-flat.js',
241+
'template-literals-flat.js',
242242
{
243243
'properties-order': ['position', 'top', 'display', 'z-index'],
244244
},
245245
__dirname,
246246
));
247247

248-
test('Ignore template literals in nested components (styled)', () =>
248+
test('Template literals in nested components (styled)', () =>
249249
runTest(
250-
'ignore-template-literals-nested.js',
250+
'template-literals-nested.js',
251251
{
252252
'properties-order': ['position', 'top', 'display', 'z-index'],
253253
},

0 commit comments

Comments
 (0)