checkAgainstRule.cjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Result = require('postcss/lib/result');
  5. const getStylelintRule = require('./getStylelintRule.cjs');
  6. const validateTypes = require('./validateTypes.cjs');
  7. const cssTree = require('css-tree');
  8. const normalizeRuleSettings = require('../normalizeRuleSettings.cjs');
  9. /**
  10. * @type {import('stylelint').Utils['checkAgainstRule']}
  11. */
  12. async function checkAgainstRule(options, callback) {
  13. if (!validateTypes.isPlainObject(options)) throw new Error('Expected an options object');
  14. if (!callback) throw new Error('Expected a callback function');
  15. const { ruleName, ruleSettings, root, result, context = { lexer: cssTree.lexer } } = options;
  16. if (!ruleName) throw new Error('Expected a "ruleName" option');
  17. const rule = await getStylelintRule(ruleName, result?.stylelint?.config);
  18. if (!rule) throw new Error(`Rule "${ruleName}" does not exist`);
  19. if (!ruleSettings) throw new Error('Expected a "ruleSettings" option');
  20. if (!root) throw new Error('Expected a "root" option');
  21. const settings = normalizeRuleSettings(ruleSettings, rule);
  22. if (!settings) {
  23. return;
  24. }
  25. const tmpPostcssResult = new Result(
  26. // NOTE: The first argument is unused, so passing `undefined` raises no problems.
  27. // But this PostCSS's behavior may change in the future.
  28. // @ts-expect-error -- TS2345: Argument of type 'undefined' is not assignable to parameter of type 'Processor'.
  29. undefined,
  30. undefined,
  31. undefined,
  32. );
  33. /** @type {import('stylelint').StylelintPostcssResult} */
  34. const stylelint = result?.stylelint ?? {
  35. ruleSeverities: {},
  36. customMessages: {},
  37. customUrls: {},
  38. ruleMetadata: {},
  39. fixersData: {},
  40. rangesOfComputedEditInfos: [],
  41. disabledRanges: {},
  42. };
  43. // @ts-expect-error -- TS2339: Property 'stylelint' does not exist on type 'Result<undefined>'.
  44. tmpPostcssResult.stylelint = stylelint;
  45. const [primary, secondary] = settings;
  46. const ruleFunc = rule(primary, secondary || {}, context);
  47. await ruleFunc(
  48. root,
  49. // NOTE: This temporary PostCSS result doesn't have a property for Stylelint use.
  50. // Problems may occur if some rules use the property.
  51. // @ts-expect-error -- TS2345: Argument of type 'Result' is not assignable to parameter of type 'PostcssResult'.
  52. tmpPostcssResult,
  53. );
  54. for (const warning of tmpPostcssResult.warnings()) callback(warning);
  55. }
  56. module.exports = checkAgainstRule;