]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-restricted-exports.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / no-restricted-exports.js
1 /**
2 * @fileoverview Tests for the no-restricted-exports rule
3 * @author Milos Djermanovic
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-restricted-exports");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022, sourceType: "module" } });
20
21 ruleTester.run("no-restricted-exports", rule, {
22 valid: [
23
24 // nothing configured
25 "export var a;",
26 "export function a() {}",
27 "export class A {}",
28 "var a; export { a };",
29 "var b; export { b as a };",
30 "export { a } from 'foo';",
31 "export { b as a } from 'foo';",
32 { code: "export var a;", options: [{}] },
33 { code: "export function a() {}", options: [{}] },
34 { code: "export class A {}", options: [{}] },
35 { code: "var a; export { a };", options: [{}] },
36 { code: "var b; export { b as a };", options: [{}] },
37 { code: "export { a } from 'foo';", options: [{}] },
38 { code: "export { b as a } from 'foo';", options: [{}] },
39 { code: "export var a;", options: [{ restrictedNamedExports: [] }] },
40 { code: "export function a() {}", options: [{ restrictedNamedExports: [] }] },
41 { code: "export class A {}", options: [{ restrictedNamedExports: [] }] },
42 { code: "var a; export { a };", options: [{ restrictedNamedExports: [] }] },
43 { code: "var b; export { b as a };", options: [{ restrictedNamedExports: [] }] },
44 { code: "export { a } from 'foo';", options: [{ restrictedNamedExports: [] }] },
45 { code: "export { b as a } from 'foo';", options: [{ restrictedNamedExports: [] }] },
46
47 // not a restricted name
48 { code: "export var a;", options: [{ restrictedNamedExports: ["x"] }] },
49 { code: "export let a;", options: [{ restrictedNamedExports: ["x"] }] },
50 { code: "export const a = 1;", options: [{ restrictedNamedExports: ["x"] }] },
51 { code: "export function a() {}", options: [{ restrictedNamedExports: ["x"] }] },
52 { code: "export function *a() {}", options: [{ restrictedNamedExports: ["x"] }] },
53 { code: "export async function a() {}", options: [{ restrictedNamedExports: ["x"] }] },
54 { code: "export async function *a() {}", options: [{ restrictedNamedExports: ["x"] }] },
55 { code: "export class A {}", options: [{ restrictedNamedExports: ["x"] }] },
56 { code: "var a; export { a };", options: [{ restrictedNamedExports: ["x"] }] },
57 { code: "var b; export { b as a };", options: [{ restrictedNamedExports: ["x"] }] },
58 { code: "export { a } from 'foo';", options: [{ restrictedNamedExports: ["x"] }] },
59 { code: "export { b as a } from 'foo';", options: [{ restrictedNamedExports: ["x"] }] },
60 { code: "export { '' } from 'foo';", options: [{ restrictedNamedExports: ["undefined"] }] },
61 { code: "export { '' } from 'foo';", options: [{ restrictedNamedExports: [" "] }] },
62 { code: "export { ' ' } from 'foo';", options: [{ restrictedNamedExports: [""] }] },
63 { code: "export { ' a', 'a ' } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
64
65 // does not mistakenly disallow non-exported names that appear in named export declarations
66 { code: "export var b = a;", options: [{ restrictedNamedExports: ["a"] }] },
67 { code: "export let [b = a] = [];", options: [{ restrictedNamedExports: ["a"] }] },
68 { code: "export const [b] = [a];", options: [{ restrictedNamedExports: ["a"] }] },
69 { code: "export var { a: b } = {};", options: [{ restrictedNamedExports: ["a"] }] },
70 { code: "export let { b = a } = {};", options: [{ restrictedNamedExports: ["a"] }] },
71 { code: "export const { c: b = a } = {};", options: [{ restrictedNamedExports: ["a"] }] },
72 { code: "export function b(a) {}", options: [{ restrictedNamedExports: ["a"] }] },
73 { code: "export class A { a(){} }", options: [{ restrictedNamedExports: ["a"] }] },
74 { code: "export class A extends B {}", options: [{ restrictedNamedExports: ["B"] }] },
75 { code: "var a; export { a as b };", options: [{ restrictedNamedExports: ["a"] }] },
76 { code: "var a; export { a as 'a ' };", options: [{ restrictedNamedExports: ["a"] }] },
77 { code: "export { a as b } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
78 { code: "export { a as 'a ' } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
79 { code: "export { 'a' as 'a ' } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
80
81 // does not check source in re-export declarations
82 { code: "export { b } from 'a';", options: [{ restrictedNamedExports: ["a"] }] },
83 { code: "export * as b from 'a';", options: [{ restrictedNamedExports: ["a"] }] },
84
85 // does not check non-export declarations
86 { code: "var a;", options: [{ restrictedNamedExports: ["a"] }] },
87 { code: "let a;", options: [{ restrictedNamedExports: ["a"] }] },
88 { code: "const a = 1;", options: [{ restrictedNamedExports: ["a"] }] },
89 { code: "function a() {}", options: [{ restrictedNamedExports: ["a"] }] },
90 { code: "class A {}", options: [{ restrictedNamedExports: ["A"] }] },
91 { code: "import a from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
92 { code: "import { a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
93 { code: "import { b as a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
94
95 // does not check re-export all declarations
96 { code: "export * from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
97 { code: "export * from 'a';", options: [{ restrictedNamedExports: ["a"] }] },
98
99 // does not mistakenly disallow identifiers in export default declarations (a default export will export "default" name)
100 { code: "export default a;", options: [{ restrictedNamedExports: ["a"] }] },
101 { code: "export default function a() {}", options: [{ restrictedNamedExports: ["a"] }] },
102 { code: "export default class A {}", options: [{ restrictedNamedExports: ["A"] }] },
103 { code: "export default (function a() {});", options: [{ restrictedNamedExports: ["a"] }] },
104 { code: "export default (class A {});", options: [{ restrictedNamedExports: ["A"] }] },
105
106 // by design, restricted name "default" does not apply to default export declarations, although they do export the "default" name.
107 { code: "export default 1;", options: [{ restrictedNamedExports: ["default"] }] },
108
109 // "default" does not disallow re-exporting a renamed default export from another module
110 { code: "export { default as a } from 'foo';", options: [{ restrictedNamedExports: ["default"] }] },
111
112 // restrictDefaultExports.direct option
113 { code: "export default foo;", options: [{ restrictDefaultExports: { direct: false } }] },
114 { code: "export default 42;", options: [{ restrictDefaultExports: { direct: false } }] },
115 { code: "export default function foo() {}", options: [{ restrictDefaultExports: { direct: false } }] },
116
117 // restrictDefaultExports.named option
118 { code: "const foo = 123;\nexport { foo as default };", options: [{ restrictDefaultExports: { named: false } }] },
119
120 // restrictDefaultExports.defaultFrom option
121 { code: "export { default } from 'mod';", options: [{ restrictDefaultExports: { defaultFrom: false } }] },
122 { code: "export { default as default } from 'mod';", options: [{ restrictDefaultExports: { defaultFrom: false } }] },
123 { code: "export { foo as default } from 'mod';", options: [{ restrictDefaultExports: { defaultFrom: true } }] },
124 { code: "export { default } from 'mod';", options: [{ restrictDefaultExports: { named: true, defaultFrom: false } }] },
125 { code: "export { 'default' } from 'mod'; ", options: [{ restrictDefaultExports: { defaultFrom: false } }] },
126
127 // restrictDefaultExports.namedFrom option
128 { code: "export { foo as default } from 'mod';", options: [{ restrictDefaultExports: { namedFrom: false } }] },
129 { code: "export { default as default } from 'mod';", options: [{ restrictDefaultExports: { namedFrom: true } }] },
130 { code: "export { default as default } from 'mod';", options: [{ restrictDefaultExports: { namedFrom: false } }] },
131 { code: "export { 'default' } from 'mod'; ", options: [{ restrictDefaultExports: { defaultFrom: false, namedFrom: true } }] },
132
133 // restrictDefaultExports.namespaceFrom option
134 { code: "export * as default from 'mod';", options: [{ restrictDefaultExports: { namespaceFrom: false } }] }
135 ],
136
137 invalid: [
138
139 {
140 code: "export function someFunction() {}",
141 options: [{ restrictedNamedExports: ["someFunction"] }],
142 errors: [{ messageId: "restrictedNamed", data: { name: "someFunction" }, type: "Identifier" }]
143 },
144
145 // basic tests
146 {
147 code: "export var a;",
148 options: [{ restrictedNamedExports: ["a"] }],
149 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
150 },
151 {
152 code: "export var a = 1;",
153 options: [{ restrictedNamedExports: ["a"] }],
154 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
155 },
156 {
157 code: "export let a;",
158 options: [{ restrictedNamedExports: ["a"] }],
159 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
160 },
161 {
162 code: "export let a = 1;",
163 options: [{ restrictedNamedExports: ["a"] }],
164 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
165 },
166 {
167 code: "export const a = 1;",
168 options: [{ restrictedNamedExports: ["a"] }],
169 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
170 },
171 {
172 code: "export function a() {}",
173 options: [{ restrictedNamedExports: ["a"] }],
174 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
175 },
176 {
177 code: "export function *a() {}",
178 options: [{ restrictedNamedExports: ["a"] }],
179 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
180 },
181 {
182 code: "export async function a() {}",
183 options: [{ restrictedNamedExports: ["a"] }],
184 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
185 },
186 {
187 code: "export async function *a() {}",
188 options: [{ restrictedNamedExports: ["a"] }],
189 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
190 },
191 {
192 code: "export class A {}",
193 options: [{ restrictedNamedExports: ["A"] }],
194 errors: [{ messageId: "restrictedNamed", data: { name: "A" }, type: "Identifier" }]
195 },
196 {
197 code: "let a; export { a };",
198 options: [{ restrictedNamedExports: ["a"] }],
199 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
200 },
201 {
202 code: "export { a }; var a;",
203 options: [{ restrictedNamedExports: ["a"] }],
204 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 10 }]
205 },
206 {
207 code: "let b; export { b as a };",
208 options: [{ restrictedNamedExports: ["a"] }],
209 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
210 },
211 {
212 code: "export { a } from 'foo';",
213 options: [{ restrictedNamedExports: ["a"] }],
214 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
215 },
216 {
217 code: "export { b as a } from 'foo';",
218 options: [{ restrictedNamedExports: ["a"] }],
219 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
220 },
221
222 // string literals
223 {
224 code: "let a; export { a as 'a' };",
225 options: [{ restrictedNamedExports: ["a"] }],
226 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Literal", column: 22 }]
227 },
228 {
229 code: "let a; export { a as 'b' };",
230 options: [{ restrictedNamedExports: ["b"] }],
231 errors: [{ messageId: "restrictedNamed", data: { name: "b" }, type: "Literal", column: 22 }]
232 },
233 {
234 code: "let a; export { a as ' b ' };",
235 options: [{ restrictedNamedExports: [" b "] }],
236 errors: [{ messageId: "restrictedNamed", data: { name: " b " }, type: "Literal", column: 22 }]
237 },
238 {
239 code: "let a; export { a as '👍' };",
240 options: [{ restrictedNamedExports: ["👍"] }],
241 errors: [{ messageId: "restrictedNamed", data: { name: "👍" }, type: "Literal", column: 22 }]
242 },
243 {
244 code: "export { 'a' } from 'foo';",
245 options: [{ restrictedNamedExports: ["a"] }],
246 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Literal" }]
247 },
248 {
249 code: "export { '' } from 'foo';",
250 options: [{ restrictedNamedExports: [""] }],
251 errors: [{ messageId: "restrictedNamed", data: { name: "" }, type: "Literal" }]
252 },
253 {
254 code: "export { ' ' } from 'foo';",
255 options: [{ restrictedNamedExports: [" "] }],
256 errors: [{ messageId: "restrictedNamed", data: { name: " " }, type: "Literal" }]
257 },
258 {
259 code: "export { b as 'a' } from 'foo';",
260 options: [{ restrictedNamedExports: ["a"] }],
261 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Literal" }]
262 },
263 {
264 code: "export { b as '\\u0061' } from 'foo';",
265 options: [{ restrictedNamedExports: ["a"] }],
266 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Literal" }]
267 },
268 {
269 code: "export * as 'a' from 'foo';",
270 options: [{ restrictedNamedExports: ["a"] }],
271 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Literal" }]
272 },
273
274
275 // destructuring
276 {
277 code: "export var [a] = [];",
278 options: [{ restrictedNamedExports: ["a"] }],
279 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
280 },
281 {
282 code: "export let { a } = {};",
283 options: [{ restrictedNamedExports: ["a"] }],
284 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
285 },
286 {
287 code: "export const { b: a } = {};",
288 options: [{ restrictedNamedExports: ["a"] }],
289 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
290 },
291 {
292 code: "export var [{ a }] = [];",
293 options: [{ restrictedNamedExports: ["a"] }],
294 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
295 },
296 {
297 code: "export let { b: { c: a = d } = e } = {};",
298 options: [{ restrictedNamedExports: ["a"] }],
299 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
300 },
301
302 // reports the correct identifier node in the case of a redeclaration. Note: functions cannot be redeclared in a module.
303 {
304 code: "var a; export var a;",
305 options: [{ restrictedNamedExports: ["a"] }],
306 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 19 }]
307 },
308 {
309 code: "export var a; var a;",
310 options: [{ restrictedNamedExports: ["a"] }],
311 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 12 }]
312 },
313
314 // reports the correct identifier node when the same identifier appears elsewhere in the declaration
315 {
316 code: "export var a = a;",
317 options: [{ restrictedNamedExports: ["a"] }],
318 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 12 }]
319 },
320 {
321 code: "export let b = a, a;",
322 options: [{ restrictedNamedExports: ["a"] }],
323 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 19 }]
324 },
325 {
326 code: "export const a = 1, b = a;",
327 options: [{ restrictedNamedExports: ["a"] }],
328 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 14 }]
329 },
330 {
331 code: "export var [a] = a;",
332 options: [{ restrictedNamedExports: ["a"] }],
333 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 13 }]
334 },
335 {
336 code: "export let { a: a } = {};",
337 options: [{ restrictedNamedExports: ["a"] }],
338 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
339 },
340 {
341 code: "export const { a: b, b: a } = {};",
342 options: [{ restrictedNamedExports: ["a"] }],
343 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 25 }]
344 },
345 {
346 code: "export var { b: a, a: b } = {};",
347 options: [{ restrictedNamedExports: ["a"] }],
348 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
349 },
350 {
351 code: "export let a, { a: b } = {};",
352 options: [{ restrictedNamedExports: ["a"] }],
353 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 12 }]
354 },
355 {
356 code: "export const { a: b } = {}, a = 1;",
357 options: [{ restrictedNamedExports: ["a"] }],
358 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 29 }]
359 },
360 {
361 code: "export var [a = a] = [];",
362 options: [{ restrictedNamedExports: ["a"] }],
363 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 13 }]
364 },
365 {
366 code: "export var { a: a = a } = {};",
367 options: [{ restrictedNamedExports: ["a"] }],
368 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
369 },
370 {
371 code: "export let { a } = { a };",
372 options: [{ restrictedNamedExports: ["a"] }],
373 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 14 }]
374 },
375 {
376 code: "export function a(a) {};",
377 options: [{ restrictedNamedExports: ["a"] }],
378 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
379 },
380 {
381 code: "export class A { A(){} };",
382 options: [{ restrictedNamedExports: ["A"] }],
383 errors: [{ messageId: "restrictedNamed", data: { name: "A" }, type: "Identifier", column: 14 }]
384 },
385 {
386 code: "var a; export { a as a };",
387 options: [{ restrictedNamedExports: ["a"] }],
388 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 22 }]
389 },
390 {
391 code: "let a, b; export { a as b, b as a };",
392 options: [{ restrictedNamedExports: ["a"] }],
393 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 33 }]
394 },
395 {
396 code: "const a = 1, b = 2; export { b as a, a as b };",
397 options: [{ restrictedNamedExports: ["a"] }],
398 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 35 }]
399 },
400 {
401 code: "var a; export { a as b, a };",
402 options: [{ restrictedNamedExports: ["a"] }],
403 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 25 }]
404 },
405 {
406 code: "export { a as a } from 'a';",
407 options: [{ restrictedNamedExports: ["a"] }],
408 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 15 }]
409 },
410 {
411 code: "export { a as b, b as a } from 'foo';",
412 options: [{ restrictedNamedExports: ["a"] }],
413 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 23 }]
414 },
415 {
416 code: "export { b as a, a as b } from 'foo';",
417 options: [{ restrictedNamedExports: ["a"] }],
418 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 15 }]
419 },
420 {
421 code: "export * as a from 'a';",
422 options: [{ restrictedNamedExports: ["a"] }],
423 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 13 }]
424 },
425
426 // Note: duplicate identifiers in the same export declaration are a 'duplicate export' syntax error. Example: export var a, a;
427
428 // invalid and valid or multiple invalid in the same declaration
429 {
430 code: "export var a, b;",
431 options: [{ restrictedNamedExports: ["a"] }],
432 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
433 },
434 {
435 code: "export let b, a;",
436 options: [{ restrictedNamedExports: ["a"] }],
437 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
438 },
439 {
440 code: "export const b = 1, a = 2;",
441 options: [{ restrictedNamedExports: ["a", "b"] }],
442 errors: [
443 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
444 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }
445 ]
446 },
447 {
448 code: "export var a, b, c;",
449 options: [{ restrictedNamedExports: ["a", "c"] }],
450 errors: [
451 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
452 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
453 ]
454 },
455 {
456 code: "export let { a, b, c } = {};",
457 options: [{ restrictedNamedExports: ["b", "c"] }],
458 errors: [
459 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
460 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
461 ]
462 },
463 {
464 code: "export const [a, b, c, d] = {};",
465 options: [{ restrictedNamedExports: ["b", "c"] }],
466 errors: [
467 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
468 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
469 ]
470 },
471 {
472 code: "export var { a, x: b, c, d, e: y } = {}, e, f = {};",
473 options: [{ restrictedNamedExports: ["foo", "a", "b", "bar", "d", "e", "baz"] }],
474 errors: [
475 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
476 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
477 { messageId: "restrictedNamed", data: { name: "d" }, type: "Identifier" },
478 { messageId: "restrictedNamed", data: { name: "e" }, type: "Identifier", column: 42 }
479 ]
480 },
481 {
482 code: "var a, b; export { a, b };",
483 options: [{ restrictedNamedExports: ["a"] }],
484 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
485 },
486 {
487 code: "let a, b; export { b, a };",
488 options: [{ restrictedNamedExports: ["a"] }],
489 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
490 },
491 {
492 code: "const a = 1, b = 1; export { a, b };",
493 options: [{ restrictedNamedExports: ["a", "b"] }],
494 errors: [
495 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
496 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" }
497 ]
498 },
499 {
500 code: "export { a, b, c }; var a, b, c;",
501 options: [{ restrictedNamedExports: ["a", "c"] }],
502 errors: [
503 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
504 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
505 ]
506 },
507 {
508 code: "export { b as a, b } from 'foo';",
509 options: [{ restrictedNamedExports: ["a"] }],
510 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
511 },
512 {
513 code: "export { b as a, b } from 'foo';",
514 options: [{ restrictedNamedExports: ["b"] }],
515 errors: [{ messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier", column: 18 }]
516 },
517 {
518 code: "export { b as a, b } from 'foo';",
519 options: [{ restrictedNamedExports: ["a", "b"] }],
520 errors: [
521 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
522 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier", column: 18 }
523 ]
524 },
525 {
526 code: "export { a, b, c, d, x as e, f, g } from 'foo';",
527 options: [{ restrictedNamedExports: ["foo", "b", "bar", "d", "e", "f", "baz"] }],
528 errors: [
529 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
530 { messageId: "restrictedNamed", data: { name: "d" }, type: "Identifier" },
531 { messageId: "restrictedNamed", data: { name: "e" }, type: "Identifier" },
532 { messageId: "restrictedNamed", data: { name: "f" }, type: "Identifier" }
533 ]
534 },
535
536 // reports "default" in named export declarations (when configured)
537 {
538 code: "var a; export { a as default };",
539 options: [{ restrictedNamedExports: ["default"] }],
540 errors: [{ messageId: "restrictedNamed", data: { name: "default" }, type: "Identifier", column: 22 }]
541 },
542 {
543 code: "export { default } from 'foo';",
544 options: [{ restrictedNamedExports: ["default"] }],
545 errors: [{ messageId: "restrictedNamed", data: { name: "default" }, type: "Identifier", column: 10 }]
546 },
547
548 // restrictDefaultExports.direct option
549 {
550 code: "export default foo;",
551 options: [{ restrictDefaultExports: { direct: true } }],
552 errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
553 },
554 {
555 code: "export default 42;",
556 options: [{ restrictDefaultExports: { direct: true } }],
557 errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
558 },
559 {
560 code: "export default function foo() {}",
561 options: [{ restrictDefaultExports: { direct: true } }],
562 errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
563 },
564 {
565 code: "export default foo;",
566 options: [{ restrictedNamedExports: ["bar"], restrictDefaultExports: { direct: true } }],
567 errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
568 },
569
570 // restrictDefaultExports.named option
571 {
572 code: "const foo = 123;\nexport { foo as default };",
573 options: [{ restrictDefaultExports: { named: true } }],
574 errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 2, column: 17 }]
575 },
576
577 // restrictDefaultExports.defaultFrom option
578 {
579 code: "export { default } from 'mod';",
580 options: [{ restrictDefaultExports: { defaultFrom: true } }],
581 errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 1, column: 10 }]
582 },
583 {
584 code: "export { default as default } from 'mod';",
585 options: [{ restrictDefaultExports: { defaultFrom: true } }],
586 errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 1, column: 21 }]
587 },
588 {
589 code: "export { 'default' } from 'mod';",
590 options: [{ restrictDefaultExports: { defaultFrom: true } }],
591 errors: [{ messageId: "restrictedDefault", type: "Literal", line: 1, column: 10 }]
592 },
593
594 // restrictDefaultExports.namedFrom option
595 {
596 code: "export { foo as default } from 'mod';",
597 options: [{ restrictDefaultExports: { namedFrom: true } }],
598 errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 1, column: 17 }]
599 },
600
601 // restrictDefaultExports.namespaceFrom option
602 {
603 code: "export * as default from 'mod';",
604 options: [{ restrictDefaultExports: { namespaceFrom: true } }],
605 errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 1, column: 13 }]
606 }
607 ]
608 });