Skip to content

Commit 64ff5c5

Browse files
Fixed an incorrect nesting error (#167)
* Fixed a bug that could cause regexp to issue an error * Tests have been changed * Fixed the error of incorrect processing of several rules * Added a test for several rules with comments
1 parent 9f75b38 commit 64ff5c5

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

index.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -75,35 +75,25 @@ function mergeSelectors(parent, child) {
7575
/**
7676
* Move a child and its preceding comment(s) to after "after"
7777
*/
78-
function breakOut(child, parent) {
79-
let changeParent = true
80-
let lastNode = parent
81-
82-
for (let node of parent.nodes) {
83-
if (!node.nodes) continue
84-
85-
let prevNode = node.prev()
86-
if (prevNode?.type !== 'comment') continue
87-
88-
let parentRule = parent.toString()
78+
function breakOut(child, currentNode) {
79+
if (child.prev()?.type !== 'comment') {
80+
currentNode.after(child)
81+
return child
82+
}
8983

90-
/* Checking that the comment "describes" the rule following. Like this:
91-
/* comment about the rule below /*
92-
.rule {}
93-
*/
94-
let regexp = /[*]\/ *\n.*{/
84+
let prevNode = child.prev()
9585

96-
if (parentRule.match(regexp)) {
97-
changeParent = false
98-
lastNode.after(node).after(prevNode)
86+
/* Checking that the comment "describes" the rule following. Like this:
87+
/* comment about the rule below *\
88+
.rule {}
89+
*/
90+
let regexp = /[*]\/ *\n.*{/
9991

100-
lastNode = node
101-
}
92+
if (child.parent.toString().match(regexp)) {
93+
currentNode.after(child).after(prevNode)
10294
}
103-
104-
// It is necessary if the above child has never been moved
105-
if (changeParent) {
106-
parent.after(child)
95+
else {
96+
currentNode.after(child)
10797
}
10898

10999
return child

index.test.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -618,21 +618,20 @@ div {
618618
'/* Comment with ^ $ . | ? * + () */ div[data-roots-all^=1] * #id .class {}')
619619
})
620620

621-
// !
622-
// test("Save the parent's comment with newline", () => {
623-
// run(
624-
// `
625-
// a {
626-
// /*i*/
627-
628-
// /*i2*/
629-
// b {}
630-
// /*i3*/
631-
// s {}
632-
// }`,
633-
// `a { /*i*/ } /*i2*/ a b {} /*i3*/ a s {}`
634-
// )
635-
// })
621+
test("Save several rules with attached comments", () => {
622+
run(
623+
`
624+
a {
625+
/*i*/
626+
627+
/*i2*/
628+
b {}
629+
/*i3*/
630+
s {}
631+
}`,
632+
`a { /*i*/ } /*i2*/ a b {} /*i3*/ a s {}`
633+
)
634+
})
636635

637636
test("Save the parent's comment with newline", () => {
638637
run(
@@ -647,10 +646,12 @@ test("Save the parent's comment with newline", () => {
647646

648647
test('Save the comments for the parent and child', () => {
649648
run(
650-
`a {
651-
/*i*/
652-
/*o*/
653-
b {} }`,
649+
`
650+
a {
651+
/*i*/
652+
/*o*/
653+
b {}
654+
}`,
654655

655656
`a { /*i*/ } /*o*/ a b {}`
656657
)

0 commit comments

Comments
 (0)