index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const getDeclarationValue = require('../../utils/getDeclarationValue');
  4. const isSingleLineString = require('../../utils/isSingleLineString');
  5. const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
  6. const report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const setDeclarationValue = require('../../utils/setDeclarationValue');
  9. const validateOptions = require('../../utils/validateOptions');
  10. const valueParser = require('postcss-value-parser');
  11. const ruleName = 'function-parentheses-space-inside';
  12. const messages = ruleMessages(ruleName, {
  13. expectedOpening: 'Expected single space after "("',
  14. rejectedOpening: 'Unexpected whitespace after "("',
  15. expectedClosing: 'Expected single space before ")"',
  16. rejectedClosing: 'Unexpected whitespace before ")"',
  17. expectedOpeningSingleLine: 'Expected single space after "(" in a single-line function',
  18. rejectedOpeningSingleLine: 'Unexpected whitespace after "(" in a single-line function',
  19. expectedClosingSingleLine: 'Expected single space before ")" in a single-line function',
  20. rejectedClosingSingleLine: 'Unexpected whitespace before ")" in a single-line function',
  21. });
  22. const meta = {
  23. url: 'https://stylelint.io/user-guide/rules/list/function-parentheses-space-inside',
  24. };
  25. /** @type {import('stylelint').Rule} */
  26. const rule = (primary, _secondaryOptions, context) => {
  27. return (root, result) => {
  28. const validOptions = validateOptions(result, ruleName, {
  29. actual: primary,
  30. possible: ['always', 'never', 'always-single-line', 'never-single-line'],
  31. });
  32. if (!validOptions) {
  33. return;
  34. }
  35. root.walkDecls((decl) => {
  36. if (!decl.value.includes('(')) {
  37. return;
  38. }
  39. let hasFixed = false;
  40. const declValue = getDeclarationValue(decl);
  41. const parsedValue = valueParser(declValue);
  42. parsedValue.walk((valueNode) => {
  43. if (valueNode.type !== 'function') {
  44. return;
  45. }
  46. if (!isStandardSyntaxFunction(valueNode)) {
  47. return;
  48. }
  49. // Ignore function without parameters
  50. if (!valueNode.nodes.length) {
  51. return;
  52. }
  53. const functionString = valueParser.stringify(valueNode);
  54. const isSingleLine = isSingleLineString(functionString);
  55. // Check opening ...
  56. const openingIndex = valueNode.sourceIndex + valueNode.value.length + 1;
  57. if (primary === 'always' && valueNode.before !== ' ') {
  58. if (context.fix) {
  59. hasFixed = true;
  60. valueNode.before = ' ';
  61. } else {
  62. complain(messages.expectedOpening, openingIndex);
  63. }
  64. }
  65. if (primary === 'never' && valueNode.before !== '') {
  66. if (context.fix) {
  67. hasFixed = true;
  68. valueNode.before = '';
  69. } else {
  70. complain(messages.rejectedOpening, openingIndex);
  71. }
  72. }
  73. if (isSingleLine && primary === 'always-single-line' && valueNode.before !== ' ') {
  74. if (context.fix) {
  75. hasFixed = true;
  76. valueNode.before = ' ';
  77. } else {
  78. complain(messages.expectedOpeningSingleLine, openingIndex);
  79. }
  80. }
  81. if (isSingleLine && primary === 'never-single-line' && valueNode.before !== '') {
  82. if (context.fix) {
  83. hasFixed = true;
  84. valueNode.before = '';
  85. } else {
  86. complain(messages.rejectedOpeningSingleLine, openingIndex);
  87. }
  88. }
  89. // Check closing ...
  90. const closingIndex = valueNode.sourceIndex + functionString.length - 2;
  91. if (primary === 'always' && valueNode.after !== ' ') {
  92. if (context.fix) {
  93. hasFixed = true;
  94. valueNode.after = ' ';
  95. } else {
  96. complain(messages.expectedClosing, closingIndex);
  97. }
  98. }
  99. if (primary === 'never' && valueNode.after !== '') {
  100. if (context.fix) {
  101. hasFixed = true;
  102. valueNode.after = '';
  103. } else {
  104. complain(messages.rejectedClosing, closingIndex);
  105. }
  106. }
  107. if (isSingleLine && primary === 'always-single-line' && valueNode.after !== ' ') {
  108. if (context.fix) {
  109. hasFixed = true;
  110. valueNode.after = ' ';
  111. } else {
  112. complain(messages.expectedClosingSingleLine, closingIndex);
  113. }
  114. }
  115. if (isSingleLine && primary === 'never-single-line' && valueNode.after !== '') {
  116. if (context.fix) {
  117. hasFixed = true;
  118. valueNode.after = '';
  119. } else {
  120. complain(messages.rejectedClosingSingleLine, closingIndex);
  121. }
  122. }
  123. });
  124. if (hasFixed) {
  125. setDeclarationValue(decl, parsedValue.toString());
  126. }
  127. /**
  128. * @param {string} message
  129. * @param {number} offset
  130. */
  131. function complain(message, offset) {
  132. report({
  133. ruleName,
  134. result,
  135. message,
  136. node: decl,
  137. index: declarationValueIndex(decl) + offset,
  138. });
  139. }
  140. });
  141. };
  142. };
  143. rule.ruleName = ruleName;
  144. rule.messages = messages;
  145. rule.meta = meta;
  146. module.exports = rule;