JS/TS中的AST节点类型示例

目前仍然缺少很多ts节点定义

之前在写eslint规则的时候,需要去判断AST节点类型,但是有很多AST节点类型的定义我找不到。这里列一些我通过AST explorer试出来的节点定义(estree中有js节点定义,但是缺少ts节点定义)。


以下例子中高亮的部分为类型对应的节点(Node):

ArrayExpression

      const a = [];
    

ArrayPattern

      const [ a, b ] = [ 1, 2 ];
    

ArrowFunctionExpression

      const a = () => {};
    

AssignmentExpression

      let a;
a = 1;
    

AssignmentPattern

      const [a, b = 4] = [1, null];
    

AwaitExpression

      async function a() {
   const b = await a();
 }
    

BigIntLiteral

      const a = 123123n;
    

BinaryExpression

      const a = a + 1;
    

BlockStatement

      {}
    

BreakStatement

      switch(a) {
   case '1':
     break;
 }
    

CallExpression

      a()
    

CatchClause

      try{ }catch(e){ }
    

ChainExpression

可选链

      a?.a
    

ClassBody

      class Test {}
    

ClassDeclaration

      class Test {}
    

ClassExpression

      const a = class {};
    

ConditionalExpression

      const a = b ? c : d;
    

ContinueStatement

      for(let i = 0; i < 1; i++){
   continue;
 }
    

DebuggerStatement

      debugger;
    

Decorator

      @component
 const a = 1;
    

DoWhileStatement

      do { } while(1)
    

EmptyStatement

      ;
    

ExportAllDeclaration

      export * from '';
    

ExportDefaultDeclaration

      export default '';
    

ExportNamedDeclaration

      export const a = 1;
    

ExportSpecifier

      export { a } from '';
    

ExpressionStatement

      a = 1;
 (function () {});
    

ForInStatement

      for(let a in 3) {}
    

ForOfStatement

      for(let a of []) {}
    

ForStatement

      for(let a = 0; a < 1; a++) {}
    

FunctionDeclaration

      function a() {}
    

FunctionExpression

      const a = function() {};
    

Identifier

任何标识符, 如变量名,属性名,函数名

      const a = 1;
    

IfStatement

      if(a) {}
    

ImportDeclaration

      import a from '';
    

ImportDefaultSpecifier

      import a from '';
    

ImportExpression

      import('')
    

ImportNamespaceSpecifier

      import * as a from '';
    

ImportSpecifier

      import { a } from ''
    

JSXAttribute

      const a = <a name=''></a>;
    

JSXClosingElement

      const a = <a name=''></a>
    

JSXClosingFragment

      const a = <><Foo /></>
    

JSXElement

      const a = <a></a>
 const a = <a />
    

JSXEmptyExpression

      const a = <a>{  }</a>
    

JSXExpressionContainer

      const a = <a>{ }</a>
    

JSXFragment

      const a = <></>
    

JSXIdentifier

      const b = <a a=''></a>
    

JSXMemberExpression

      const a = <A.a></A.a>
    

JSXOpeningElement

      const a = <a></a>
    

JSXOpeningFragment

      const a = <></>
    

JSXSpreadAttribute

      const a = <a {...a}></a>
    

JSXSpreadChild

      const a = <a>{...a}</a>
    

JSXText

      const a = <a>text</a>
    

LabeledStatement

      label:a
    

Literal

字面量

      1
    

LogicalExpression

      a && b
 a || b
    

MemberExpression

      a.a
    

MetaProperty

      new.target
 import.meta
    

MethodDefinition

      class Test { a(){ } }
    

NewExpression

      new Test();
    

ObjectExpression

      const a = { };
    

ObjectPattern

      const { a, b } = { a: 1, b: 2};
    

Program

表示完整源码树

      { sourceType: "script" || "module", body: [] }
    

Property

      const a = { a: 1 };
    

PropertyDefinition

      class Test { a = 1 }
    

RestElement

      const [ a, b, ...c ] = [ 1, 2, 3, 4 ];
    

ReturnStatement

      return
    

SequenceExpression

      a, b
 a = 1, b = 2
    

SpreadElement

      const a = [ 1, ...b ]
 const a = { a: 1, ...b }
    

Super

      class bar extends foo {
   constructor() {
     super();
   }
 }
    

SwitchCase

      switch(a){
   case '1':
     break;
   default:
 }
    

SwitchStatement

      switch(a){
   case '1':
     break;
   default:
 }
    

TaggedTemplateExpression

      foo`test`
    

TemplateElement

      foo`test`
    

TemplateLiteral

      foo`test`
    

ThisExpression

      this
    

ThrowStatement

      throw new Error
    

TryStatement

      try{ }catch(e) { }
    

TSAbstractKeyword

这个不确定

      abstract class Test { }
    

TSAbstractMethodDefinition

      abstract class Test {
   public abstract a(): void
 }
    

TSAbstractPropertyDefinition

      abstract class Test {
   public abstract a: number
 }
    

TSAnyKeyword

      let a: any;
    

TSArrayType

      let a: string[];
    

TSAsExpression

      let a = '' as number;
    

TSAsyncKeyword

      
    

TSBigIntKeyword

      let a: bigint;
    

TSBooleanKeyword

      let a: boolean;
    

TSCallSignatureDeclaration

      type Greet = { (name: string): void }
    

TSClassImplements

      
    

TSConditionalType

      type A<T, U> = T extends U ? true : false;
    

TSConstructorType

      
    

TSConstructSignatureDeclaration

      
    

TSDeclareFunction

      
    

TSDeclareKeyword

      
    

TSEmptyBodyFunctionExpression

      
    

TSEnumDeclaration

      
    

TSEnumMember

      
    

TSExportAssignment

      
    

TSExportKeyword

      
    

TSExternalModuleReference

      
    

TSFunctionType

      
    

TSImportEqualsDeclaration

      
    

TSImportType

      
    

TSIndexedAccessType

      
    

TSIndexSignature

      
    

TSInferType

      
    

TSInterfaceBody

      
    

TSInterfaceDeclaration

      
    

TSInterfaceHeritage

      
    

TSIntersectionType

      
    

TSLiteralType

      
    

TSMappedType

      
    

TSMethodSignature

      
    

TSModuleBlock

      
    

TSModuleDeclaration

      
    

TSNamespaceExportDeclaration

      
    

TSNeverKeyword

      
    

TSNonNullExpression

      
    

TSNullKeyword

      
    

TSNumberKeyword

      
    

TSObjectKeyword

      
    

TSOptionalType

      
    

TSParameterProperty

      
    

TSPrivateKeyword

      
    

TSPropertySignature

      
    

TSProtectedKeyword

      
    

TSPublicKeyword

      
    

TSQualifiedName

      
    

TSReadonlyKeyword

      
    

TSRestType

      
    

TSStaticKeyword

      
    

TSStringKeyword

      
    

TSSymbolKeyword

      
    

TSThisType

      
    

TSTupleType

      
    

TSTypeAliasDeclaration

      
    

TSTypeAnnotation

      
    

TSTypeAssertion

      
    

TSTypeLiteral

      
    

TSTypeOperator

      
    

TSTypeParameter

      
    

TSTypeParameterDeclaration

      
    

TSTypeParameterInstantiation

      
    

TSTypePredicate

      
    

TSTypeQuery

      
    

TSTypeReference

      
    

TSUndefinedKeyword

      
    

TSUnionType

      
    

TSUnknownKeyword

      
    

TSVoidKeyword

      
    

UnaryExpression

      !a
 typeof
 delete
 void 0
    

UpdateExpression

      a++
 a--
    

VariableDeclaration

      let a = 1;
    

VariableDeclarator

      let a = 1;
    

WhileStatement

      while(1){ }
    

WithStatement

      with(a){ }
    

YieldExpression

      function* gen(x) {
   const y = yield x + 2;
   return y;
 }
    
评论加载中 (ง •̀ω•́)ง
Table of Contents