minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-restricted-modules.js
1 /**
2  * @fileoverview Restrict usage of specified node modules.
3  * @author Christian Schulz
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Helpers
9 //------------------------------------------------------------------------------
10
11 const DEFAULT_MESSAGE_TEMPLATE = "'{{moduleName}}' module is restricted from being used.";
12 const CUSTOM_MESSAGE_TEMPLATE = "'{{moduleName}}' module is restricted from being used. {{customMessage}}";
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 const ignore = require("ignore");
19
20 const arrayOfStrings = {
21     type: "array",
22     items: { type: "string" },
23     uniqueItems: true
24 };
25
26 const arrayOfStringsOrObjects = {
27     type: "array",
28     items: {
29         anyOf: [
30             { type: "string" },
31             {
32                 type: "object",
33                 properties: {
34                     name: { type: "string" },
35                     message: {
36                         type: "string",
37                         minLength: 1
38                     }
39                 },
40                 additionalProperties: false,
41                 required: ["name"]
42             }
43         ]
44     },
45     uniqueItems: true
46 };
47
48 module.exports = {
49     meta: {
50         type: "suggestion",
51
52         docs: {
53             description: "disallow specified modules when loaded by `require`",
54             category: "Node.js and CommonJS",
55             recommended: false,
56             url: "https://eslint.org/docs/rules/no-restricted-modules"
57         },
58
59         schema: {
60             anyOf: [
61                 arrayOfStringsOrObjects,
62                 {
63                     type: "array",
64                     items: {
65                         type: "object",
66                         properties: {
67                             paths: arrayOfStringsOrObjects,
68                             patterns: arrayOfStrings
69                         },
70                         additionalProperties: false
71                     },
72                     additionalItems: false
73                 }
74             ]
75         }
76     },
77
78     create(context) {
79         const options = Array.isArray(context.options) ? context.options : [];
80         const isPathAndPatternsObject =
81             typeof options[0] === "object" &&
82             (Object.prototype.hasOwnProperty.call(options[0], "paths") || Object.prototype.hasOwnProperty.call(options[0], "patterns"));
83
84         const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || [];
85         const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || [];
86
87         const restrictedPathMessages = restrictedPaths.reduce((memo, importName) => {
88             if (typeof importName === "string") {
89                 memo[importName] = null;
90             } else {
91                 memo[importName.name] = importName.message;
92             }
93             return memo;
94         }, {});
95
96         // if no imports are restricted we don"t need to check
97         if (Object.keys(restrictedPaths).length === 0 && restrictedPatterns.length === 0) {
98             return {};
99         }
100
101         const ig = ignore().add(restrictedPatterns);
102
103
104         /**
105          * Function to check if a node is a string literal.
106          * @param {ASTNode} node The node to check.
107          * @returns {boolean} If the node is a string literal.
108          */
109         function isString(node) {
110             return node && node.type === "Literal" && typeof node.value === "string";
111         }
112
113         /**
114          * Function to check if a node is a require call.
115          * @param {ASTNode} node The node to check.
116          * @returns {boolean} If the node is a require call.
117          */
118         function isRequireCall(node) {
119             return node.callee.type === "Identifier" && node.callee.name === "require";
120         }
121
122         /**
123          * Report a restricted path.
124          * @param {node} node representing the restricted path reference
125          * @returns {void}
126          * @private
127          */
128         function reportPath(node) {
129             const moduleName = node.arguments[0].value.trim();
130             const customMessage = restrictedPathMessages[moduleName];
131             const message = customMessage
132                 ? CUSTOM_MESSAGE_TEMPLATE
133                 : DEFAULT_MESSAGE_TEMPLATE;
134
135             context.report({
136                 node,
137                 message,
138                 data: {
139                     moduleName,
140                     customMessage
141                 }
142             });
143         }
144
145         /**
146          * Check if the given name is a restricted path name
147          * @param {string} name name of a variable
148          * @returns {boolean} whether the variable is a restricted path or not
149          * @private
150          */
151         function isRestrictedPath(name) {
152             return Object.prototype.hasOwnProperty.call(restrictedPathMessages, name);
153         }
154
155         return {
156             CallExpression(node) {
157                 if (isRequireCall(node)) {
158
159                     // node has arguments and first argument is string
160                     if (node.arguments.length && isString(node.arguments[0])) {
161                         const moduleName = node.arguments[0].value.trim();
162
163                         // check if argument value is in restricted modules array
164                         if (isRestrictedPath(moduleName)) {
165                             reportPath(node);
166                         }
167
168                         if (restrictedPatterns.length > 0 && ig.ignores(moduleName)) {
169                             context.report({
170                                 node,
171                                 message: "'{{moduleName}}' module is restricted from being used by a pattern.",
172                                 data: { moduleName }
173                             });
174                         }
175                     }
176                 }
177             }
178         };
179     }
180 };