index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  3. const findMediaOperator = require('../findMediaOperator');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const whitespaceChecker = require('../../utils/whitespaceChecker');
  8. const ruleName = 'media-feature-range-operator-space-before';
  9. const messages = ruleMessages(ruleName, {
  10. expectedBefore: () => 'Expected single space before range operator',
  11. rejectedBefore: () => 'Unexpected whitespace before range operator',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/list/media-feature-range-operator-space-before',
  15. };
  16. /** @type {import('stylelint').Rule} */
  17. const rule = (primary, _secondaryOptions, context) => {
  18. const checker = whitespaceChecker('space', primary, messages);
  19. return (root, result) => {
  20. const validOptions = validateOptions(result, ruleName, {
  21. actual: primary,
  22. possible: ['always', 'never'],
  23. });
  24. if (!validOptions) {
  25. return;
  26. }
  27. root.walkAtRules(/^media$/i, (atRule) => {
  28. /** @type {number[]} */
  29. const fixOperatorIndices = [];
  30. /** @type {((index: number) => void) | null} */
  31. const fix = context.fix ? (index) => fixOperatorIndices.push(index) : null;
  32. findMediaOperator(atRule, (match, params, node) => {
  33. checkBeforeOperator(match, params, node, fix);
  34. });
  35. if (fixOperatorIndices.length) {
  36. let params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
  37. for (const index of fixOperatorIndices.sort((a, b) => b - a)) {
  38. const beforeOperator = params.slice(0, index);
  39. const afterOperator = params.slice(index);
  40. if (primary === 'always') {
  41. params = beforeOperator.replace(/\s*$/, ' ') + afterOperator;
  42. } else if (primary === 'never') {
  43. params = beforeOperator.replace(/\s*$/, '') + afterOperator;
  44. }
  45. }
  46. if (atRule.raws.params) {
  47. atRule.raws.params.raw = params;
  48. } else {
  49. atRule.params = params;
  50. }
  51. }
  52. });
  53. /**
  54. * @param {import('style-search').StyleSearchMatch} match
  55. * @param {string} params
  56. * @param {import('postcss').AtRule} node
  57. * @param {((index: number) => void) | null} fix
  58. */
  59. function checkBeforeOperator(match, params, node, fix) {
  60. // The extra `+ 1` is because the match itself contains
  61. // the character before the operator
  62. checker.before({
  63. source: params,
  64. index: match.startIndex,
  65. err: (m) => {
  66. if (fix) {
  67. fix(match.startIndex);
  68. return;
  69. }
  70. report({
  71. message: m,
  72. node,
  73. index: match.startIndex - 1 + atRuleParamIndex(node),
  74. result,
  75. ruleName,
  76. });
  77. },
  78. });
  79. }
  80. };
  81. };
  82. rule.ruleName = ruleName;
  83. rule.messages = messages;
  84. rule.meta = meta;
  85. module.exports = rule;