index.js 4.8 KB

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