Skip to content

Commit df5f8a9

Browse files
docs: paths and patterns difference in no-restricted-imports (#18273)
* docs: mention paths and pattern difference * docs: add examples for clarification * fix typo * update examples
1 parent c537d76 commit df5f8a9

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

‎docs/src/rules/no-restricted-imports.md‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,31 @@ import { AllowedObject } from "foo";
286286

287287
This is also an object option whose value is an array. This option allows you to specify multiple modules to restrict using `gitignore`-style patterns.
288288

289+
Where `paths` option takes exact import paths, `patterns` option can be used to specify the import paths with more flexibility, allowing for the restriction of multiple modules within the same directory. For example:
290+
291+
```json
292+
"no-restricted-imports": ["error", {
293+
"paths": [{
294+
"name": "import-foo",
295+
}]
296+
}]
297+
```
298+
299+
This configuration restricts import of the `import-foo` module but wouldn't restrict the import of `import-foo/bar` or `import-foo/baz`. You can use `patterns` to restrict both:
300+
301+
```json
302+
"no-restricted-imports": ["error", {
303+
"paths": [{
304+
"name": "import-foo",
305+
}],
306+
"patterns": [{
307+
"group": ["import-foo/ba*"],
308+
}]
309+
}]
310+
```
311+
312+
This configuration restricts imports not just from `import-foo` using `path`, but also `import-foo/bar` and `import-foo/baz` using `patterns`.
313+
289314
Because the patterns follow the `gitignore`-style, if you want to reinclude any particular module this can be done by prefixing a negation (`!`) mark in front of the pattern. (Negated patterns should come last in the array because order is important.)
290315

291316
```json
@@ -294,7 +319,7 @@ Because the patterns follow the `gitignore`-style, if you want to reinclude any
294319
}]
295320
```
296321

297-
Examples of **incorrect** code for `pattern` option:
322+
Examples of **incorrect** code for `patterns` option:
298323

299324
::: incorrect { "sourceType": "module" }
300325

@@ -328,7 +353,7 @@ import pick from 'import1/private/someModule';
328353

329354
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"`.)
330355

331-
Examples of **correct** code for `pattern` option:
356+
Examples of **correct** code for `patterns` option:
332357

333358
::: correct { "sourceType": "module" }
334359

0 commit comments

Comments
 (0)