Specification of UAST
July 6, 2026 · View on GitHub
版本
0.1.64
介绍
UAST(Unified Abstract Syntax Tree) 是一种统一的抽象语法树(AST)表达,旨在为程序语言提供高层次(higher level)的通用中间表达形式。 UAST的直接或者间接产物,将以安全、查错、优化等程序分析场景为目的,并以此目标作为设计时的考量。
UAST的设计理念是:
- 简单 尽量用最少的语法节点信息
- 通用 尽可能高的通用性,即同一语法节点能够适配不同程序语言,也即是,更少的特有语言定制语法
- 详尽 包含尽可能多的节点元信息,用于作程序分析辅助或者功能扩展,比如location信息,modifier等
注意:
UAST不提供后续编译可执行承诺,即是说,和程序分析无关的信息可能会被去掉。 UAST会对不同语言里面的语法语义信息在程序分析层面作有损统一抽象,目的是用尽可能简单的语法信息做统一表达。
语法节点
基础节点属性
以下属性为每一个AST节点都会有的属性,表示一些节点本身的信息
- type 表示节点的类型,具体类型信息可以看下文对于type的枚举。
- loc 表示位置,通过start、end、sourcefile表示节点表示源码的位置,start、end包含line与column两个信息。line、column均从1开始。
loc:{
start: {
line: number,
column: number
},
end: {
line: number,
column: number
},
sourcefile:string
}
- meta 附加属性,通常为不同语言的parser实现时存放的扩展内容,以及一些节点的附加信息。如函数/类的注解(装饰器)会放在里面。
Noop
Aliases: Standardized, Instruction, Expression, Statement
Literal
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
value | null | number | string | boolean | required | |
literalType | "null" | "number" | "string" | "boolean" | required |
Aliases: Standardized, Instruction, Expression
Identifier
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
name | string | required |
Aliases: Standardized, Instruction, Expression, LVal, Type
CompileUnit
一个最小的编译单元。例如一个.js或.py或.class文件,都是一个最小编译单元
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
body | Array | required | |
language | "javascript" | "typescript" | "java" | "golang" | "python" | "php" | required | 源语言种类 |
languageVersion | number | string | boolean | default: null | 源语言版本 |
uri | string | required | 该AST的唯一标识 |
version | string | required | UAST版本 |
Aliases: Standardized
ExportStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | required | |
alias | Identifier | required |
Aliases: Standardized, Instruction, Statement
IfStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
test | Expression | required | |
consequent | Instruction | required | |
alternative | Instruction | default: null |
Aliases: Standardized, Instruction, Statement, Conditional
SwitchStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
discriminant | Expression | required | |
cases | Array | required |
Aliases: Standardized, Instruction, Statement, Expression, Conditional
CaseClause
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
test | Expression | default: null | |
body | Instruction | required |
Aliases: Standardized
ForStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
init | Expression | VariableDeclaration | default: null | |
test | Expression | default: null | |
update | Expression | default: null | |
body | Instruction | required |
Aliases: Standardized, Instruction, Statement, Loop
WhileStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
test | Expression | required | |
body | Instruction | required | |
isPostTest | boolean | default: null |
Aliases: Standardized, Instruction, Statement, Loop, Scopable
RangeStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
key | VariableDeclaration | Expression | default: null | |
value | VariableDeclaration | Expression | default: null | |
right | Expression | required | |
body | Instruction | required |
Aliases: Standardized, Instruction, Statement, Loop, Scopable
LabeledStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
label | Identifier | default: null | |
body | Instruction | required |
Aliases: Standardized, Instruction, Statement
ReturnStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | default: null | |
isYield | boolean | default: false |
Aliases: Standardized, Instruction, Statement, Expression
BreakStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
label | Identifier | default: null |
Aliases: Standardized, Instruction, Statement
ContinueStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
label | Identifier | default: null |
Aliases: Standardized, Instruction, Statement
ThrowStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | default: null |
Aliases: Standardized, Instruction, Statement
TryStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
body | Statement | required | |
handlers | Array<null | CatchClause> | default: null | |
finalizer | Instruction | default: null |
Aliases: Standardized, Instruction, Statement
CatchClause
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
parameter | Array<null | VariableDeclaration | Sequence> | required | |
body | Instruction | required |
Aliases: Standardized, Instruction, Scopable
ExpressionStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
expression | Expression | required |
Aliases: Standardized, Instruction, Statement
ScopedStatement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
body | Array | required | |
id | Identifier | default: null |
Aliases: Standardized, Instruction, Statement, Scopable
BinaryExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
operator | "+" | "-" | "*" | "/" | "**" | "%" | "<<" | ">>" | ">>>" | "<<<" | "&&" | "||" | ",," | "&" | "," | "^" | "<" | ">" | "<=" | ">=" | "==" | "!=" | "|" | "instanceof" | "in" | "push" | "===" | "!==" | "??" | required | |
left | Expression | required | |
right | Expression | required |
Aliases: Standardized, Instruction, Expression
UnaryExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
operator | "-" | "+" | "++" | "--" | "~" | "delete" | "!" | "typeof" | "void" | required | |
argument | Expression | required | |
isSuffix | boolean | default: true |
Aliases: Standardized, Instruction, Expression
AssignmentExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
left | LVal | required | |
right | Expression | required | |
operator | "=" | "^=" | "&=" | "<<=" | ">>=" | ">>>=" | "+=" | "-=" | "*=" | "/=" | "%=" | "|=" | "**=" | "||=" | "&&=" | "??=" | required | |
cloned | boolean | default: null |
Aliases: Standardized, Instruction, Expression
Sequence
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
expressions | Array | required |
Aliases: Standardized, Instruction, Expression, Statement
CastExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
expression | Expression | required | |
as | Type | required |
Aliases: Standardized, Instruction, Expression
ConditionalExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
test | Expression | required | |
consequent | Expression | required | |
alternative | Expression | required |
Aliases: Standardized, Instruction, Expression, Conditional
SuperExpression
Aliases: Standardized, Instruction, Expression
ThisExpression
Aliases: Standardized, Instruction, Expression
MemberAccess
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
object | Expression | required | |
property | Expression | required | |
computed | boolean | default: false |
Aliases: Standardized, Instruction, Expression, LVal
SliceExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
start | Instruction | default: null | |
end | Instruction | default: null | |
step | Instruction | default: null |
Aliases: Standardized, Instruction, Expression
TupleExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
elements | Array<Expression | Instruction> | required | |
modifiable | boolean | default: null |
Aliases: Standardized, Instruction, Expression
ObjectExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
properties | Array<ObjectProperty | SpreadElement> | required | |
id | Identifier | default: null |
Aliases: Standardized, Instruction, Expression
ObjectProperty
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
key | Expression | required | |
value | null | Expression | required |
Aliases: Standardized, Instruction, Expression
CallExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
callee | Expression | required | |
arguments | Array<null | Expression> | required |
Aliases: Standardized, Instruction, Expression
NewExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
callee | Expression | required | |
arguments | Array | required |
Aliases: Standardized, Instruction, Expression
FunctionDefinition
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Expression | default: null | |
parameters | Array<null | VariableDeclaration> | required | |
returnType | Type | required | |
body | Instruction | required | |
modifiers | Array<null | string> | required |
Aliases: Standardized, Instruction, Expression, Declaration, Scopable, Statement
ClassDefinition
History
| 版本 | 更新 |
|---|---|
v7.6.0 | Supports static |
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | default: null | |
body | Array<null | Instruction> | required | |
supers | Array<null | Expression> | required |
Aliases: Standardized, Instruction, Expression, Declaration, Scopable, Statement
VariableDeclaration
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Expression | required | |
init | Expression | default: null | |
cloned | boolean | default: null | |
varType | Type | required | |
variableParam | boolean | default: null |
Aliases: Standardized, Instruction, Expression, Declaration, Statement
DereferenceExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | required |
Aliases: Standardized, Instruction, Expression
ReferenceExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | required |
Aliases: Standardized, Instruction, Expression
ImportExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
from | Literal | required | |
local | Identifier | default: null | |
imported | Identifier | Literal | default: null |
Aliases: Standardized, Instruction, Expression
SpreadElement
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | required |
Aliases: Standardized, Instruction, Expression
YieldExpression
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
argument | Expression | default: null |
Aliases: Standardized, Instruction, Statement, Expression
PackageDeclaration
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
name | Expression | required |
Aliases: Standardized, Instruction, Expression, Declaration, Statement
PrimitiveType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
typeArguments | Array | default: null | |
kind | "string" | "number" | "boolean" | "null" | required |
Aliases: Standardized, Type
ArrayType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
element | Type | required | |
typeArguments | Array | default: null | |
size | Expression | default: null |
Aliases: Standardized, Type
PointerType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
element | Type | required | |
typeArguments | Array | default: null | |
kind | "pointer" | "reference" | required |
Aliases: Standardized, Type
MapType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
keyType | Type | required | |
valueType | Type | required | |
typeArguments | Array | default: null |
Aliases: Standardized, Type
ScopedType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
scope | null | Type | required | |
typeArguments | Array | default: null |
Aliases: Standardized, Type
TupleType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
elements | Array | required | |
typeArguments | Array | default: null |
Aliases: Standardized, Type
ChanType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
dir | string | required | |
valueType | Type | required |
Aliases: Standardized, Type
FuncType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | required | |
typeParams | Array | required | |
params | Array | required | |
results | Array | required |
Aliases: Standardized, Type
DynamicType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | default: null | |
typeArguments | Array | default: null |
Aliases: Standardized, Type
VoidType
属性描述
| 属性名 | 类型 | 约束 | 说明 |
|---|---|---|---|
id | Identifier | default: null | |
typeArguments | Array | default: null |
Aliases: Standardized, Type
Aliases
Conditional
A cover of ConditionalExpression and IfStatement, which share the same AST shape.
t.isConditional(node);
Covered nodes:
Declaration
A cover of any Declarations.
t.isDeclaration(node);
Covered nodes:
Expression
A cover of any Expressions.
t.isExpression(node);
Covered nodes:
AssignmentExpressionBinaryExpressionCallExpressionCastExpressionClassDefinitionConditionalExpressionDereferenceExpressionFunctionDefinitionIdentifierImportExpressionLiteralMemberAccessNewExpressionNoopObjectExpressionObjectPropertyPackageDeclarationReferenceExpressionReturnStatementSequenceSliceExpressionSpreadElementSuperExpressionSwitchStatementThisExpressionTupleExpressionUnaryExpressionVariableDeclarationYieldExpression
Instruction
t.isInstruction(node);
Covered nodes:
AssignmentExpressionBinaryExpressionBreakStatementCallExpressionCastExpressionCatchClauseClassDefinitionConditionalExpressionContinueStatementDereferenceExpressionExportStatementExpressionStatementForStatementFunctionDefinitionIdentifierIfStatementImportExpressionLabeledStatementLiteralMemberAccessNewExpressionNoopObjectExpressionObjectPropertyPackageDeclarationRangeStatementReferenceExpressionReturnStatementScopedStatementSequenceSliceExpressionSpreadElementSuperExpressionSwitchStatementThisExpressionThrowStatementTryStatementTupleExpressionUnaryExpressionVariableDeclarationWhileStatementYieldExpression
LVal
A cover of left hand side expressions used in the left of assignment expressions and ForXStatements.
t.isLVal(node);
Covered nodes:
Loop
A cover of loop statements.
t.isLoop(node);
Covered nodes:
Scopable
A cover of FunctionParent and BlockParent.
t.isScopable(node);
Covered nodes:
Standardized
A cover of AST nodes which are part of an official ECMAScript specification.
t.isStandardized(node);
Covered nodes:
ArrayTypeAssignmentExpressionBinaryExpressionBreakStatementCallExpressionCaseClauseCastExpressionCatchClauseChanTypeClassDefinitionCompileUnitConditionalExpressionContinueStatementDereferenceExpressionDynamicTypeExportStatementExpressionStatementForStatementFuncTypeFunctionDefinitionIdentifierIfStatementImportExpressionLabeledStatementLiteralMapTypeMemberAccessNewExpressionNoopObjectExpressionObjectPropertyPackageDeclarationPointerTypePrimitiveTypeRangeStatementReferenceExpressionReturnStatementScopedStatementScopedTypeSequenceSliceExpressionSpreadElementSuperExpressionSwitchStatementThisExpressionThrowStatementTryStatementTupleExpressionTupleTypeUnaryExpressionVariableDeclarationVoidTypeWhileStatementYieldExpression
Statement
A cover of any Statements.
t.isStatement(node);
Covered nodes:
BreakStatementClassDefinitionContinueStatementExportStatementExpressionStatementForStatementFunctionDefinitionIfStatementLabeledStatementNoopPackageDeclarationRangeStatementReturnStatementScopedStatementSequenceSwitchStatementThrowStatementTryStatementVariableDeclarationWhileStatementYieldExpression
Type
t.isType(node);
Covered nodes: