validateDisableSettings.cjs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const constants = require('./constants.cjs');
  6. const validateOptions = require('./utils/validateOptions.cjs');
  7. /** @import {DisablePropertyName, DisableOptions, PostcssResult} from 'stylelint' */
  8. /**
  9. * Validates that the stylelint config for `result` has a valid disable field
  10. * named `field`, and returns the result in normalized form.
  11. *
  12. * Returns `[]` if no disables should be reported, and automatically reports
  13. * an invalid configuration.
  14. *
  15. * @param {PostcssResult} result
  16. * @param {DisablePropertyName} field
  17. * @returns {[boolean, Required<DisableOptions>] | []}
  18. */
  19. function validateDisableSettings(result, field) {
  20. const stylelintResult = result.stylelint;
  21. // Files with linting errors may not have configs associated with them.
  22. if (!stylelintResult.config) return [];
  23. const rawSettings = stylelintResult.config[field];
  24. /** @type {boolean} */
  25. let enabled;
  26. /** @type {DisableOptions} */
  27. let options;
  28. if (Array.isArray(rawSettings)) {
  29. enabled = rawSettings[0];
  30. options = rawSettings[1] || {};
  31. } else {
  32. enabled = rawSettings || false;
  33. options = {};
  34. }
  35. const validOptions = validateOptions(
  36. result,
  37. field,
  38. {
  39. actual: enabled,
  40. possible: [true, false],
  41. },
  42. {
  43. actual: options,
  44. possible: {
  45. except: [validateTypes.isString, validateTypes.isRegExp],
  46. },
  47. },
  48. );
  49. if (!validOptions) return [];
  50. // If the check is disabled with no exceptions, there's no reason to run
  51. // it at all.
  52. if (!enabled && !options.except) return [];
  53. return [
  54. enabled,
  55. {
  56. except: options.except || [],
  57. severity: options.severity || stylelintResult.config.defaultSeverity || constants.DEFAULT_SEVERITY,
  58. },
  59. ];
  60. }
  61. module.exports = validateDisableSettings;