@mrhenry/stylelint-mrhenry-nesting 1.0.2 → 2.0.0

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/README.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  [![version](https://img.shields.io/npm/v/@mrhenry/stylelint-mrhenry-nesting.svg)](https://www.npmjs.com/package/@mrhenry/stylelint-mrhenry-nesting)
4
4
 
5
+ Specify a strict coding style for CSS nesting.
6
+
7
+ ```css
8
+ /* valid, the nested selector is a "filter" for "focus" */
9
+ .element {
10
+ &:focus {}
11
+ }
12
+
13
+ /* invalid, the nested selector is not a "filter" on the elements matched by the parent */
14
+ .foo {
15
+ > .bar {}
16
+ }
17
+ ```
18
+
5
19
  Use CSS nesting only for conditional styling.
6
20
  This style of CSS aims to be expressive and readable.
7
21
 
@@ -9,6 +23,7 @@ This style of CSS aims to be expressive and readable.
9
23
  - every nested selector must start with `&`
10
24
  - only a single pseudo after `&`
11
25
 
26
+ **Valid CSS :**
12
27
 
13
28
  ```css
14
29
  .element {
@@ -26,8 +41,7 @@ This style of CSS aims to be expressive and readable.
26
41
  }
27
42
  ```
28
43
 
29
- --------
30
-
44
+ **Invalid CSS :**
31
45
 
32
46
  ```css
33
47
  /* invalid, the nested selector is not a "filter" on the elements matched by the parent */
@@ -70,3 +84,18 @@ This style of CSS aims to be expressive and readable.
70
84
  }
71
85
  ```
72
86
 
87
+ ## Usage
88
+
89
+ `npm install --save-dev @mrhenry/stylelint-mrhenry-nesting`
90
+
91
+ ```js
92
+ // stylelint.config.js
93
+ module.exports = {
94
+ plugins: [
95
+ "@mrhenry/stylelint-mrhenry-nesting",
96
+ ],
97
+ rules: {
98
+ "@mrhenry/stylelint-mrhenry-nesting": true,
99
+ },
100
+ }
101
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrhenry/stylelint-mrhenry-nesting",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "Mr. Henry's preferred way of writing nested CSS",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,7 +1,7 @@
1
1
  const stylelint = require("stylelint");
2
2
  const selectorParser = require('postcss-selector-parser');
3
3
 
4
- const ruleName = "plugin/stylelint-mrhenry-nesting";
4
+ const ruleName = "@mrhenry/stylelint-mrhenry-nesting";
5
5
  const messages = stylelint.utils.ruleMessages(ruleName, {
6
6
  rejectedAtRule: (name) => {
7
7
  return `Nested at-rules with name "${name}" is not allowed.`;
@@ -180,6 +180,21 @@ const ruleFunction = (primaryOption, secondaryOptionObject, context) => {
180
180
  };
181
181
 
182
182
  function fixSelector(rule, selectorsAST, selectorAST) {
183
+ if (
184
+ selectorAST.nodes?.length === 1 &&
185
+ selectorAST.nodes?.[0].type === 'pseudo'
186
+ ) {
187
+ selectorAST.replaceWith(selectorParser.selector({
188
+ nodes: [
189
+ selectorParser.nesting(),
190
+ selectorAST.nodes[0]
191
+ ]
192
+ }))
193
+
194
+ rule.selector = selectorsAST.toString();
195
+ return;
196
+ }
197
+
183
198
  selectorAST.replaceWith(selectorParser.selector({
184
199
  nodes: [
185
200
  selectorParser.nesting(),
@@ -191,6 +206,7 @@ function fixSelector(rule, selectorsAST, selectorAST) {
191
206
  }),
192
207
  ]
193
208
  }))
209
+
194
210
  rule.selector = selectorsAST.toString();
195
211
  }
196
212