index.js 2.8 KB

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