1 | /**
|
---|
2 | * @fileoverview Rule to disallow specified names in exports
|
---|
3 | * @author Milos Djermanovic
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 | // Requirements
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | const astUtils = require("./utils/ast-utils");
|
---|
13 |
|
---|
14 | //------------------------------------------------------------------------------
|
---|
15 | // Rule Definition
|
---|
16 | //------------------------------------------------------------------------------
|
---|
17 |
|
---|
18 | /** @type {import('../shared/types').Rule} */
|
---|
19 | module.exports = {
|
---|
20 | meta: {
|
---|
21 | type: "suggestion",
|
---|
22 |
|
---|
23 | docs: {
|
---|
24 | description: "Disallow specified names in exports",
|
---|
25 | recommended: false,
|
---|
26 | url: "https://eslint.org/docs/latest/rules/no-restricted-exports"
|
---|
27 | },
|
---|
28 |
|
---|
29 | schema: [{
|
---|
30 | anyOf: [
|
---|
31 | {
|
---|
32 | type: "object",
|
---|
33 | properties: {
|
---|
34 | restrictedNamedExports: {
|
---|
35 | type: "array",
|
---|
36 | items: {
|
---|
37 | type: "string"
|
---|
38 | },
|
---|
39 | uniqueItems: true
|
---|
40 | }
|
---|
41 | },
|
---|
42 | additionalProperties: false
|
---|
43 | },
|
---|
44 | {
|
---|
45 | type: "object",
|
---|
46 | properties: {
|
---|
47 | restrictedNamedExports: {
|
---|
48 | type: "array",
|
---|
49 | items: {
|
---|
50 | type: "string",
|
---|
51 | pattern: "^(?!default$)"
|
---|
52 | },
|
---|
53 | uniqueItems: true
|
---|
54 | },
|
---|
55 | restrictDefaultExports: {
|
---|
56 | type: "object",
|
---|
57 | properties: {
|
---|
58 |
|
---|
59 | // Allow/Disallow `export default foo; export default 42; export default function foo() {}` format
|
---|
60 | direct: {
|
---|
61 | type: "boolean"
|
---|
62 | },
|
---|
63 |
|
---|
64 | // Allow/Disallow `export { foo as default };` declarations
|
---|
65 | named: {
|
---|
66 | type: "boolean"
|
---|
67 | },
|
---|
68 |
|
---|
69 | // Allow/Disallow `export { default } from "mod"; export { default as default } from "mod";` declarations
|
---|
70 | defaultFrom: {
|
---|
71 | type: "boolean"
|
---|
72 | },
|
---|
73 |
|
---|
74 | // Allow/Disallow `export { foo as default } from "mod";` declarations
|
---|
75 | namedFrom: {
|
---|
76 | type: "boolean"
|
---|
77 | },
|
---|
78 |
|
---|
79 | // Allow/Disallow `export * as default from "mod"`; declarations
|
---|
80 | namespaceFrom: {
|
---|
81 | type: "boolean"
|
---|
82 | }
|
---|
83 | },
|
---|
84 | additionalProperties: false
|
---|
85 | }
|
---|
86 | },
|
---|
87 | additionalProperties: false
|
---|
88 | }
|
---|
89 | ]
|
---|
90 | }],
|
---|
91 |
|
---|
92 | messages: {
|
---|
93 | restrictedNamed: "'{{name}}' is restricted from being used as an exported name.",
|
---|
94 | restrictedDefault: "Exporting 'default' is restricted."
|
---|
95 | }
|
---|
96 | },
|
---|
97 |
|
---|
98 | create(context) {
|
---|
99 |
|
---|
100 | const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
|
---|
101 | const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
|
---|
102 | const sourceCode = context.sourceCode;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Checks and reports given exported name.
|
---|
106 | * @param {ASTNode} node exported `Identifier` or string `Literal` node to check.
|
---|
107 | * @returns {void}
|
---|
108 | */
|
---|
109 | function checkExportedName(node) {
|
---|
110 | const name = astUtils.getModuleExportName(node);
|
---|
111 |
|
---|
112 | if (restrictedNames.has(name)) {
|
---|
113 | context.report({
|
---|
114 | node,
|
---|
115 | messageId: "restrictedNamed",
|
---|
116 | data: { name }
|
---|
117 | });
|
---|
118 | return;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (name === "default") {
|
---|
122 | if (node.parent.type === "ExportAllDeclaration") {
|
---|
123 | if (restrictDefaultExports && restrictDefaultExports.namespaceFrom) {
|
---|
124 | context.report({
|
---|
125 | node,
|
---|
126 | messageId: "restrictedDefault"
|
---|
127 | });
|
---|
128 | }
|
---|
129 |
|
---|
130 | } else { // ExportSpecifier
|
---|
131 | const isSourceSpecified = !!node.parent.parent.source;
|
---|
132 | const specifierLocalName = astUtils.getModuleExportName(node.parent.local);
|
---|
133 |
|
---|
134 | if (!isSourceSpecified && restrictDefaultExports && restrictDefaultExports.named) {
|
---|
135 | context.report({
|
---|
136 | node,
|
---|
137 | messageId: "restrictedDefault"
|
---|
138 | });
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (isSourceSpecified && restrictDefaultExports) {
|
---|
143 | if (
|
---|
144 | (specifierLocalName === "default" && restrictDefaultExports.defaultFrom) ||
|
---|
145 | (specifierLocalName !== "default" && restrictDefaultExports.namedFrom)
|
---|
146 | ) {
|
---|
147 | context.report({
|
---|
148 | node,
|
---|
149 | messageId: "restrictedDefault"
|
---|
150 | });
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | return {
|
---|
158 | ExportAllDeclaration(node) {
|
---|
159 | if (node.exported) {
|
---|
160 | checkExportedName(node.exported);
|
---|
161 | }
|
---|
162 | },
|
---|
163 |
|
---|
164 | ExportDefaultDeclaration(node) {
|
---|
165 | if (restrictDefaultExports && restrictDefaultExports.direct) {
|
---|
166 | context.report({
|
---|
167 | node,
|
---|
168 | messageId: "restrictedDefault"
|
---|
169 | });
|
---|
170 | }
|
---|
171 | },
|
---|
172 |
|
---|
173 | ExportNamedDeclaration(node) {
|
---|
174 | const declaration = node.declaration;
|
---|
175 |
|
---|
176 | if (declaration) {
|
---|
177 | if (declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") {
|
---|
178 | checkExportedName(declaration.id);
|
---|
179 | } else if (declaration.type === "VariableDeclaration") {
|
---|
180 | sourceCode.getDeclaredVariables(declaration)
|
---|
181 | .map(v => v.defs.find(d => d.parent === declaration))
|
---|
182 | .map(d => d.name) // Identifier nodes
|
---|
183 | .forEach(checkExportedName);
|
---|
184 | }
|
---|
185 | } else {
|
---|
186 | node.specifiers
|
---|
187 | .map(s => s.exported)
|
---|
188 | .forEach(checkExportedName);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | };
|
---|
192 | }
|
---|
193 | };
|
---|