Repository: stylelint-scss/stylelint-scss
Pull request #608 (2022-05-19 04:29:27)

Add `at-import-partial-extension` autofix only when `"never"`
This PR adds the *autofix* support to the `scss/at-import-partial-extension` rule only when the `"never"` option is given.

I believe we cannot support autofix when `"always"` is given because it's impossible to determine file extensions to be added.

kristerkari: Thanks! More stuff for my review queue :D

Patch:
diff --git a/README.md b/README.md
index b4b2488..13944c9 100644
--- a/README.md
+++ b/README.md
@@ -84,7 +84,7 @@ Please also see the [example configs](./docs/examples/) for special cases.
 ### `@`-import
 
 - [`at-import-no-partial-leading-underscore`](./src/rules/at-import-no-partial-leading-underscore/README.md): Disallow leading underscore in partial names in `@import`.
-- [`at-import-partial-extension`](./src/rules/at-import-partial-extension/README.md): Require or disallow extension in `@import` commands.
+- [`at-import-partial-extension`](./src/rules/at-import-partial-extension/README.md): Require or disallow extension in `@import` commands (Autofixable).
 - [`at-import-partial-extension-blacklist`](./src/rules/at-import-partial-extension-blacklist/README.md): Specify blacklist of disallowed file extensions for partial names in `@import` commands.
 - [`at-import-partial-extension-whitelist`](./src/rules/at-import-partial-extension-whitelist/README.md): Specify whitelist of allowed file extensions for partial names in `@import` commands.
 
diff --git a/src/rules/at-else-closing-brace-newline-after/README.md b/src/rules/at-else-closing-brace-newline-after/README.md
index e5e2d7f..8f12537 100644
--- a/src/rules/at-else-closing-brace-newline-after/README.md
+++ b/src/rules/at-else-closing-brace-newline-after/README.md
@@ -11,7 +11,7 @@ Require or disallow a newline after the closing brace of `@else` statements.
  * The newline after these braces */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 This rule might have conflicts with stylelint's core rule [`block-closing-brace-newline-after`](https://stylelint.io/user-guide/rules/block-closing-brace-newline-after) if it doesn't have `"ignoreAtRules": ["else"]` in a `.stylelintrc` config file.  That's because an `@else { ... }` statement can be successfully parsed as an at-rule with a block. You might also want to set `"ignoreAtRules": ["else"]` for another stylelint's core rule - [`at-rule-empty-line-before`](https://stylelint.io/user-guide/rules/at-rule-empty-line-before) that could be forcing empty lines before at-rules (including `@else`s that follow `@if`s or other `@else`s).
 
diff --git a/src/rules/at-else-closing-brace-space-after/README.md b/src/rules/at-else-closing-brace-space-after/README.md
index 720deaa..142aad5 100644
--- a/src/rules/at-else-closing-brace-space-after/README.md
+++ b/src/rules/at-else-closing-brace-space-after/README.md
@@ -10,7 +10,7 @@ Require a single space or disallow whitespace after the closing brace of `@else`
  * The space after this brace */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 This rule might have conflicts with stylelint's core [`block-closing-brace-space-after`](https://stylelint.io/user-guide/rules/block-closing-brace-space-after) rule if the latter is set up in your `.stylelintrc` config file.
 
@@ -43,7 +43,7 @@ The following patterns are considered warnings:
   // ...
 } @else if ($x == 2) {
   // ...
-} 
+}
 @else { }
 
 @if ($x == 1) {
@@ -61,7 +61,7 @@ The following patterns are *not* considered warnings:
 } @else if ($x == 2) {
   // ...
 } @else {}
-      
+
 a {
   @if ($x == 1) {
     // ...
@@ -71,7 +71,7 @@ a {
   width: 10px;
 }
 
-@if ($x == 1) { } @else if ($x == 2) { 
+@if ($x == 1) { } @else if ($x == 2) {
   // ...
 } @include x;
 
diff --git a/src/rules/at-else-empty-line-before/README.md b/src/rules/at-else-empty-line-before/README.md
index 724a83e..aa94c12 100644
--- a/src/rules/at-else-empty-line-before/README.md
+++ b/src/rules/at-else-empty-line-before/README.md
@@ -11,7 +11,7 @@ Require an empty line or disallow empty lines before `@`-else.
  * This empty line */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 `@if` and `@else` statements might need to have different behavior than all the other at-rules. For that you might need to set `"ignoreAtRules": ["else"]` for stylelint's core rule [`at-rule-empty-line-before`](https://stylelint.io/user-guide/rules/at-rule-empty-line-before). But that would make you unable to disallow empty lines before `@else` while forcing it to be on a new line. This rule is designed to solve exactly that.
 
@@ -53,7 +53,7 @@ The following patterns are *not* considered warnings:
 } @else if ($x == 2) {
   // ...
 } @else {}
-      
+
 a {
   @if ($x == 1) {
     // ...
diff --git a/src/rules/at-else-if-parentheses-space-before/README.md b/src/rules/at-else-if-parentheses-space-before/README.md
index b56af2b..93de15e 100644
--- a/src/rules/at-else-if-parentheses-space-before/README.md
+++ b/src/rules/at-else-if-parentheses-space-before/README.md
@@ -8,7 +8,7 @@ Require or disallow a space before `@else if` parentheses.
  * The space before this parenthesis */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/at-function-parentheses-space-before/README.md b/src/rules/at-function-parentheses-space-before/README.md
index 9d4d362..4029156 100644
--- a/src/rules/at-function-parentheses-space-before/README.md
+++ b/src/rules/at-function-parentheses-space-before/README.md
@@ -8,7 +8,7 @@ Require or disallow a space before `@function` parentheses.
  * The space before this parenthesis */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/at-if-closing-brace-newline-after/README.md b/src/rules/at-if-closing-brace-newline-after/README.md
index 79f79b0..cc45118 100644
--- a/src/rules/at-if-closing-brace-newline-after/README.md
+++ b/src/rules/at-if-closing-brace-newline-after/README.md
@@ -9,7 +9,7 @@ Require or disallow a newline after the closing brace of `@if` statements.
  * The newline after this brace */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 This rule might have conflicts with stylelint's core rule [`block-closing-brace-newline-after`](https://stylelint.io/user-guide/rules/block-closing-brace-newline-after) if it doesn't have `"ignoreAtRules": ["if"]` in a `.stylelintrc` config file. That's because an `@if { ... }` statement can be successfully parsed as an at-rule with a block. You might also want to set `"ignoreAtRules": ["else"]` for another stylelint's core rule - [`at-rule-empty-line-before`](https://stylelint.io/user-guide/rules/at-rule-empty-line-before) that could be forcing empty lines before at-rules (including `@else`s that follow `@if`s or other `@else`s).
 
diff --git a/src/rules/at-if-closing-brace-space-after/README.md b/src/rules/at-if-closing-brace-space-after/README.md
index de93f77..2c9d46a 100644
--- a/src/rules/at-if-closing-brace-space-after/README.md
+++ b/src/rules/at-if-closing-brace-space-after/README.md
@@ -9,7 +9,7 @@ Require a single space or disallow whitespace after the closing brace of `@if` s
  * The space after this brace */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 This rule might have conflicts with stylelint's core [`block-closing-brace-space-after`](https://stylelint.io/user-guide/rules/block-closing-brace-space-after) rule if the latter is set up in your `.stylelintrc` config file.
 
@@ -36,7 +36,7 @@ The following patterns are considered warnings:
 // `@if` has a space and a newline after the closing brace
 @if ($x == 1) {
   // ...
-} 
+}
 @else { }
 
 @if ($x == 1) {
@@ -86,7 +86,7 @@ The following patterns are *not* considered warnings:
 @if ($x == 1) {
   // ...
 }@else {}
-      
+
 a {
   @if ($x == 1) {}
   width: 10px;
diff --git a/src/rules/at-import-partial-extension/README.md b/src/rules/at-import-partial-extension/README.md
index 56603f5..b059c5b 100644
--- a/src/rules/at-import-partial-extension/README.md
+++ b/src/rules/at-import-partial-extension/README.md
@@ -8,6 +8,8 @@ Require or disallow extension in `@import` commands.
  * This extension */
 ```
 
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule only when `"never"` is given.
+
 The rule ignores [cases](https://sass-lang.com/documentation/at-rules/import) when Sass considers an `@import` command just a plain CSS import:
 
 - If the file’s extension is `.css`.
diff --git a/src/rules/at-import-partial-extension/index.js b/src/rules/at-import-partial-extension/index.js
index 0f90b44..9a5db91 100644
--- a/src/rules/at-import-partial-extension/index.js
+++ b/src/rules/at-import-partial-extension/index.js
@@ -28,7 +28,7 @@ const mediaQueryTypesRE = new RegExp(`(${mediaQueryTypes.join("|")})$`, "i");
 const stripPath = path =>
   path.replace(/^\s*(["'])\s*/, "").replace(/\s*(["'])\s*$/, "");
 
-export default function(expectation) {
+export default function(expectation, _, context) {
   return (root, result) => {
     const validOptions = utils.validateOptions(result, ruleName, {
       actual: expectation,
@@ -72,6 +72,13 @@ export default function(expectation) {
         }
 
         if (extension && expectation === "never") {
+          if (context.fix) {
+            const extPattern = new RegExp(`\\.${extension}(['" ]*)`, "g");
+            decl.params = decl.params.replace(extPattern, "$1");
+
+            return;
+          }
+
           utils.report({
             message: messages.rejected(extension),
             node: decl,
diff --git a/src/rules/at-mixin-argumentless-call-parentheses/README.md b/src/rules/at-mixin-argumentless-call-parentheses/README.md
index 0ee3d05..50089c1 100644
--- a/src/rules/at-mixin-argumentless-call-parentheses/README.md
+++ b/src/rules/at-mixin-argumentless-call-parentheses/README.md
@@ -8,7 +8,7 @@ Require or disallow parentheses in argumentless `@mixin` calls.
  *                 Such mixin calls */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/at-mixin-parentheses-space-before/README.md b/src/rules/at-mixin-parentheses-space-before/README.md
index 39253ab..6e291f3 100644
--- a/src/rules/at-mixin-parentheses-space-before/README.md
+++ b/src/rules/at-mixin-parentheses-space-before/README.md
@@ -8,7 +8,7 @@ Require or disallow a space before `@mixin` parentheses.
  * The space before this parenthesis */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
@@ -16,7 +16,7 @@ The `--fix` option on the [command line](https://github.com/stylelint/stylelint/
 
 ### `"always"`
 
-There *must always* be exactly one space between the mixin name and the opening parenthesis. 
+There *must always* be exactly one space between the mixin name and the opening parenthesis.
 
 *Note: This rule does not enforce parentheses to be present in a declaration without arguments.*
 
diff --git a/src/rules/at-rule-conditional-no-parentheses/README.md b/src/rules/at-rule-conditional-no-parentheses/README.md
index 7c90591..e389992 100644
--- a/src/rules/at-rule-conditional-no-parentheses/README.md
+++ b/src/rules/at-rule-conditional-no-parentheses/README.md
@@ -8,7 +8,7 @@ Disallow parentheses in conditional @ rules (if, elsif, while)
  * Get rid of parentheses like this. */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/dollar-variable-colon-newline-after/README.md b/src/rules/dollar-variable-colon-newline-after/README.md
index 238218b..c194597 100644
--- a/src/rules/dollar-variable-colon-newline-after/README.md
+++ b/src/rules/dollar-variable-colon-newline-after/README.md
@@ -11,7 +11,7 @@ $box-shadow:
  * The newline after this colon */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/dollar-variable-colon-space-after/README.md b/src/rules/dollar-variable-colon-space-after/README.md
index df0d41c..bc594c6 100644
--- a/src/rules/dollar-variable-colon-space-after/README.md
+++ b/src/rules/dollar-variable-colon-space-after/README.md
@@ -8,7 +8,7 @@ $variable: 10px;
  * The space after this colon */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/dollar-variable-colon-space-before/README.md b/src/rules/dollar-variable-colon-space-before/README.md
index 536a70f..1036dae 100644
--- a/src/rules/dollar-variable-colon-space-before/README.md
+++ b/src/rules/dollar-variable-colon-space-before/README.md
@@ -8,7 +8,7 @@ $variable: 10px;
  * The space before this colon */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/dollar-variable-empty-line-after/README.md b/src/rules/dollar-variable-empty-line-after/README.md
index ffe3520..3ba9cff 100644
--- a/src/rules/dollar-variable-empty-line-after/README.md
+++ b/src/rules/dollar-variable-empty-line-after/README.md
@@ -4,7 +4,7 @@ Require an empty line or disallow empty lines after `$`-variable declarations.
 
 If the `$`-variable declaration is the last declaration in a file, it's ignored.
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
@@ -143,7 +143,7 @@ The following patterns are *not* considered warnings:
 a {
   $var: 1;
   $var1: 2;
-  
+
   b {
     width: 100%;
   }
diff --git a/src/rules/dollar-variable-empty-line-before/README.md b/src/rules/dollar-variable-empty-line-before/README.md
index 92e5f0e..c1b9519 100644
--- a/src/rules/dollar-variable-empty-line-before/README.md
+++ b/src/rules/dollar-variable-empty-line-before/README.md
@@ -11,7 +11,7 @@ $width: 10px;   ↑
  * This empty line */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 ## Options
 
diff --git a/src/rules/double-slash-comment-empty-line-before/README.md b/src/rules/double-slash-comment-empty-line-before/README.md
index 7cf8a09..52bda18 100644
--- a/src/rules/double-slash-comment-empty-line-before/README.md
+++ b/src/rules/double-slash-comment-empty-line-before/README.md
@@ -10,7 +10,7 @@ a {}
 *     This line */
 ```
 
-The `--fix` option on the [command line](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/cli.md#autofixing-errors) can automatically fix all of the problems reported by this rule.
+The [`fix` option](https://stylelint.io/user-guide/usage/options#fix) can automatically fix all of the problems reported by this rule.
 
 This rule only works with SCSS-like [single-line comments](https://sass-lang.com/documentation/syntax/comments) and ignores:
 * comments that are the very first nodes in a file;


Test patch:
diff --git a/src/rules/at-import-partial-extension/__tests__/index.js b/src/rules/at-import-partial-extension/__tests__/index.js
index 647298b..7c83881 100644
--- a/src/rules/at-import-partial-extension/__tests__/index.js
+++ b/src/rules/at-import-partial-extension/__tests__/index.js
@@ -182,6 +182,7 @@ testRule({
   ruleName,
   config: ["never"],
   customSyntax: "postcss-scss",
+  fix: true,
 
   accept: [
     {
@@ -300,7 +301,10 @@ testRule({
     {
       code: `
       @import "fff.scss";
-    `,
+     `,
+      fixed: `
+      @import "fff";
+     `,
       line: 2,
       column: 20,
       message: messages.rejected("scss"),
@@ -309,6 +313,9 @@ testRule({
     {
       code: `
       @import "screen.scss";
+    `,
+      fixed: `
+      @import "screen";
     `,
       line: 2,
       column: 23,
@@ -318,6 +325,9 @@ testRule({
     {
       code: `
       @import "fff.scss ";
+    `,
+      fixed: `
+      @import "fff ";
     `,
       line: 2,
       column: 20,
@@ -327,6 +337,9 @@ testRule({
     {
       code: `
       @import " fff.scss ";
+    `,
+      fixed: `
+      @import " fff ";
     `,
       line: 2,
       column: 21,
@@ -336,6 +349,9 @@ testRule({
     {
       code: `
       @import "df/fff.scss";
+    `,
+      fixed: `
+      @import "df/fff";
     `,
       line: 2,
       column: 23,
@@ -345,6 +361,9 @@ testRule({
     {
       code: `
       @import "df\\fff.scss";
+    `,
+      fixed: `
+      @import "df\\fff";
     `,
       line: 2,
       column: 23,
@@ -355,6 +374,9 @@ testRule({
     {
       code: `
       @import "df/fff", '_1.scss';
+    `,
+      fixed: `
+      @import "df/fff", '_1';
     `,
       line: 2,
       column: 29,