index.js 4.5 KB

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