如何将打字稿定义解析为json?

时间:2016-09-20 07:45:25

标签: json typescript

我想将我的打字稿定义解析为json,例如

namespace call.common {
    namespace login {
        interface Request {
          userName: string;
          password: string;
        }

        interface Response {
            isLogin: boolean
        }
    }
}

json可能是这样的:

{
    namespace: "call.common.login",
    interfaces: [
        {
            "name": "Request",
            params: [{
                name: "userName",
                type: "string",
            }, {
                name: "password",
                type: "string"
            }]
        }, {
            "name": "Response",
            params: [{
                name: "isLogin",
                type: boolean
            }]
        }
    ]
}

根据文档Using-the-Compiler-API我正在尝试使用编译器:

function parseGrpcTypings(fileName, options) {
    var program = ts.createProgram(fileName, options);
    var checker = program.getTypeChecker();

    const sourceFile = program.getSourceFiles()[0];
    ts.forEachChild(sourceFile, visit);

    function visit(node) {
        if (!isNodeExported(node)) {
            return;
        }

        if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
            ts.forEachChild(node, visit);
        } else if (node.kind === ts.SyntaxKind.InterfaceDeclaration) {
            // how to parse interface
        } else {
            // how to deal with namespace
        }
    }

    function isNodeExported(node) {
        return (node.flags & ts.NodeFlags.Export) !== 0 || (node.parent && node.parent.kind === ts.SyntaxKind.SourceFile);
    }
}

parseGrpcTypings("/tmp/test.d.ts", {
    target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
});

2 个答案:

答案 0 :(得分:2)

根据您的代码,这是一个ts2json.js模块,可将.d.ts文件转换为纯JavaScript对象:

const ts = require('typescript');

const PROPERTY_TYPES = {
    any: ts.SyntaxKind.AnyKeyword,
    boolean: ts.SyntaxKind.BooleanKeyword,
    number: ts.SyntaxKind.NumberKeyword,
    string: ts.SyntaxKind.StringKeyword,
};
class TSNode {
    constructor(name, type) {
        this.children = [];
        this.addChildren = (name, type) => {
            let node = new TSNode(name, type);
            this.children.push(node);
            return node;
        };
        this.getType = () => this.type;
        this.getObject = () => {
            let map = {};
            map[this.name] = this.children.length
                ? this.children
                      .map(child => child.getObject())
                      .reduce((pv, child) => {
                          for (let key in child) {
                              if (pv.hasOwnProperty(key) || key in pv) {
                                  Object.assign(pv[key], child[key]);
                              } else {
                                  pv[key] = child[key];
                              }
                          }
                          return pv;
                      }, {})
                : this.type;
            return map;
        };
        this.name = name;
        this.type = type;
    }
}
let visit = parent => node => {
    switch (node.kind) {
        case ts.SyntaxKind.ModuleDeclaration:
            let moduleName = node.name.text;
            ts.forEachChild(node, visit(parent.addChildren(moduleName)));
            break;
        case ts.SyntaxKind.ModuleBlock:
            ts.forEachChild(node, visit(parent));
            break;
        case ts.SyntaxKind.InterfaceDeclaration:
            let interfaceName = node.name.text;
            parent[interfaceName] = {};
            // console.log('interface');
            ts.forEachChild(node, visit(parent.addChildren(interfaceName)));
            break;
        case ts.SyntaxKind.PropertySignature:
            let propertyName = node.name;
            let propertyType = node.type;
            let arrayDeep = 0;
            let realPropertyName =
                'string' !== typeof propertyName && 'text' in propertyName
                    ? propertyName.text
                    : propertyName;
            while (propertyType.kind === ts.SyntaxKind.ArrayType) {
                arrayDeep++;
                propertyType = propertyType.elementType;
            }
            if (propertyType.kind === ts.SyntaxKind.TypeReference) {
                let realPropertyType = propertyType.typeName;
                parent.addChildren(
                    realPropertyName,
                    'Array<'.repeat(arrayDeep) +
                        (realPropertyType.kind === ts.SyntaxKind.QualifiedName
                            ? realPropertyType.getText()
                            : 'text' in realPropertyType
                              ? realPropertyType.text
                              : realPropertyType) +
                        '>'.repeat(arrayDeep)
                );
            } else {
                for (let type in PROPERTY_TYPES) {
                    if (propertyType.kind === PROPERTY_TYPES[type]) {
                        parent.addChildren(realPropertyName, type);
                        break;
                    }
                }
            }
            break;
        default:
    }
};

module.exports = function(filename, options) {
    const ROOT_NAME = 'root';
    const node = new TSNode(ROOT_NAME);

    let program = ts.createProgram([filename], options);
    let checker = program.getTypeChecker();
    let sourceFile = program.getSourceFiles()[1];

    ts.forEachChild(sourceFile, visit(node));

    return node.getObject()[ROOT_NAME];
};

您可以使用JSON.stringify方法获取JSON字符串。 以下TypeScript定义文件eg.d.ts

declare namespace School.Users {
    interface User {
        lastName: string;
        firstName: string;
        email: string;
    }

    interface Student extends User {
        graduation: number;
        mainTeacher: Teacher;
    }

    interface Teacher extends User {
        classes: Student[][];
        room: School.Building.Room;
    }
}

declare namespace School.Building {
    interface Room {
        name: string;
    }
}

可以使用以下代码解析为JSON:

node -e "console.log(JSON.stringify(require('./ts2json.js')('./eg.d.ts', {}), null, '\t'))"

输出:

{
    "School": {
        "Users": {
            "User": {
                "lastName": "string",
                "firstName": "string",
                "email": "string"
            },
            "Student": {
                "graduation": "number",
                "mainTeacher": "Teacher"
            },
            "Teacher": {
                "classes": "Array<Array<Student>>",
                "room": "School.Building.Room"
            }
        },
        "Building": {
            "Room": {
                "name": "string"
            }
        }
    }
}

答案 1 :(得分:0)

MrMaxie/ts-types-parser 可以解析 *.d.ts 文件中的单个类型

const TSTypesParser = require('ts-types-parser');
const parser = new TSTypesParser();

// input file
parser.setSource('node_modules/typescript/lib/lib.dom.d.ts');
parser.setTarget('lib.dom.d.ts.js'); // required, will create empty file

// entry point
const mainType = 'CanvasRenderingContext2D';
parser.mainType(mainType);

const result = [];

parser.on('level-up', levelData => {
  const fullPath = `${mainType}.${levelData.path.join('.')}`;
  const r = `${fullPath} /*: ${levelData.type} */`;
  result.push(r);
});

parser.on('done', () => {
  // restore output
  console.log = console._log;
  console.warn = console._warn;

  console.log(result.join('\n'));
})

// disable output from parser.run
console._log = console.log;
console._warn = console.warn;
console.log = () => {};
console.warn = () => {}; 

parser.run();