functionCommaSpaceChecker.js 3.0 KB

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