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
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"`.) |
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! till here
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" | ||
], | ||
``` | ||
|
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
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 pageIs there anything you'd like reviewers to focus on?
Fixes: #18002