index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  3. const parseSelector = require('../../utils/parseSelector');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const ruleName = 'selector-pseudo-class-parentheses-space-inside';
  8. const messages = ruleMessages(ruleName, {
  9. expectedOpening: 'Expected single space after "("',
  10. rejectedOpening: 'Unexpected whitespace after "("',
  11. expectedClosing: 'Expected single space before ")"',
  12. rejectedClosing: 'Unexpected whitespace before ")"',
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/list/selector-pseudo-class-parentheses-space-inside',
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, _secondaryOptions, context) => {
  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.walkRules((ruleNode) => {
  28. if (!isStandardSyntaxRule(ruleNode)) {
  29. return;
  30. }
  31. if (!ruleNode.selector.includes('(')) {
  32. return;
  33. }
  34. let hasFixed = false;
  35. const selector = ruleNode.raws.selector ? ruleNode.raws.selector.raw : ruleNode.selector;
  36. const fixedSelector = parseSelector(selector, result, ruleNode, (selectorTree) => {
  37. selectorTree.walkPseudos((pseudoNode) => {
  38. if (!pseudoNode.length) {
  39. return;
  40. }
  41. const paramString = pseudoNode.map((node) => String(node)).join(',');
  42. const nextCharIsSpace = paramString.startsWith(' ');
  43. const openIndex = pseudoNode.sourceIndex + pseudoNode.value.length + 1;
  44. if (nextCharIsSpace && primary === 'never') {
  45. if (context.fix) {
  46. hasFixed = true;
  47. setFirstNodeSpaceBefore(pseudoNode, '');
  48. } else {
  49. complain(messages.rejectedOpening, openIndex);
  50. }
  51. }
  52. if (!nextCharIsSpace && primary === 'always') {
  53. if (context.fix) {
  54. hasFixed = true;
  55. setFirstNodeSpaceBefore(pseudoNode, ' ');
  56. } else {
  57. complain(messages.expectedOpening, openIndex);
  58. }
  59. }
  60. const prevCharIsSpace = paramString.endsWith(' ');
  61. const closeIndex = openIndex + paramString.length - 1;
  62. if (prevCharIsSpace && primary === 'never') {
  63. if (context.fix) {
  64. hasFixed = true;
  65. setLastNodeSpaceAfter(pseudoNode, '');
  66. } else {
  67. complain(messages.rejectedClosing, closeIndex);
  68. }
  69. }
  70. if (!prevCharIsSpace && primary === 'always') {
  71. if (context.fix) {
  72. hasFixed = true;
  73. setLastNodeSpaceAfter(pseudoNode, ' ');
  74. } else {
  75. complain(messages.expectedClosing, closeIndex);
  76. }
  77. }
  78. });
  79. });
  80. if (hasFixed && fixedSelector) {
  81. if (!ruleNode.raws.selector) {
  82. ruleNode.selector = fixedSelector;
  83. } else {
  84. ruleNode.raws.selector.raw = fixedSelector;
  85. }
  86. }
  87. /**
  88. * @param {string} message
  89. * @param {number} index
  90. */
  91. function complain(message, index) {
  92. report({
  93. message,
  94. index,
  95. result,
  96. ruleName,
  97. node: ruleNode,
  98. });
  99. }
  100. });
  101. };
  102. };
  103. /**
  104. * @param {import('postcss-selector-parser').Container} node
  105. * @param {string} value
  106. * @returns {void}
  107. */
  108. function setFirstNodeSpaceBefore(node, value) {
  109. const target = node.first;
  110. if (target.type === 'selector') {
  111. setFirstNodeSpaceBefore(target, value);
  112. } else {
  113. target.spaces.before = value;
  114. }
  115. }
  116. /**
  117. * @param {import('postcss-selector-parser').Container} node
  118. * @param {string} value
  119. * @returns {void}
  120. */
  121. function setLastNodeSpaceAfter(node, value) {
  122. const target = node.last;
  123. if (target.type === 'selector') {
  124. setLastNodeSpaceAfter(target, value);
  125. } else {
  126. target.spaces.after = value;
  127. }
  128. }
  129. rule.ruleName = ruleName;
  130. rule.messages = messages;
  131. rule.meta = meta;
  132. module.exports = rule;