支持20种开发语言50种语法格式的在线代码解析器 AST 插件示例集合【含源码】

非常棒的在线AST浏览器
https://astexplorer.net/
源码:https://github.com/fkling/astexplorer

一、支持开发语言

二、示例

1、vue

  • @vue/compiler-dom
  • vue-eslint-parser
  • vue-template-compiler
    源码:
<template>
  <p>{{ greeting }} World!</p>
</template>

<script>
export default {
  data () {
    return {
      greeting: "Hello"
    };
  }
};
</script>

<style scoped>
p {
  font-size: 2em;
  text-align: center;
}
</style>

解析的语法树json太长就不做展示了

image.png

2、JavaScript

  • babel-plugin-macros
  • babelv7
  • eslint-v4
  • jscodeshift
  • prettier
  • tslint

支持的语法格式

  • acorn
  • @babel/eslint-parser
  • babel-eslint9
  • @babel/parser
  • esformatter
  • espree
  • esprima
  • flow
  • meriyah
  • recast
  • seafox
  • shift
  • tenko
  • traceur
  • @typescript-eslint/parser
  • typescript
  • uglify-js
  • 例如:typescript源码:
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;
  • 解析后的语法树
{
  "type": "Program",
  "start": 0,
  "end": 528,
  "body": [
    {
      "type": "ImportDeclaration",
      "start": 0,
      "end": 30,
      "specifiers": [
        {
          "type": "ImportDefaultSpecifier",
          "start": 7,
          "end": 11,
          "local": {
            "type": "Identifier",
            "start": 7,
            "end": 11,
            "name": "logo"
          }
        }
      ],
      "source": {
        "type": "Literal",
        "start": 17,
        "end": 29,
        "value": "./logo.svg",
        "raw": "'./logo.svg'"
      }
    },
    {
      "type": "ImportDeclaration",
      "start": 31,
      "end": 50,
      "specifiers": [],
      "source": {
        "type": "Literal",
        "start": 38,
        "end": 49,
        "value": "./App.css",
        "raw": "'./App.css'"
      }
    },
    {
      "type": "FunctionDeclaration",
      "start": 52,
      "end": 506,
      "id": {
        "type": "Identifier",
        "start": 61,
        "end": 64,
        "name": "App"
      },
      "expression": false,
      "generator": false,
      "async": false,
      "params": [],
      "body": {
        "type": "BlockStatement",
        "start": 67,
        "end": 506,
        "body": [
          {
            "type": "ReturnStatement",
            "start": 71,
            "end": 504,
            "argument": {
              "type": "JSXElement",
              "start": 84,
              "end": 499,
              "openingElement": {
                "type": "JSXOpeningElement",
                "start": 84,
                "end": 105,
                "attributes": [
                  {
                    "type": "JSXAttribute",
                    "start": 89,
                    "end": 104,
                    "name": {
                      "type": "JSXIdentifier",
                      "start": 89,
                      "end": 98,
                      "name": "className"
                    },
                    "value": {
                      "type": "Literal",
                      "start": 99,
                      "end": 104,
                      "value": "App",
                      "raw": "\"App\""
                    }
                  }
                ],
                "name": {
                  "type": "JSXIdentifier",
                  "start": 85,
                  "end": 88,
                  "name": "div"
                },
                "selfClosing": false
              },
              "closingElement": {
                "type": "JSXClosingElement",
                "start": 493,
                "end": 499,
                "name": {
                  "type": "JSXIdentifier",
                  "start": 495,
                  "end": 498,
                  "name": "div"
                }
              },
              "children": [
                {
                  "type": "JSXText",
                  "start": 105,
                  "end": 112,
                  "value": "\n      ",
                  "raw": "\n      "
                },
                {
                  "type": "JSXElement",
                  "start": 112,
                  "end": 488,
                  "openingElement": {
                    "type": "JSXOpeningElement",
                    "start": 112,
                    "end": 143,
                    "attributes": [
                      {
                        "type": "JSXAttribute",
                        "start": 120,
                        "end": 142,
                        "name": {
                          "type": "JSXIdentifier",
                          "start": 120,
                          "end": 129,
                          "name": "className"
                        },
                        "value": {
                          "type": "Literal",
                          "start": 130,
                          "end": 142,
                          "value": "App-header",
                          "raw": "\"App-header\""
                        }
                      }
                    ],
                    "name": {
                      "type": "JSXIdentifier",
                      "start": 113,
                      "end": 119,
                      "name": "header"
                    },
                    "selfClosing": false
                  },
                  "closingElement": {
                    "type": "JSXClosingElement",
                    "start": 479,
                    "end": 488,
                    "name": {
                      "type": "JSXIdentifier",
                      "start": 481,
                      "end": 487,
                      "name": "header"
                    }
                  },
                  "children": [
                    {
                      "type": "JSXText",
                      "start": 143,
                      "end": 152,
                      "value": "\n        ",
                      "raw": "\n        "
                    },
                    {
                      "type": "JSXElement",
                      "start": 152,
                      "end": 202,
                      "openingElement": {
                        "type": "JSXOpeningElement",
                        "start": 152,
                        "end": 202,
                        "attributes": [
                          {
                            "type": "JSXAttribute",
                            "start": 157,
                            "end": 167,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 157,
                              "end": 160,
                              "name": "src"
                            },
                            "value": {
                              "type": "JSXExpressionContainer",
                              "start": 161,
                              "end": 167,
                              "expression": {
                                "type": "Identifier",
                                "start": 162,
                                "end": 166,
                                "name": "logo"
                              }
                            }
                          },
                          {
                            "type": "JSXAttribute",
                            "start": 168,
                            "end": 188,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 168,
                              "end": 177,
                              "name": "className"
                            },
                            "value": {
                              "type": "Literal",
                              "start": 178,
                              "end": 188,
                              "value": "App-logo",
                              "raw": "\"App-logo\""
                            }
                          },
                          {
                            "type": "JSXAttribute",
                            "start": 189,
                            "end": 199,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 189,
                              "end": 192,
                              "name": "alt"
                            },
                            "value": {
                              "type": "Literal",
                              "start": 193,
                              "end": 199,
                              "value": "logo",
                              "raw": "\"logo\""
                            }
                          }
                        ],
                        "name": {
                          "type": "JSXIdentifier",
                          "start": 153,
                          "end": 156,
                          "name": "img"
                        },
                        "selfClosing": true
                      },
                      "closingElement": null,
                      "children": []
                    },
                    {
                      "type": "JSXText",
                      "start": 202,
                      "end": 211,
                      "value": "\n        ",
                      "raw": "\n        "
                    },
                    {
                      "type": "JSXElement",
                      "start": 211,
                      "end": 286,
                      "openingElement": {
                        "type": "JSXOpeningElement",
                        "start": 211,
                        "end": 214,
                        "attributes": [],
                        "name": {
                          "type": "JSXIdentifier",
                          "start": 212,
                          "end": 213,
                          "name": "p"
                        },
                        "selfClosing": false
                      },
                      "closingElement": {
                        "type": "JSXClosingElement",
                        "start": 282,
                        "end": 286,
                        "name": {
                          "type": "JSXIdentifier",
                          "start": 284,
                          "end": 285,
                          "name": "p"
                        }
                      },
                      "children": [
                        {
                          "type": "JSXText",
                          "start": 214,
                          "end": 230,
                          "value": "\n          Edit ",
                          "raw": "\n          Edit "
                        },
                        {
                          "type": "JSXElement",
                          "start": 230,
                          "end": 253,
                          "openingElement": {
                            "type": "JSXOpeningElement",
                            "start": 230,
                            "end": 236,
                            "attributes": [],
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 231,
                              "end": 235,
                              "name": "code"
                            },
                            "selfClosing": false
                          },
                          "closingElement": {
                            "type": "JSXClosingElement",
                            "start": 246,
                            "end": 253,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 248,
                              "end": 252,
                              "name": "code"
                            }
                          },
                          "children": [
                            {
                              "type": "JSXText",
                              "start": 236,
                              "end": 246,
                              "value": "src/App.js",
                              "raw": "src/App.js"
                            }
                          ]
                        },
                        {
                          "type": "JSXText",
                          "start": 253,
                          "end": 282,
                          "value": " and save to reload.\n        ",
                          "raw": " and save to reload.\n        "
                        }
                      ]
                    },
                    {
                      "type": "JSXText",
                      "start": 286,
                      "end": 295,
                      "value": "\n        ",
                      "raw": "\n        "
                    },
                    {
                      "type": "JSXElement",
                      "start": 295,
                      "end": 472,
                      "openingElement": {
                        "type": "JSXOpeningElement",
                        "start": 295,
                        "end": 437,
                        "attributes": [
                          {
                            "type": "JSXAttribute",
                            "start": 308,
                            "end": 328,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 308,
                              "end": 317,
                              "name": "className"
                            },
                            "value": {
                              "type": "Literal",
                              "start": 318,
                              "end": 328,
                              "value": "App-link",
                              "raw": "\"App-link\""
                            }
                          },
                          {
                            "type": "JSXAttribute",
                            "start": 339,
                            "end": 365,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 339,
                              "end": 343,
                              "name": "href"
                            },
                            "value": {
                              "type": "Literal",
                              "start": 344,
                              "end": 365,
                              "value": "https://reactjs.org",
                              "raw": "\"https://reactjs.org\""
                            }
                          },
                          {
                            "type": "JSXAttribute",
                            "start": 376,
                            "end": 391,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 376,
                              "end": 382,
                              "name": "target"
                            },
                            "value": {
                              "type": "Literal",
                              "start": 383,
                              "end": 391,
                              "value": "_blank",
                              "raw": "\"_blank\""
                            }
                          },
                          {
                            "type": "JSXAttribute",
                            "start": 402,
                            "end": 427,
                            "name": {
                              "type": "JSXIdentifier",
                              "start": 402,
                              "end": 405,
                              "name": "rel"
                            },
                            "value": {
                              "type": "Literal",
                              "start": 406,
                              "end": 427,
                              "value": "noopener noreferrer",
                              "raw": "\"noopener noreferrer\""
                            }
                          }
                        ],
                        "name": {
                          "type": "JSXIdentifier",
                          "start": 296,
                          "end": 297,
                          "name": "a"
                        },
                        "selfClosing": false
                      },
                      "closingElement": {
                        "type": "JSXClosingElement",
                        "start": 468,
                        "end": 472,
                        "name": {
                          "type": "JSXIdentifier",
                          "start": 470,
                          "end": 471,
                          "name": "a"
                        }
                      },
                      "children": [
                        {
                          "type": "JSXText",
                          "start": 437,
                          "end": 468,
                          "value": "\n          Learn React\n        ",
                          "raw": "\n          Learn React\n        "
                        }
                      ]
                    },
                    {
                      "type": "JSXText",
                      "start": 472,
                      "end": 479,
                      "value": "\n      ",
                      "raw": "\n      "
                    }
                  ]
                },
                {
                  "type": "JSXText",
                  "start": 488,
                  "end": 493,
                  "value": "\n    ",
                  "raw": "\n    "
                }
              ]
            }
          }
        ]
      }
    },
    {
      "type": "ExportDefaultDeclaration",
      "start": 508,
      "end": 527,
      "declaration": {
        "type": "Identifier",
        "start": 523,
        "end": 526,
        "name": "App"
      }
    }
  ],
  "sourceType": "module"
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,009评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,808评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,891评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,283评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,285评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,409评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,809评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,487评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,680评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,499评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,548评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,268评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,815评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,872评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,102评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,683评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,253评论 2 341

推荐阅读更多精彩内容