Schematics 语法节点查询1

6.tsc 语法节点查询 1

Typescript

  • typescript 自带 api
let nodeOrString = ts.createSourceFile('', nodeOrString, ts.ScriptTarget.Latest);

function tsNodeVisit(node: ts.Node, fn: (node: ts.Node) => unknown) {
  ts.forEachChild(node, (node) => {
    fn(node);
    tsNodeVisit(node, fn);
  });
}

json

  • packages/angular_devkit/core/src/json/parser.ts:920
  • ng 推荐使用jsonc-parser
let parseTreeResult = parseTree(jsonString, parseError);

html

  • @angular/compiler
let parser = new HtmlParser();
let parseTreeResult = parser.parse(htmlString, '');

使用封装好的库

cyia-code-util

  • https://github.com/wszgrcy/cyia-code-util
  • 同时支持html,ts,json
  • 查询,修改,更新

视频