functionCommaSpaceChecker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. result = result.replace(/( *\/(\*.*\*\/(?!\S)|\/.*)|(\/(\*.*\*\/|\/.*)))/, '');
  43. return result;
  44. })();
  45. /**
  46. * Gets the index of the comma for checking.
  47. * @param {ValueParserDivNode} commaNode The comma node
  48. * @param {number} nodeIndex The index of the comma node
  49. * @returns {number} The index of the comma for checking
  50. */
  51. const getCommaCheckIndex = (commaNode, nodeIndex) => {
  52. let commaBefore =
  53. valueNode.before + argumentStrings.slice(0, nodeIndex).join('') + commaNode.before;
  54. // 1. Remove comments including preceding whitespace (when only succeeded by whitespace)
  55. // 2. Remove all other comments, but leave adjacent whitespace intact
  56. commaBefore = commaBefore.replace(/( *\/(\*.*\*\/(?!\S)|\/.*)|(\/(\*.*\*\/|\/.*)))/, '');
  57. return commaBefore.length;
  58. };
  59. /** @type {{ commaNode: ValueParserDivNode, checkIndex: number, nodeIndex: number }[]} */
  60. const commaDataList = [];
  61. for (const [nodeIndex, node] of valueNode.nodes.entries()) {
  62. if (node.type !== 'div' || node.value !== ',') {
  63. continue;
  64. }
  65. const checkIndex = getCommaCheckIndex(node, nodeIndex);
  66. commaDataList.push({
  67. commaNode: node,
  68. checkIndex,
  69. nodeIndex,
  70. });
  71. }
  72. for (const { commaNode, checkIndex, nodeIndex } of commaDataList) {
  73. opts.locationChecker({
  74. source: functionArguments,
  75. index: checkIndex,
  76. err: (message) => {
  77. const index =
  78. declarationValueIndex(decl) + commaNode.sourceIndex + commaNode.before.length;
  79. if (opts.fix && opts.fix(commaNode, nodeIndex, valueNode.nodes)) {
  80. hasFixed = true;
  81. return;
  82. }
  83. report({
  84. index,
  85. message,
  86. node: decl,
  87. result: opts.result,
  88. ruleName: opts.checkedRuleName,
  89. });
  90. },
  91. });
  92. }
  93. });
  94. if (hasFixed) {
  95. setDeclarationValue(decl, parsedValue.toString());
  96. }
  97. });
  98. };