normalizeRuleSettings.cjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const validateTypes = require('./utils/validateTypes.cjs');
  5. // Rule settings can take a number of forms, e.g.
  6. // a. "rule-name": null
  7. // b. "rule-name": [null, ...]
  8. // c. "rule-name": primaryOption
  9. // d. "rule-name": [primaryOption]
  10. // e. "rule-name": [primaryOption, secondaryOption]
  11. // Where primaryOption can be anything: primitive, Object, or Array.
  12. /**
  13. * This function normalizes all the possibilities into the
  14. * standard form: [primaryOption, secondaryOption]
  15. * Except in the cases with null, a & b, in which case
  16. * null is returned
  17. * @template T
  18. * @template {object} O
  19. * @param {import('stylelint').ConfigRuleSettings<T, O>} rawSettings
  20. * @param {import('stylelint').Rule<T, O>} [rule]
  21. * @returns {[T] | [T, O] | null}
  22. */
  23. function normalizeRuleSettings(rawSettings, rule) {
  24. if (rawSettings === null || rawSettings === undefined) {
  25. return null;
  26. }
  27. if (!Array.isArray(rawSettings)) {
  28. return [rawSettings];
  29. }
  30. // Everything below is an array ...
  31. const [primary, secondary] = rawSettings;
  32. if (rawSettings.length > 0 && (primary === null || primary === undefined)) {
  33. return null;
  34. }
  35. if (rule && !rule.primaryOptionArray) {
  36. return rawSettings;
  37. }
  38. // Everything below is a rule that CAN have an array for a primary option ...
  39. // (they might also have something else, e.g. rule-properties-order can
  40. // have the string "alphabetical")
  41. if (rawSettings.length === 1 && Array.isArray(primary)) {
  42. return rawSettings;
  43. }
  44. if (rawSettings.length === 2 && !validateTypes.isPlainObject(primary) && validateTypes.isPlainObject(secondary)) {
  45. return rawSettings;
  46. }
  47. // `T` must be an array type, but TSC thinks it's probably invalid to
  48. // cast `[T]` to `T` so we cast through `any` first.
  49. return [/** @type {T} */ (/** @type {any} */ (rawSettings))];
  50. }
  51. module.exports = normalizeRuleSettings;