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.

rules.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @fileoverview Defines a storage for rules.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. const loadRules = require("./load-rules");
  11. const ruleReplacements = require("../conf/replacements").rules;
  12. //------------------------------------------------------------------------------
  13. // Helpers
  14. //------------------------------------------------------------------------------
  15. /**
  16. * Creates a stub rule that gets used when a rule with a given ID is not found.
  17. * @param {string} ruleId The ID of the missing rule
  18. * @returns {{create: function(RuleContext): Object}} A rule that reports an error at the first location
  19. * in the program. The report has the message `Definition for rule '${ruleId}' was not found` if the rule is unknown,
  20. * or `Rule '${ruleId}' was removed and replaced by: ${replacements.join(", ")}` if the rule is known to have been
  21. * replaced.
  22. */
  23. const createMissingRule = lodash.memoize(ruleId => {
  24. const message = Object.prototype.hasOwnProperty.call(ruleReplacements, ruleId)
  25. ? `Rule '${ruleId}' was removed and replaced by: ${ruleReplacements[ruleId].join(", ")}`
  26. : `Definition for rule '${ruleId}' was not found`;
  27. return {
  28. create: context => ({
  29. Program() {
  30. context.report({
  31. loc: { line: 1, column: 0 },
  32. message
  33. });
  34. }
  35. })
  36. };
  37. });
  38. /**
  39. * Normalizes a rule module to the new-style API
  40. * @param {(Function|{create: Function})} rule A rule object, which can either be a function
  41. * ("old-style") or an object with a `create` method ("new-style")
  42. * @returns {{create: Function}} A new-style rule.
  43. */
  44. function normalizeRule(rule) {
  45. return typeof rule === "function" ? Object.assign({ create: rule }, rule) : rule;
  46. }
  47. //------------------------------------------------------------------------------
  48. // Public Interface
  49. //------------------------------------------------------------------------------
  50. class Rules {
  51. constructor() {
  52. this._rules = Object.create(null);
  53. this.load();
  54. }
  55. /**
  56. * Registers a rule module for rule id in storage.
  57. * @param {string} ruleId Rule id (file name).
  58. * @param {Function} ruleModule Rule handler.
  59. * @returns {void}
  60. */
  61. define(ruleId, ruleModule) {
  62. this._rules[ruleId] = normalizeRule(ruleModule);
  63. }
  64. /**
  65. * Loads and registers all rules from passed rules directory.
  66. * @param {string} [rulesDir] Path to rules directory, may be relative. Defaults to `lib/rules`.
  67. * @param {string} cwd Current working directory
  68. * @returns {void}
  69. */
  70. load(rulesDir, cwd) {
  71. const newRules = loadRules(rulesDir, cwd);
  72. Object.keys(newRules).forEach(ruleId => {
  73. this.define(ruleId, newRules[ruleId]);
  74. });
  75. }
  76. /**
  77. * Registers all given rules of a plugin.
  78. * @param {Object} plugin The plugin object to import.
  79. * @param {string} pluginName The name of the plugin without prefix (`eslint-plugin-`).
  80. * @returns {void}
  81. */
  82. importPlugin(plugin, pluginName) {
  83. if (plugin.rules) {
  84. Object.keys(plugin.rules).forEach(ruleId => {
  85. const qualifiedRuleId = `${pluginName}/${ruleId}`,
  86. rule = plugin.rules[ruleId];
  87. this.define(qualifiedRuleId, rule);
  88. });
  89. }
  90. }
  91. /**
  92. * Access rule handler by id (file name).
  93. * @param {string} ruleId Rule id (file name).
  94. * @returns {{create: Function, schema: JsonSchema[]}}
  95. * A rule. This is normalized to always have the new-style shape with a `create` method.
  96. */
  97. get(ruleId) {
  98. if (!Object.prototype.hasOwnProperty.call(this._rules, ruleId)) {
  99. return createMissingRule(ruleId);
  100. }
  101. if (typeof this._rules[ruleId] === "string") {
  102. return normalizeRule(require(this._rules[ruleId]));
  103. }
  104. return this._rules[ruleId];
  105. }
  106. /**
  107. * Get an object with all currently loaded rules
  108. * @returns {Map} All loaded rules
  109. */
  110. getAllLoadedRules() {
  111. const allRules = new Map();
  112. Object.keys(this._rules).forEach(name => {
  113. const rule = this.get(name);
  114. allRules.set(name, rule);
  115. });
  116. return allRules;
  117. }
  118. }
  119. module.exports = Rules;