🖐🏻 免责声明
本教程仅供学习交流使用,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,请各读者自觉遵守相关法律法规。
# 在线解析网站
https://astexplorer.net/
# AST输出树结构

- type: 表示当前节点的类型,我们常用的类型判断方法,就是判断当前的节点是否为某个类型。
- start: 表示当前节点的起始位。
- end: 表示当前节点的末尾。
- loc : 表示当前节点所在的行列位置,里面也有start与end节点,这里的start与上面的start是不同 的,这里的start是表示节点所在起始的行列位置,而end表示的是节点所在末尾的行列位置。
- errors:是File节点所特有的属性,可以不用理会。
- program:包含整个源代码,不包含注释节点。
- sourceType: 通常用于标识源代码的类型,以告诉解析器或编译器它正在处理的代码是模块代码还是脚本代码(Script, Module)
- body:包含了程序的主体代码,即程序的主要逻辑。
- 语句块:"body" 可能表示一组语句,通常是一个代码块,这些语句按顺序执行。
- 函数体:对于函数或方法定义,"body" 包含了函数的主体代码,即函数内部的语句和逻辑。
- 类定义:对于类定义,"body" 可能包含类的成员,如属性和方法。
- 模块内容:对于模块或文件,"body" 可能包含文件中的顶级语句和声明。
declarations:通常用于表示变量、常量、函数、类等的声明id:是函数,变量,类的名称init: 通常代表声明的初始化值
- comments:源代码中所有的注释会在这里显示。
# 常见类型

Program
// 示例代码
let x = 1;
let y = 2;
ExpressionStatement
// 示例代码
console.log("Hello, world!");
BlockStatement
// 示例代码
{
let x = 1;
}
EmptyStatement
// 示例代码
;
DebuggerStatement
// 示例代码
debugger;
WithStatement
// 示例代码
with (obj) {
console.log(prop);
}
ReturnStatement
// 示例代码
function foo() {
return 42;
}
LabeledStatement
// 示例代码
label: for (let i = 0; i < 10; i++) {
break label;
}
BreakStatement
// 示例代码
for (let i = 0; i < 10; i++) {
break;
}
ContinueStatement
// 示例代码
for (let i = 0; i < 10; i++) {
continue;
}
IfStatement
// 示例代码
if (x > 0) {
console.log(x);
}
SwitchStatement
// 示例代码
switch (x) {
case 1:
console.log(1);
break;
default:
console.log("default");
}
ThrowStatement
// 示例代码
throw new Error("Error!");
TryStatement
// 示例代码
try {
let x = 1 / 0;
} catch (e) {
console.error(e);
}
WhileStatement
// 示例代码
while (true) {
break;
}
DoWhileStatement
// 示例代码
do {
console.log("loop");
} while (false);
ForStatement
// 示例代码
for (let i = 0; i < 10; i++) {
console.log(i);
}
ForInStatement
// 示例代码
for (let key in obj) {
console.log(key);
}
ForOfStatement
// 示例代码
for (let value of array) {
console.log(value);
}
FunctionDeclaration
// 示例代码
function foo() {}
VariableDeclaration
// 示例代码
let x = 1;
ClassDeclaration
// 示例代码
class Bar {}
ImportDeclaration
// 示例代码
import fs from 'fs';
ExportNamedDeclaration
// 示例代码
export const x = 1;
ExportDefaultDeclaration
// 示例代码
export default function() {}
ExportAllDeclaration
// 示例代码
export * from 'module';
MethodDefinition
// 示例代码
class Bar {
foo() {}
}
YieldExpression
// 示例代码
function* generator() {
yield 1;
}
AwaitExpression
// 示例代码
async function foo() {
await bar();
}
ArrayExpression
// 示例代码
let arr = [1, 2, 3];
ObjectExpression
// 示例代码
let obj = { key: "value" };
FunctionExpression
// 示例代码
let foo = function() {};
ArrowFunctionExpression
// 示例代码
let foo = () => {};
ThisExpression
// 示例代码
console.log(this);
Super
// 示例代码
class Bar extends Foo {
constructor() {
super();
}
}
UnaryExpression
// 示例代码
let y = -x;
UpdateExpression
// 示例代码
x++;
BinaryExpression
// 示例代码
let z = x + y;
AssignmentExpression
// 示例代码
x = 1;
LogicalExpression
// 示例代码
let result = a && b;
MemberExpression
// 示例代码
let value = obj.prop;
ConditionalExpression
// 示例代码
let result = condition ? trueValue : falseValue;
CallExpression
// 示例代码
foo();
NewExpression
// 示例代码
let obj = new Foo();
SequenceExpression
// 示例代码
let x = (1, 2, 3);
TemplateLiteral
// 示例代码
let str = `Hello, ${name}!`;
TaggedTemplateExpression
// 示例代码
tag`Hello, ${name}!`;
ObjectPattern
// 示例代码
let {a, b} = obj;
ArrayPattern
// 示例代码
let [a, b] = arr;
RestElement
// 示例代码
let [a, ...rest] = arr;
AssignmentPattern
// 示例代码
function foo(x = 1) {}
ClassBody
// 示例代码
class Bar {
constructor() {}
}
ClassExpression
// 示例代码
let Bar = class {};
MetaProperty
// 示例代码
new.target;
ImportSpecifier
// 示例代码
import { readFile } from 'fs';
ImportDefaultSpecifier
// 示例代码
import fs from 'fs';
ImportNamespaceSpecifier
// 示例代码
import * as fs from 'fs';
ExportSpecifier
// 示例代码
export { foo };
ExportDefaultSpecifier
// 示例代码
export default foo;
ExportNamespaceSpecifier
// 示例代码
export * from 'module';

