index.js 2.7 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-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. 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. checkAfterOperator(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 + 1);
  40. const afterOperator = params.slice(index + 1);
  41. if (primary === 'always') {
  42. params = beforeOperator + afterOperator.replace(/^\s*/, ' ');
  43. } else if (primary === 'never') {
  44. params = beforeOperator + afterOperator.replace(/^\s*/, '');
  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 checkAfterOperator(match, params, node, fix) {
  61. const endIndex = match.startIndex + match.target.length - 1;
  62. checker.after({
  63. source: params,
  64. index: endIndex,
  65. err: (m) => {
  66. if (fix) {
  67. fix(endIndex);
  68. return;
  69. }
  70. report({
  71. message: m,
  72. node,
  73. index: endIndex + atRuleParamIndex(node) + 1,
  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;