Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: no-restricted-imports #18454

Closed
1 task
ArielGueta opened this issue May 15, 2024 · 9 comments
Closed
1 task

Docs: no-restricted-imports #18454

ArielGueta opened this issue May 15, 2024 · 9 comments
Labels
documentation Relates to ESLint's documentation needs info Not enough information has been provided to triage this issue

Comments

@ArielGueta
Copy link

Docs page(s)

https://eslint.org/docs/latest/rules/no-restricted-imports

What documentation issue do you want to solve?

I have the following configuration:

            "patterns": [
              {
                "importNamePattern": "_Enum$",
                "group": ["@app/api"]
              }
            ],

The issue is that it also errors for "@app/api/foo" etc. How can I make it exact?

What do you think is the correct solution?

To include "exact" example

Participation

  • I am willing to submit a pull request for this change.

Additional comments

No response

@ArielGueta ArielGueta added the documentation Relates to ESLint's documentation label May 15, 2024
@mdjermanovic
Copy link
Member

Hi @ArielGueta, thanks for the issue!

The issue is that it also errors for "@app/api/foo" etc. How can I make it exact?

Can you clarify with a few examples of code that should and code that shouldn't error what you mean by "exact"?

If you want to restrict imports from "@app/api" only, you can use paths.

@mdjermanovic mdjermanovic added the needs info Not enough information has been provided to triage this issue label May 15, 2024
@ArielGueta
Copy link
Author

Sure.

// Should be lint error
import { Foo_Enum } from '@app/api';

// These should be OK
import { Foo_Enum } from '@app/api/bar';
import { Foo_Enum } from '@app/api/baz';

@Tanujkanti4441
Copy link
Contributor

Hi @ArielGueta, to prevent the files inside @app/api from being reported you can use ! negation mark

eslint no-restricted-imports: ["error", { patterns: [{
    "importNamePattern": "_Enum$",
    "group": ["@app/api", "!@app/api/"]
}]}]

// error
import { Foo_Enum } from '@app/api';

// no error
import { Foo_Enum } from '@app/api/bar';
import { Foo_Enum } from '@app/api/baz';

playground

let us know if this solves your issue!

@ArielGueta
Copy link
Author

Works like a charm. Thanks!

It'll be valuable example to add to the docs.

@eslint-github-bot
Copy link

It looks like there wasn't enough information for us to know how to help you, so we're closing the issue.

Thanks for your understanding.

@ArielGueta
Copy link
Author

ArielGueta commented May 30, 2024

Hi,

How can I allow only one import while exclude the others. This doesn't work from some reason:

/*eslint no-restricted-imports: ["error", { patterns: [{
    "importNamePattern": "_Enum$",
    "group": ["@app/api", "!@app/api/enums"]
}]}]*/

// error
import { Foo_Enum } from '@app/api';
import { Bar_Enum } from '@app/api/bar';
import { Baz_Enum } from '@app/api/baz';

// no error
import { B_Enum } from '@app/api/enums';

@Tanujkanti4441
Copy link
Contributor

To achieve this we have to write a series of patterns like this:

/*eslint no-restricted-imports: ["error", { patterns: [{
    "importNamePattern": "_Enum$",
    "group": ["@app/api", "!@app/api/", "@app/api/**", "!@app/api/enums"]
}]}]*/

// error
import { Foo_Enum } from '@app/api';
import { Bar_Enum } from '@app/api/bar';
import { Baz_Enum } from '@app/api/baz';

// no error
import { B_Enum } from '@app/api/enums';

playground

@ArielGueta
Copy link
Author

Thanks! I wish it was simple as just one regex.

@Tanujkanti4441
Copy link
Contributor

Thanks! I wish it was simple as just one regex.

You can open an issue for this idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Relates to ESLint's documentation needs info Not enough information has been provided to triage this issue
Projects
Archived in project
Development

No branches or pull requests

3 participants