functionCommaSpaceChecker.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. const declarationValueIndex = require('../utils/declarationValueIndex');
  3. const getDeclarationValue = require('../utils/getDeclarationValue');
  4. const isStandardSyntaxFunction = require('../utils/isStandardSyntaxFunction');
  5. const report = require('../utils/report');
  6. const setDeclarationValue = require('../utils/setDeclarationValue');
  7. const valueParser = require('postcss-value-parser');
  8. /** @typedef {import('postcss-value-parser').Node} ValueParserNode */
  9. /** @typedef {import('postcss-value-parser').DivNode} ValueParserDivNode */
  10. /** @typedef {(args: { source: string, index: number, err: (message: string) => void }) => void} LocationChecker */
  11. /**
  12. * @param {{
  13. * root: import('postcss').Root,
  14. * locationChecker: LocationChecker,
  15. * fix: ((node: ValueParserDivNode, index: number, nodes: ValueParserNode[]) => boolean) | null,
  16. * result: import('stylelint').PostcssResult,
  17. * checkedRuleName: string,
  18. * }} opts
  19. */
  20. module.exports = function functionCommaSpaceChecker(opts) {
  21. opts.root.walkDecls((decl) => {
  22. const declValue = getDeclarationValue(decl);
  23. let hasFixed;
  24. const parsedValue = valueParser(declValue);
  25. parsedValue.walk((valueNode) => {
  26. if (valueNode.type !== 'function') {
  27. return;
  28. }
  29. if (!isStandardSyntaxFunction(valueNode)) {
  30. return;
  31. }
  32. // Ignore `url()` arguments, which may contain data URIs or other funky stuff
  33. if (valueNode.value.toLowerCase() === 'url') {
  34. return;
  35. }
  36. const argumentStrings = valueNode.nodes.map((node) => valueParser.stringify(node));
  37. const functionArguments = (() => {
  38. // Remove function name and parens
  39. let result = valueNode.before + argumentStrings.join('') + valueNode.after;
  40. // 1. Remove comments including preceding whitespace (when only succeeded by whitespace)
  41. // 2. Remove all other comments, but leave adjacent whitespace intact
  42. // eslint-disable-next-line regexp/no-dupe-disjunctions -- TODO: Possible to simplify the regex.
  43. result = result.replace(/( *\/(\*.*\*\/(?!\S)|\/.*)|(\/(\*.*\*\/|\/.*)))/, '');
  44. return result;
  45. })();
  46. /**
  47. * Gets the index of the comma for checking.
  48. * @param {ValueParserDivNode} commaNode The comma node
  49. * @param {number} nodeIndex The index of the comma node
  50. * @returns {number} The index of the comma for checking
  51. */
  52. const getCommaCheckIndex = (commaNode, nodeIndex) => {
  53. let commaBefore =
  54. valueNode.before + argumentStrings.slice(0, nodeIndex).join('') + commaNode.before;
  55. // 1. Remove comments including preceding whitespace (when only succeeded by whitespace)
  56. // 2. Remove all other comments, but leave adjacent whitespace intact
  57. // eslint-disable-next-line regexp/no-dupe-disjunctions -- TODO: Possible to simplify the regex.
  58. commaBefore = commaBefore.replace(/( *\/(\*.*\*\/(?!\S)|\/.*)|(\/(\*.*\*\/|\/.*)))/, '');
  59. return commaBefore.length;
  60. };
  61. /** @type {{ commaNode: ValueParserDivNode, checkIndex: number, nodeIndex: number }[]} */
  62. const commaDataList = [];
  63. for (const [nodeIndex, node] of valueNode.nodes.entries()) {
  64. if (node.type !== 'div' || node.value !== ',') {
  65. continue;
  66. }
  67. const checkIndex = getCommaCheckIndex(node, nodeIndex);
  68. commaDataList.push({
  69. commaNode: node,
  70. checkIndex,
  71. nodeIndex,
  72. });
  73. }
  74. for (const { commaNode, checkIndex, nodeIndex } of commaDataList) {
  75. opts.locationChecker({
  76. source: functionArguments,
  77. index: checkIndex,
  78. err: (message) => {
  79. const index =
  80. declarationValueIndex(decl) + commaNode.sourceIndex + commaNode.before.length;
  81. if (opts.fix && opts.fix(commaNode, nodeIndex, valueNode.nodes)) {
  82. hasFixed = true;
  83. return;
  84. }
  85. report({
  86. index,
  87. message,
  88. node: decl,
  89. result: opts.result,
  90. ruleName: opts.checkedRuleName,
  91. });
  92. },
  93. });
  94. }
  95. });
  96. if (hasFixed) {
  97. setDeclarationValue(decl, parsedValue.toString());
  98. }
  99. });
  100. };