validateOptions.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. const arrayEqual = require('./arrayEqual');
  3. const { isPlainObject } = require('./validateTypes');
  4. const IGNORED_OPTIONS = new Set(['severity', 'message', 'reportDisables', 'disableFix']);
  5. /** @typedef {import('stylelint').RuleOptions} RuleOptions */
  6. /** @typedef {import('stylelint').RuleOptionsPossible} RuleOptionsPossible */
  7. /**
  8. * @type {import('stylelint').Utils['validateOptions']}
  9. */
  10. module.exports = function validateOptions(result, ruleName, ...optionDescriptions) {
  11. let noErrors = true;
  12. for (const optionDescription of optionDescriptions) {
  13. validate(optionDescription, ruleName, complain);
  14. }
  15. /**
  16. * @param {string} message
  17. */
  18. function complain(message) {
  19. noErrors = false;
  20. result.warn(message, {
  21. stylelintType: 'invalidOption',
  22. });
  23. result.stylelint = result.stylelint || {
  24. disabledRanges: {},
  25. ruleSeverities: {},
  26. customMessages: {},
  27. ruleMetadata: {},
  28. };
  29. result.stylelint.stylelintError = true;
  30. }
  31. return noErrors;
  32. };
  33. /**
  34. * @param {RuleOptions} opts
  35. * @param {string} ruleName
  36. * @param {(message: string) => void} complain
  37. */
  38. function validate(opts, ruleName, complain) {
  39. const possible = opts.possible;
  40. const actual = opts.actual;
  41. const optional = opts.optional;
  42. if (actual === false && !ruleName.startsWith('report')) {
  43. return complain(
  44. `Invalid option value "false" for rule "${ruleName}". Are you trying to disable this rule? If so use "null" instead`,
  45. );
  46. }
  47. if (actual === null || arrayEqual(actual, [null])) {
  48. return;
  49. }
  50. const nothingPossible =
  51. possible === undefined || (Array.isArray(possible) && possible.length === 0);
  52. if (nothingPossible && actual === true) {
  53. return;
  54. }
  55. if (actual === undefined) {
  56. if (nothingPossible || optional) {
  57. return;
  58. }
  59. complain(`Expected option value for rule "${ruleName}"`);
  60. return;
  61. }
  62. if (nothingPossible) {
  63. if (optional) {
  64. complain(
  65. `Incorrect configuration for rule "${ruleName}". Rule should have "possible" values for options validation`,
  66. );
  67. return;
  68. }
  69. complain(`Unexpected option value ${stringify(actual)} for rule "${ruleName}"`);
  70. return;
  71. }
  72. if (typeof possible === 'function') {
  73. if (!possible(actual)) {
  74. complain(`Invalid option ${stringify(actual)} for rule "${ruleName}"`);
  75. }
  76. return;
  77. }
  78. // If `possible` is an array instead of an object ...
  79. if (Array.isArray(possible)) {
  80. for (const a of [actual].flat()) {
  81. if (isValid(possible, a)) {
  82. continue;
  83. }
  84. complain(`Invalid option value ${stringify(a)} for rule "${ruleName}"`);
  85. }
  86. return;
  87. }
  88. // If actual is NOT an object ...
  89. if (!isPlainObject(actual) || typeof actual !== 'object' || actual == null) {
  90. complain(
  91. `Invalid option value ${stringify(actual)} for rule "${ruleName}": should be an object`,
  92. );
  93. return;
  94. }
  95. for (const [optionName, optionValue] of Object.entries(actual)) {
  96. if (IGNORED_OPTIONS.has(optionName)) {
  97. continue;
  98. }
  99. const possibleValue = possible && possible[optionName];
  100. if (!possibleValue) {
  101. complain(`Invalid option name "${optionName}" for rule "${ruleName}"`);
  102. continue;
  103. }
  104. for (const a of [optionValue].flat()) {
  105. if (isValid(possibleValue, a)) {
  106. continue;
  107. }
  108. complain(`Invalid value ${stringify(a)} for option "${optionName}" of rule "${ruleName}"`);
  109. }
  110. }
  111. }
  112. /**
  113. * @param {RuleOptionsPossible | RuleOptionsPossible[]} possible
  114. * @param {unknown} actual
  115. * @returns {boolean}
  116. */
  117. function isValid(possible, actual) {
  118. for (const possibility of [possible].flat()) {
  119. if (typeof possibility === 'function' && possibility(actual)) {
  120. return true;
  121. }
  122. if (actual === possibility) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. /**
  129. * @param {unknown} value
  130. * @returns {string}
  131. */
  132. function stringify(value) {
  133. if (typeof value === 'string') {
  134. return `"${value}"`;
  135. }
  136. return `"${JSON.stringify(value)}"`;
  137. }