@frontboi/prettier-plugin-ramses-style
Version:
Pretty your javascript and typescript project to Ramses style.
126 lines (125 loc) • 6.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.debugObjectProperty = debugObjectProperty;
exports.debugJSXAttribute = debugJSXAttribute;
exports.debugAST = debugAST;
exports.debugNodes = debugNodes;
const traverse = require('@babel/traverse').default;
/**
* Permet de visualiser simplement une propriété et afficher les informations importantes pour ce projet
* @param property la propriété
*/
function debugObjectProperty(property) {
if (property.key.type === 'Identifier') {
if (property.value.type === 'ArrowFunctionExpression') {
console.log(`${property.key.type} | ${property.value.type}`);
}
else if (property.value.type === 'NumericLiteral') {
const fieldNameLength = Math.abs((property.key.end || 0) - (property.key.start || 0));
const valueLength = Math.abs((property.value.end || 0) - (property.value.start || 0));
console.log(`${property.key.type} "${property.key.name}" (length: ${fieldNameLength}) | ${property.value.type} "${property.value.value}" (length: ${valueLength})`);
}
else if (property.value.type === 'ArrayExpression') {
const fieldNameLength = Math.abs((property.key.end || 0) - (property.key.start || 0));
const valueLength = Math.abs((property.value.end || 0) - (property.value.start || 0));
console.log(`${property.key.type} "${property.key.name}" (length: ${fieldNameLength}) | ${property.value.type} "${property.value.elements}" (length: ${valueLength})`);
}
}
}
/**
* Permet de visualiser simplement une propriété JSX et afficher les informations importantes pour ce projet
* @param attribute l'attribut
*/
function debugJSXAttribute(attribute) {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (((_a = attribute.value) === null || _a === void 0 ? void 0 : _a.type) === 'JSXExpressionContainer') {
if (((_b = attribute.value) === null || _b === void 0 ? void 0 : _b.expression.type) === 'MemberExpression') {
console.log(`"${attribute.name.name}" = "${(_c = attribute.value) === null || _c === void 0 ? void 0 : _c.expression.property}" (${(_d = attribute.value) === null || _d === void 0 ? void 0 : _d.expression.type})`);
}
else {
console.log(`"${attribute.name.name}" = "${(_e = attribute.value) === null || _e === void 0 ? void 0 : _e.expression}" (${(_f = attribute.value) === null || _f === void 0 ? void 0 : _f.expression.type})`);
}
}
else if (((_g = attribute.value) === null || _g === void 0 ? void 0 : _g.type) === 'StringLiteral') {
console.log(`"${attribute.name.name}" = "${(_h = attribute.value) === null || _h === void 0 ? void 0 : _h.value}" (${attribute.value.type})`);
}
else if (!attribute.value) {
console.log(`"${attribute.name.name}" = "${attribute.value}"`);
}
}
/**
* Permet de visualiser un AST de manière simple.
* @param ast l'AST à parcourir
*/
function debugAST(ast) {
let depth = 0;
traverse(ast, {
enter(path) {
var _a, _b;
depth++;
let tab = ' ';
let tabs = tab.repeat(depth);
const node = path.node;
let name;
switch (node.type) {
case 'VariableDeclarator':
name = node.id.name;
break;
case 'ObjectExpression':
name = 'no name';
break;
case 'Identifier':
name = node.name;
break;
case 'ObjectProperty':
name = node.key.name;
break;
}
console.log(`${tabs}"${name}" (${node.type}) l.${node.loc.start.line} to l.${node.loc.end.line}`);
if (node.leadingComments || node.trailingComments) {
console.log(`${tabs}${tab}comments:`);
if (node.leadingComments) {
console.log(`${tabs}${tab}${tab}${node.leadingComments.length} leading comments:`);
for (let i = 0; i < (((_a = node.leadingComments) === null || _a === void 0 ? void 0 : _a.length) || 0); i++) {
console.log(`${tabs}${tab}${tab}${tab}// ${node.leadingComments[i].value} (l.${node.leadingComments[i].loc.start.line})`);
}
}
if (node.trailingComments) {
console.log(`${tabs}${tab}${tab}${node.trailingComments.length} trailing comments:`);
for (let i = 0; i < (((_b = node.trailingComments) === null || _b === void 0 ? void 0 : _b.length) || 0); i++) {
console.log(`${tabs}${tab}${tab}${tab}// ${node.trailingComments[i].value} (l.${node.trailingComments[i].loc.start.line})`);
}
}
}
console.log(``);
},
exit(path) {
depth--;
},
});
}
/**
* Permet d'afficher des informations sur un tableau de nœuds
* @param nodes les nœuds à parcourir
*/
function debugNodes(nodes) {
var _a;
console.log(`============================ DEBUG NODES ============================`);
for (const node of nodes) {
console.log(`Nœud "${((_a = node.key) === null || _a === void 0 ? void 0 : _a.name) || 'pas de nom'}" (l.${node.loc.start.line} to l.${node.loc.end.line})`);
if (node.leadingComments) {
console.log(`\t${node.leadingComments.length} leading comments:`);
for (const c of node.leadingComments) {
console.log(`\t\t ${c.value} (l.${c.loc.start.line})`);
}
}
if (node.trailingComments) {
console.log(`\t${node.trailingComments.length} trailing comments:`);
for (const c of node.trailingComments) {
console.log(`\t\t ${c.value} (l.${c.loc.start.line})`);
}
}
console.log(``);
}
console.log(`=====================================================================\n`);
}