Skip to content

Commit 85b9ace

Browse files
committed
postcss-nesting : fix #285
1 parent 99abb1e commit 85b9ace

7 files changed

+88
-9
lines changed
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+
/* lose 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+
/* lose 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+
/* lose 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)