0.4.0 • Published 3 months ago

transformer-query v0.4.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

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

...
0.4.0

3 months ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago