Skip to content

Commit

Permalink
Fixed an incorrect nesting error (#167)
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

* Fixed the error of incorrect processing of several rules

* Added a test for several rules with comments
  • Loading branch information
Ulyanov-programmer authored Nov 1, 2024
1 parent 9f75b38 commit 64ff5c5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
40 changes: 15 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,25 @@ function mergeSelectors(parent, child) {
/**
* Move a child and its preceding comment(s) to after "after"
*/
function breakOut(child, parent) {
let changeParent = true
let lastNode = parent

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

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

let parentRule = parent.toString()
function breakOut(child, currentNode) {
if (child.prev()?.type !== 'comment') {
currentNode.after(child)
return child
}

/* Checking that the comment "describes" the rule following. Like this:
/* comment about the rule below /*
.rule {}
*/
let regexp = /[*]\/ *\n.*{/
let prevNode = child.prev()

if (parentRule.match(regexp)) {
changeParent = false
lastNode.after(node).after(prevNode)
/* Checking that the comment "describes" the rule following. Like this:
/* comment about the rule below *\
.rule {}
*/
let regexp = /[*]\/ *\n.*{/

lastNode = node
}
if (child.parent.toString().match(regexp)) {
currentNode.after(child).after(prevNode)
}

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

return child
Expand Down
39 changes: 20 additions & 19 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,21 +618,20 @@ div {
'/* 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 several rules with attached comments", () => {
run(
`
a {
/*i*/
/*i2*/
b {}
/*i3*/
s {}
}`,
`a { /*i*/ } /*i2*/ a b {} /*i3*/ a s {}`
)
})

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

test('Save the comments for the parent and child', () => {
run(
`a {
/*i*/
/*o*/
b {} }`,
`
a {
/*i*/
/*o*/
b {}
}`,

`a { /*i*/ } /*o*/ a b {}`
)
Expand Down

0 comments on commit 64ff5c5

Please sign in to comment.