UNPKG

prettier-plugin-multiline-arrays

Version:
38 lines (37 loc) 1.01 kB
const ignoreTheseKeys = ['tokens']; const ignoreTheseChildTypes = [ 'string', 'number', ]; const commentTypes = [ 'Line', 'Block', 'CommentBlock', 'CommentLine', ]; function isMaybeComment(input) { return !(!input || typeof input !== 'object' || !('type' in input) || !commentTypes.includes(input.type) || !('value' in input)); } export function extractComments(node) { if (!node || typeof node !== 'object') { return []; } const comments = []; if (Array.isArray(node)) { comments.push(...node.filter(isMaybeComment)); } Object.keys(node).forEach((nodeKey) => { if (!ignoreTheseKeys.includes(nodeKey)) { const nodeChild = node[nodeKey]; if (!ignoreTheseChildTypes.includes(typeof nodeChild)) { comments.push(...extractComments(nodeChild)); } } }); // this might duplicate comments but our later code doesn't care return comments; }