admin管理员组文章数量:1391975
I'm using @phenomnomnominal/tsquery and I seem to be having a really hard time with child selectors. Here is some demo code:
import { tsquery, SyntaxKind } from '@phenomnomnominal/tsquery';
import { getProperties } from '@phenomnomnominal/tsquery/dist/src/traverse';
const codeString = `
class MyClass {
private appName: string;
constructor(name: string) {
this.appName = name;
}
get someData() {
const data: { [key: string]: string } = {
test: 'foo',
};
return data[this.appName];
}
}
`;
const ast = tsquery.ast(
stubClassContents,
'user-exp.service.stub.ts',
ts.ScriptKind.TS
);
const nodes = tsquery(
ast,
'ClassDeclaration > GetAccessor > Block > SyntaxList'
);
nodes[0].getChildren().forEach((child) => {
console.log('CHILD PROPERTIES: ', getProperties(child));
});
This code prints the following:
CHILD PROPERTIES: {
kindName: 'VariableStatement',
text: "const data: { [key: string]: string } = {\n test: 'foo',\n };"
}
CHILD PROPERTIES: { kindName: 'ReturnStatement', text: 'return data[this.appName];' }
I would like to select the const data: {...
so I figures that if I added VariableStatement
to my selectors it would work.
The issue is if I add VariableStatement
to my selector like this: ClassDeclaration > GetAccessor > Block > SyntaxList > VariableStatement
, it doesn't seem to think that anything is there. It gives me back an empty array for nodes.
Does anyone know what is going on with that?
Thank you!
本文标签: typescriptHow do I select a variablestatement using tsquery (ast)Stack Overflow
版权声明:本文标题:typescript - How do I select a variablestatement using tsquery (ast)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744737727a2622431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论