You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to have multiple configurations for a rule run on different but potentially overlapping file sets
Use case 1: Composable no-restricted-imports
I have a monorepo with multiple types of applications (react apps, node servers, etc). I am using flat configs (and loving them!) to create configs for different topics (ex. typescript, react, next). Relevant configs are then composed in an app's eslint.config.mjs.
For certain rules such as no-restricted-imports I want multiple topic configs to restrict relevant imports
But this requires users to know the internals of these configs and is very easy to get wrong or unintentionally break, and becomes very difficult to manage as the number of configs/fileset permutations increase
Use case 2: Warning + Errors
Extending the reactConfig example there are some dependencies that we flat out do not want to use and others that we are deprecating. We would like to flag the former as error but the latter with warn
What do you think is the correct solution?
The easiest solution here is to create separate rules for the different configs. This can be accomplished by duplicating the implementation of no-restricted-imports but this adds maintenance overhead of keeping the rules up-to-date with eslint's implementation.
An idea that I had and am trialing in my repo is to create a factory that creates custom no-restricted-imports rules by wrapping the actual implementation of no-restricted-imports
// eslint does not list this file in `exports` so we have to import it directlyimportnoRestrictedImportsfrom"../../../../node_modules/eslint/lib/rules/no-restricted-imports.js";/** * Creates a custom `no-restricted-imports` rule with the given options. * * `options` should be the 2nd parameter when configuring the default no-restricted-imports rule. * ``` * "no-restricted-imports": ["error", options] * ``` */exportfunctioncreateNoRestrictedImportsRule(options){return{meta: {
...noRestrictedImports.meta,schema: [],},create: (context)=>{returnnoRestrictedImports.create({
...context,options: [options],});},};}constnoRestrictedReactImportsRule=createNoRestrictedImportsRule({paths: [{name: "<dependency-that-we-are-deprecating>"}]})constreactConfig={plugins: {"@myorg-react": {rules: {"no-restricted-imports": noRestrictedReactImportsRule}}},rules: {"@myorg-react/no-restricted-imports": ["error"]}
This is hacky because I have to absolutely import the file to bypass the exports in the package.json not exposing this file. If my use-case is valid I would like eslint to support creating variants of rules with specific options baked into them. This can be accomplished through a factory function. This could either be a factory at the rule-level or a factory that works for any rule
ESLint version
v8.57.0
What problem do you want to solve?
On a high level
I would like to be able to have multiple configurations for a rule run on different but potentially overlapping file sets
Use case 1: Composable
no-restricted-imports
I have a monorepo with multiple types of applications (react apps, node servers, etc). I am using flat configs (and loving them!) to create configs for different topics (ex.
typescript
,react
,next
). Relevant configs are then composed in an app'seslint.config.mjs
.For certain rules such as
no-restricted-imports
I want multiple topic configs to restrict relevant importsThe problem is that only the restrictions from the last config will apply. I want the restrictions from all to apply.
It is possible to manually process these in the eslint config
But this requires users to know the internals of these configs and is very easy to get wrong or unintentionally break, and becomes very difficult to manage as the number of configs/fileset permutations increase
Use case 2: Warning + Errors
Extending the
reactConfig
example there are some dependencies that we flat out do not want to use and others that we are deprecating. We would like to flag the former aserror
but the latter withwarn
What do you think is the correct solution?
The easiest solution here is to create separate rules for the different configs. This can be accomplished by duplicating the implementation of
no-restricted-imports
but this adds maintenance overhead of keeping the rules up-to-date with eslint's implementation.An idea that I had and am trialing in my repo is to create a factory that creates custom
no-restricted-imports
rules by wrapping the actual implementation ofno-restricted-imports
This is hacky because I have to absolutely import the file to bypass the
exports
in the package.json not exposing this file. If my use-case is valid I would like eslint to support creating variants of rules with specific options baked into them. This can be accomplished through a factory function. This could either be a factory at the rule-level or a factory that works for any ruleParticipation
Additional comments
No response
The text was updated successfully, but these errors were encountered: