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