@typescript-eslint/typescript-estree
The underlying code used by
@typescript-eslint/parser
that converts TypeScript source code into an ESTree-compatible form. ✨
This parser is designed to be generic and robust. It can be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatible AST.
It is most known for use within these hyper-popular open-source projects to power their TypeScript support:
It works by:
parse(code, options)
Parses the given string of code with the options provided and returns an ESTree-compatible AST.
interface ParseOptions {
sourceType?: SourceType;
allowInvalidAST?: boolean;
comment?: boolean;
suppressDeprecatedPropertyWarnings?: boolean;
debugLevel?: boolean | ('typescript-eslint' | 'eslint' | 'typescript')[];
errorOnUnknownASTType?: boolean;
filePath?: string;
jsDocParsingMode?: JSDocParsingMode;
jsx?: boolean;
loc?: boolean;
loggerFn?: Function | false;
range?: boolean;
tokens?: boolean;
}
const PARSE_DEFAULT_OPTIONS: ParseOptions = {
comment: false,
filePath: 'estree.ts',
jsDocParsingMode: 'all',
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 {
cacheLifetime?: {
glob?: number | 'Infinity';
};
disallowAutomaticSingleRunInference?: boolean;
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;
extraFileExtensions?: string[];
filePath?: string;
preserveNodeMaps?: boolean;
project?: string[] | string | boolean | null;
projectFolderIgnoreList?: string[];
projectService?: boolean | ProjectServiceOptions;
tsconfigRootDir?: string;
programs?: Program[];
}
interface ProjectServiceOptions {
allowDefaultProject?: string[];
defaultProject?: string;
loadTypeScriptPlugins?: boolean;
maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING?: number;
}
interface ParserServices {
program: ts.Program;
esTreeNodeToTSNodeMap: WeakMap<TSESTree.Node, ts.Node | ts.Token>;
tsNodeToESTreeNodeMap: WeakMap<ts.Node | ts.Token, TSESTree.Node>;
}
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,
});
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.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,
});
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
.
This will include TypeScript server logs. To turn off these logs, include -typescript-eslint:typescript-estree:tsserver:*
when setting the environment variable. I.e. for this repo change to: DEBUG='typescript-eslint:*,-typescript-eslint:typescript-estree:tsserver:*' yarn lint
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4