index.js 4.8 KB

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