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: update no-restricted-imports rule #18015

Merged
merged 5 commits into from Jan 29, 2024

Conversation

Tanujkanti4441
Copy link
Contributor

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[x] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Putted everything under a particular heading in no-restricted-imports rule docs page

Is there anything you'd like reviewers to focus on?

Fixes: #18002

@Tanujkanti4441 Tanujkanti4441 requested a review from a team as a code owner January 20, 2024 16:27
@eslint-github-bot eslint-github-bot bot added the documentation Relates to ESLint's documentation label Jan 20, 2024
Copy link

netlify bot commented Jan 20, 2024

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 82e08e2
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65b1c34ba5893b0008b1bc35
😎 Deploy Preview https://deploy-preview-18015--docs-eslint.netlify.app/rules/no-restricted-imports
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Tanujkanti4441 Tanujkanti4441 added the rule Relates to ESLint's core rules label Jan 20, 2024
@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Jan 21, 2024
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I've tried to clean up the language throughout, please take a look.

The syntax to specify restricted imports looks like this:
This rule has both string and object options to specify the imported modules to restrict.

Simplest way to specify a module that you want to restrict from being imported is to pass it's name as a string to the options:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Simplest way to specify a module that you want to restrict from being imported is to pass it's name as a string to the options:
Using string options, you can specify the name of a module that you want to restrict from being imported as a value in the rule options array:

"message": "Please use import-quux instead."
}]
```
Specified string can also restrict the module from being exported:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Specified string can also restrict the module from being exported:
String options also restrict the module from being exported, as in this example:

Regex patterns can also be used to restrict specific import Name:
:::

You may also specify a custom message for a particular module simply using `name` and `message` properties inside an object, where the value of the `name` property will be the name of the module and `message` property will contain the custom message. The custom message will be appended to the default error message:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You may also specify a custom message for a particular module simply using `name` and `message` properties inside an object, where the value of the `name` property will be the name of the module and `message` property will contain the custom message. The custom message will be appended to the default error message:
You may also specify a custom message for a particular module using the `name` and `message` properties inside an object, where the value of the `name` is the name of the module and `message` property contains the custom message. (The custom message is appended to the default error message from the rule.)


```js
/*eslint no-restricted-imports: ["error", "fs"]*/
This is an object option whose value is an array and the name of the module you want to restrict can be specified inside this array, same as in string option above.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is an object option whose value is an array and the name of the module you want to restrict can be specified inside this array, same as in string option above.
This is an object option whose value is an array containing the names of the modules you want to restrict.

```

:::

::: incorrect { "sourceType": "module" }
Custom messages for a particular module can also be specified in `paths` array option using objects as above.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Custom messages for a particular module can also be specified in `paths` array option using objects as above.
Custom messages for a particular module can also be specified in `paths` array using objects with `name` and `message`.

```

:::

Examples of **correct** code for this rule:
In last example `"!import1/private/*"` is not reincluding the modules inside private directory because in `gitignore-style` negation mark (`!`) does not reinclude the files if it's parent directory is excluded by a pattern. In this case `import1/private` directory is already excluded by the `import1/*` pattern, but excluded directory can be reincluded like using `"!import1/private"`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In last example `"!import1/private/*"` is not reincluding the modules inside private directory because in `gitignore-style` negation mark (`!`) does not reinclude the files if it's parent directory is excluded by a pattern. In this case `import1/private` directory is already excluded by the `import1/*` pattern, but excluded directory can be reincluded like using `"!import1/private"`.
In this example, `"!import1/private/*"` is not reincluding the modules inside `private` because the negation mark (`!`) does not reinclude the files if it's parent directory is excluded by a pattern. In this case, `import1/private` directory is already excluded by the `import1/*` pattern. (The excluded directory can be reincluded using `"!import1/private"`.)

Comment on lines 313 to 315
Alternatively you can use this object property which is an array to specify the `gitignore-style` patterns for restricting modules. Custom messages can also be specified like `paths`.

This is a required property if you are using object options inside the `patterns` array:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Alternatively you can use this object property which is an array to specify the `gitignore-style` patterns for restricting modules. Custom messages can also be specified like `paths`.
This is a required property if you are using object options inside the `patterns` array:
The `patterns` array can also include objects. The `group` property is used to specify the `gitignore`-style patterns for restricting modules and the `message` property is used to specify a custom message.
The `group` property is required property when using objects inside the `patterns` array.

@@ -381,6 +356,36 @@ import lodash from 'lodash';

:::

#### caseSensitive

This is a boolean option and allows the patterns specified in the `group` array to be case-sensitive when sets to `true`. Default is `false`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is a boolean option and allows the patterns specified in the `group` array to be case-sensitive when sets to `true`. Default is `false`:
This is a boolean option and sets the patterns specified in the `group` array to be case-sensitive when `true`. Default is `false`.

@@ -394,6 +399,38 @@ import pick from 'food';

:::

#### importNames

`patterns` too has an `importNames` property and this works similar as in `paths`, it just looks for patterns specified inside the `group` array:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`patterns` too has an `importNames` property and this works similar as in `paths`, it just looks for patterns specified inside the `group` array:
You can also specify `importNames` on objects inside of `patterns`. In this case, the specified names are applied only to the specified `group`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! till here

Comment on lines 521 to 528
To restrict the use of all Node.js core imports (via <https://github.com/nodejs/node/tree/master/lib>):

```json
"no-restricted-imports": ["error",
"assert","buffer","child_process","cluster","crypto","dgram","dns","domain","events","freelist","fs","http","https","module","net","os","path","punycode","querystring","readline","repl","smalloc","stream","string_decoder","sys","timers","tls","tracing","tty","url","util","vm","zlib"
],
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave this out. It's a very specific use case that it easy to figure out if you need it. (It would probably also be a better idea to use the globals package for this purpose.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i this means we can remove this section?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we can remove this section. It's not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@nzakas nzakas merged commit 505fbf4 into eslint:main Jan 29, 2024
17 of 18 checks passed
@Tanujkanti4441 Tanujkanti4441 deleted the docs-refactor branch January 30, 2024 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion documentation Relates to ESLint's documentation rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Docs: options section in no-restricted-import rule
3 participants