@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). A SourceFile 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 to ScriptKind.TSX. Set this to ScriptKind.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 given string of code or AST Node matching a Selector.

      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 query.

      Parameter selector

      a TSQuery Selector (using the [ESQuery selector syntax](https://github.com/estools/esquery)).

      Returns

      true if the code contains matches for the Selector, false if not.

      Modifiers

      • @public

    function map

    map: (
    sourceFile: SourceFile,
    selector: string | Selector,
    nodeTransformer: NodeTransformer
    ) => SourceFile;
    • Transform AST Nodes within a given Node matching a Selector. Can be used to do Node-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 original Node is returned, there is no change. If a new Node is returned, the original Node is replaced. If undefined is returned, the original Node 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 AST Node matching a Selector.

      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 query.

      Parameter selector

      a TSQuery Selector (using the [ESQuery selector syntax](https://github.com/estools/esquery)).

      Returns

      an Array of Nodes which match the Selector.

      Modifiers

      • @public

    function parse

    parse: typeof parse;
    • Parse a string into an ESQuery Selector.

      Parameter selector

      a TSQuery Selector (using the [ESQuery selector syntax](https://github.com/estools/esquery)).

      Returns

      a validated Selector or null if the input string 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 or SourceFile to a string, using the default TypeScript printer.

      Parameter source

      the Node or SourceFile 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 the SourceFiles 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 given string of code or AST Node matching a Selector.

      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 a Node 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 a string of code. Defaults to ScriptKind.TSX. Set this to ScriptKind.TS if your code uses the <Type> syntax for casting.

      Returns

      an Array of Nodes which match the Selector.

      Modifiers

      • @public

    function remove

    remove: (sourceFile: SourceFile, selector: string | Selector) => SourceFile;
    • Remove AST Nodes within a given Node matching a Selector.

      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 matching Nodes removed.

      Modifiers

      • @public

    function replace

    replace: (
    source: string,
    selector: string,
    stringTransformer: StringTransformer,
    scriptKind?: ScriptKind
    ) => string;
    • Transform AST Nodes within a given Node matching a Selector. 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. If null is returned, there is no change. If a new string is returned, the original Node is replaced.

      Parameter scriptKind

      the TypeScript [ScriptKind](https://github.com/microsoft/TypeScript/blob/main/src/compiler/types.ts#L7305) of the code. Defaults to ScriptKind.TSX. Set this to ScriptKind.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

          namespace ast

          namespace ast {}

            variable ensure

            var ensure: { (code: string, scriptKind: ScriptKind): Node; (code: Node): Node };

              namespace parse

              namespace parse {}

                function ensure

                ensure: (selector: string | Selector) => Selector;

                  Package Files (11)

                  Dependencies (2)

                  Dev Dependencies (12)

                  Peer Dependencies (1)

                  Badge

                  To add a badge like this onejsDocs.io badgeto 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>