]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-restricted-exports.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-restricted-exports.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-restricted-exports
8f9d1d4d
DC
3rule_type: suggestion
4---
5
eb39fafa
DC
6
7In a project, certain names may be disallowed from being used as exported names for various reasons.
8
9## Rule Details
10
11This rule disallows specified names from being used as exported names.
12
13## Options
14
15By default, this rule doesn't disallow any names. Only the names you specify in the configuration will be disallowed.
16
17This rule has an object option:
18
19* `"restrictedNamedExports"` is an array of strings, where each string is a name to be restricted.
f2a92ac6
DC
20* `"restrictDefaultExports"` is an object option with boolean properties to restrict certain default export declarations. The option works only if the `restrictedNamedExports` option does not contain the `"default"` value. The following properties are allowed:
21 * `direct`: restricts `export default` declarations.
22 * `named`: restricts `export { foo as default };` declarations.
23 * `defaultFrom`: restricts `export { default } from 'foo';` declarations.
24 * `namedFrom`: restricts `export { foo as default } from 'foo';` declarations.
25 * `namespaceFrom`: restricts `export * as default from 'foo';` declarations.
eb39fafa 26
f2a92ac6
DC
27### restrictedNamedExports
28
29Examples of **incorrect** code for the `"restrictedNamedExports"` option:
eb39fafa 30
8f9d1d4d
DC
31::: incorrect
32
eb39fafa
DC
33```js
34/*eslint no-restricted-exports: ["error", {
8f9d1d4d 35 "restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"]
eb39fafa
DC
36}]*/
37
38export const foo = 1;
39
40export function bar() {}
41
42export class Baz {}
43
44const a = {};
45export { a };
46
47function someFunction() {}
48export { someFunction as b };
49
8f9d1d4d
DC
50export { c } from "some_module";
51
52export { "d" } from "some_module";
53
54export { something as e } from "some_module";
eb39fafa 55
8f9d1d4d 56export { "👍" } from "some_module";
eb39fafa
DC
57```
58
8f9d1d4d
DC
59:::
60
f2a92ac6 61Examples of **correct** code for the `"restrictedNamedExports"` option:
eb39fafa 62
8f9d1d4d
DC
63::: correct
64
eb39fafa
DC
65```js
66/*eslint no-restricted-exports: ["error", {
8f9d1d4d 67 "restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"]
eb39fafa
DC
68}]*/
69
70export const quux = 1;
71
72export function myFunction() {}
73
74export class MyClass {}
75
76const a = {};
77export { a as myObject };
78
79function someFunction() {}
80export { someFunction };
81
8f9d1d4d
DC
82export { c as someName } from "some_module";
83
84export { "d" as " d " } from "some_module";
85
86export { something } from "some_module";
eb39fafa 87
8f9d1d4d 88export { "👍" as thumbsUp } from "some_module";
eb39fafa
DC
89```
90
8f9d1d4d
DC
91:::
92
f2a92ac6 93#### Default exports
eb39fafa 94
f2a92ac6 95By design, the `"restrictedNamedExports"` option doesn't disallow `export default` declarations. If you configure `"default"` as a restricted name, that restriction will apply only to named export declarations.
eb39fafa 96
f2a92ac6 97Examples of additional **incorrect** code for the `"restrictedNamedExports": ["default"]` option:
eb39fafa 98
8f9d1d4d
DC
99::: incorrect
100
eb39fafa
DC
101```js
102/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/
103
104function foo() {}
105
106export { foo as default };
107```
108
8f9d1d4d
DC
109:::
110
111::: incorrect
112
eb39fafa
DC
113```js
114/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/
115
8f9d1d4d 116export { default } from "some_module";
eb39fafa
DC
117```
118
8f9d1d4d
DC
119:::
120
f2a92ac6 121Examples of additional **correct** code for the `"restrictedNamedExports": ["default"]` option:
eb39fafa 122
8f9d1d4d
DC
123::: correct
124
eb39fafa
DC
125```js
126/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default", "foo"] }]*/
127
128export default function foo() {}
129```
130
8f9d1d4d
DC
131:::
132
f2a92ac6
DC
133### restrictDefaultExports
134
135This option allows you to restrict certain `default` declarations. The option works only if the `restrictedNamedExports` option does not contain the `"default"` value. This option accepts the following properties:
136
137#### direct
138
139Examples of **incorrect** code for the `"restrictDefaultExports": { "direct": true }` option:
140
141::: incorrect
142
143```js
144/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "direct": true } }]*/
145
146export default foo;
147export default 42;
148export default function foo() {}
149```
150
151:::
152
153#### named
154
155Examples of **incorrect** code for the `"restrictDefaultExports": { "named": true }` option:
156
157::: incorrect
158
159```js
160/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "named": true } }]*/
161
162const foo = 123;
163
164export { foo as default };
165```
166
167:::
168
169#### defaultFrom
170
171Examples of **incorrect** code for the `"restrictDefaultExports": { "defaultFrom": true }` option:
172
173::: incorrect
174
175```js
176/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "defaultFrom": true } }]*/
177
178export { default } from 'foo';
179export { default as default } from 'foo';
180```
181
182:::
183
184#### namedFrom
185
186Examples of **incorrect** code for the `"restrictDefaultExports": { "namedFrom": true }` option:
187
188::: incorrect
189
190```js
191/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "namedFrom": true } }]*/
192
193export { foo as default } from 'foo';
194```
195
196:::
197
198#### namespaceFrom
199
200Examples of **incorrect** code for the `"restrictDefaultExports": { "namespaceFrom": true }` option:
201
202::: incorrect
203
204```js
205/*eslint no-restricted-exports: ["error", { "restrictDefaultExports": { "namespaceFrom": true } }]*/
206
207export * as default from 'foo';
208```
209
210:::
211
eb39fafa
DC
212## Known Limitations
213
214This rule doesn't inspect the content of source modules in re-export declarations. In particular, if you are re-exporting everything from another module's export, that export may include a restricted name. This rule cannot detect such cases.
215
216```js
217
218//----- some_module.js -----
219export function foo() {}
220
221//----- my_module.js -----
222/*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["foo"] }]*/
223
8f9d1d4d 224export * from "some_module"; // allowed, although this declaration exports "foo" from my_module
eb39fafa 225```