Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-restricted-modules.js 5.9KB

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