source: frontend/node_modules/eslint-plugin-import/docs/rules/no-restricted-paths.md@ 9af201e

Last change on this file since 9af201e was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.7 KB
Line 
1# import/no-restricted-paths
2
3<!-- end auto-generated rule header -->
4
5Some projects contain files which are not always meant to be executed in the same environment.
6For example consider a web application that contains specific code for the server and some specific code for the browser/client. In this case you don’t want to import server-only files in your client code.
7
8In order to prevent such scenarios this rule allows you to define restricted zones where you can forbid files from being imported if they match a specific path.
9
10## Rule Details
11
12This 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.
13The default for `basePath` is the current working directory.
14
15Each 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`.*
34
35### Examples
36
37Given this folder structure:
38
39```pt
40.
41├── client
42│ ├── foo.js
43│ └── baz.js
44└── server
45 └── bar.js
46```
47
48And this configuration:
49
50```json
51{
52 "zones": [
53 {
54 "target": "./client",
55 "from": "./server"
56 }
57 ]
58}
59```
60
61:x: The following is considered incorrect:
62
63```js
64// client/foo.js
65import bar from '../server/bar';
66```
67
68:white_check_mark: The following is considered correct:
69
70```js
71// server/bar.js
72import baz from '../client/baz';
73```
74
75---------------
76
77Given this folder structure:
78
79```pt
80.
81├── client
82│ └── ...
83└── server
84 ├── one
85 │ ├── a.js
86 │ └── b.js
87 └── two
88 └── a.js
89```
90
91And this configuration:
92
93```json
94{
95 "zones": [
96 {
97 "target": "./server/one",
98 "from": "./server",
99 "except": ["./one"]
100 }
101 ]
102}
103```
104
105:x: The following is considered incorrect:
106
107```js
108// server/one/a.js
109import a from '../two/a'
110```
111
112:white_check_mark: The following is considered correct:
113
114```js
115// server/one/a.js
116import b from './b'
117```
118
119---------------
120
121Given this folder structure:
122
123```pt
124.
125└── client
126 ├── foo.js
127 └── sub-module
128 ├── bar.js
129 └── baz.js
130```
131
132And this configuration:
133
134```json
135{
136 "zones": [
137 {
138 "target": "./client/!(sub-module)/**/*",
139 "from": "./client/sub-module/**/*",
140 }
141 ]
142}
143```
144
145:x: The following is considered incorrect:
146
147```js
148// client/foo.js
149import a from './sub-module/baz'
150```
151
152:white_check_mark: The following is considered correct:
153
154```js
155// client/sub-module/bar.js
156import b from './baz'
157```
158
159---------------
160
161Given this folder structure:
162
163```pt
164.
165├── one
166│ ├── a.js
167│ └── b.js
168├── two
169│ ├── a.js
170│ └── b.js
171└── three
172 ├── a.js
173 └── b.js
174```
175
176And this configuration:
177
178```json
179{
180 "zones": [
181 {
182 "target": [
183 "./two/*",
184 "./three/*"
185 ],
186 "from": [
187 "./one",
188 "./three"
189 ]
190 }
191 ]
192}
193```
194
195:white_check_mark: The following is considered correct:
196
197```js
198// one/b.js
199import a from '../three/a'
200import a from './a'
201```
202
203```js
204// two/b.js
205import a from './a'
206```
207
208:x: The following is considered incorrect:
209
210```js
211// two/a.js
212import a from '../one/a'
213import a from '../three/a'
214```
215
216```js
217// three/b.js
218import a from '../one/a'
219import a from './a'
220```
Note: See TracBrowser for help on using the repository browser.