transformer-query
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

transformer-query

This is the proof-of-concept library of querying ast nodes with valid string.

Thanks for ttypescript, this made it easy to develop this library.

How to use

Install ttypescript

npm install ttypescript -D

And use ttsc instead of tsc.

(Optional) Register Transformer-Query Transform

To use imported wildcard query, you should register transform.

Just add this

{
    ...
    "compilerOptions": {
        "plugins": { "transform": "transformer-query/lib/transform" }
    }
}

to your tsconfig.json

That's it!

Make Transform

import { makeTransform } from "transformer-query";

export default makeTransform([
    (source, checker) => {
        ...
    }
]);

Query

import { q } from "transformer-query";

...

(source, checker) => {
    source.query(q`class ToTransform {}`)
}

...

Query Wildcard

import { q, Identifier } from "transformer-query";
import { MyClass } from "./MyClass";

...

(source, checker) => {
    source.query(q`class ${Identifier} extends ${MyClass} {}`)
}

...

Note: To use Imported Wildcard, you should follow instruction on above

Replacing

Transformer-Query provides five kinds of replace method.

Function Signatures

replace(replacer: (node: ts.Node) => ts.Node): Source<T>;

replaceWith(toReplace: ts.Node): Source<T>;

replaceText(replacer: (node: ts.Node) => string): Source<T>;

replaceWithText(toReplace: string): Source<T>;

remove(): void;

To replace, just call replace method with call-chain.

Ex)

...

(source, checker) => {
    source
        .query(q`class ${Identifier} {}`)
        .replaceWithText("class Transformed {}");
}

...

Dependencies (0)

    Dev Dependencies (4)

    Package Sidebar

    Install

    npm i transformer-query

    Weekly Downloads

    1

    Version

    0.4.0

    License

    MIT

    Unpacked Size

    138 kB

    Total Files

    40

    Last publish

    Collaborators

    • sier-vr