All files / json-type/src/typescript toText.ts

59.15% Statements 42/71
46.93% Branches 23/49
55.55% Functions 5/9
58.06% Lines 36/62

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 1281x   1x   1x 22x         1x 63x   59x   59x                 2x 2x 2x 2x 2x 2x 2x     1x 1x 1x 1x     19x 19x 19x 19x           5x 5x 5x     10x 3x   10x     2x     1x     13x     5x     4x                       2x     2x                                                                                  
import {wordWrap} from '@jsonjoy.com/util/lib/strings/wordWrap';
import type {TsIdentifier, TsNode, TsParameter} from './types';
import {TAB, isSimpleType, normalizeKey} from './util';
 
const formatComment = (comment: string | undefined, __: string): string => {
  if (!comment) return '';
  const lines = wordWrap(comment, {width: 80 - 3 - __.length});
  return __ + '/**\n' + __ + ' * ' + lines.join('\n' + __ + ' * ') + '\n' + __ + ' */\n';
};
 
export const toText = (node: TsNode | TsNode[] | TsIdentifier | TsParameter, __: string = ''): string => {
  if (Array.isArray(node)) return node.map((s) => toText(s, __)).join('\n');
 
  const ____ = __ + TAB;
 
  switch (node.node) {
    case 'ModuleDeclaration': {
      let out: string = '';
      out += `${__}${node.export ? 'export ' : ''}namespace ${node.name} {\n`;
      out += toText(node.statements, ____);
      out += `${__}}\n`;
      return out;
    }
    case 'InterfaceDeclaration': {
      const {name, members, comment} = node;
      let out: string = '';
      out += formatComment(comment, __);
      out += `${__}${node.export ? 'export ' : ''}interface ${name} {\n`;
      out += toText(members, ____);
      out += `\n${__}}\n`;
      return out;
    }
    case 'TypeAliasDeclaration': {
      let out: string = '';
      out += formatComment(node.comment, __);
      out += `${__}${node.export ? 'export ' : ''}type ${node.name} = ${toText(node.type, __)};\n`;
      return out;
    }
    case 'PropertySignature': {
      const name = normalizeKey(node.name);
      let out: string = '';
      out += formatComment(node.comment, __);
      return out + `${__}${name}${node.optional ? '?' : ''}: ${toText(node.type, __)};`;
    }
    case 'IndexSignature': {
      return `${__}[key: string]: ${toText(node.type, __)};`;
    }
    case 'ArrType': {
      const simple = isSimpleType(node.elementType);
      const inner = toText(node.elementType, __);
      return simple ? `${inner}[]` : `Array<${inner}>`;
    }
    case 'TupleType': {
      const hasObject = node.elements.some((e) => e.node === 'TypeLiteral');
      Iif (hasObject) {
        return `[\n${____}${node.elements.map((e) => toText(e, ____)).join(',\n' + ____)}\n${__}]`;
      } else return `[${node.elements.map((e) => toText(e, __)).join(', ')}]`;
    }
    case 'RestType': {
      return '...' + toText(node.type, __);
    }
    case 'GenericTypeAnnotation': {
      return node.id.name;
    }
    case 'StringKeyword': {
      return 'string';
    }
    case 'NumberKeyword': {
      return 'number';
    }
    case 'BooleanKeyword': {
      return 'boolean';
    }
    case 'NullKeyword': {
      return 'null';
    }
    case 'AnyKeyword': {
      return 'any';
    }
    case 'UnknownKeyword': {
      return 'unknown';
    }
    case 'TypeLiteral': {
      return !node.members.length ? '{}' : `{\n${toText(node.members, ____)}\n${__}}`;
    }
    case 'StringLiteral': {
      return JSON.stringify(node.text);
    }
    case 'NumericLiteral': {
      return node.text;
    }
    case 'TrueKeyword': {
      return 'true';
    }
    case 'FalseKeyword': {
      return 'false';
    }
    case 'UnionType': {
      return node.types.map((t) => toText(t, ____)).join(' | ');
    }
    case 'TypeReference': {
      return (
        (typeof node.typeName === 'string' ? node.typeName : toText(node.typeName, __)) +
        (node.typeArguments && node.typeArguments.length > 0
          ? `<${node.typeArguments.map((t) => toText(t, __)).join(', ')}>`
          : '')
      );
    }
    case 'Identifier': {
      return node.name;
    }
    case 'FnType': {
      const {parameters, type} = node;
      const params = parameters.map((p) => toText(p, __)).join(', ');
      return `(${params}) => ${toText(type, __)}`;
    }
    case 'ObjectKeyword': {
      return 'object';
    }
    case 'Parameter': {
      const {name, type} = node;
      return `${toText(name, __)}: ${toText(type, __)}`;
    }
  }
 
  return '';
};