normalizeRuleSettings.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. const rules = require('./rules');
  3. const { isPlainObject } = require('./utils/validateTypes');
  4. // Rule settings can take a number of forms, e.g.
  5. // a. "rule-name": null
  6. // b. "rule-name": [null, ...]
  7. // c. "rule-name": primaryOption
  8. // d. "rule-name": [primaryOption]
  9. // e. "rule-name": [primaryOption, secondaryOption]
  10. // Where primaryOption can be anything: primitive, Object, or Array.
  11. /**
  12. * This function normalizes all the possibilities into the
  13. * standard form: [primaryOption, secondaryOption]
  14. * Except in the cases with null, a & b, in which case
  15. * null is returned
  16. * @template T
  17. * @template {Object} O
  18. * @param {import('stylelint').ConfigRuleSettings<T, O>} rawSettings
  19. * @param {string} ruleName
  20. * @param {boolean} [primaryOptionArray] If primaryOptionArray is not provided, we try to get it from the rules themselves, which will not work for plugins
  21. * @return {[T] | [T, O] | null}
  22. */
  23. module.exports = function normalizeRuleSettings(
  24. rawSettings,
  25. ruleName,
  26. // If primaryOptionArray is not provided, we try to get it from the
  27. // rules themselves, which will not work for plugins
  28. primaryOptionArray,
  29. ) {
  30. if (rawSettings === null || rawSettings === undefined) {
  31. return null;
  32. }
  33. if (!Array.isArray(rawSettings)) {
  34. return [rawSettings];
  35. }
  36. // Everything below is an array ...
  37. if (rawSettings.length > 0 && (rawSettings[0] === null || rawSettings[0] === undefined)) {
  38. return null;
  39. }
  40. if (primaryOptionArray === undefined) {
  41. const rule = rules[ruleName];
  42. if (rule && 'primaryOptionArray' in rule) {
  43. primaryOptionArray = rule.primaryOptionArray;
  44. }
  45. }
  46. if (!primaryOptionArray) {
  47. return rawSettings;
  48. }
  49. // Everything below is a rule that CAN have an array for a primary option ...
  50. // (they might also have something else, e.g. rule-properties-order can
  51. // have the string "alphabetical")
  52. if (rawSettings.length === 1 && Array.isArray(rawSettings[0])) {
  53. return rawSettings;
  54. }
  55. if (rawSettings.length === 2 && !isPlainObject(rawSettings[0]) && isPlainObject(rawSettings[1])) {
  56. return rawSettings;
  57. }
  58. // `T` must be an array type, but TSC thinks it's probably invalid to
  59. // cast `[T]` to `T` so we cast through `any` first.
  60. return [/** @type {T} */ (/** @type {any} */ (rawSettings))];
  61. };