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.
Install ttypescript
npm install ttypescript -D
And use ttsc
instead of tsc
.
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!
import { makeTransform } from "transformer-query";
export default makeTransform([
(source, checker) => {
...
}
]);
import { q } from "transformer-query";
...
(source, checker) => {
source.query(q`class ToTransform {}`)
}
...
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
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 {}");
}
...