config-ops.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @fileoverview Config file operations. This file must be usable in the browser,
  3. * so no Node-specific code can be here.
  4. * @author Nicholas C. Zakas
  5. */
  6. //------------------------------------------------------------------------------
  7. // Private
  8. //------------------------------------------------------------------------------
  9. const RULE_SEVERITY_STRINGS = ["off", "warn", "error"],
  10. RULE_SEVERITY = RULE_SEVERITY_STRINGS.reduce((map, value, index) => {
  11. map[value] = index;
  12. return map;
  13. }, {}),
  14. VALID_SEVERITIES = [0, 1, 2, "off", "warn", "error"];
  15. //------------------------------------------------------------------------------
  16. // Public Interface
  17. //------------------------------------------------------------------------------
  18. /**
  19. * Normalizes the severity value of a rule's configuration to a number
  20. * @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally
  21. * received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0),
  22. * the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array
  23. * whose first element is one of the above values. Strings are matched case-insensitively.
  24. * @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0.
  25. */
  26. function getRuleSeverity(ruleConfig) {
  27. const severityValue = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
  28. if (severityValue === 0 || severityValue === 1 || severityValue === 2) {
  29. return severityValue;
  30. }
  31. if (typeof severityValue === "string") {
  32. return RULE_SEVERITY[severityValue.toLowerCase()] || 0;
  33. }
  34. return 0;
  35. }
  36. /**
  37. * Converts old-style severity settings (0, 1, 2) into new-style
  38. * severity settings (off, warn, error) for all rules. Assumption is that severity
  39. * values have already been validated as correct.
  40. * @param {Object} config The config object to normalize.
  41. * @returns {void}
  42. */
  43. function normalizeToStrings(config) {
  44. if (config.rules) {
  45. Object.keys(config.rules).forEach(ruleId => {
  46. const ruleConfig = config.rules[ruleId];
  47. if (typeof ruleConfig === "number") {
  48. config.rules[ruleId] = RULE_SEVERITY_STRINGS[ruleConfig] || RULE_SEVERITY_STRINGS[0];
  49. } else if (Array.isArray(ruleConfig) && typeof ruleConfig[0] === "number") {
  50. ruleConfig[0] = RULE_SEVERITY_STRINGS[ruleConfig[0]] || RULE_SEVERITY_STRINGS[0];
  51. }
  52. });
  53. }
  54. }
  55. /**
  56. * Determines if the severity for the given rule configuration represents an error.
  57. * @param {int|string|Array} ruleConfig The configuration for an individual rule.
  58. * @returns {boolean} True if the rule represents an error, false if not.
  59. */
  60. function isErrorSeverity(ruleConfig) {
  61. return getRuleSeverity(ruleConfig) === 2;
  62. }
  63. /**
  64. * Checks whether a given config has valid severity or not.
  65. * @param {number|string|Array} ruleConfig The configuration for an individual rule.
  66. * @returns {boolean} `true` if the configuration has valid severity.
  67. */
  68. function isValidSeverity(ruleConfig) {
  69. let severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
  70. if (typeof severity === "string") {
  71. severity = severity.toLowerCase();
  72. }
  73. return VALID_SEVERITIES.indexOf(severity) !== -1;
  74. }
  75. /**
  76. * Checks whether every rule of a given config has valid severity or not.
  77. * @param {Object} config The configuration for rules.
  78. * @returns {boolean} `true` if the configuration has valid severity.
  79. */
  80. function isEverySeverityValid(config) {
  81. return Object.keys(config).every(ruleId => isValidSeverity(config[ruleId]));
  82. }
  83. /**
  84. * Normalizes a value for a global in a config
  85. * @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in
  86. * a global directive comment
  87. * @returns {("readable"|"writeable"|"off")} The value normalized as a string
  88. * @throws Error if global value is invalid
  89. */
  90. function normalizeConfigGlobal(configuredValue) {
  91. switch (configuredValue) {
  92. case "off":
  93. return "off";
  94. case true:
  95. case "true":
  96. case "writeable":
  97. case "writable":
  98. return "writable";
  99. case null:
  100. case false:
  101. case "false":
  102. case "readable":
  103. case "readonly":
  104. return "readonly";
  105. default:
  106. throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`);
  107. }
  108. }
  109. export {
  110. getRuleSeverity,
  111. normalizeToStrings,
  112. isErrorSeverity,
  113. isValidSeverity,
  114. isEverySeverityValid,
  115. normalizeConfigGlobal
  116. };