Skip to content

Commit

Permalink
Fixed a bug that could cause regexp to issue an error (#165)
Browse files Browse the repository at this point in the history
* Fixed a bug that could cause regexp to issue an error

* Tests have been changed
  • Loading branch information
Ulyanov-programmer authored Oct 31, 2024
1 parent 3a73b52 commit 12fc98c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,36 @@ function mergeSelectors(parent, child) {

/**
* Move a child and its preceding comment(s) to after "after"
* ! It is necessary to clarify the comment
*/
function breakOut(child, after) {
function breakOut(child, parent) {
let changeParent = true
let lastNode = parent

for (let node of after.nodes) {
for (let node of parent.nodes) {
if (!node.nodes) continue

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

let parentRule = after.toString()
let parentRule = parent.toString()

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

if (parentRule.match(regexp)) {
changeParent = false
after.after(node).after(prevNode)
lastNode.after(node).after(prevNode)

lastNode = node
}
}

// It is necessary if the above child has never been moved
if (changeParent) {
after.after(child)
parent.after(child)
}

return child
Expand Down
29 changes: 28 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,39 @@ test("Save the parent's comment", () => {
run('a { /*i*/ b {} }', 'a { /*i*/ } a b {}')
})

test("Save the parent's comment", () => {
run(
`
div {
/* Comment with ^ $ . | ? * + () */
&[data-roots-all^=1] * #id .class {}
}`,
'/* Comment with ^ $ . | ? * + () */ div[data-roots-all^=1] * #id .class {}')
})

// !
// test("Save the parent's comment with newline", () => {
// run(
// `
// a {
// /*i*/

// /*i2*/
// b {}
// /*i3*/
// s {}
// }`,
// `a { /*i*/ } /*i2*/ a b {} /*i3*/ a s {}`
// )
// })

test("Save the parent's comment with newline", () => {
run(
`a {
/*i*/
b {} }`,
b {}
}`,
`a { /*i*/ } a b {}`
)
})
Expand Down

0 comments on commit 12fc98c

Please sign in to comment.