postcss-merge-rules 5.1.1 → 5.1.2

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.
Files changed (2) hide show
  1. package/package.json +3 -4
  2. package/src/index.js +22 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-merge-rules",
3
- "version": "5.1.1",
3
+ "version": "5.1.2",
4
4
  "description": "Merge CSS rules with PostCSS.",
5
5
  "types": "types/index.d.ts",
6
6
  "main": "src/index.js",
@@ -38,10 +38,9 @@
38
38
  "devDependencies": {
39
39
  "@types/caniuse-api": "^3.0.2",
40
40
  "postcss": "^8.2.15",
41
- "postcss-discard-comments": "^5.1.0"
41
+ "postcss-discard-comments": "^5.1.2"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "postcss": "^8.2.15"
45
- },
46
- "readme": "# [postcss][postcss]-merge-rules\n\n> Merge CSS rules with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-merge-rules) do:\n\n```\nnpm install postcss-merge-rules --save\n```\n\n## Examples\n\nThis module will attempt to merge *adjacent* CSS rules:\n\n### By declarations\n\n#### Input\n\n```css\na {\n color: blue;\n font-weight: bold\n}\n\np {\n color: blue;\n font-weight: bold\n}\n```\n\n#### Output\n\n```css\na,p {\n color: blue;\n font-weight: bold\n}\n```\n\n### By selectors\n\n#### Input\n\n```css\na {\n color: blue\n}\n\na {\n font-weight: bold\n}\n```\n\n#### Output\n\n```css\na {\n color: blue;\n font-weight: bold\n}\n```\n\n### By partial declarations\n\n#### Input\n\n```css\na {\n font-weight: bold\n}\n\np {\n color: blue;\n font-weight: bold\n}\n```\n\n#### Output\n\n```css\na,p {\n font-weight: bold\n}\n\np {\n color: blue\n}\n```\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
45
+ }
47
46
  }
package/src/index.js CHANGED
@@ -36,7 +36,7 @@ function indexOfDeclaration(array, decl) {
36
36
  */
37
37
  function intersect(a, b, not) {
38
38
  return a.filter((c) => {
39
- const index = ~indexOfDeclaration(b, c);
39
+ const index = indexOfDeclaration(b, c) !== -1;
40
40
  return not ? !index : index;
41
41
  });
42
42
  }
@@ -74,21 +74,32 @@ function canMerge(ruleA, ruleB, browsers, compatibilityCache) {
74
74
  /** @type {any} */ (ruleA),
75
75
  /** @type {any} */ (ruleB)
76
76
  );
77
- const { name } = /** @type {any} */ (ruleA).parent;
78
- if (parent && name && name.includes('keyframes')) {
77
+ if (
78
+ parent &&
79
+ ruleA.parent &&
80
+ ruleA.parent.type === 'atrule' &&
81
+ /** @type {import('postcss').AtRule} */ (ruleA.parent).name.includes(
82
+ 'keyframes'
83
+ )
84
+ ) {
79
85
  return false;
80
86
  }
81
87
  return parent && (selectors.every(noVendor) || sameVendor(a, b));
82
88
  }
83
89
 
90
+ /**
91
+ * @param {import('postcss').ChildNode} node
92
+ * @return {node is import('postcss').Declaration}
93
+ */
94
+ function isDeclaration(node) {
95
+ return node.type === 'decl';
96
+ }
84
97
  /**
85
98
  * @param {import('postcss').Rule} rule
86
99
  * @return {import('postcss').Declaration[]}
87
100
  */
88
101
  function getDecls(rule) {
89
- return /** @type {import('postcss').Declaration[]} */ (
90
- rule.nodes.filter((node) => node.type === 'decl')
91
- );
102
+ return rule.nodes.filter(isDeclaration);
92
103
  }
93
104
 
94
105
  /** @type {(...rules: import('postcss').Rule[]) => string} */
@@ -290,7 +301,7 @@ function partialMerge(first, second) {
290
301
  */
291
302
  function moveDecl(callback) {
292
303
  return (decl) => {
293
- if (~indexOfDeclaration(intersection, decl)) {
304
+ if (indexOfDeclaration(intersection, decl) !== -1) {
294
305
  callback.call(this, decl);
295
306
  }
296
307
  };
@@ -359,17 +370,12 @@ function selectorMerger(browsers, compatibilityCache) {
359
370
  // e.g. a { color: blue } a { font-weight: bold }
360
371
  if (cache.selector === rule.selector) {
361
372
  const cached = getDecls(cache);
362
- rule.walk((decl) => {
363
- if (
364
- ~indexOfDeclaration(
365
- cached,
366
- /** @type {import('postcss').Declaration} */ (decl)
367
- )
368
- ) {
369
- decl.remove();
373
+ rule.walk((node) => {
374
+ if (node.type === 'decl' && indexOfDeclaration(cached, node) !== -1) {
375
+ node.remove();
370
376
  return;
371
377
  }
372
- /** @type {import('postcss').Rule} */ (cache).append(decl);
378
+ /** @type {import('postcss').Rule} */ (cache).append(node);
373
379
  });
374
380
  rule.remove();
375
381
  return;