postcss-unique-selectors 6.0.4 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-unique-selectors",
3
- "version": "6.0.4",
3
+ "version": "7.0.1",
4
4
  "description": "Ensure CSS selectors are unique.",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -23,16 +23,16 @@
23
23
  },
24
24
  "repository": "cssnano/cssnano",
25
25
  "dependencies": {
26
- "postcss-selector-parser": "^6.0.16"
26
+ "postcss-selector-parser": "^6.1.0"
27
27
  },
28
28
  "bugs": {
29
29
  "url": "https://github.com/cssnano/cssnano/issues"
30
30
  },
31
31
  "engines": {
32
- "node": "^14 || ^16 || >=18.0"
32
+ "node": "^18.12.0 || ^20.9.0 || >=22.0"
33
33
  },
34
34
  "devDependencies": {
35
- "postcss": "^8.4.37"
35
+ "postcss": "^8.4.38"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "postcss": "^8.4.31"
package/src/index.js CHANGED
@@ -3,23 +3,45 @@ const selectorParser = require('postcss-selector-parser');
3
3
 
4
4
  /**
5
5
  * @param {string} selectors
6
- * @param {selectorParser.SyncProcessor<void>} callback
7
6
  * @return {string}
8
7
  */
9
- function parseSelectors(selectors, callback) {
10
- return selectorParser(callback).processSync(selectors);
11
- }
8
+ function generateUniqueSelector(selectors) {
9
+ /** @type {Map<string, string>} */
10
+ const uniqueSelectors = new Map();
12
11
 
13
- /**
14
- * @param {import('postcss').Rule} rule
15
- * @return {string}
16
- */
17
- function unique(rule) {
18
- const selector = [...new Set(rule.selectors)];
19
- selector.sort();
20
- return selector.join();
21
- }
12
+ /** @type {selectorParser.SyncProcessor<void>} */
13
+ const collectUniqueSelectors = (selNode) => {
14
+ for (const node of selNode.nodes) {
15
+ /** @type {string[]} */
16
+ const comments = [];
17
+
18
+ // Duplicates are removed by stripping the comments and using the results as the Map key.
19
+ const keyNode = node.clone();
20
+ keyNode.walk((sel) => {
21
+ if (sel.type === 'comment') {
22
+ comments.push(sel.value);
23
+ sel.remove();
24
+ }
25
+ });
26
+ const key = keyNode.toString().trim();
27
+
28
+ const dupeSelector = uniqueSelectors.get(key);
29
+ if (!dupeSelector) {
30
+ uniqueSelectors.set(key, node.toString());
31
+ } else if (comments.length) {
32
+ // If the duplicate selector has a comment, it is concatenated to the end of the selector.
33
+ uniqueSelectors.set(key, `${dupeSelector}${comments.join('')}`);
34
+ }
35
+ }
36
+ };
37
+
38
+ selectorParser(collectUniqueSelectors).processSync(selectors);
22
39
 
40
+ return [...uniqueSelectors.entries()]
41
+ .sort(([a], [b]) => (a > b ? 1 : a < b ? -1 : 0))
42
+ .map(([, selector]) => selector)
43
+ .join();
44
+ }
23
45
  /**
24
46
  * @type {import('postcss').PluginCreator<void>}
25
47
  * @return {import('postcss').Plugin}
@@ -29,27 +51,13 @@ function pluginCreator() {
29
51
  postcssPlugin: 'postcss-unique-selectors',
30
52
  OnceExit(css) {
31
53
  css.walkRules((nodes) => {
32
- /** @type {string[]} */
33
- let comments = [];
34
- /** @type {selectorParser.SyncProcessor<void>} */
35
- const removeAndSaveComments = (selNode) => {
36
- selNode.walk((sel) => {
37
- if (sel.type === 'comment') {
38
- comments.push(sel.value);
39
- sel.remove();
40
- return;
41
- } else {
42
- return;
43
- }
44
- });
45
- };
46
54
  if (nodes.raws.selector && nodes.raws.selector.raw) {
47
- parseSelectors(nodes.raws.selector.raw, removeAndSaveComments);
48
- nodes.raws.selector.raw = unique(nodes);
55
+ nodes.raws.selector.raw = generateUniqueSelector(
56
+ nodes.raws.selector.raw
57
+ );
58
+ } else {
59
+ nodes.selector = generateUniqueSelector(nodes.selector);
49
60
  }
50
- nodes.selector = parseSelectors(nodes.selector, removeAndSaveComments);
51
- nodes.selector = unique(nodes);
52
- nodes.selectors = nodes.selectors.concat(comments);
53
61
  });
54
62
  },
55
63
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AAsBA;;;GAGG;AACH,kCAFY,OAAO,SAAS,EAAE,MAAM,CA+BnC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AA4CA;;;GAGG;AACH,kCAFY,OAAO,SAAS,EAAE,MAAM,CAiBnC"}