AST parsing

Source: src/engine/graph/ast-parser.ts

Creates a ts-morph Project and loads every collected file into it. Three later stages read that AST: module graph building, provider resolution, and rule execution. One shared Project means no file is parsed twice.

Input is the list of absolute paths from the file collection stage:

files: string[]    // absolute file paths from the file collection stage

The result is a ts-morph Project with every source file loaded:

Project    // ts-morph Project with all source files loaded

How it works

The parser builds the project like this:

const project = new Project({
  compilerOptions: {
    strict: true,
    target: ScriptTarget.ESNext,
    module: ModuleKind.ESNext,
    skipFileDependencyResolution: true,
  },
  skipAddingFilesFromTsConfig: true,
})
 
for (const file of files) {
  project.addSourceFileAtPath(file)
}

Key configuration choices:

  • skipFileDependencyResolution: true: only the files you provide are analyzed. import statements into node_modules or other directories are not followed, which keeps the scan fast and scoped.
  • skipAddingFilesFromTsConfig: true: the project's tsconfig.json file list is ignored. The file collector already decided which files to include.
  • strict: true: enables strict type checking for accurate type information.
  • ESNext target: uses the latest language features, so the AST carries no downlevel transform artifacts.

Debugging tips

  • If a rule cannot find the AST nodes you expect, the file may not have been added to the project. Check the file collection stage.
  • ts-morph wraps the TypeScript compiler API. Use the TypeScript AST Viewer to explore the AST for a given code snippet.
  • The Project object is passed by reference to every later stage.