isFirstNested.cjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 typeGuards = require('./typeGuards.cjs');
  5. /**
  6. * @param {import('postcss').Node} statement
  7. * @returns {boolean}
  8. */
  9. function isFirstNested(statement) {
  10. const parentNode = statement.parent;
  11. if (parentNode === undefined) {
  12. return false;
  13. }
  14. if (typeGuards.isRoot(parentNode) && !isInDocument(parentNode)) {
  15. return false;
  16. }
  17. if (statement === parentNode.first) {
  18. return true;
  19. }
  20. /*
  21. * Search for the statement in the parent's nodes, ignoring comment
  22. * nodes on the same line as the parent's opening brace.
  23. */
  24. const parentNodes = parentNode.nodes;
  25. if (!parentNodes) {
  26. return false;
  27. }
  28. const firstNode = parentNodes[0];
  29. if (!firstNode) {
  30. return false;
  31. }
  32. if (
  33. !typeGuards.isComment(firstNode) ||
  34. (typeof firstNode.raws.before === 'string' && firstNode.raws.before.includes('\n'))
  35. ) {
  36. return false;
  37. }
  38. if (!typeGuards.hasSource(firstNode) || !firstNode.source.start) {
  39. return false;
  40. }
  41. const openingBraceLine = firstNode.source.start.line;
  42. if (!firstNode.source.end || openingBraceLine !== firstNode.source.end.line) {
  43. return false;
  44. }
  45. for (const [index, node] of parentNodes.entries()) {
  46. if (index === 0) {
  47. continue;
  48. }
  49. if (node === statement) {
  50. return true;
  51. }
  52. if (
  53. !typeGuards.isComment(node) ||
  54. (typeGuards.hasSource(node) && node.source.end && node.source.end.line !== openingBraceLine)
  55. ) {
  56. return false;
  57. }
  58. }
  59. /* istanbul ignore next: Should always return in the loop */
  60. return false;
  61. }
  62. /**
  63. * @param {import('postcss').Node} node
  64. * @returns {boolean}
  65. */
  66. function isInDocument({ parent }) {
  67. return Boolean(parent && typeGuards.isDocument(parent));
  68. }
  69. module.exports = isFirstNested;