From 12fc98c5183ced0a344e8aa1e591c8edac701f67 Mon Sep 17 00:00:00 2001 From: Ulyanov Ivan Date: Thu, 31 Oct 2024 18:10:51 +0400 Subject: [PATCH] Fixed a bug that could cause regexp to issue an error (#165) * Fixed a bug that could cause regexp to issue an error * Tests have been changed --- index.js | 16 +++++++++------- index.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 8 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 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 {}` ) })