]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-restricted-exports.js
import eslint 7.28.0
[pve-eslint.git] / eslint / tests / lib / rules / no-restricted-exports.js
CommitLineData
eb39fafa
DC
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
12const rule = require("../../../lib/rules/no-restricted-exports");
13const { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
d3726936 19const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020, sourceType: "module" } });
eb39fafa
DC
20
21ruleTester.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
5422a9cc 61 // does not mistakenly disallow non-exported identifiers that appear in named export declarations
eb39fafa
DC
62 { code: "export var b = a;", options: [{ restrictedNamedExports: ["a"] }] },
63 { code: "export let [b = a] = [];", options: [{ restrictedNamedExports: ["a"] }] },
64 { code: "export const [b] = [a];", options: [{ restrictedNamedExports: ["a"] }] },
65 { code: "export var { a: b } = {};", options: [{ restrictedNamedExports: ["a"] }] },
66 { code: "export let { b = a } = {};", options: [{ restrictedNamedExports: ["a"] }] },
67 { code: "export const { c: b = a } = {};", options: [{ restrictedNamedExports: ["a"] }] },
68 { code: "export function b(a) {}", options: [{ restrictedNamedExports: ["a"] }] },
69 { code: "export class A { a(){} }", options: [{ restrictedNamedExports: ["a"] }] },
70 { code: "export class A extends B {}", options: [{ restrictedNamedExports: ["B"] }] },
71 { code: "var a; export { a as b };", options: [{ restrictedNamedExports: ["a"] }] },
72 { code: "export { a as b } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
73
74 // does not check source in re-export declarations
75 { code: "export { b } from 'a';", options: [{ restrictedNamedExports: ["a"] }] },
d3726936 76 { code: "export * as b from 'a';", options: [{ restrictedNamedExports: ["a"] }] },
eb39fafa
DC
77
78 // does not check non-export declarations
79 { code: "var a;", options: [{ restrictedNamedExports: ["a"] }] },
80 { code: "let a;", options: [{ restrictedNamedExports: ["a"] }] },
81 { code: "const a = 1;", options: [{ restrictedNamedExports: ["a"] }] },
82 { code: "function a() {}", options: [{ restrictedNamedExports: ["a"] }] },
83 { code: "class A {}", options: [{ restrictedNamedExports: ["A"] }] },
84 { code: "import a from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
85 { code: "import { a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
86 { code: "import { b as a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
87
88 // does not check re-export all declarations
89 { code: "export * from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
90 { code: "export * from 'a';", options: [{ restrictedNamedExports: ["a"] }] },
91
92 // does not mistakenly disallow identifiers in export default declarations (a default export will export "default" name)
93 { code: "export default a;", options: [{ restrictedNamedExports: ["a"] }] },
94 { code: "export default function a() {}", options: [{ restrictedNamedExports: ["a"] }] },
95 { code: "export default class A {}", options: [{ restrictedNamedExports: ["A"] }] },
96 { code: "export default (function a() {});", options: [{ restrictedNamedExports: ["a"] }] },
97 { code: "export default (class A {});", options: [{ restrictedNamedExports: ["A"] }] },
98
99 // by design, restricted name "default" does not apply to default export declarations, although they do export the "default" name.
100 { code: "export default 1;", options: [{ restrictedNamedExports: ["default"] }] },
101
102 // "default" does not disallow re-exporting a renamed default export from another module
103 { code: "export { default as a } from 'foo';", options: [{ restrictedNamedExports: ["default"] }] }
104 ],
105
106 invalid: [
107
108 {
109 code: "export function someFunction() {}",
110 options: [{ restrictedNamedExports: ["someFunction"] }],
111 errors: [{ messageId: "restrictedNamed", data: { name: "someFunction" }, type: "Identifier" }]
112 },
113
114 // basic tests
115 {
116 code: "export var a;",
117 options: [{ restrictedNamedExports: ["a"] }],
118 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
119 },
120 {
121 code: "export var a = 1;",
122 options: [{ restrictedNamedExports: ["a"] }],
123 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
124 },
125 {
126 code: "export let a;",
127 options: [{ restrictedNamedExports: ["a"] }],
128 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
129 },
130 {
131 code: "export let a = 1;",
132 options: [{ restrictedNamedExports: ["a"] }],
133 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
134 },
135 {
136 code: "export const a = 1;",
137 options: [{ restrictedNamedExports: ["a"] }],
138 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
139 },
140 {
141 code: "export function a() {}",
142 options: [{ restrictedNamedExports: ["a"] }],
143 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
144 },
145 {
146 code: "export function *a() {}",
147 options: [{ restrictedNamedExports: ["a"] }],
148 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
149 },
150 {
151 code: "export async function a() {}",
152 options: [{ restrictedNamedExports: ["a"] }],
153 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
154 },
155 {
156 code: "export async function *a() {}",
157 options: [{ restrictedNamedExports: ["a"] }],
158 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
159 },
160 {
161 code: "export class A {}",
162 options: [{ restrictedNamedExports: ["A"] }],
163 errors: [{ messageId: "restrictedNamed", data: { name: "A" }, type: "Identifier" }]
164 },
165 {
166 code: "let a; export { a };",
167 options: [{ restrictedNamedExports: ["a"] }],
168 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
169 },
170 {
171 code: "export { a }; var a;",
172 options: [{ restrictedNamedExports: ["a"] }],
173 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 10 }]
174 },
175 {
176 code: "let b; export { b as a };",
177 options: [{ restrictedNamedExports: ["a"] }],
178 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
179 },
180 {
181 code: "export { a } from 'foo';",
182 options: [{ restrictedNamedExports: ["a"] }],
183 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
184 },
185 {
186 code: "export { b as a } from 'foo';",
187 options: [{ restrictedNamedExports: ["a"] }],
188 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
189 },
190
191 // destructuring
192 {
193 code: "export var [a] = [];",
194 options: [{ restrictedNamedExports: ["a"] }],
195 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
196 },
197 {
198 code: "export let { a } = {};",
199 options: [{ restrictedNamedExports: ["a"] }],
200 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
201 },
202 {
203 code: "export const { b: a } = {};",
204 options: [{ restrictedNamedExports: ["a"] }],
205 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
206 },
207 {
208 code: "export var [{ a }] = [];",
209 options: [{ restrictedNamedExports: ["a"] }],
210 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
211 },
212 {
213 code: "export let { b: { c: a = d } = e } = {};",
214 options: [{ restrictedNamedExports: ["a"] }],
215 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
216 },
217
218 // reports the correct identifier node in the case of a redeclaration. Note: functions cannot be redeclared in a module.
219 {
220 code: "var a; export var a;",
221 options: [{ restrictedNamedExports: ["a"] }],
222 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 19 }]
223 },
224 {
225 code: "export var a; var a;",
226 options: [{ restrictedNamedExports: ["a"] }],
227 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 12 }]
228 },
229
230 // reports the correct identifier node when the same identifier appears elsewhere in the declaration
231 {
232 code: "export var a = a;",
233 options: [{ restrictedNamedExports: ["a"] }],
234 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 12 }]
235 },
236 {
237 code: "export let b = a, a;",
238 options: [{ restrictedNamedExports: ["a"] }],
239 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 19 }]
240 },
241 {
242 code: "export const a = 1, b = a;",
243 options: [{ restrictedNamedExports: ["a"] }],
244 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 14 }]
245 },
246 {
247 code: "export var [a] = a;",
248 options: [{ restrictedNamedExports: ["a"] }],
249 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 13 }]
250 },
251 {
252 code: "export let { a: a } = {};",
253 options: [{ restrictedNamedExports: ["a"] }],
254 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
255 },
256 {
257 code: "export const { a: b, b: a } = {};",
258 options: [{ restrictedNamedExports: ["a"] }],
259 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 25 }]
260 },
261 {
262 code: "export var { b: a, a: b } = {};",
263 options: [{ restrictedNamedExports: ["a"] }],
264 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
265 },
266 {
267 code: "export let a, { a: b } = {};",
268 options: [{ restrictedNamedExports: ["a"] }],
269 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 12 }]
270 },
271 {
272 code: "export const { a: b } = {}, a = 1;",
273 options: [{ restrictedNamedExports: ["a"] }],
274 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 29 }]
275 },
276 {
277 code: "export var [a = a] = [];",
278 options: [{ restrictedNamedExports: ["a"] }],
279 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 13 }]
280 },
281 {
282 code: "export var { a: a = a } = {};",
283 options: [{ restrictedNamedExports: ["a"] }],
284 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
285 },
286 {
287 code: "export let { a } = { a };",
288 options: [{ restrictedNamedExports: ["a"] }],
289 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 14 }]
290 },
291 {
292 code: "export function a(a) {};",
293 options: [{ restrictedNamedExports: ["a"] }],
294 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 17 }]
295 },
296 {
297 code: "export class A { A(){} };",
298 options: [{ restrictedNamedExports: ["A"] }],
299 errors: [{ messageId: "restrictedNamed", data: { name: "A" }, type: "Identifier", column: 14 }]
300 },
301 {
302 code: "var a; export { a as a };",
303 options: [{ restrictedNamedExports: ["a"] }],
304 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 22 }]
305 },
306 {
307 code: "let a, b; export { a as b, b as a };",
308 options: [{ restrictedNamedExports: ["a"] }],
309 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 33 }]
310 },
311 {
312 code: "const a = 1, b = 2; export { b as a, a as b };",
313 options: [{ restrictedNamedExports: ["a"] }],
314 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 35 }]
315 },
316 {
317 code: "var a; export { a as b, a };",
318 options: [{ restrictedNamedExports: ["a"] }],
319 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 25 }]
320 },
321 {
322 code: "export { a as a } from 'a';",
323 options: [{ restrictedNamedExports: ["a"] }],
324 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 15 }]
325 },
326 {
327 code: "export { a as b, b as a } from 'foo';",
328 options: [{ restrictedNamedExports: ["a"] }],
329 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 23 }]
330 },
331 {
332 code: "export { b as a, a as b } from 'foo';",
333 options: [{ restrictedNamedExports: ["a"] }],
334 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 15 }]
335 },
d3726936
TL
336 {
337 code: "export * as a from 'a';",
338 options: [{ restrictedNamedExports: ["a"] }],
339 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier", column: 13 }]
340 },
eb39fafa
DC
341
342 // Note: duplicate identifiers in the same export declaration are a 'duplicate export' syntax error. Example: export var a, a;
343
344 // invalid and valid or multiple ivalid in the same declaration
345 {
346 code: "export var a, b;",
347 options: [{ restrictedNamedExports: ["a"] }],
348 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
349 },
350 {
351 code: "export let b, a;",
352 options: [{ restrictedNamedExports: ["a"] }],
353 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
354 },
355 {
356 code: "export const b = 1, a = 2;",
357 options: [{ restrictedNamedExports: ["a", "b"] }],
358 errors: [
359 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
360 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }
361 ]
362 },
363 {
364 code: "export var a, b, c;",
365 options: [{ restrictedNamedExports: ["a", "c"] }],
366 errors: [
367 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
368 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
369 ]
370 },
371 {
372 code: "export let { a, b, c } = {};",
373 options: [{ restrictedNamedExports: ["b", "c"] }],
374 errors: [
375 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
376 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
377 ]
378 },
379 {
380 code: "export const [a, b, c, d] = {};",
381 options: [{ restrictedNamedExports: ["b", "c"] }],
382 errors: [
383 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
384 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
385 ]
386 },
387 {
388 code: "export var { a, x: b, c, d, e: y } = {}, e, f = {};",
389 options: [{ restrictedNamedExports: ["foo", "a", "b", "bar", "d", "e", "baz"] }],
390 errors: [
391 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
392 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
393 { messageId: "restrictedNamed", data: { name: "d" }, type: "Identifier" },
394 { messageId: "restrictedNamed", data: { name: "e" }, type: "Identifier", column: 42 }
395 ]
396 },
397 {
398 code: "var a, b; export { a, b };",
399 options: [{ restrictedNamedExports: ["a"] }],
400 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
401 },
402 {
403 code: "let a, b; export { b, a };",
404 options: [{ restrictedNamedExports: ["a"] }],
405 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
406 },
407 {
408 code: "const a = 1, b = 1; export { a, b };",
409 options: [{ restrictedNamedExports: ["a", "b"] }],
410 errors: [
411 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
412 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" }
413 ]
414 },
415 {
416 code: "export { a, b, c }; var a, b, c;",
417 options: [{ restrictedNamedExports: ["a", "c"] }],
418 errors: [
419 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
420 { messageId: "restrictedNamed", data: { name: "c" }, type: "Identifier" }
421 ]
422 },
423 {
424 code: "export { b as a, b } from 'foo';",
425 options: [{ restrictedNamedExports: ["a"] }],
426 errors: [{ messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" }]
427 },
428 {
429 code: "export { b as a, b } from 'foo';",
430 options: [{ restrictedNamedExports: ["b"] }],
431 errors: [{ messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier", column: 18 }]
432 },
433 {
434 code: "export { b as a, b } from 'foo';",
435 options: [{ restrictedNamedExports: ["a", "b"] }],
436 errors: [
437 { messageId: "restrictedNamed", data: { name: "a" }, type: "Identifier" },
438 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier", column: 18 }
439 ]
440 },
441 {
442 code: "export { a, b, c, d, x as e, f, g } from 'foo';",
443 options: [{ restrictedNamedExports: ["foo", "b", "bar", "d", "e", "f", "baz"] }],
444 errors: [
445 { messageId: "restrictedNamed", data: { name: "b" }, type: "Identifier" },
446 { messageId: "restrictedNamed", data: { name: "d" }, type: "Identifier" },
447 { messageId: "restrictedNamed", data: { name: "e" }, type: "Identifier" },
448 { messageId: "restrictedNamed", data: { name: "f" }, type: "Identifier" }
449 ]
450 },
451
452 // reports "default" in named export declarations (when configured)
453 {
454 code: "var a; export { a as default };",
455 options: [{ restrictedNamedExports: ["default"] }],
456 errors: [{ messageId: "restrictedNamed", data: { name: "default" }, type: "Identifier", column: 22 }]
457 },
458 {
459 code: "export { default } from 'foo';",
460 options: [{ restrictedNamedExports: ["default"] }],
461 errors: [{ messageId: "restrictedNamed", data: { name: "default" }, type: "Identifier", column: 10 }]
462 }
463 ]
464});