index.js 2.8 KB

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