UNPKG

1.35 kBJavaScriptView Raw
1'use strict';
2
3const { isComment, hasSource } = require('./typeGuards');
4
5/**
6 * @param {import('postcss').Node} statement
7 * @returns {boolean}
8 */
9module.exports = function (statement) {
10 const parentNode = statement.parent;
11
12 if (parentNode === undefined || parentNode.type === 'root') {
13 return false;
14 }
15
16 if (statement === parentNode.first) {
17 return true;
18 }
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
25 const parentNodes = parentNode.nodes;
26
27 if (!parentNodes) {
28 return false;
29 }
30
31 const firstNode = parentNodes[0];
32
33 if (
34 !isComment(firstNode) ||
35 (typeof firstNode.raws.before === 'string' && firstNode.raws.before.includes('\n'))
36 ) {
37 return false;
38 }
39
40 if (!hasSource(firstNode) || !firstNode.source.start) {
41 return false;
42 }
43
44 const openingBraceLine = firstNode.source.start.line;
45
46 if (!firstNode.source.end || openingBraceLine !== firstNode.source.end.line) {
47 return false;
48 }
49
50 for (let i = 1; i < parentNodes.length; i++) {
51 const node = parentNodes[i];
52
53 if (node === statement) {
54 return true;
55 }
56
57 if (
58 !isComment(node) ||
59 (hasSource(node) && node.source.end && node.source.end.line !== openingBraceLine)
60 ) {
61 return false;
62 }
63 }
64
65 /* istanbul ignore next: Should always return in the loop */
66 return false;
67};