]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-restricted-modules.js
c37694f90461f9b4933c67bab0265bd5097a4004
2 * @fileoverview Restrict usage of specified node modules.
3 * @author Christian Schulz
4 * @deprecated in ESLint v7.0.0
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const ignore
= require("ignore");
14 const arrayOfStrings
= {
16 items
: { type
: "string" },
20 const arrayOfStringsOrObjects
= {
28 name
: { type
: "string" },
34 additionalProperties
: false,
42 /** @type {import('../shared/types').Rule} */
52 description
: "Disallow specified modules when loaded by `require`",
54 url
: "https://eslint.org/docs/rules/no-restricted-modules"
59 arrayOfStringsOrObjects
,
65 paths
: arrayOfStringsOrObjects
,
66 patterns
: arrayOfStrings
68 additionalProperties
: false
70 additionalItems
: false
76 defaultMessage
: "'{{name}}' module is restricted from being used.",
77 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
78 customMessage
: "'{{name}}' module is restricted from being used. {{customMessage}}",
79 patternMessage
: "'{{name}}' module is restricted from being used by a pattern."
84 const options
= Array
.isArray(context
.options
) ? context
.options
: [];
85 const isPathAndPatternsObject
=
86 typeof options
[0] === "object" &&
87 (Object
.prototype.hasOwnProperty
.call(options
[0], "paths") || Object
.prototype.hasOwnProperty
.call(options
[0], "patterns"));
89 const restrictedPaths
= (isPathAndPatternsObject
? options
[0].paths
: context
.options
) || [];
90 const restrictedPatterns
= (isPathAndPatternsObject
? options
[0].patterns
: []) || [];
92 const restrictedPathMessages
= restrictedPaths
.reduce((memo
, importName
) => {
93 if (typeof importName
=== "string") {
94 memo
[importName
] = null;
96 memo
[importName
.name
] = importName
.message
;
101 // if no imports are restricted we don't need to check
102 if (Object
.keys(restrictedPaths
).length
=== 0 && restrictedPatterns
.length
=== 0) {
106 // relative paths are supported for this rule
107 const ig
= ignore({ allowRelativePaths
: true }).add(restrictedPatterns
);
111 * Function to check if a node is a string literal.
112 * @param {ASTNode} node The node to check.
113 * @returns {boolean} If the node is a string literal.
115 function isStringLiteral(node
) {
116 return node
&& node
.type
=== "Literal" && typeof node
.value
=== "string";
120 * Function to check if a node is a static string template literal.
121 * @param {ASTNode} node The node to check.
122 * @returns {boolean} If the node is a string template literal.
124 function isStaticTemplateLiteral(node
) {
125 return node
&& node
.type
=== "TemplateLiteral" && node
.expressions
.length
=== 0;
129 * Function to check if a node is a require call.
130 * @param {ASTNode} node The node to check.
131 * @returns {boolean} If the node is a require call.
133 function isRequireCall(node
) {
134 return node
.callee
.type
=== "Identifier" && node
.callee
.name
=== "require";
138 * Extract string from Literal or TemplateLiteral node
139 * @param {ASTNode} node The node to extract from
140 * @returns {string|null} Extracted string or null if node doesn't represent a string
142 function getFirstArgumentString(node
) {
143 if (isStringLiteral(node
)) {
144 return node
.value
.trim();
147 if (isStaticTemplateLiteral(node
)) {
148 return node
.quasis
[0].value
.cooked
.trim();
155 * Report a restricted path.
156 * @param {node} node representing the restricted path reference
157 * @param {string} name restricted path
161 function reportPath(node
, name
) {
162 const customMessage
= restrictedPathMessages
[name
];
163 const messageId
= customMessage
178 * Check if the given name is a restricted path name
179 * @param {string} name name of a variable
180 * @returns {boolean} whether the variable is a restricted path or not
183 function isRestrictedPath(name
) {
184 return Object
.prototype.hasOwnProperty
.call(restrictedPathMessages
, name
);
188 CallExpression(node
) {
189 if (isRequireCall(node
)) {
191 // node has arguments
192 if (node
.arguments
.length
) {
193 const name
= getFirstArgumentString(node
.arguments
[0]);
195 // if first argument is a string literal or a static string template literal
198 // check if argument value is in restricted modules array
199 if (isRestrictedPath(name
)) {
200 reportPath(node
, name
);
203 if (restrictedPatterns
.length
> 0 && ig
.ignores(name
)) {
206 messageId
: "patternMessage",