Repository: azat-io/eslint-plugin-perfectionist
Pull request #490 (2025-03-12 18:36:31)

fix: throw an error if RegExp expressions are entered
- Issue: https://github.com/azat-io/eslint-plugin-perfectionist/issues/488
- Alternative PR: https://github.com/azat-io/eslint-plugin-perfectionist/pull/489

## Description

This PR throws a custom error when a RegExp option is entered instead of a string.

![Screenshot 2025-03-13 at 12 55 17](https://github.com/user-attachments/assets/283fc3bb-0cbe-44a1-97c2-01d1d2aa5753)

In the next major version, the error can be added on the JSON schema level.

### What is the purpose of this pull request?

- [x] Bug fix


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/azat-io/eslint-plugin-perfectionist/pull/490?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azat+S.) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 100.00%. Comparing base [(`9da25bd`)](https://app.codecov.io/gh/azat-io/eslint-plugin-perfectionist/commit/9da25bd93ef71f0c6f357fdfd2411254e48de094?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azat+S.) to head [(`9081e4e`)](https://app.codecov.io/gh/azat-io/eslint-plugin-perfectionist/commit/9081e4e8e78e5a0a4c24aa6d6b85ef74eed89634?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azat+S.).
> Report is 1 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##              main      #490   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           94        94           
  Lines         7620      7621    +1     
  Branches      1634      1634           
=========================================
+ Hits          7620      7621    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/azat-io/eslint-plugin-perfectionist/pull/490?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azat+S.).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azat+S.).

<details><summary>🚀 New features to boost your workflow: </summary>

- ❄ [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- 📦 [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
hugop95: @azat-io Let me know if you prefer this fix over https://github.com/azat-io/eslint-plugin-perfectionist/pull/489 🙂
azat-io: I don't have a clear opinion on that. What do you think is the best way to do it?
hugop95: @azat-io I think that we can throw an error today, but customized (not through JSON schema yet):
- Configurations using RegExp expressions (not strings) simply do not work (as seen in https://github.com/azat-io/eslint-plugin-perfectionist/issues/488) but don't throw: I don't think that many users would keep them in their configs without realizing that this simply does nothing.
- Throwing "Please enter your RegExp expression as a string" will make it clear for users that still have invalid configurations.
- Allowing `RegExp` expressions would allow a bad habit that eventually would have to be fixed in the next major version by users.

I'll update this PR by:
- Commenting out the JSON schema check: this should be activated in next major version.
- Throw a custom error in the meantime.

Patch:
diff --git a/utils/common-json-schemas.ts b/utils/common-json-schemas.ts
index aafc0a9..f589256 100644
--- a/utils/common-json-schemas.ts
+++ b/utils/common-json-schemas.ts
@@ -137,6 +137,9 @@ let singleRegexJsonSchema: JSONSchema4 = {
         },
       },
       additionalProperties: false,
+      // https://github.com/azat-io/eslint-plugin-perfectionist/pull/490#issuecomment-2720969705
+      // Uncomment the code below in the next major version (v5)
+      // To uncomment: required: ['pattern'],
       type: 'object',
     },
     {
diff --git a/utils/matches.ts b/utils/matches.ts
index d6c54c8..38748f4 100644
--- a/utils/matches.ts
+++ b/utils/matches.ts
@@ -9,5 +9,14 @@ export let matches = (value: string, regexOption: RegexOption): boolean => {
     return new RegExp(regexOption).test(value)
   }
 
+  // Handler for non-string regexes until an error is thrown on the JSON schema
+  // Level
+  if ('source' in regexOption) {
+    throw new Error(
+      'Invalid configuration: please enter your RegExp expressions as strings.\n' +
+        'For example, write ".*foo" instead of /.*foo/',
+    )
+  }
+
   return new RegExp(regexOption.pattern, regexOption.flags).test(value)
 }


Test patch:
diff --git a/test/utils/matches.test.ts b/test/utils/matches.test.ts
index 0085ee0..fc2464c 100644
--- a/test/utils/matches.test.ts
+++ b/test/utils/matches.test.ts
@@ -31,4 +31,11 @@ describe('matches', () => {
       ]),
     ).toBeTruthy()
   })
+
+  it('should throw an error if the regex option is not a string', () => {
+    expect(() => matches('foo', /foo/u as unknown as string)).toThrow(
+      'Invalid configuration: please enter your RegExp expressions as strings.\n' +
+        'For example, write ".*foo" instead of /.*foo/',
+    )
+  })
 })