Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug that could cause regexp to issue an error #165

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {}`
// )
// })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test to see what happens if there are several rules with comments in the description.

The test does not work - it does not connect selectors for the second nested one.
I tried to fix it, but in your code, the devil will break my leg.

Does it make sense to fix it?

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

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