You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
2 years ago
|
"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) {
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TODO: Add a non-printing renderAST method, for embedding in existing console.* calls
|
||
|
|
||
|
module.exports = function printAST(ast) {
|
||
|
return printNode(ast, 0);
|
||
|
};
|