Skip to content

Commit 12fc98c

Browse files
Fixed a bug that could cause regexp to issue an error (#165)
* Fixed a bug that could cause regexp to issue an error * Tests have been changed
1 parent 3a73b52 commit 12fc98c

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

index.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,36 @@ function mergeSelectors(parent, child) {
7474

7575
/**
7676
* Move a child and its preceding comment(s) to after "after"
77-
* ! It is necessary to clarify the comment
7877
*/
79-
function breakOut(child, after) {
78+
function breakOut(child, parent) {
8079
let changeParent = true
80+
let lastNode = parent
8181

82-
for (let node of after.nodes) {
82+
for (let node of parent.nodes) {
8383
if (!node.nodes) continue
8484

8585
let prevNode = node.prev()
8686
if (prevNode?.type !== 'comment') continue
8787

88-
let parentRule = after.toString()
88+
let parentRule = parent.toString()
8989

9090
/* Checking that the comment "describes" the rule following. Like this:
9191
/* comment about the rule below /*
9292
.rule {}
9393
*/
94-
let regexp = new RegExp(`${prevNode.toString()} *\n *${node.toString()}`)
94+
let regexp = /[*]\/ *\n.*{/
9595

9696
if (parentRule.match(regexp)) {
9797
changeParent = false
98-
after.after(node).after(prevNode)
98+
lastNode.after(node).after(prevNode)
99+
100+
lastNode = node
99101
}
100102
}
101103

102104
// It is necessary if the above child has never been moved
103105
if (changeParent) {
104-
after.after(child)
106+
parent.after(child)
105107
}
106108

107109
return child

index.test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,39 @@ test("Save the parent's comment", () => {
608608
run('a { /*i*/ b {} }', 'a { /*i*/ } a b {}')
609609
})
610610

611+
test("Save the parent's comment", () => {
612+
run(
613+
`
614+
div {
615+
/* Comment with ^ $ . | ? * + () */
616+
&[data-roots-all^=1] * #id .class {}
617+
}`,
618+
'/* Comment with ^ $ . | ? * + () */ div[data-roots-all^=1] * #id .class {}')
619+
})
620+
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+
// })
636+
611637
test("Save the parent's comment with newline", () => {
612638
run(
613639
`a {
614640
/*i*/
615641
616-
b {} }`,
642+
b {}
643+
}`,
617644
`a { /*i*/ } a b {}`
618645
)
619646
})

0 commit comments

Comments
 (0)