@phenomnomnominal/tsquery
- Version 6.1.3
- Published
- 86.2 kB
- 2 dependencies
- MIT license
Install
npm i @phenomnomnominal/tsquery
yarn add @phenomnomnominal/tsquery
pnpm add @phenomnomnominal/tsquery
Overview
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). ASourceFile
is 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.TS
if 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.json
file.Returns
an
Array
of the file paths for all files in the project.Modifiers
@public
function includes
includes: (node: Node, selector: string | Selector) => boolean;
Check for
Nodes
within a givenstring
of code or ASTNode
matching aSelector
.Parameter node
the
Node
to be searched. This could be a TypeScript [SourceFile
](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or aNode
from a previous query.Parameter selector
a TSQuery
Selector
(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
true
if the code contains matches for theSelector
,false
if not.Modifiers
@public
function map
map: ( sourceFile: SourceFile, selector: string | Selector, nodeTransformer: NodeTransformer) => SourceFile;
Transform AST
Nodes
within a givenNode
matching 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 originalNode
is returned, there is no change. If a newNode
is returned, the originalNode
is replaced. Ifundefined
is returned, the originalNode
is removed.Returns
a transformed
SourceFile
.Modifiers
@public
function match
match: <T extends Node = Node>( node: Node, selector: string | Selector) => Array<T>;
Find AST
Nodes
within a given ASTNode
matching aSelector
.Parameter node
the
Node
to be searched. This could be a TypeScript [SourceFile
](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or aNode
from a previous query.Parameter selector
a TSQuery
Selector
(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
an
Array
ofNodes
which match theSelector
.Modifiers
@public
function parse
parse: typeof parse;
Parse a
string
into an ESQuerySelector
.Parameter selector
a TSQuery
Selector
(using the [ESQuery selector syntax](https://github.com/estools/esquery)).Returns
a validated
Selector
ornull
if the inputstring
is invalid.Throws
if the
Selector
is syntactically valid, but contains an invalid TypeScript Node kind.Modifiers
@public
function print
print: (source: Node | SourceFile, options?: PrinterOptions) => string;
Print a given
Node
orSourceFile
to a string, using the default TypeScript printer.Parameter source
the
Node
orSourceFile
to print.Parameter options
any
PrinterOptions
.Returns
the printed code
Modifiers
@public
function project
project: (configFilePath: string) => Array<SourceFile>;
Get all the
SourceFiles
included 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.json
file.Returns
an
Array
of theSourceFiles
for 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
Nodes
within a givenstring
of code or ASTNode
matching aSelector
.Parameter code
the code to be searched. This could be a
string
of TypeScript code, a TypeScript [SourceFile
](https://github.com/microsoft/TypeScript/blob/main/src/services/types.ts#L159), or aNode
from 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 astring
of code. Defaults toScriptKind.TSX
. Set this toScriptKind.TS
if your code uses the<Type>
syntax for casting.Returns
an
Array
ofNodes
which match theSelector
.Modifiers
@public
function remove
remove: (sourceFile: SourceFile, selector: string | Selector) => SourceFile;
Remove AST
Nodes
within a givenNode
matching 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
SourceFile
with the matchingNodes
removed.Modifiers
@public
function replace
replace: ( source: string, selector: string, stringTransformer: StringTransformer, scriptKind?: ScriptKind) => string;
Transform AST
Nodes
within a givenNode
matching 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
Node
to 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
. Ifnull
is returned, there is no change. If a newstring
is returned, the originalNode
is 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.TS
if 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[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](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.