JS常见AST与示例代码对应

🖐🏻 免责声明

本教程仅供学习交流使用,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,请各读者自觉遵守相关法律法规。

# 在线解析网站

https://astexplorer.net/

# AST输出树结构

1717111447419

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

# 常见类型

1717111383147

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';

# ☕ 请我喝咖啡

如果本文章对您有所帮助,不妨请作者我喝杯咖啡 :)

pay


# ☀️ 广告时间

现承接以下业务,欢迎大家支持:)

  • Web 2.0 & Web 3.0应用定制
  • Web 3.0专项脚本定制与优化
  • 数据爬虫需求快速响应
  • 网站/公众号/小程序一站式开发
  • 毕业设计与科研项目支持
  • 企业管理软件定制:ERP, MES, CRM, 进销存系统等

联系方式:

X:@motuoka

V:ck742931485

wx