]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-restricted-exports | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
eb39fafa DC |
6 | |
7 | In a project, certain names may be disallowed from being used as exported names for various reasons. | |
8 | ||
9 | ## Rule Details | |
10 | ||
11 | This rule disallows specified names from being used as exported names. | |
12 | ||
13 | ## Options | |
14 | ||
15 | By default, this rule doesn't disallow any names. Only the names you specify in the configuration will be disallowed. | |
16 | ||
17 | This 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 | ||
29 | Examples 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 | ||
38 | export const foo = 1; | |
39 | ||
40 | export function bar() {} | |
41 | ||
42 | export class Baz {} | |
43 | ||
44 | const a = {}; | |
45 | export { a }; | |
46 | ||
47 | function someFunction() {} | |
48 | export { someFunction as b }; | |
49 | ||
8f9d1d4d DC |
50 | export { c } from "some_module"; |
51 | ||
52 | export { "d" } from "some_module"; | |
53 | ||
54 | export { something as e } from "some_module"; | |
eb39fafa | 55 | |
8f9d1d4d | 56 | export { "👍" } from "some_module"; |
eb39fafa DC |
57 | ``` |
58 | ||
8f9d1d4d DC |
59 | ::: |
60 | ||
f2a92ac6 | 61 | Examples 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 | ||
70 | export const quux = 1; | |
71 | ||
72 | export function myFunction() {} | |
73 | ||
74 | export class MyClass {} | |
75 | ||
76 | const a = {}; | |
77 | export { a as myObject }; | |
78 | ||
79 | function someFunction() {} | |
80 | export { someFunction }; | |
81 | ||
8f9d1d4d DC |
82 | export { c as someName } from "some_module"; |
83 | ||
84 | export { "d" as " d " } from "some_module"; | |
85 | ||
86 | export { something } from "some_module"; | |
eb39fafa | 87 | |
8f9d1d4d | 88 | export { "👍" as thumbsUp } from "some_module"; |
eb39fafa DC |
89 | ``` |
90 | ||
8f9d1d4d DC |
91 | ::: |
92 | ||
f2a92ac6 | 93 | #### Default exports |
eb39fafa | 94 | |
f2a92ac6 | 95 | By 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 | 97 | Examples 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 | ||
104 | function foo() {} | |
105 | ||
106 | export { 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 | 116 | export { default } from "some_module"; |
eb39fafa DC |
117 | ``` |
118 | ||
8f9d1d4d DC |
119 | ::: |
120 | ||
f2a92ac6 | 121 | Examples 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 | ||
128 | export default function foo() {} | |
129 | ``` | |
130 | ||
8f9d1d4d DC |
131 | ::: |
132 | ||
f2a92ac6 DC |
133 | ### restrictDefaultExports |
134 | ||
135 | This 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 | ||
139 | Examples 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 | ||
146 | export default foo; | |
147 | export default 42; | |
148 | export default function foo() {} | |
149 | ``` | |
150 | ||
151 | ::: | |
152 | ||
153 | #### named | |
154 | ||
155 | Examples 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 | ||
162 | const foo = 123; | |
163 | ||
164 | export { foo as default }; | |
165 | ``` | |
166 | ||
167 | ::: | |
168 | ||
169 | #### defaultFrom | |
170 | ||
171 | Examples 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 | ||
178 | export { default } from 'foo'; | |
179 | export { default as default } from 'foo'; | |
180 | ``` | |
181 | ||
182 | ::: | |
183 | ||
184 | #### namedFrom | |
185 | ||
186 | Examples 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 | ||
193 | export { foo as default } from 'foo'; | |
194 | ``` | |
195 | ||
196 | ::: | |
197 | ||
198 | #### namespaceFrom | |
199 | ||
200 | Examples 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 | ||
207 | export * as default from 'foo'; | |
208 | ``` | |
209 | ||
210 | ::: | |
211 | ||
eb39fafa DC |
212 | ## Known Limitations |
213 | ||
214 | This 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 ----- | |
219 | export function foo() {} | |
220 | ||
221 | //----- my_module.js ----- | |
222 | /*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["foo"] }]*/ | |
223 | ||
8f9d1d4d | 224 | export * from "some_module"; // allowed, although this declaration exports "foo" from my_module |
eb39fafa | 225 | ``` |