From 2f617af604dfc42d7575cf477c0774d2ad21b11c Mon Sep 17 00:00:00 2001 From: Ulyanov-programmer Date: Thu, 31 Oct 2024 16:28:10 +0400 Subject: [PATCH 1/4] Fixed a bug that could cause regexp to issue an error --- index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 9ba04eb..d8652db 100644 --- a/index.js +++ b/index.js @@ -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 From 444b998edce2a2a965bb3d8ed16c1633ef86d050 Mon Sep 17 00:00:00 2001 From: Ulyanov-programmer Date: Thu, 31 Oct 2024 16:28:27 +0400 Subject: [PATCH 2/4] Tests have been changed --- index.test.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index a18af98..c619935 100644 --- a/index.test.js +++ b/index.test.js @@ -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 {}` ) }) From 35475ad7f0e37ed2dd5add3cbbabdfd5b164abd5 Mon Sep 17 00:00:00 2001 From: Ulyanov-programmer Date: Thu, 31 Oct 2024 18:14:48 +0400 Subject: [PATCH 3/4] Fixed the error of incorrect processing of several rules --- index.js | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index d8652db..4b479b1 100644 --- a/index.js +++ b/index.js @@ -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 From c4dada8c6d48af29f01108309334b4df000e9955 Mon Sep 17 00:00:00 2001 From: Ulyanov-programmer Date: Thu, 31 Oct 2024 18:15:16 +0400 Subject: [PATCH 4/4] Added a test for several rules with comments --- index.test.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/index.test.js b/index.test.js index c619935..931adbd 100644 --- a/index.test.js +++ b/index.test.js @@ -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(