Diagnostics with ts-simple-ast

The following code snippet implements a very small application that uses ts-simple-ast to find errors in a TypeScript file using the error diagnostic APIs. The application uses the chalk npm module to display errors using a red font in the command-line interface:

import chalk from "chalk"; 
import Ast, { DiagnosticMessageChain } from "ts-simple-ast"; 

The following function is the same getAst function that we used in the preceding section:

function getAst(tsConfigPath: string, sourceFilesPath: string) { 
  const ast = new Ast({ 
    tsConfigFilePath: tsConfigPath, 
    addFilesFromTsConfig: false 
  }); 
  ast.addExistingSourceFiles(sourceFilesPath); 
  return ast; 
} 

The AST provided by ts-simple-ast includes a method named getDiagnostics ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.