最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - How do I select a variablestatement using tsquery (ast)? - Stack Overflow

programmeradmin0浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论