All files / dist renderer.js

17.29% Statements 23/133
7.05% Branches 6/85
2.04% Functions 1/49
17.96% Lines 23/128

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232  2x 4x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x               2x                                         2x       2x                                                                                                                                                                                                                                                                                                 2x         2x                   2x 2x           2x           2x                   2x  
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.render = exports.renderer = exports.build = void 0;
const ts_morph_1 = require("ts-morph");
const TS_1 = __importDefault(require("./TS"));
const SyntaxKindDelegator_1 = require("./SyntaxKindDelegator");
const SyntaxKindDelegator_types_1 = __importDefault(require("./SyntaxKindDelegator.types"));
const decorators_1 = require("./decorators");
const console_log_colors_1 = require("console-log-colors");
const node_tools_1 = require("./node-tools");
const node_signature_1 = require("./node-signature");
const constants_1 = require("./constants");
/**
 * Standardizes the properties handled by different function like declarations and expressions.
 * @param typeParams
 * @param args
 * @param returnNode
 * @returns
 */
const renderFNDetails = (typeParams, args, returnNode) => {
    const tp = (0, exports.build)(...typeParams);
    const ag = (0, exports.build)(...args);
    const rt = (0, exports.build)(returnNode);
    return [
        ...tp ? [
            (0, decorators_1.$section)('---', (0, decorators_1.$h)(5, undefined, "Type Arguments:"), tp)
        ] : [],
        ...ag ? [
            (0, decorators_1.$section)('---', (0, decorators_1.$h)(5, undefined, "Arguments:"), ag)
        ] : [],
        ...rt ? [
            (0, decorators_1.$section)('---', (0, decorators_1.$h)(5, undefined, "Returns:"), rt)
        ] : []
    ];
};
/**
 * Just a convenient way to combine strings as blocks of text within a file.
 * @param blocks
 * @returns
 */
const block = (...blocks) => blocks.filter(b => b.trim()).join('\n');
/**
 * Tweaking node handling via syntax kind
 */
const RENDER_MAP = {
    [SyntaxKindDelegator_types_1.default.TypeAliasDeclaration]: node => {
        const tn = node.getTypeNode();
        const args = node.getTypeParameters();
        const tArgs = (0, exports.build)(...args);
        const typed = (0, exports.build)();
        return block((0, decorators_1.$h)(2, node, (0, decorators_1.$kd) `type`, node.getName() + `${args.map(node_signature_1.getSignature).join(', ').wrap('<', '>')}`, ':', (0, node_signature_1.getSignature)(tn)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), tArgs ? (0, decorators_1.$section)(tArgs) : '', typed ? (0, decorators_1.$section)(typed) : '');
    },
    [SyntaxKindDelegator_types_1.default.TupleType]: node => (0, exports.build)(...node.getElements()),
    [SyntaxKindDelegator_types_1.default.NamedTupleMember]: node => {
        const tn = node.getTypeNode();
        const typed = (0, exports.build)(tn);
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `tuple item`, node.getName(), ':', (0, node_signature_1.getSignature)(tn)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), typed ? (0, decorators_1.$section)(typed) : '');
    },
    [SyntaxKindDelegator_types_1.default.TypeLiteral]: node => {
        const properties = node.getProperties();
        const methods = node.getMethods();
        return block(properties.length ? (0, decorators_1.$h)(5, undefined, 'Properties:') : '', (0, exports.build)(...properties), methods.length ? (0, decorators_1.$h)(5, undefined, 'Methods:') : '', (0, exports.build)(...methods));
    },
    [SyntaxKindDelegator_types_1.default.PropertySignature]: node => {
        const tn = node.getTypeNode();
        const typed = (0, exports.build)(tn);
        return block((0, decorators_1.$h)(4, node, (0, node_signature_1.getModifiers)(node), (0, decorators_1.$kd) `${(0, node_tools_1.isMethodLike)(tn) ? 'method1' : 'property1'}`, node.getName(), node.hasQuestionToken() ? '?' : '', ':', (0, node_signature_1.getSignature)(tn)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), typed ? (0, decorators_1.$section)(typed) : '');
    },
    [SyntaxKindDelegator_types_1.default.PropertyDeclaration]: node => {
        const tn = node.getTypeNode();
        const typed = (0, exports.build)(tn);
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `${node.isStatic() ? 'static ' : ''}${(0, node_tools_1.isMethodLike)(tn) ? 'method' : 'property'}`, node.getName(), ':', (0, node_signature_1.getSignature)(tn)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), typed ? (0, decorators_1.$section)(typed) : '');
    },
    [SyntaxKindDelegator_types_1.default.MethodSignature]: node => {
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `method`, node.getName(), ':', (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), ...renderFNDetails(node.getTypeParameters(), node.getParameters(), node.getReturnTypeNode()));
    },
    //unlike the method signature
    [SyntaxKindDelegator_types_1.default.FunctionType]: node => {
        return block(...renderFNDetails(node.getTypeParameters(), node.getParameters(), node.getReturnTypeNode()));
    },
    [SyntaxKindDelegator_types_1.default.MethodDeclaration]: node => {
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `${node.isStatic() ? 'static ' : ''}method`, node.getName(), ':', (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), ...renderFNDetails(node.getTypeParameters(), node.getParameters(), node.getReturnTypeNode()));
    },
    [SyntaxKindDelegator_types_1.default.Parameter]: node => {
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `argument`, (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), ts_morph_1.Node.isObjectBindingPattern(node.getNameNode()) ? (0, exports.build)(node.getNameNode()) : '');
    },
    [SyntaxKindDelegator_types_1.default.TypeParameter]: node => {
        const constraint = (0, exports.build)(node.getConstraint());
        if (!constraint)
            return '';
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `param`, (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), constraint ? (0, decorators_1.$section)(constraint) : '');
    },
    [SyntaxKindDelegator_types_1.default.TypeReference]: node => '',
    [SyntaxKindDelegator_types_1.default.UnionType]: node => (0, exports.build)(...node.getTypeNodes()),
    [SyntaxKindDelegator_types_1.default.IntersectionType]: node => (0, exports.build)(...node.getTypeNodes()),
    [SyntaxKindDelegator_types_1.default.ArrayType]: node => (0, exports.build)(node.getElementTypeNode()),
    [SyntaxKindDelegator_types_1.default.ClassDeclaration]: node => {
        const constructors = (0, exports.build)(...node.getConstructors());
        const staticBlocks = (0, exports.build)(...node.getStaticBlocks());
        const staticProperties = (0, exports.build)(...node.getStaticProperties());
        const staticMethods = (0, exports.build)(...node.getStaticMethods());
        const instanceProperties = (0, exports.build)(...node.getInstanceProperties());
        const instanceMethods = (0, exports.build)(...node.getInstanceMethods());
        return block((0, decorators_1.$h)(2, node, (0, decorators_1.$kd) `class`, node.getName(), (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), (0, decorators_1.$section)(constructors, staticBlocks, staticProperties, staticMethods, instanceProperties, instanceMethods));
    },
    [SyntaxKindDelegator_types_1.default.ClassStaticBlockDeclaration]: node => {
        const comments = (0, node_tools_1.getComments)(node).trim();
        return comments ? block((0, decorators_1.$h)(4, undefined, (0, decorators_1.$kd) `static block:`), comments, (0, node_tools_1.getExample)(node)) : '';
    },
    [SyntaxKindDelegator_types_1.default.Constructor]: node => block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `constructor`, (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node)),
    [SyntaxKindDelegator_types_1.default.InterfaceDeclaration]: node => {
        const typeParams = (0, exports.build)(...node.getTypeParameters());
        const extensions = (0, exports.build)(...node.getExtends());
        const properties = [];
        const methods = [];
        for (const nds of node.getProperties()) {
            if ((0, node_tools_1.isMethodLike)(nds.getTypeNode()))
                methods.push(nds);
            else
                properties.push(nds);
        }
        methods.push(...node.getMethods());
        let data = '';
        if (properties.length)
            data += (0, decorators_1.$h)(5, undefined, 'Properties:') + `
${(0, exports.build)(...properties)}`;
        if (methods.length)
            data += (0, decorators_1.$h)(5, undefined, 'Methods:') + `
${(0, exports.build)(...methods)}`;
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `interface`, node.getName(), (0, node_signature_1.getSignature)(node)), ...(typeParams ? [
            '---',
            (0, decorators_1.$h)(5, undefined, 'Type Parameters:'),
            (0, decorators_1.$section)(typeParams)
        ] : []), ...(extensions ? [
            '---',
            (0, decorators_1.$h)(5, undefined, 'Extends:'),
            (0, decorators_1.$section)(typeParams)
        ] : []), (0, decorators_1.$section)(data));
    },
    [SyntaxKindDelegator_types_1.default.ExpressionWithTypeArguments]: node => {
        return (0, exports.build)(...node.getTypeArguments());
    },
    [SyntaxKindDelegator_types_1.default.GetAccessor]: node => {
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `${node.isStatic() ? 'static ' : ''}get`, (0, node_tools_1.getName)(node), ':', (0, node_signature_1.getSignature)(node)));
    },
    [SyntaxKindDelegator_types_1.default.SetAccessor]: node => {
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `${node.isStatic() ? 'static ' : ''}set`, (0, node_tools_1.getName)(node), ':', (0, node_signature_1.getSignature)(node)));
    },
    [SyntaxKindDelegator_types_1.default.ConditionalType]: node => (0, exports.build)(node.getExtendsType(), node.getTrueType(), node.getFalseType()),
    [SyntaxKindDelegator_types_1.default.VariableStatement]: node => {
        return (0, exports.build)(...node.getDeclarations());
    },
    [SyntaxKindDelegator_types_1.default.VariableDeclaration]: node => {
        const statement = node.getVariableStatement();
        if (!statement)
            return '';
        const dk = statement.getDeclarationKind();
        const tn = node.getTypeNode();
        const initializer = node.getInitializer();
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `${dk}`, (0, node_tools_1.getName)(node), ':', (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(statement), (0, node_tools_1.getExample)(statement), (0, exports.build)(tn !== null && tn !== void 0 ? tn : initializer));
    },
    [SyntaxKindDelegator_types_1.default.ObjectBindingPattern]: node => {
        return (0, exports.build)(...node.getElements());
    },
    [SyntaxKindDelegator_types_1.default.BindingElement]: node => {
        return (0, exports.build)(node.getPropertyNameNode());
    },
    [SyntaxKindDelegator_types_1.default.IndexedAccessType]: node => {
        return (0, exports.build)(node.getObjectTypeNode(), node.getIndexTypeNode());
    },
    [SyntaxKindDelegator_types_1.default.FunctionDeclaration]: node => {
        return block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `function`, node.getName(), ':', (0, node_signature_1.getSignature)(node)));
    },
    [SyntaxKindDelegator_types_1.default.ExpressionStatement]: node => '', //expressions are blocks of logic. Currently I dont plan on handling these instances. 
    [SyntaxKindDelegator_types_1.default.ClassExpression]: node => {
        const constructors = (0, exports.build)(...node.getConstructors());
        const staticBlocks = (0, exports.build)(...node.getStaticBlocks());
        const staticProperties = (0, exports.build)(...node.getStaticProperties());
        const staticMethods = (0, exports.build)(...node.getStaticMethods());
        const instanceProperties = (0, exports.build)(...node.getInstanceProperties());
        const instanceMethods = (0, exports.build)(...node.getInstanceMethods());
        return block((0, decorators_1.$h)(2, node, (0, decorators_1.$kd) `class`, node.getName(), (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), (0, decorators_1.$section)(constructors, staticBlocks, staticProperties, staticMethods, instanceProperties, instanceMethods));
    },
    [SyntaxKindDelegator_types_1.default.ArrayLiteralExpression]: node => (0, exports.build)(...node.getElements()),
    [SyntaxKindDelegator_types_1.default.ObjectLiteralExpression]: node => block((0, decorators_1.$section)((0, exports.build)(...node.getProperties()))),
    [SyntaxKindDelegator_types_1.default.PropertyAssignment]: node => block((0, decorators_1.$h)(4, node, (0, decorators_1.$kd) `property`, (0, node_signature_1.getSignature)(node)), (0, node_tools_1.getComments)(node), (0, node_tools_1.getExample)(node), (0, exports.build)()),
    [SyntaxKindDelegator_types_1.default.ArrowFunction]: node => (0, exports.build)(node.getReturnTypeNode()),
    [SyntaxKindDelegator_types_1.default.NewExpression]: () => ``
};
const IGNOREKINDS = new Set([
    SyntaxKindDelegator_types_1.default.ExportDeclaration,
    SyntaxKindDelegator_types_1.default.MultiLineCommentTrivia,
    SyntaxKindDelegator_types_1.default.SingleLineCommentTrivia
]);
const build = (...nodes) => nodes.map(node => {
    if (!TS_1.default.documentPrivate && (0, node_tools_1.isPrivate)(node))
        return '';
    return (0, SyntaxKindDelegator_1.bySyntax)(node, RENDER_MAP, n => {
        if (!n || (0, node_tools_1.isPrimitive)(n))
            return '';
        TS_1.default.err("No support", (0, console_log_colors_1.red)(n.getKindName()), (0, console_log_colors_1.cyan)(n.getKind()), n.getText(), (0, node_tools_1.getFullName)(n));
        return '';
    });
}).filter(b => b.trim()).join('\n');
exports.build = build;
const renderer = (node, df = n => {
    if (!n || (0, node_tools_1.isPrimitive)(n) || IGNOREKINDS.has(n.getKind()))
        return '';
    TS_1.default.err("No support", (0, console_log_colors_1.red)(n.getKindName()), (0, console_log_colors_1.yellow)(n.getKind()), (0, node_tools_1.getFullName)(n));
    return '';
}) => (0, SyntaxKindDelegator_1.bySyntax)(node, RENDER_MAP, df);
exports.renderer = renderer;
/**
 * A conveniece to render a source file.
 * @param node
 * @returns
 */
const render = (title, node) => {
    var _a;
    const data = (_a = node.getChildSyntaxList()) === null || _a === void 0 ? void 0 : _a.getChildren().map(c => (0, exports.renderer)(c)).filter(n => !!n.replace(/\s/g, '')).join('\n---\n');
    if (!data)
        return;
    return `${constants_1.STORY_BOOK_BLOCK}
<Meta title="${title}"/>
 
${data}`;
};
exports.render = render;