nodeFieldIndices.cjs 3.0 KB

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