source: frontend/node_modules/eslint-plugin-import/docs/rules/no-relative-parent-imports.md

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.6 KB
Line 
1# import/no-relative-parent-imports
2
3<!-- end auto-generated rule header -->
4
5Use this rule to prevent imports to folders in relative parent paths.
6
7This rule is useful for enforcing tree-like folder structures instead of complex graph-like folder structures. While this restriction might be a departure from Node's default resolution style, it can lead large, complex codebases to be easier to maintain. If you've ever had debates over "where to put files" this rule is for you.
8
9To fix violations of this rule there are three general strategies. Given this example:
10
11```pt
12numbers
13└── three.js
14add.js
15```
16
17```js
18// ./add.js
19export default function (numbers) {
20 return numbers.reduce((sum, n) => sum + n, 0);
21}
22
23// ./numbers/three.js
24import add from '../add'; // violates import/no-relative-parent-imports
25
26export default function three() {
27 return add([1, 2]);
28}
29```
30
31You can,
32
331. Move the file to be in a sibling folder (or higher) of the dependency.
34
35 `three.js` could be be in the same folder as `add.js`:
36
37 ```pt
38 three.js
39 add.js
40 ```
41
42 or since `add` doesn't have any imports, it could be in it's own directory (namespace):
43
44 ```pt
45 math
46 └── add.js
47 three.js
48 ```
49
502. Pass the dependency as an argument at runtime (dependency injection)
51
52 ```js
53 // three.js
54 export default function three(add) {
55 return add([1, 2]);
56 }
57
58 // somewhere else when you use `three.js`:
59 import add from './add';
60 import three from './numbers/three';
61 console.log(three(add));
62 ```
63
643. Make the dependency a package so it's globally available to all files in your project:
65
66 ```js
67 import add from 'add'; // from https://www.npmjs.com/package/add
68 export default function three() {
69 return add([1,2]);
70 }
71 ```
72
73These are (respectively) static, dynamic & global solutions to graph-like dependency resolution.
74
75## Examples
76
77Given the following folder structure:
78
79```pt
80my-project
81├── lib
82│ ├── a.js
83│ └── b.js
84└── main.js
85```
86
87And the .eslintrc file:
88
89```json
90{
91 ...
92 "rules": {
93 "import/no-relative-parent-imports": "error"
94 }
95}
96```
97
98The following patterns are considered problems:
99
100```js
101/**
102 * in my-project/lib/a.js
103 */
104
105import bar from '../main'; // Import parent file using a relative path
106```
107
108The following patterns are NOT considered problems:
109
110```js
111/**
112 * in my-project/main.js
113 */
114
115import foo from 'foo'; // Import package using module path
116import a from './lib/a'; // Import child file using relative path
117
118/**
119 * in my-project/lib/a.js
120 */
121
122import b from './b'; // Import sibling file using relative path
123```
Note: See TracBrowser for help on using the repository browser.