Skip to content

Commit ef97463

Browse files
authored
postcss-nesting : fix #285 (#288)
* postcss-nesting : fix #285 * add changelog * typo
1 parent 99abb1e commit ef97463

8 files changed

+111
-9
lines changed

plugins/postcss-nesting/CHANGELOG.md

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Changes to PostCSS Nesting
22

3+
### Unreleased (patch)
4+
5+
- Avoid creating duplicate selectors containing only comments.
6+
7+
```diff
8+
.alpha {
9+
/* loose comment */
10+
& .beta {
11+
order: 1;
12+
}
13+
}
14+
15+
/* becomes */
16+
17+
- .alpha {
18+
- /* loose comment */
19+
- }
20+
+ /* loose comment */
21+
.alpha .beta {
22+
order: 1;
23+
}
24+
```
25+
326
### 10.1.2 (January 12, 2022)
427

528
- Improved : selector specificity calculation
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
export default function cleanupParent(parent) {
22
if (!parent.nodes.length) {
33
parent.remove();
4+
return;
5+
}
6+
7+
const commentNodes = parent.nodes.filter(node => node.type === 'comment');
8+
if (commentNodes.length === parent.nodes.length) {
9+
parent.replaceWith(...commentNodes);
410
}
511
}

plugins/postcss-nesting/src/lib/shift-nodes-before-parent.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import cleanupParent from './cleanup-parent';
2+
13
export default function shiftNodesBeforeParent(node) {
24
const parent = node.parent;
35
const index = parent.index(node);
46

57
// conditionally move previous siblings into a clone of the parent
68
if (index) {
7-
parent.cloneBefore().removeAll().append(parent.nodes.slice(0, index));
9+
const newParent = parent.cloneBefore().removeAll().append(parent.nodes.slice(0, index));
10+
cleanupParent(newParent);
811
}
912

1013
// move the current node before the parent (and after the conditional clone)

plugins/postcss-nesting/test/basic.css

+28
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,31 @@ a {
115115
order: 51;
116116
}
117117
}
118+
119+
/* leading : root */
120+
.comments {
121+
/* leading : 1 */
122+
order: 1;
123+
/* trailing: 2 */
124+
125+
& .comment {
126+
order: 2;
127+
}
128+
129+
/* loose comment */
130+
& .comment {
131+
order: 3;
132+
}
133+
134+
/* leading : 4 */
135+
order: 4;
136+
/* trailing: 5 */
137+
138+
& .comment {
139+
/* nested deeper */
140+
141+
& .comment {
142+
order: 5;
143+
}
144+
}
145+
}

plugins/postcss-nesting/test/basic.expect.css

+24
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,27 @@ a b[a="a&b"] {
128128
.a:hover::before, .b:focus::before, .a:hover::after, .b:focus::after {
129129
order: 51;
130130
}
131+
132+
/* leading : root */
133+
.comments {
134+
/* leading : 1 */
135+
order: 1
136+
/* trailing: 2 */
137+
}
138+
.comments .comment {
139+
order: 2;
140+
}
141+
/* loose comment */
142+
.comments .comment {
143+
order: 3;
144+
}
145+
.comments {
146+
147+
/* leading : 4 */
148+
order: 4
149+
/* trailing: 5 */
150+
}
151+
/* nested deeper */
152+
.comments .comment .comment {
153+
order: 5;
154+
}

plugins/postcss-nesting/test/basic.no-is-pseudo-selector.expect.css

+24
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,27 @@ a b[a="a&b"] {
128128
.a:hover::before, .b:focus::before, .a:hover::after, .b:focus::after {
129129
order: 51;
130130
}
131+
132+
/* leading : root */
133+
.comments {
134+
/* leading : 1 */
135+
order: 1
136+
/* trailing: 2 */
137+
}
138+
.comments .comment {
139+
order: 2;
140+
}
141+
/* loose comment */
142+
.comments .comment {
143+
order: 3;
144+
}
145+
.comments {
146+
147+
/* leading : 4 */
148+
order: 4
149+
/* trailing: 5 */
150+
}
151+
/* nested deeper */
152+
.comments .comment .comment {
153+
order: 5;
154+
}

plugins/postcss-nesting/test/spec-examples.expect.css

+1-4
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,4 @@ article.foo {
390390
color: yellow;
391391
}
392392

393-
article {
394-
395-
/* valid! */
396-
}
393+
/* valid! */

plugins/postcss-nesting/test/spec-examples.no-is-pseudo-selector.expect.css

+1-4
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,4 @@ article.foo {
390390
color: yellow;
391391
}
392392

393-
article {
394-
395-
/* valid! */
396-
}
393+
/* valid! */

0 commit comments

Comments
 (0)