TypeScript ESTree
A parser that converts TypeScript source code into an ESTree-compatible form
Getting Started
You can find our Getting Started docs here
About
This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatible AST.
In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
- ESLint, the pluggable linting utility for JavaScript and JSX
- Prettier, an opinionated code formatter
Installation
yarn add -D @typescript-eslint/typescript-estree
API
Parsing
parse(code, options)
Parses the given string of code with the options provided and returns an ESTree-compatible AST.
interface ParseOptions {
comment?: boolean;
debugLevel?: boolean | ('typescript-eslint' | 'eslint' | 'typescript')[];
errorOnUnknownASTType?: boolean;
filePath?: string;
jsx?: boolean;
loc?: boolean;
loggerFn?: Function | false;
range?: boolean;
tokens?: boolean;
}
const PARSE_DEFAULT_OPTIONS: ParseOptions = {
comment: false,
errorOnUnknownASTType: false,
filePath: 'estree.ts',
jsx: false,
loc: false,
loggerFn: undefined,
range: false,
tokens: false,
};
declare function parse(
code: string,
options: ParseOptions = PARSE_DEFAULT_OPTIONS,
): TSESTree.Program;
Example usage:
import { parse } from '@typescript-eslint/typescript-estree';
const code = `const hello: string = 'world';`;
const ast = parse(code, {
loc: true,
range: true,
});
parseAndGenerateServices(code, options)
Parses the given string of code with the options provided and returns an ESTree-compatible AST. Accepts additional options which can be used to generate type information along with the AST.
interface ParseAndGenerateServicesOptions extends ParseOptions {
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;
EXPERIMENTAL_useSourceOfProjectReferenceRedirect?: boolean;
extraFileExtensions?: string[];
filePath?: string;
preserveNodeMaps?: boolean;
project?: string | string[];
projectFolderIgnoreList?: string[];
tsconfigRootDir?: string;
programs?: Program[];
createDefaultProgram?: boolean;
allowAutomaticSingleRunInference?: boolean;
moduleResolver?: string;
}
interface ParserServices {
program: ts.Program;
esTreeNodeToTSNodeMap: WeakMap<TSESTree.Node, ts.Node | ts.Token>;
tsNodeToESTreeNodeMap: WeakMap<ts.Node | ts.Token, TSESTree.Node>;
hasFullTypeInformation: boolean;
}
interface ParseAndGenerateServicesResult<T extends TSESTreeOptions> {
ast: TSESTree.Program;
services: ParserServices;
}
const PARSE_AND_GENERATE_SERVICES_DEFAULT_OPTIONS: ParseOptions = {
...PARSE_DEFAULT_OPTIONS,
errorOnTypeScriptSyntacticAndSemanticIssues: false,
extraFileExtensions: [],
preserveNodeMaps: false,
project: undefined,
projectFolderIgnoreList: ['/node_modules/'],
tsconfigRootDir: process.cwd(),
};
declare function parseAndGenerateServices(
code: string,
options: ParseOptions = PARSE_DEFAULT_OPTIONS,
): ParseAndGenerateServicesResult;
Example usage:
import { parseAndGenerateServices } from '@typescript-eslint/typescript-estree';
const code = `const hello: string = 'world';`;
const { ast, services } = parseAndGenerateServices(code, {
filePath: '/some/path/to/file/foo.ts',
loc: true,
project: './tsconfig.json',
range: true,
});
parseWithNodeMaps(code, options)
Parses the given string of code with the options provided and returns both the ESTree-compatible AST as well as the node maps.
This allows you to work with both ASTs without the overhead of types that may come with parseAndGenerateServices
.
interface ParseWithNodeMapsResult<T extends TSESTreeOptions> {
ast: TSESTree.Program;
esTreeNodeToTSNodeMap: ParserServices['esTreeNodeToTSNodeMap'];
tsNodeToESTreeNodeMap: ParserServices['tsNodeToESTreeNodeMap'];
}
declare function parseWithNodeMaps(
code: string,
options: ParseOptions = PARSE_DEFAULT_OPTIONS,
): ParseWithNodeMapsResult;
Example usage:
import { parseWithNodeMaps } from '@typescript-eslint/typescript-estree';
const code = `const hello: string = 'world';`;
const { ast, esTreeNodeToTSNodeMap, tsNodeToESTreeNodeMap } = parseWithNodeMaps(
code,
{
loc: true,
range: true,
},
);
TSESTree
, AST_NODE_TYPES
and AST_TOKEN_TYPES
Types for the AST produced by the parse functions.
TSESTree
is a namespace which contains object types representing all of the AST Nodes produced by the parser.AST_NODE_TYPES
is an enum which provides the values for every single AST node's type
property.AST_TOKEN_TYPES
is an enum which provides the values for every single AST token's type
property.
Utilities
createProgram(configFile, projectDirectory)
This serves as a utility method for users of the ParseOptions.programs
feature to create a TypeScript program instance from a config file.
declare function createProgram(
configFile: string,
projectDirectory: string = process.cwd(),
): import('typescript').Program;
Example usage:
const tsESTree = require('@typescript-eslint/typescript-estree');
const program = tsESTree.createProgram('tsconfig.json');
const code = `const hello: string = 'world';`;
const { ast, services } = parseAndGenerateServices(code, {
filePath: '/some/path/to/file/foo.ts',
loc: true,
program,
range: true,
});
Supported TypeScript Version
See the Supported TypeScript Version section in the project root.
If you use a non-supported version of TypeScript, the parser will log a warning to the console.
Please ensure that you are using a supported version before submitting any issues/bug reports.
Reporting Issues
Please check the current list of open and known issues and ensure the issue has not been reported before. When creating a new issue provide as much information about your environment as possible. This includes:
- TypeScript version
- The
typescript-estree
version
AST Alignment Tests
A couple of years after work on this parser began, the TypeScript Team at Microsoft began officially supporting TypeScript parsing via Babel.
I work closely with the TypeScript Team and we are gradually aligning the AST of this project with the one produced by Babel's parser. To that end, I have created a full test harness to compare the ASTs of the two projects which runs on every PR, please see the code for more details.
Debugging
If you encounter a bug with the parser that you want to investigate, you can turn on the debug logging via setting the environment variable: DEBUG=typescript-eslint:*
.
I.e. in this repo you can run: DEBUG=typescript-eslint:* yarn lint
.
License
TypeScript ESTree inherits from the the original TypeScript ESLint Parser license, as the majority of the work began there. It is licensed under a permissive BSD 2-clause license.
Contributing
See the contributing guide here