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 an incorrect nesting error #167

Merged
merged 5 commits into from
Nov 1, 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
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