TypeScript:获取语法树 [英] TypeScript: get syntax tree

查看:35
本文介绍了TypeScript:获取语法树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过整个互联网",但找不到任何有关从 TypeScrypt 源获取语法树(就像在 Esprima 中一样)的示例.我的意思是我怎样才能得到这样的对象(Esprima Parser 示例)

I had read "whole internet", but can't find any examples about getting syntax tree (just like in Esprima) from TypeScrypt source. I mean how can i get object like this (Esprima Parser example)

{
    "type": "Program",
    "body": [
        {
            "type": "VariableDeclaration",
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "id": {
                        "type": "Identifier",
                        "name": "answer"
                    },
                    "init": {
                        "type": "BinaryExpression",
                        "operator": "*",
                        "left": {
                            "type": "Literal",
                            "value": 6,
                            "raw": "6"
                        },
                        "right": {
                            "type": "Literal",
                            "value": 7,
                            "raw": "7"
                        }
                    }
                }
            ],
            "kind": "var"
        }
    ]
}

来自javascript代码

from javascript code

var answer = 6 * 7;

仅用于 TypeScript 源文本?

only for TypeScript source text?

附言非常希望得到你的帮助,因为我不想写你自己的可怕的自行车)

P.S. I hope very much for your help, because I do not want to write your own terrible bicycle)

P.P.S.我认为 lib 文件 typescript.ts(.js) 和 typescriptServices.ts(.js) 可以帮助我,但我不知道如何:(

P.P.S. I think the lib files typescript.ts(.js) and typescriptServices.ts(.js) to help me, but I do not know how :(

已解决

非常感谢用户史蒂夫芬顿.这是我的代码,如果有人感兴趣:

Thanks a lot to the user Steve Fenton. Here is my code, if anyone interested in:

// uses
var typeScriptLS =  new Harness.TypeScriptLS();
var ServicesFactory = new Services.TypeScriptServicesFactory();
var serviceShim = ServicesFactory.createLanguageServiceShim(typeScriptLS);

// add lib.d.ts
var _libText = window.document.getElementById('lib.d.ts').innerText;
typeScriptLS.addScript('lib.d.ts', _libText.replace(/
?/g,"
"), true);

// add greeter.ts
var _sourceText = window.document.getElementById('greeter.ts').innerText;
typeScriptLS.addScript('greeter.ts', _sourceText.replace(/
?/g,"
"), true);

// script name
var _scriptName = 'greeter.ts';
// get syntax tree
var _st = serviceShim.languageService.getSyntaxTree(_scriptName);
//console.log(_st);
console.log(JSON.stringify(_st, "", 2));

推荐答案

这个问题出现在 回到九月.

目前没有什么可以为你做这件事 - 没有神奇的 getSyntaxTree 方法可以调用来做到这一点.

There isn't currently something that will do this for you - there is no magic getSyntaxTree method to call that will do this.

不过,TypeScript 编译器是开源的 - 并且完全用 TypeScript 编写,因此您可以扫描它以找出是否可以使用/添加句柄.

The TypeScript compiler is open-source, though - and written entirely in TypeScript so you can scan it to find out if there is something you can use / add a handle to.

这样做的好处是,你有很大的机会将你的作品作为开源项目发布,从这两个问题的投票来看,有一些需求.

The up-side of this is that you have a big opportunity to release your work as an open-source project as judging by the up-votes on the two questions, there is some demand for this.

或者,使用 EsprimaSpiderMonkey.

这篇关于TypeScript:获取语法树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆