]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-restricted-modules.js
2 * @fileoverview Restrict usage of specified node modules.
3 * @author Christian Schulz
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const ignore
= require("ignore");
13 const arrayOfStrings
= {
15 items
: { type
: "string" },
19 const arrayOfStringsOrObjects
= {
27 name
: { type
: "string" },
33 additionalProperties
: false,
45 replacedBy
: ["node/no-restricted-require"],
50 description
: "disallow specified modules when loaded by `require`",
51 category
: "Node.js and CommonJS",
53 url
: "https://eslint.org/docs/rules/no-restricted-modules"
58 arrayOfStringsOrObjects
,
64 paths
: arrayOfStringsOrObjects
,
65 patterns
: arrayOfStrings
67 additionalProperties
: false
69 additionalItems
: false
75 defaultMessage
: "'{{name}}' module is restricted from being used.",
76 // eslint-disable-next-line eslint-plugin/report-message-format
77 customMessage
: "'{{name}}' module is restricted from being used. {{customMessage}}",
78 patternMessage
: "'{{name}}' module is restricted from being used by a pattern."
83 const options
= Array
.isArray(context
.options
) ? context
.options
: [];
84 const isPathAndPatternsObject
=
85 typeof options
[0] === "object" &&
86 (Object
.prototype.hasOwnProperty
.call(options
[0], "paths") || Object
.prototype.hasOwnProperty
.call(options
[0], "patterns"));
88 const restrictedPaths
= (isPathAndPatternsObject
? options
[0].paths
: context
.options
) || [];
89 const restrictedPatterns
= (isPathAndPatternsObject
? options
[0].patterns
: []) || [];
91 const restrictedPathMessages
= restrictedPaths
.reduce((memo
, importName
) => {
92 if (typeof importName
=== "string") {
93 memo
[importName
] = null;
95 memo
[importName
.name
] = importName
.message
;
100 // if no imports are restricted we don"t need to check
101 if (Object
.keys(restrictedPaths
).length
=== 0 && restrictedPatterns
.length
=== 0) {
105 const ig
= ignore().add(restrictedPatterns
);
109 * Function to check if a node is a string literal.
110 * @param {ASTNode} node The node to check.
111 * @returns {boolean} If the node is a string literal.
113 function isStringLiteral(node
) {
114 return node
&& node
.type
=== "Literal" && typeof node
.value
=== "string";
118 * Function to check if a node is a static string template literal.
119 * @param {ASTNode} node The node to check.
120 * @returns {boolean} If the node is a string template literal.
122 function isStaticTemplateLiteral(node
) {
123 return node
&& node
.type
=== "TemplateLiteral" && node
.expressions
.length
=== 0;
127 * Function to check if a node is a require call.
128 * @param {ASTNode} node The node to check.
129 * @returns {boolean} If the node is a require call.
131 function isRequireCall(node
) {
132 return node
.callee
.type
=== "Identifier" && node
.callee
.name
=== "require";
136 * Extract string from Literal or TemplateLiteral node
137 * @param {ASTNode} node The node to extract from
138 * @returns {string|null} Extracted string or null if node doesn't represent a string
140 function getFirstArgumentString(node
) {
141 if (isStringLiteral(node
)) {
142 return node
.value
.trim();
145 if (isStaticTemplateLiteral(node
)) {
146 return node
.quasis
[0].value
.cooked
.trim();
153 * Report a restricted path.
154 * @param {node} node representing the restricted path reference
155 * @param {string} name restricted path
159 function reportPath(node
, name
) {
160 const customMessage
= restrictedPathMessages
[name
];
161 const messageId
= customMessage
176 * Check if the given name is a restricted path name
177 * @param {string} name name of a variable
178 * @returns {boolean} whether the variable is a restricted path or not
181 function isRestrictedPath(name
) {
182 return Object
.prototype.hasOwnProperty
.call(restrictedPathMessages
, name
);
186 CallExpression(node
) {
187 if (isRequireCall(node
)) {
189 // node has arguments
190 if (node
.arguments
.length
) {
191 const name
= getFirstArgumentString(node
.arguments
[0]);
193 // if first argument is a string literal or a static string template literal
196 // check if argument value is in restricted modules array
197 if (isRestrictedPath(name
)) {
198 reportPath(node
, name
);
201 if (restrictedPatterns
.length
> 0 && ig
.ignores(name
)) {
204 messageId
: "patternMessage",