How to make tsquery works with ts-morph?
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 238
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
Hi @dsherret maybe you can help me.
Since I upgrade `ts-morph` to the last version I get a typescript error and the integration with `tsquery` doesn't work.
```
Argument of type 'Node' is not assignable to parameter of type 'string | Node'.
```
I don't think the `tsquery` expect to `node.compilerNode`.
What can I do? Which property on ts-morph I can pass to tsquery to make it work?
https://codesandbox.io/s/elastic-chatterjee-j77h5j?file=/src/index.ts
```
import { tsquery } from "@phenomnomnominal/tsquery";
import { Project, Node, ClassDeclaration } from "ts-morph";
console.clear();
export const searchQuery = (node: Node, query: string) => {
return tsquery(node.compilerNode as any, query).map(
(n) => (node as any)._getNodeFromCompilerNode(n) as Node
);
};
const project = new Project({});
project.createSourceFile(
"foo.ts",
`
class Foo {
init() {
const foo = 1;
}
}
`
);
const fooClass = project
.getSourceFile("foo.ts")
?.getClass("Foo") as ClassDeclaration;
const res = searchQuery(fooClass, 'Identifier[name="Animal"]');
console.log({ res });
```
Contributor guide
Research direction
Start with the linked CodeSandbox reproduction and compare the ts-morph and tsquery versions involved in the TypeScript error. Trace the types of node.compilerNode and the value accepted by tsquery, then confirm the supported interoperability path and document or reproduce a working result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- developer-experience, devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100