nodeFieldIndices.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import getAtRuleParams from './getAtRuleParams.mjs';
  2. import getRuleSelector from './getRuleSelector.mjs';
  3. import { isObject, isString } from './validateTypes.mjs';
  4. /** @import {AtRule, Declaration, Rule} from 'postcss' */
  5. /**
  6. * @param {AtRule} atRule
  7. * @returns {number}
  8. */
  9. export function atRuleParamIndex(atRule) {
  10. const index = atRuleAfterNameIndex(atRule);
  11. return index + (atRule.raws.afterName?.length ?? 0);
  12. }
  13. /**
  14. * @param {AtRule} atRule
  15. * @returns {number}
  16. */
  17. export function atRuleAfterIndex(atRule) {
  18. // subtract 1 for `}`
  19. const endOffset = atRule.source?.end?.offset;
  20. if (!endOffset) return atRule.toString().length - 1;
  21. const afterLength = atRule.raws?.after?.length;
  22. if (!afterLength) return endOffset - 1;
  23. return endOffset - (afterLength + 1);
  24. }
  25. /**
  26. * @param {AtRule} atRule
  27. * @returns {number}
  28. */
  29. export function atRuleAfterNameIndex(atRule) {
  30. // Initial 1 is for the `@`
  31. return 1 + atRule.name.length;
  32. }
  33. /**
  34. * @param {AtRule} atRule
  35. * @returns {number}
  36. */
  37. export function atRuleBetweenIndex(atRule) {
  38. return atRuleParamIndex(atRule) + getAtRuleParams(atRule).length;
  39. }
  40. /**
  41. * @param {Declaration} decl
  42. * @returns {number}
  43. */
  44. export function declarationBetweenIndex(decl) {
  45. const { prop } = decl.raws;
  46. const propIsObject = isObject(prop);
  47. return countChars([
  48. propIsObject && 'prefix' in prop && prop.prefix,
  49. (propIsObject && 'raw' in prop && prop.raw) || decl.prop,
  50. propIsObject && 'suffix' in prop && prop.suffix,
  51. ]);
  52. }
  53. /**
  54. * Get the index of a declaration's value
  55. *
  56. * @param {Declaration} decl
  57. * @returns {number}
  58. */
  59. export function declarationValueIndex(decl) {
  60. const { between, value } = decl.raws;
  61. return (
  62. declarationBetweenIndex(decl) +
  63. countChars([between || ':', value && 'prefix' in value && value.prefix])
  64. );
  65. }
  66. /**
  67. * @param {Rule} rule
  68. * @returns {number}
  69. */
  70. export function ruleBetweenIndex(rule) {
  71. return getRuleSelector(rule).length;
  72. }
  73. /**
  74. * @param {Rule} rule
  75. * @returns {number}
  76. */
  77. export function ruleAfterIndex(rule) {
  78. // subtract 1 for `}`
  79. const endOffset = rule.source?.end?.offset;
  80. if (!endOffset) return rule.toString().length - 1;
  81. const afterLength = rule.raws?.after?.length;
  82. if (!afterLength) return endOffset - 1;
  83. return endOffset - (afterLength + 1);
  84. }
  85. /**
  86. * @param {unknown[]} values
  87. * @returns {number}
  88. */
  89. function countChars(values) {
  90. return values.reduce((/** @type {number} */ count, value) => {
  91. if (isString(value)) return count + value.length;
  92. return count;
  93. }, 0);
  94. }