Tuesday, December 21, 2021

TypeScript Compiler API; AST Viewer

 Static Analysis and Source Code Manipulation in TypeScript - YouTube

TypeScript Compiler API: from string to AST - Stack Overflow

import * as ts from "typescript";

const sourceFile = ts.createSourceFile(
    "file.ts",                  // filePath
    "function myFunction() {}", // fileText
    ts.ScriptTarget.Latest,     // scriptTarget
    true                        // setParentNodes -- sets the `parent` property
);
dsherret (David Sherret) @GitHub
dsherret/ts-ast-viewer: TypeScript AST viewer. @GitHub

TypeScript AST Viewer (demo)
dsherret/ts-morph: TypeScript Compiler API wrapper for static analysis and programmatic code changes.
Using the Compiler API · microsoft/TypeScript Wiki
Compiler API (TypeScript) | learning-notes
TypeScript: Developers - TypeScript VFS




JavaScript & TypeScript AST Tools

 Read JavaScript Source Code, Using an AST | DigitalOcean

An AST is the result of parsing code. For JavaScript, an AST is a JavaScript object containing a tree representation of your source. Before we use it, we have to create it. Depending on the code we are parsing, we choose the appropriate parser.

Tree graph from @babel/parser

 some of the most well known Open Source ECMAScript parsers:

ParserSupported LanguagesGithub
acornesnext & JSX (using acorn-jsx)https://github.com/acornjs/acorn
esprimaesnext & olderhttps://github.com/jquery/esprima
cherowesnext & olderhttps://github.com/cherow/cherow
espreeesnext & olderhttps://github.com/eslint/espree
shiftesnext & olderhttps://github.com/shapesecurity/shift-parser-js
babelesnext, JSX & typescripthttps://github.com/babel/babel
TypeScriptesnext & typescripthttps://github.com/Microsoft/TypeScript
Using the Compiler API · microsoft/TypeScript Wiki


Once upon a time, an unsuspecting Mozilla engineer created an API in Firefox that exposed the SpiderMonkey engine's JavaScript parser as a JavaScript API. Said engineer documented the format it produced, and this format caught on as a lingua franca for tools that manipulate JavaScript source code.

Meanwhile JavaScript is evolving. This site will serve as a community standard for people involved in building and using these tools to help evolve this format to keep up with the evolution of the JavaScript language.

estree/es2015.md at master · estree/estree

This document specifies the extensions to the core ESTree AST types to support the ES2015 grammar.