UNPKG

1.3 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const getNextNonSharedLineCommentNode = require('./getNextNonSharedLineCommentNode');
5const getPreviousNonSharedLineCommentNode = require('./getPreviousNonSharedLineCommentNode');
6
7/** @typedef {import('postcss').Node} PostcssNode */
8
9/**
10 *
11 * @param {PostcssNode | void} a
12 * @param {PostcssNode | void} b
13 */
14function nodesShareLines(a, b) {
15 return _.get(a, 'source.end.line') === _.get(b, 'source.start.line');
16}
17
18/**
19 * @param {PostcssNode} node
20 * @returns {boolean}
21 */
22module.exports = function isSharedLineComment(node) {
23 if (node.type !== 'comment') {
24 return false;
25 }
26
27 const previousNonSharedLineCommentNode = getPreviousNonSharedLineCommentNode(node);
28
29 if (nodesShareLines(previousNonSharedLineCommentNode, node)) {
30 return true;
31 }
32
33 const nextNonSharedLineCommentNode = getNextNonSharedLineCommentNode(node);
34
35 if (nextNonSharedLineCommentNode && nodesShareLines(node, nextNonSharedLineCommentNode)) {
36 return true;
37 }
38
39 const parentNode = node.parent;
40
41 // It's a first child and located on the same line as block start
42 if (
43 parentNode !== undefined &&
44 parentNode.type !== 'root' &&
45 parentNode.index(node) === 0 &&
46 node.raws.before !== undefined &&
47 !node.raws.before.includes('\n')
48 ) {
49 return true;
50 }
51
52 return false;
53};