UNPKG

2.78 kBJavaScriptView Raw
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
12module.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 `Identifier` 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 ExportAllDeclaration(node) {
65 if (node.exported) {
66 checkExportedName(node.exported);
67 }
68 },
69
70 ExportNamedDeclaration(node) {
71 const declaration = node.declaration;
72
73 if (declaration) {
74 if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") {
75 checkExportedName(declaration.id);
76 } else if (declaration.type === "VariableDeclaration") {
77 context.getDeclaredVariables(declaration)
78 .map(v => v.defs.find(d => d.parent === declaration))
79 .map(d => d.name) // Identifier nodes
80 .forEach(checkExportedName);
81 }
82 } else {
83 node.specifiers
84 .map(s => s.exported)
85 .forEach(checkExportedName);
86 }
87 }
88 };
89 }
90};