From 12fc98c5183ced0a344e8aa1e591c8edac701f67 Mon Sep 17 00:00:00 2001 From: Ulyanov Ivan Date: Thu, 31 Oct 2024 18:10:51 +0400 Subject: [PATCH 1/3] 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 {}` ) }) From 60759a225f0148f00e4b43e8aea8198211fbd9f6 Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Thu, 31 Oct 2024 17:40:49 +0000 Subject: [PATCH 2/3] Update dependencies --- package.json | 2 +- pnpm-lock.yaml | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 2cdb99c..96b86b8 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "eslint": "^9.13.0", "eslint-config-standard": "^17.1.0", "eslint-plugin-import": "^2.31.0", - "eslint-plugin-n": "^17.11.1", + "eslint-plugin-n": "^17.12.0", "eslint-plugin-node-import": "^1.0.4", "eslint-plugin-perfectionist": "^3.9.1", "eslint-plugin-prefer-let": "^4.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1e3c97f..7766f3c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,13 +26,13 @@ importers: version: 9.13.0 eslint-config-standard: specifier: ^17.1.0 - version: 17.1.0(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0))(eslint-plugin-n@17.11.1(eslint@9.13.0))(eslint-plugin-promise@7.1.0(eslint@9.13.0))(eslint@9.13.0) + version: 17.1.0(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0))(eslint-plugin-n@17.12.0(eslint@9.13.0))(eslint-plugin-promise@7.1.0(eslint@9.13.0))(eslint@9.13.0) eslint-plugin-import: specifier: ^2.31.0 version: 2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0) eslint-plugin-n: - specifier: ^17.11.1 - version: 17.11.1(eslint@9.13.0) + specifier: ^17.12.0 + version: 17.12.0(eslint@9.13.0) eslint-plugin-node-import: specifier: ^1.0.4 version: 1.0.4(eslint@9.13.0) @@ -507,8 +507,8 @@ packages: '@typescript-eslint/parser': optional: true - eslint-plugin-n@17.11.1: - resolution: {integrity: sha512-93IUD82N6tIEgjztVI/l3ElHtC2wTa9boJHrD8iN+NyDxjxz/daZUZKfkedjBZNdg6EqDk4irybUsiPwDqXAEA==} + eslint-plugin-n@17.12.0: + resolution: {integrity: sha512-zNAtz/erDn0v78bIY3MASSQlyaarV4IOTvP5ldHsqblRFrXriikB6ghkDTkHjUad+nMRrIbOy9euod2azjRfBg==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: '>=8.23.0' @@ -1141,8 +1141,8 @@ packages: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} - ts-api-utils@1.3.0: - resolution: {integrity: sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==} + ts-api-utils@1.4.0: + resolution: {integrity: sha512-032cPxaEKwM+GT3vA5JXNzIaizx388rhsSW79vGRNGXfRRAdEAn2mvk36PvK5HnOchyWZ7afLEXqYCvPCrzuzQ==} engines: {node: '>=16'} peerDependencies: typescript: '>=4.2.0' @@ -1320,9 +1320,9 @@ snapshots: dependencies: '@eslint/eslintrc': 3.1.0 eslint: 9.13.0 - eslint-config-standard: 17.1.0(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0))(eslint-plugin-n@17.11.1(eslint@9.13.0))(eslint-plugin-promise@7.1.0(eslint@9.13.0))(eslint@9.13.0) + eslint-config-standard: 17.1.0(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0))(eslint-plugin-n@17.12.0(eslint@9.13.0))(eslint-plugin-promise@7.1.0(eslint@9.13.0))(eslint@9.13.0) eslint-plugin-import: 2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0) - eslint-plugin-n: 17.11.1(eslint@9.13.0) + eslint-plugin-n: 17.12.0(eslint@9.13.0) eslint-plugin-perfectionist: 3.9.1(eslint@9.13.0)(typescript@5.5.3) eslint-plugin-prefer-let: 4.0.0 eslint-plugin-promise: 7.1.0(eslint@9.13.0) @@ -1374,7 +1374,7 @@ snapshots: graphemer: 1.4.0 ignore: 5.3.2 natural-compare: 1.4.0 - ts-api-utils: 1.3.0(typescript@5.5.3) + ts-api-utils: 1.4.0(typescript@5.5.3) optionalDependencies: typescript: 5.5.3 transitivePeerDependencies: @@ -1403,7 +1403,7 @@ snapshots: '@typescript-eslint/typescript-estree': 8.12.2(typescript@5.5.3) '@typescript-eslint/utils': 8.12.2(eslint@9.13.0)(typescript@5.5.3) debug: 4.3.7 - ts-api-utils: 1.3.0(typescript@5.5.3) + ts-api-utils: 1.4.0(typescript@5.5.3) optionalDependencies: typescript: 5.5.3 transitivePeerDependencies: @@ -1421,7 +1421,7 @@ snapshots: is-glob: 4.0.3 minimatch: 9.0.5 semver: 7.6.3 - ts-api-utils: 1.3.0(typescript@5.5.3) + ts-api-utils: 1.4.0(typescript@5.5.3) optionalDependencies: typescript: 5.5.3 transitivePeerDependencies: @@ -1738,11 +1738,11 @@ snapshots: eslint: 9.13.0 semver: 7.6.3 - eslint-config-standard@17.1.0(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0))(eslint-plugin-n@17.11.1(eslint@9.13.0))(eslint-plugin-promise@7.1.0(eslint@9.13.0))(eslint@9.13.0): + eslint-config-standard@17.1.0(eslint-plugin-import@2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0))(eslint-plugin-n@17.12.0(eslint@9.13.0))(eslint-plugin-promise@7.1.0(eslint@9.13.0))(eslint@9.13.0): dependencies: eslint: 9.13.0 eslint-plugin-import: 2.31.0(@typescript-eslint/parser@8.12.2(eslint@9.13.0)(typescript@5.5.3))(eslint@9.13.0) - eslint-plugin-n: 17.11.1(eslint@9.13.0) + eslint-plugin-n: 17.12.0(eslint@9.13.0) eslint-plugin-promise: 7.1.0(eslint@9.13.0) eslint-import-resolver-node@0.3.9: @@ -1799,7 +1799,7 @@ snapshots: - eslint-import-resolver-webpack - supports-color - eslint-plugin-n@17.11.1(eslint@9.13.0): + eslint-plugin-n@17.12.0(eslint@9.13.0): dependencies: '@eslint-community/eslint-utils': 4.4.1(eslint@9.13.0) enhanced-resolve: 5.17.1 @@ -2441,7 +2441,7 @@ snapshots: dependencies: is-number: 7.0.0 - ts-api-utils@1.3.0(typescript@5.5.3): + ts-api-utils@1.4.0(typescript@5.5.3): dependencies: typescript: 5.5.3 From 9f75b3804d67ed80e91fdcb7fa36b113e5ddc93c Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Thu, 31 Oct 2024 18:41:36 +0100 Subject: [PATCH 3/3] Release 7.0.1 version --- CHANGELOG.md | 3 +++ package.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fe861a..1646602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Change Log This project adheres to [Semantic Versioning](http://semver.org/). +## 7.0.1 +* Fixed RegExp issue with nested comments (by @Ulyanov-programmer). + ## 7.0.0 * More complex logic of when to move comments (by @Ulyanov-programmer). * Removed Node.js 16, 14, and 12 support. diff --git a/package.json b/package.json index 96b86b8..74e0b03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-nested", - "version": "7.0.0", + "version": "7.0.1", "description": "PostCSS plugin to unwrap nested rules like how Sass does it", "keywords": [ "postcss",