no-restricted-modules.js 6.9 KB

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