"use strict"; const chalk = require("chalk"); const util = require("util"); const INDENT = chalk.gray("│") + " "; function formatProperties(node) { let relevantProperties = Object.keys(node) .filter((property) => property !== "type" && typeof node[property] !== "object"); return relevantProperties .map((property) => `${chalk.yellow.bold(property)}: ${util.inspect(node[property], { colors: true })}`) .join(" "); } function printNode(node, level) { if (node != null) { console.log(INDENT.repeat(level) + chalk.bold(node.type ?? "") + " " + formatProperties(node)); for (let [ key, value ] of Object.entries(node)) { if (typeof value === "object" && value != null) { let isArray = Array.isArray(value); console.log(INDENT.repeat(level + 1) + chalk.cyan(key) + ":" + (isArray ? chalk.gray(" []") : "")); if (isArray) { value.forEach((item) => printNode(item, level + 1)); } else { printNode(value, level + 1); } } } } else { // This should never happen! console.log(INDENT.repeat(level) + chalk.bold.red(`! ${node}`)); } } // TODO: Add a non-printing renderAST method, for embedding in existing console.* calls module.exports = function printAST(ast) { return printNode(ast, 0); };