Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function generateCapnpImport(ctx: CodeGeneratorFileContext): void {
// Look for the special importPath annotation on the file to see if we need a different import path for capnp-ts.
const fileNode = lookupNode(ctx, ctx.file);
const tsFileId = capnp.Uint64.fromHexString(TS_FILE_ID);
// This may be undefined if ts.capnp is not imported; fine, we'll just use the default.
const tsAnnotationFile = ctx.nodes.find(n => n.getId().equals(tsFileId));
// We might not find the importPath annotation; that's definitely a bug but let's move on.
const tsImportPathAnnotation =
tsAnnotationFile &&
tsAnnotationFile.getNestedNodes().find(n => n.getName() === "importPath");
// There may not necessarily be an import path annotation on the file node. That's fine.
const importAnnotation =
tsImportPathAnnotation &&
fileNode
.getAnnotations()
.find(a => a.getId().equals(tsImportPathAnnotation.getId()));
const importPath =
importAnnotation === undefined
? "capnp-ts"
: importAnnotation.getValue().getText();
export function lookupNode(
ctx: CodeGeneratorFileContext,
lookup: { getId(): capnp.Uint64 } | capnp.Uint64
): s.Node {
const id = lookup instanceof capnp.Uint64 ? lookup : lookup.getId();
const node = ctx.nodes.find(n => n.getId().equals(id));
if (node === undefined) throw new Error(format(E.GEN_NODE_LOOKUP_FAIL, id));
return node;
}