TypeScript: get syntax tree

后端 未结 4 1459
悲&欢浪女
悲&欢浪女 2020-12-12 20:39

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 (E

4条回答
  •  孤街浪徒
    2020-12-12 21:25

    I found recast to be working very good. Example:

    var recast = require('recast');
    var ast = recast.parse(`var answer = 6 * 7;`);
    console.log(ast);
    

    This will output all needed information and event TypeAnnotation, so this lib is really amazing :)

    [
       {
          "type": "VariableDeclaration",
          "declarations": [
             {
                "type": "VariableDeclarator",
                "id": {
                   "type": "Identifier",
                   "name": "answer",
                   "typeAnnotation": {
                      "type": "TypeAnnotation",
                      "typeAnnotation": {
                         "type": "NumberTypeAnnotation",
                         "loc": {
                            "start": {
                               "line": 1,
                               "column": 12
                            },
                            "end": {
                               "line": 1,
                               "column": 18
                            },
                            "lines": {},
                            "indent": 0
                         }
                      },
                      "loc": {
                         "start": {
                            "line": 1,
                            "column": 10
                         },
                         "end": {
                            "line": 1,
                            "column": 18
                         },
                         "lines": {},
                         "indent": 0
                      }
                   },
                   "loc": {
                      "start": {
                         "line": 1,
                         "column": 4
                      },
                      "end": {
                         "line": 1,
                         "column": 18
                      },
                      "lines": {},
                      "indent": 0
                   }
                },
                "init": {
                   "type": "BinaryExpression",
                   "operator": "*",
                   "left": {
                      "type": "Literal",
                      "value": 6,
                      "raw": "6",
                      "loc": {
                         "start": {
                            "line": 1,
                            "column": 21
                         },
                         "end": {
                            "line": 1,
                            "column": 22
                         },
                         "lines": {},
                         "indent": 0
                      }
                   },
                   "right": {
                      "type": "Literal",
                      "value": 7,
                      "raw": "7",
                      "loc": {
                         "start": {
                            "line": 1,
                            "column": 25
                         },
                         "end": {
                            "line": 1,
                            "column": 26
                         },
                         "lines": {},
                         "indent": 0
                      }
                   },
                   "loc": {
                      "start": {
                         "line": 1,
                         "column": 21
                      },
                      "end": {
                         "line": 1,
                         "column": 26
                      },
                      "lines": {},
                      "indent": 0
                   }
                },
                "loc": {
                   "start": {
                      "line": 1,
                      "column": 4
                   },
                   "end": {
                      "line": 1,
                      "column": 26
                   },
                   "lines": {},
                   "indent": 0
                }
             }
          ],
          "kind": "var",
          "loc": {
             "start": {
                "line": 1,
                "column": 0
             },
             "end": {
                "line": 1,
                "column": 27
             },
             "lines": {},
             "indent": 0
          }
       }
    ]
    

提交回复
热议问题