How to get the next sibling and ignore tokens in ts-morph?

39 Views Asked by At

I try to get the next sibling using getNextSibling from ts-morph.

The problem is I get the node DotToken or OpenBracketToken which I don't care about them. I want the node with the content. the problem this is can be any node (Identifier or StringLiteral or BinaryExpression or anything else).

Is there a safe way to get the node and ignore the tokens?

import { Identifier, Project, SyntaxKind } from "ts-morph";

console.clear();

const project = new Project();

const sourceFile = project.createSourceFile(
  "foo.ts",
  `
  foo.bar.some();
  foo[bar].some();
  foo['bar'].some();
  foo['bar' + 'other'].some();
`
);

const foos = sourceFile
  .getDescendantsOfKind(SyntaxKind.Identifier)
  .filter((i) => i.getText() === "foo");

foos.forEach((foo) => {
  const s = foo.getNextSibling();
  console.log({ s: s.getText() }); // outputs: . [ [ [
});

codesandbox.io

ts-ast-viewer.com

0

There are 0 best solutions below