identifierAt

tcmts.studio.identifierAt · #mnzoaavj · term · runs anywhere

The identifier at offset in the scratch file abs as a analyzed it, if there's one there.

import type { Analysis } from "#codebase/tcmts/core/analyze";
import { forEachChild } from "#codebase/tcmts/core/ts";
import { isIdentifier as ts_isIdentifier } from "typescript/ast";
import type { Identifier as ts_Identifier, Node as ts_Node } from "typescript/ast";

export function identifierAt(a: Analysis, abs: string, offset: number): ts_Identifier | undefined {
  const sf = a.program.getSourceFile(abs);
  if (!sf) return undefined;
  let id: ts_Identifier | undefined;
  const find = (n: ts_Node) => {
    if (offset < n.getStart(sf) || offset > n.end) return;
    if (ts_isIdentifier(n)) id = n;
    forEachChild(n, find);
  };
  find(sf);
  return id;
}

uses

used by