Repository: stylelint-scss/stylelint-scss (js)

Issue:
[BUG] block-no-redundant-nesting autofix 
node: 20.3.1
stylelint: 15.11.0
stylelint-scss: 5.3.0

```
sni-brick {
   ...
  &[selected] {
    &:not(.wrong) { // <--- stylelint-scss correctly says it's unexpected.
      &[word=""] {
        opacity: .7 !important;
      }

      &:not([word=""]) {
        opacity: .7 !important;
      }
    }
  }
}
```

Autofixes that code to this which is wrong:

```
sni-brick {
   ...
   sni-brick[selected]:not(.wrong) {
      &[word=""] {
        opacity: .7 !important;
      }

      &:not([word=""]) {
        opacity: .7 !important;
      }
  }
}
```

Correct fix would be this:

```
sni-brick {
   ...
  &[selected]:not(.wrong) {
    &[word=""] {
      opacity: .7 !important;
    }

    &:not([word=""]) {
      opacity: .7 !important;
    }
  }
}
```


Pull request description:
Improve `block-no-redundant-nesting` autofix
* Fixes #875.

Patch:
diff --git a/src/rules/block-no-redundant-nesting/index.js b/src/rules/block-no-redundant-nesting/index.js
index bc9bfb0..8e2d3cb 100644
--- a/src/rules/block-no-redundant-nesting/index.js
+++ b/src/rules/block-no-redundant-nesting/index.js
@@ -1,6 +1,5 @@
 "use strict";
 
-const resolveNestedSelector = require("postcss-resolve-nested-selector");
 const { utils } = require("stylelint");
 const namespace = require("../../utils/namespace");
 const ruleUrl = require("../../utils/ruleUrl");
@@ -15,6 +14,13 @@ const meta = {
   url: ruleUrl(ruleName)
 };
 
+function resolveNestedSelector(parentSelector, nestedSelector) {
+  if (nestedSelector.includes("&")) {
+    return nestedSelector.replace(/&/g, parentSelector);
+  }
+  return [parentSelector, nestedSelector].join(" ");
+}
+
 function processRuleNode(ruleNode, result, context) {
   if (ruleNode.nodes.length !== 1) {
     return;
@@ -32,10 +38,11 @@ function processRuleNode(ruleNode, result, context) {
 
   if (context.fix) {
     ruleNode.selector = resolveNestedSelector(
-      nestedRuleNode.selector,
-      nestedRuleNode
-    )[0];
+      ruleNode.selector,
+      nestedRuleNode.selector
+    );
     ruleNode.nodes = nestedRuleNode.nodes;
+    ruleNode.raws.semicolon = nestedRuleNode.raws.semicolon;
 
     if (ruleNode.nodes.length === 1) {
       for (const rule of ruleNode.nodes) {


Test patch:
diff --git a/src/rules/block-no-redundant-nesting/__tests__/index.js b/src/rules/block-no-redundant-nesting/__tests__/index.js
index 31b14b4..445d3cc 100644
--- a/src/rules/block-no-redundant-nesting/__tests__/index.js
+++ b/src/rules/block-no-redundant-nesting/__tests__/index.js
@@ -109,7 +109,7 @@ testRule({
       `,
       fixed: `
         .foo-bar {
-            color: red
+            color: red;
         }
       `,
       description: "single child block with &-prefix and property",
@@ -161,7 +161,7 @@ testRule({
         .foo .bar {
         }
         .foo-bar-baz {
-              color: red
+              color: red;
         }
       `,
       description: "multiple single child blocks",
@@ -188,6 +188,39 @@ testRule({
           message: messages.rejected
         }
       ]
+    },
+    {
+      // https://github.com/stylelint-scss/stylelint-scss/issues/875
+      code: `
+        .foo {
+          &-bar {
+            &-baz {
+              color: red;
+            }
+          }
+
+          .bar {}
+        }
+      `,
+      fixed: `
+        .foo {
+          &-bar-baz {
+              color: red;
+          }
+
+          .bar {}
+        }
+      `,
+      description: "selector should not be fully resolved",
+      warnings: [
+        {
+          line: 4,
+          column: 13,
+          endLine: 6,
+          endColumn: 14,
+          message: messages.rejected
+        }
+      ]
     }
   ]
 });