Skip to content

Instantly share code, notes, and snippets.

@cancerberoSgx
Created May 25, 2018 00:21
Show Gist options
  • Save cancerberoSgx/ac521bd40a993aaf5cb07003084e993f to your computer and use it in GitHub Desktop.
Save cancerberoSgx/ac521bd40a993aaf5cb07003084e993f to your computer and use it in GitHub Desktop.
getChildren vs forEachChildren in TypeScript API

For a SourceFile with the following code :

/** Test1 description */ 
export class Test1 {
  dance(){ ; return}
}

Transverse it using getChildren()

SourceFile : "export class Test1 { dance(){ ; return} }"
  SyntaxList : "export class Test1 { dance(){ ; return} }"
    ClassDeclaration : "export class Test1 { dance(){ ; return} }"
      JSDocComment : "/** Test1 description */"
      SyntaxList : "export"
        ExportKeyword : "export"
      ClassKeyword : "class"
      Test1 Identifier : "Test1"
      OpenBraceToken : "{"
      SyntaxList : "dance(){ ; return}"
        MethodDeclaration : "dance(){ ; return}"
          dance Identifier : "dance"
          OpenParenToken : "("
          SyntaxList : ""
          CloseParenToken : ")"
          Block : "{ ; return}"
            OpenBraceToken : "{"
            SyntaxList : "; return"
              EmptyStatement : ";"
                SemicolonToken : ";"
              ReturnStatement : "return"
                ReturnKeyword : "return"
            CloseBraceToken : "}"
      CloseBraceToken : "}"
  EndOfFileToken : ""

Transverse it using `forEachNode()```:

SourceFile : "export class Test1 { dance(){ ; return} }"
  ClassDeclaration : "export class Test1 { dance(){ ; return} }"
    ExportKeyword : "export"
    Test1 Identifier : "Test1"
    MethodDeclaration : "dance(){ ; return}"
      dance Identifier : "dance"
      Block : "{ ; return}"
        EmptyStatement : ";"
        ReturnStatement : "return"

Noticeable differences:

  • SyntaxList (block , params)
  • braces and parents tokens
  • semicolon tokens
  • JSDocComment
  • keywords appear in parent statements or expressions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment