]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-restricted-exports.js
5b5c7d9bffb99c2d8cb1e242f9c22d69d17d8294
[pve-eslint.git] / eslint / lib / rules / no-restricted-exports.js
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",
18 category: "ECMAScript 6",
19 recommended: false,
20 url: "https://eslint.org/docs/rules/no-restricted-exports"
21 },
22
23 schema: [{
24 type: "object",
25 properties: {
26 restrictedNamedExports: {
27 type: "array",
28 items: {
29 type: "string"
30 },
31 uniqueItems: true
32 }
33 },
34 additionalProperties: false
35 }],
36
37 messages: {
38 restrictedNamed: "'{{name}}' is restricted from being used as an exported name."
39 }
40 },
41
42 create(context) {
43
44 const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
45
46 /**
47 * Checks and reports given exported identifier.
48 * @param {ASTNode} node exported `Identifer` node to check.
49 * @returns {void}
50 */
51 function checkExportedName(node) {
52 const name = node.name;
53
54 if (restrictedNames.has(name)) {
55 context.report({
56 node,
57 messageId: "restrictedNamed",
58 data: { name }
59 });
60 }
61 }
62
63 return {
64 ExportNamedDeclaration(node) {
65 const declaration = node.declaration;
66
67 if (declaration) {
68 if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") {
69 checkExportedName(declaration.id);
70 } else if (declaration.type === "VariableDeclaration") {
71 context.getDeclaredVariables(declaration)
72 .map(v => v.defs.find(d => d.parent === declaration))
73 .map(d => d.name) // Identifier nodes
74 .forEach(checkExportedName);
75 }
76 } else {
77 node.specifiers
78 .map(s => s.exported)
79 .forEach(checkExportedName);
80 }
81 }
82 };
83 }
84 };