validateOptions.cjs 4.1 KB

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