Skip to content

SieR-VR/transformer-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 {}");
}

...