@phenomnomnominal/tsquery
- Version 6.1.3
- Published
- 86.2 kB
- 2 dependencies
- MIT license
Install
npm i @phenomnomnominal/tsqueryyarn add @phenomnomnominal/tsquerypnpm add @phenomnomnominal/tsqueryOverview
Query TypeScript ASTs with the esquery API!
Index
Variables
variable tsquery
const tsquery: API;Functions
function ast
ast: typeof ast;Parse a string of code into an Abstract Syntax Tree which can then be queried with TSQuery Selectors.
Parameter source
the code that should be parsed into a [
SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159). ASourceFileis the TypeScript implementation of an Abstract Syntax Tree (with extra details).Parameter fileName
a name (if known) for the
SourceFile. Defaults to empty string.Parameter scriptKind
the TypeScript [
ScriptKind](https://github.com/microsoft/TypeScript/blob/main/src/compiler/types.ts#L7305) of the code. Defaults toScriptKind.TSX. Set this toScriptKind.TSif your code uses the<Type>syntax for casting.Returns
a TypeScript
SourceFile.Modifiers
@public
function files
files: (configFilePath: string) => Array<string>;Get all the file paths included ina the TypeScript project described by a given config file.
Parameter configFilePath
the path to the TypeScript config file, or a directory containing a
tsconfig.jsonfile.Returns
an
Arrayof the file paths for all files in the project.Modifiers
@public
function includes
includes: (node: Node, selector: string | Selector) => boolean;Check for
Nodeswithin a givenstringof code or ASTNodematching aSelector.Parameter node
the
Nodeto be searched. This could be a TypeScript [SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or aNodefrom a previous query.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
trueif the code contains matches for theSelector,falseif not.Modifiers
@public
function map
map: ( sourceFile: SourceFile, selector: string | Selector, nodeTransformer: NodeTransformer) => SourceFile;Transform AST
Nodeswithin a givenNodematching aSelector. Can be used to doNode-based replacement or removal of parts of the input AST.Parameter sourceFile
the TypeScript [
SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159) to be searched.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Parameter nodeTransformer
a function to transform any matched
Nodes. If the originalNodeis returned, there is no change. If a newNodeis returned, the originalNodeis replaced. Ifundefinedis returned, the originalNodeis removed.Returns
a transformed
SourceFile.Modifiers
@public
function match
match: <T extends Node = Node>( node: Node, selector: string | Selector) => Array<T>;Find AST
Nodeswithin a given ASTNodematching aSelector.Parameter node
the
Nodeto be searched. This could be a TypeScript [SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or aNodefrom a previous query.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
an
ArrayofNodeswhich match theSelector.Modifiers
@public
function parse
parse: typeof parse;Parse a
stringinto an ESQuerySelector.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
a validated
Selectorornullif the inputstringis invalid.Throws
if the
Selectoris syntactically valid, but contains an invalid TypeScript Node kind.Modifiers
@public
function print
print: (source: Node | SourceFile, options?: PrinterOptions) => string;Print a given
NodeorSourceFileto a string, using the default TypeScript printer.Parameter source
the
NodeorSourceFileto print.Parameter options
any
PrinterOptions.Returns
the printed code
Modifiers
@public
function project
project: (configFilePath: string) => Array<SourceFile>;Get all the
SourceFilesincluded in a the TypeScript project described by a given config file.Parameter configFilePath
the path to the TypeScript config file, or a directory containing a
tsconfig.jsonfile.Returns
an
Arrayof theSourceFilesfor all files in the project.Modifiers
@public
function query
query: { <T extends Node = Node>( code: string, selector: string | Selector, scriptKind?: ScriptKind ): Array<T>; <T extends Node = Node>(code: Node, selector: any): T[];};Find AST
Nodeswithin a givenstringof code or ASTNodematching aSelector.Parameter code
the code to be searched. This could be a
stringof TypeScript code, a TypeScript [SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or aNodefrom a previous query.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Parameter scriptKind
the TypeScript [
ScriptKind](https://github.com/microsoft/TypeScript/blob/main/src/compiler/types.ts#L7305) of the code. Only required when passing astringof code. Defaults toScriptKind.TSX. Set this toScriptKind.TSif your code uses the<Type>syntax for casting.Returns
an
ArrayofNodeswhich match theSelector.Modifiers
@public
function remove
remove: (sourceFile: SourceFile, selector: string | Selector) => SourceFile;Remove AST
Nodeswithin a givenNodematching aSelector.Parameter sourceFile
the TypeScript [
SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159) to be searched.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
a transformed
SourceFilewith the matchingNodesremoved.Modifiers
@public
function replace
replace: ( source: string, selector: string, stringTransformer: StringTransformer, scriptKind?: ScriptKind) => string;Transform AST
Nodeswithin a givenNodematching aSelector. Can be used to do string-based replacement or removal of parts of the input AST. The updated code will be printed with the TypeScript [Printer](https://github.com/microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md#creating-and-printing-a-typescript-ast), so you may need to run your own formatter on any output code.Parameter node
the
Nodeto be searched. This could be a TypeScript [SourceFile](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or a Node from a previous selector.Parameter selector
a TSQuery
Selector(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Parameter stringTransformer
a function to transform any matched
Nodes. Ifnullis returned, there is no change. If a newstringis returned, the originalNodeis replaced.Parameter scriptKind
the TypeScript [
ScriptKind](https://github.com/microsoft/TypeScript/blob/main/src/compiler/types.ts#L7305) of the code. Defaults toScriptKind.TSX. Set this toScriptKind.TSif your code uses the<Type>syntax for casting.Returns
a transformed
Node.Modifiers
@public
Type Aliases
type API
type API = typeof query & { ast: typeof ast; map: typeof map; match: typeof match; parse: typeof parse; project: typeof project; projectFiles: typeof files; query: typeof query; replace: typeof replace; syntaxKindName: typeof syntaxKindName;};type NodeTransformer
type NodeTransformer = (node: Node) => VisitResult<Node | undefined>;type StringTransformer
type StringTransformer = (node: Node) => string | null;Namespaces
Package Files (11)
Dependencies (2)
Dev Dependencies (12)
Peer Dependencies (1)
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@phenomnomnominal/tsquery.
- Markdown[](https://www.jsdocs.io/package/@phenomnomnominal/tsquery)
- HTML<a href="https://www.jsdocs.io/package/@phenomnomnominal/tsquery"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3970 ms. - Missing or incorrect documentation? Open an issue for this package.
