]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Rule to disallow specified names in exports | |
3 | * @author Milos Djermanovic | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Rule Definition | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | module.exports = { | |
13 | meta: { | |
14 | type: "suggestion", | |
15 | ||
16 | docs: { | |
17 | description: "disallow specified names in exports", | |
eb39fafa DC |
18 | recommended: false, |
19 | url: "https://eslint.org/docs/rules/no-restricted-exports" | |
20 | }, | |
21 | ||
22 | schema: [{ | |
23 | type: "object", | |
24 | properties: { | |
25 | restrictedNamedExports: { | |
26 | type: "array", | |
27 | items: { | |
28 | type: "string" | |
29 | }, | |
30 | uniqueItems: true | |
31 | } | |
32 | }, | |
33 | additionalProperties: false | |
34 | }], | |
35 | ||
36 | messages: { | |
37 | restrictedNamed: "'{{name}}' is restricted from being used as an exported name." | |
38 | } | |
39 | }, | |
40 | ||
41 | create(context) { | |
42 | ||
43 | const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports); | |
44 | ||
45 | /** | |
46 | * Checks and reports given exported identifier. | |
456be15e | 47 | * @param {ASTNode} node exported `Identifier` node to check. |
eb39fafa DC |
48 | * @returns {void} |
49 | */ | |
50 | function checkExportedName(node) { | |
51 | const name = node.name; | |
52 | ||
53 | if (restrictedNames.has(name)) { | |
54 | context.report({ | |
55 | node, | |
56 | messageId: "restrictedNamed", | |
57 | data: { name } | |
58 | }); | |
59 | } | |
60 | } | |
61 | ||
62 | return { | |
d3726936 TL |
63 | ExportAllDeclaration(node) { |
64 | if (node.exported) { | |
65 | checkExportedName(node.exported); | |
66 | } | |
67 | }, | |
68 | ||
eb39fafa DC |
69 | ExportNamedDeclaration(node) { |
70 | const declaration = node.declaration; | |
71 | ||
72 | if (declaration) { | |
73 | if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") { | |
74 | checkExportedName(declaration.id); | |
75 | } else if (declaration.type === "VariableDeclaration") { | |
76 | context.getDeclaredVariables(declaration) | |
77 | .map(v => v.defs.find(d => d.parent === declaration)) | |
78 | .map(d => d.name) // Identifier nodes | |
79 | .forEach(checkExportedName); | |
80 | } | |
81 | } else { | |
82 | node.specifiers | |
83 | .map(s => s.exported) | |
84 | .forEach(checkExportedName); | |
85 | } | |
86 | } | |
87 | }; | |
88 | } | |
89 | }; |