ota-meshi / eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.
https://ota-meshi.github.io/eslint-plugin-regexp/
MIT License
701 stars 10 forks source link

Generate string literal types for configs #740

Closed karlhorky closed 6 months ago

karlhorky commented 6 months ago

Hi there, first of all thanks for this ESLint plugin, great to learn about regular expression antipatterns!

Using the tseslint.config() config helper function from typescript-eslint leads to an error:

import tseslint from 'typescript-eslint';

const config = tseslint.config(
  regexp.configs['flat/recommended'] // 💥 Argument of type 'typeof import("/Users/k/p/project/node_modules/eslint-plugin-regexp/dist/configs/flat/recommended")' is not assignable to parameter of type 'ConfigWithExtends'.
                                     // (see more below)
)

Error message:

Argument of type 'typeof import("/Users/k/p/project/node_modules/eslint-plugin-regexp/dist/configs/flat/recommended")' is not assignable to parameter of type 'ConfigWithExtends'.
  Types of property 'rules' are incompatible.
    Type '{ "no-control-regex": string; "no-misleading-character-class": string; "no-regex-spaces": string; "prefer-regex-literals": string; "no-invalid-regexp": string; "no-useless-backreference": string; ... 60 more ...; "regexp/use-ignore-case": string; }' is not assignable to type 'Partial<Record<string, RuleEntry>>'.
      Property '"no-control-regex"' is incompatible with index signature.
        Type 'string' is not assignable to type 'RuleEntry | undefined'.ts(2345)

This is because the types for typescript-eslint require a string of 'error', 'off' or 'warn'

To avoid the type mismatch when using typescript-eslint, this PR aims to generate string literal config property values instead of the current property values of string

changeset-bot[bot] commented 6 months ago

🦋 Changeset detected

Latest commit: 71e159abf51e23896ee8e6bb68deee19a40dffb1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | -------------------- | ----- | | eslint-plugin-regexp | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

karlhorky commented 6 months ago

@ota-meshi thanks for the feedback!

I've added a SeverityString type and updated the files you mentioned in 7efd9e3d6ebc93043d0b8bb4a9114063e316e317

karlhorky commented 6 months ago

Thanks for the review, merge and publish in eslint-plugin-regexp@2.6.0 🙌

I can confirm that this fixed the type error mentioned above with typescript-eslint types - no type error anymore 👍 :

Screenshot 2024-05-21 at 16 55 58