How to use the recast/parsers/typescript.parse function in recast

To help you get started, we’ve selected a few recast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apollographql / apollo-tooling / packages / apollo / src / commands / service / codegen.ts View on Github external
const getGQLTagsFromSource = (source: string) => {
  const finder = lineColumn(source);
  let sdl = "";
  let loc;

  visit(parse(source), {
    visitTaggedTemplateExpression(path) {
      this.traverse(path);
      const expr = path.node;
      if (Identifier.check(expr.tag) && expr.tag.name === "gql") {
        loc = finder.toIndex(expr.loc!.start.line, expr.loc!.start.column) + 5; // 5 = offset from the gql tag to SDL
        expr.quasi.quasis.forEach(v => (sdl += v.value.raw));
      }
    }
  });

  // @ts-ignore Says `loc` is used before defined, but it isn't.
  return { sdl, loc };
  // return sdl;
};