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