flat-config-helpers.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @fileoverview Shared functions to work with configs.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Functions
  8. //-----------------------------------------------------------------------------
  9. /**
  10. * Parses a ruleId into its plugin and rule parts.
  11. * @param {string} ruleId The rule ID to parse.
  12. * @returns {{pluginName:string,ruleName:string}} The plugin and rule
  13. * parts of the ruleId;
  14. */
  15. function parseRuleId(ruleId) {
  16. let pluginName, ruleName;
  17. // distinguish between core rules and plugin rules
  18. if (ruleId.includes("/")) {
  19. // mimic scoped npm packages
  20. if (ruleId.startsWith("@")) {
  21. pluginName = ruleId.slice(0, ruleId.lastIndexOf("/"));
  22. } else {
  23. pluginName = ruleId.slice(0, ruleId.indexOf("/"));
  24. }
  25. ruleName = ruleId.slice(pluginName.length + 1);
  26. } else {
  27. pluginName = "@";
  28. ruleName = ruleId;
  29. }
  30. return {
  31. pluginName,
  32. ruleName
  33. };
  34. }
  35. /**
  36. * Retrieves a rule instance from a given config based on the ruleId.
  37. * @param {string} ruleId The rule ID to look for.
  38. * @param {FlatConfig} config The config to search.
  39. * @returns {import("../shared/types").Rule|undefined} The rule if found
  40. * or undefined if not.
  41. */
  42. function getRuleFromConfig(ruleId, config) {
  43. const { pluginName, ruleName } = parseRuleId(ruleId);
  44. const plugin = config.plugins && config.plugins[pluginName];
  45. let rule = plugin && plugin.rules && plugin.rules[ruleName];
  46. // normalize function rules into objects
  47. if (rule && typeof rule === "function") {
  48. rule = {
  49. create: rule
  50. };
  51. }
  52. return rule;
  53. }
  54. /**
  55. * Gets a complete options schema for a rule.
  56. * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
  57. * @returns {Object} JSON Schema for the rule's options.
  58. */
  59. function getRuleOptionsSchema(rule) {
  60. if (!rule) {
  61. return null;
  62. }
  63. const schema = rule.schema || rule.meta && rule.meta.schema;
  64. if (Array.isArray(schema)) {
  65. if (schema.length) {
  66. return {
  67. type: "array",
  68. items: schema,
  69. minItems: 0,
  70. maxItems: schema.length
  71. };
  72. }
  73. return {
  74. type: "array",
  75. minItems: 0,
  76. maxItems: 0
  77. };
  78. }
  79. // Given a full schema, leave it alone
  80. return schema || null;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Exports
  84. //-----------------------------------------------------------------------------
  85. module.exports = {
  86. parseRuleId,
  87. getRuleFromConfig,
  88. getRuleOptionsSchema
  89. };