index.js 2.7 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-after';
  9. const messages = ruleMessages(ruleName, {
  10. expectedAfter: () => 'Expected single space after range operator',
  11. rejectedAfter: () => 'Unexpected whitespace after range operator',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/list/media-feature-range-operator-space-after',
  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. checkAfterOperator(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 + 1);
  39. const afterOperator = params.slice(index + 1);
  40. if (primary === 'always') {
  41. params = beforeOperator + afterOperator.replace(/^\s*/, ' ');
  42. } else if (primary === 'never') {
  43. params = beforeOperator + afterOperator.replace(/^\s*/, '');
  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 checkAfterOperator(match, params, node, fix) {
  60. const endIndex = match.startIndex + match.target.length - 1;
  61. checker.after({
  62. source: params,
  63. index: endIndex,
  64. err: (m) => {
  65. if (fix) {
  66. fix(endIndex);
  67. return;
  68. }
  69. report({
  70. message: m,
  71. node,
  72. index: endIndex + atRuleParamIndex(node) + 1,
  73. result,
  74. ruleName,
  75. });
  76. },
  77. });
  78. }
  79. };
  80. };
  81. rule.ruleName = ruleName;
  82. rule.messages = messages;
  83. rule.meta = meta;
  84. module.exports = rule;