Skip to content

Commit f017790

Browse files
greimljharb
authored andcommitted
[Docs] no-restricted-paths: clarify wording and fix errors
1 parent 7d83a57 commit f017790

File tree

2 files changed

+106
-81
lines changed

2 files changed

+106
-81
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2828
- [readme] Update flatConfig example to include typescript config ([#3138], thanks [@intellix])
2929
- [Refactor] [`order`]: remove unnecessary negative check ([#3167], thanks [@JounQin])
3030
- [Docs] [`no-unused-modules`]: add missing double quote ([#3191], thanks [@albertpastrana])
31+
- [Docs] `no-restricted-paths`: clarify wording and fix errors ([#3172], thanks [@greim])
3132

3233
## [2.31.0] - 2024-10-03
3334

@@ -1179,6 +1180,7 @@ for info on changes for earlier releases.
11791180

11801181
[#3191]: https://github.com/import-js/eslint-plugin-import/pull/3191
11811182
[#3173]: https://github.com/import-js/eslint-plugin-import/pull/3173
1183+
[#3172]: https://github.com/import-js/eslint-plugin-import/pull/3172
11821184
[#3167]: https://github.com/import-js/eslint-plugin-import/pull/3167
11831185
[#3166]: https://github.com/import-js/eslint-plugin-import/pull/3166
11841186
[#3151]: https://github.com/import-js/eslint-plugin-import/pull/3151
@@ -1886,6 +1888,7 @@ for info on changes for earlier releases.
18861888
[@golopot]: https://github.com/golopot
18871889
[@GoodForOneFare]: https://github.com/GoodForOneFare
18881890
[@graingert]: https://github.com/graingert
1891+
[@greim]: https://github.com/greim
18891892
[@grit96]: https://github.com/grit96
18901893
[@guilhermelimak]: https://github.com/guilhermelimak
18911894
[@guillaumewuip]: https://github.com/guillaumewuip

docs/rules/no-restricted-paths.md

Lines changed: 103 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,190 +9,212 @@ In order to prevent such scenarios this rule allows you to define restricted zon
99

1010
## Rule Details
1111

12-
This rule has one option. The option is an object containing the definition of all restricted `zones` and the optional `basePath` which is used to resolve relative paths within.
13-
The default value for `basePath` is the current working directory.
14-
15-
Each zone consists of the `target` paths, a `from` paths, and an optional `except` and `message` attribute.
16-
17-
- `target` contains the paths where the restricted imports should be applied. It can be expressed by
18-
- directory string path that matches all its containing files
19-
- glob pattern matching all the targeted files
20-
- an array of multiple of the two types above
21-
- `from` paths define the folders that are not allowed to be used in an import. It can be expressed by
22-
- directory string path that matches all its containing files
23-
- glob pattern matching all the files restricted to be imported
24-
- an array of multiple directory string path
25-
- an array of multiple glob patterns
26-
- `except` may be defined for a zone, allowing exception paths that would otherwise violate the related `from`. Note that it does not alter the behaviour of `target` in any way.
27-
- in case `from` contains only glob patterns, `except` must be an array of glob patterns as well
28-
- in case `from` contains only directory path, `except` is relative to `from` and cannot backtrack to a parent directory
29-
- `message` - will be displayed in case of the rule violation.
12+
This rule has one option, which is an object containing all `zones` where restrictions will be applied, plus an optional `basePath` used to resolve relative paths within each zone.
13+
The default for `basePath` is the current working directory.
14+
15+
Each zone consists of a `target`, a `from`, and optional `except` and `message` attributes.
16+
17+
- `target` - Identifies which files are part of the zone. It can be expressed as:
18+
- A simple directory path, matching all files contained recursively within it
19+
- A glob pattern
20+
- An array of any of the two types above
21+
- *Example: `target: './client'` - this zone consists of all files under the 'client' dir*
22+
- `from` - Identifies folders from which the zone is not allowed to import. It can be expressed as:
23+
- A simple directory path, matching all files contained recursively within it
24+
- A glob pattern
25+
- An array of only simple directories, or of only glob patterns (mixing both types within the array is not allowed)
26+
- *Example: `from: './server'` - this zone is not allowed to import anything from the 'server' dir*
27+
- `except` - Optional. Allows exceptions that would otherwise violate the related `from`. Note that it does not alter the behaviour of `target` in any way.
28+
- If `from` is an array of glob patterns, `except` must be an array of glob patterns as well.
29+
- If `from` is an array of simple directories, `except` is relative to `from` and cannot backtrack to a parent directory.
30+
- *Example: `except: './server/config'` this zone is allowed to import server config, even if it can't import other server code*
31+
- `message` - Optional. Displayed in case of rule violation.
32+
33+
*Note: The `from` attribute is NOT matched literally against the import path string as it appears in the code. Instead, it's matched against the path to the imported file after it's been resolved against `basePath`.*
3034

3135
### Examples
3236

33-
Given the following folder structure:
37+
Given this folder structure:
3438

3539
```pt
36-
my-project
40+
.
3741
├── client
38-
── foo.js
42+
── foo.js
3943
│ └── baz.js
4044
└── server
4145
└── bar.js
4246
```
4347

44-
and the current file being linted is `my-project/client/foo.js`.
48+
And this configuration:
4549

46-
The following patterns are considered problems when configuration set to `{ "zones": [ { "target": "./client", "from": "./server" } ] }`:
50+
```json
51+
{
52+
"zones": [
53+
{
54+
"target": "./client",
55+
"from": "./server"
56+
}
57+
]
58+
}
59+
```
60+
61+
:x: The following is considered incorrect:
4762

4863
```js
64+
// client/foo.js
4965
import bar from '../server/bar';
5066
```
5167

52-
The following patterns are not considered problems when configuration set to `{ "zones": [ { "target": "./client", "from": "./server" } ] }`:
68+
:white_check_mark: The following is considered correct:
5369

5470
```js
71+
// server/bar.js
5572
import baz from '../client/baz';
5673
```
5774

5875
---------------
5976

60-
Given the following folder structure:
77+
Given this folder structure:
6178

6279
```pt
63-
my-project
80+
.
6481
├── client
65-
│ └── foo.js
66-
│ └── baz.js
82+
│ └── ...
6783
└── server
6884
├── one
69-
── a.js
85+
── a.js
7086
│ └── b.js
7187
└── two
88+
└── a.js
7289
```
7390

74-
and the current file being linted is `my-project/server/one/a.js`.
75-
76-
and the current configuration is set to:
91+
And this configuration:
7792

7893
```json
79-
{ "zones": [ {
80-
"target": "./tests/files/restricted-paths/server/one",
81-
"from": "./tests/files/restricted-paths/server",
82-
"except": ["./one"]
83-
} ] }
94+
{
95+
"zones": [
96+
{
97+
"target": "./server/one",
98+
"from": "./server",
99+
"except": ["./one"]
100+
}
101+
]
102+
}
84103
```
85104

86-
The following pattern is considered a problem:
105+
:x: The following is considered incorrect:
87106

88107
```js
108+
// server/one/a.js
89109
import a from '../two/a'
90110
```
91111

92-
The following pattern is not considered a problem:
112+
:white_check_mark: The following is considered correct:
93113

94114
```js
115+
// server/one/a.js
95116
import b from './b'
96-
97117
```
98118

99119
---------------
100120

101-
Given the following folder structure:
121+
Given this folder structure:
102122

103123
```pt
104-
my-project
105-
── client
106-
── foo.js
124+
.
125+
── client
126+
── foo.js
107127
└── sub-module
108-
── bar.js
128+
── bar.js
109129
└── baz.js
110-
111130
```
112131

113-
and the current configuration is set to:
132+
And this configuration:
114133

115134
```json
116-
{ "zones": [ {
117-
"target": "./tests/files/restricted-paths/client/!(sub-module)/**/*",
118-
"from": "./tests/files/restricted-paths/client/sub-module/**/*",
119-
} ] }
135+
{
136+
"zones": [
137+
{
138+
"target": "./client/!(sub-module)/**/*",
139+
"from": "./client/sub-module/**/*",
140+
}
141+
]
142+
}
120143
```
121144

122-
The following import is considered a problem in `my-project/client/foo.js`:
145+
:x: The following is considered incorrect:
123146

124147
```js
148+
// client/foo.js
125149
import a from './sub-module/baz'
126150
```
127151

128-
The following import is not considered a problem in `my-project/client/sub-module/bar.js`:
152+
:white_check_mark: The following is considered correct:
129153

130154
```js
155+
// client/sub-module/bar.js
131156
import b from './baz'
132157
```
133158

134159
---------------
135160

136-
Given the following folder structure:
161+
Given this folder structure:
137162

138163
```pt
139-
my-project
140-
── one
141-
── a.js
142-
└── b.js
143-
── two
144-
── a.js
145-
└── b.js
164+
.
165+
── one
166+
── a.js
167+
└── b.js
168+
── two
169+
── a.js
170+
└── b.js
146171
└── three
147-
── a.js
148-
└── b.js
172+
── a.js
173+
└── b.js
149174
```
150175

151-
and the current configuration is set to:
176+
And this configuration:
152177

153178
```json
154179
{
155180
"zones": [
156181
{
157-
"target": ["./tests/files/restricted-paths/two/*", "./tests/files/restricted-paths/three/*"],
158-
"from": ["./tests/files/restricted-paths/one", "./tests/files/restricted-paths/three"],
182+
"target": [
183+
"./two/*",
184+
"./three/*"
185+
],
186+
"from": [
187+
"./one",
188+
"./three"
189+
]
159190
}
160191
]
161192
}
162193
```
163194

164-
The following patterns are not considered a problem in `my-project/one/b.js`:
195+
:white_check_mark: The following is considered correct:
165196

166197
```js
198+
// one/b.js
167199
import a from '../three/a'
168-
```
169-
170-
```js
171200
import a from './a'
172201
```
173202

174-
The following pattern is not considered a problem in `my-project/two/b.js`:
175-
176203
```js
204+
// two/b.js
177205
import a from './a'
178206
```
179207

180-
The following patterns are considered a problem in `my-project/two/a.js`:
208+
:x: The following is considered incorrect:
181209

182210
```js
211+
// two/a.js
183212
import a from '../one/a'
184-
```
185-
186-
```js
187213
import a from '../three/a'
188214
```
189215

190-
The following patterns are considered a problem in `my-project/three/b.js`:
191-
192216
```js
217+
// three/b.js
193218
import a from '../one/a'
194-
```
195-
196-
```js
197219
import a from './a'
198220
```

0 commit comments

Comments
 (0)