@atproto/lex-builder 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/filter.d.ts +7 -0
  2. package/dist/filter.d.ts.map +1 -0
  3. package/dist/filter.js +30 -0
  4. package/dist/filter.js.map +1 -0
  5. package/dist/filtered-indexer.d.ts +2123 -0
  6. package/dist/filtered-indexer.d.ts.map +1 -0
  7. package/dist/filtered-indexer.js +56 -0
  8. package/dist/filtered-indexer.js.map +1 -0
  9. package/dist/formatter.d.ts +13 -0
  10. package/dist/formatter.d.ts.map +1 -0
  11. package/dist/formatter.js +34 -0
  12. package/dist/formatter.js.map +1 -0
  13. package/dist/index.d.ts +6 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +13 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/lex-builder.d.ts +20 -0
  18. package/dist/lex-builder.d.ts.map +1 -0
  19. package/dist/lex-builder.js +123 -0
  20. package/dist/lex-builder.js.map +1 -0
  21. package/dist/lex-def-builder.d.ts +66 -0
  22. package/dist/lex-def-builder.d.ts.map +1 -0
  23. package/dist/lex-def-builder.js +682 -0
  24. package/dist/lex-def-builder.js.map +1 -0
  25. package/dist/lexicon-directory-indexer.d.ts +11 -0
  26. package/dist/lexicon-directory-indexer.d.ts.map +1 -0
  27. package/dist/lexicon-directory-indexer.js +51 -0
  28. package/dist/lexicon-directory-indexer.js.map +1 -0
  29. package/dist/ref-resolver.d.ts +48 -0
  30. package/dist/ref-resolver.d.ts.map +1 -0
  31. package/dist/ref-resolver.js +246 -0
  32. package/dist/ref-resolver.js.map +1 -0
  33. package/dist/ts-lang.d.ts +3 -0
  34. package/dist/ts-lang.d.ts.map +1 -0
  35. package/dist/ts-lang.js +138 -0
  36. package/dist/ts-lang.js.map +1 -0
  37. package/dist/util.d.ts +11 -0
  38. package/dist/util.d.ts.map +1 -0
  39. package/dist/util.js +67 -0
  40. package/dist/util.js.map +1 -0
  41. package/package.json +49 -0
  42. package/src/filter.ts +41 -0
  43. package/src/filtered-indexer.test.ts +83 -0
  44. package/src/filtered-indexer.ts +60 -0
  45. package/src/formatter.ts +42 -0
  46. package/src/index.ts +17 -0
  47. package/src/lex-builder.ts +149 -0
  48. package/src/lex-def-builder.ts +912 -0
  49. package/src/lexicon-directory-indexer.ts +57 -0
  50. package/src/ref-resolver.ts +301 -0
  51. package/src/ts-lang.ts +134 -0
  52. package/src/util.ts +67 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lex-def-builder.js","sourceRoot":"","sources":["../src/lex-def-builder.ts"],"names":[],"mappings":";;;;AAAA,sEAAgC;AAChC,uCAA8D;AA0B9D,oDAAuC;AACvC,uDAI0B;AAC1B,6CAA+C;AAQ/C;;GAEG;AACH,MAAa,aAAa;IAIL;IACA;IACA;IALF,WAAW,CAAa;IAEzC,YACmB,OAA6B,EAC7B,IAAgB,EAChB,GAAoB,EACrC,OAAuB;QAHN,YAAO,GAAP,OAAO,CAAsB;QAC7B,SAAI,GAAJ,IAAI,CAAY;QAChB,QAAG,GAAH,GAAG,CAAiB;QAGrC,IAAI,CAAC,WAAW,GAAG,IAAI,6BAAW,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;IACxD,CAAC;IAEO,IAAI,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAC7D,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC;YAC7B,eAAe,EAAE,kCAAuB,CAAC,KAAK;YAC9C,YAAY,EAAE;gBACZ,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;aAC5D;SACF,CAAC,CAAA;QAEF,IAAI,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AW,CAAC,CAAC,CAAA;IACrD,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,WAAmB;IACvC,MAAM,MAAM,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAA;IAC5C,OAAO,KAAK,MAAM,CAAC,WAAW,GAC5B,CAAC,MAAM,CAAC,UAAU;QAChB,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;YAClC,CAAC,MAAM,CAAC,UAAU,KAAK,IAAI;gBACzB,CAAC,CAAC,aAAa;gBACf,CAAC,CAAC,eAAe,MAAM,CAAC,UAAU,EAAE,CAC5C,EAAE,CAAA;AACJ,CAAC;AAED,SAAS,wBAAwB,CAC/B,GAAM,EACN,IAAkB;IAElB,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;IACxE,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;AAC5E,CAAC;AAED,SAAS,QAAQ,CACf,GAAM;IAEN,OAAO,GAAG,CAAC,KAAK,IAAI,IAAI,CAAA;AAC1B,CAAC;AAED,SAAS,OAAO,CACd,GAAM;IAEN,OAAO,GAAG,CAAC,IAAI,IAAI,IAAI,CAAA;AACzB,CAAC;AAED,SAAS,QAAQ,CAAmB,CAAI;IACtC,OAAO,iBAAiB,CAAC,EAAE,CAAA;AAC7B,CAAC","sourcesContent":["import assert from 'node:assert'\nimport { SourceFile, VariableDeclarationKind } from 'ts-morph'\nimport {\n LexiconArray,\n LexiconArrayItems,\n LexiconBlob,\n LexiconBoolean,\n LexiconBytes,\n LexiconCid,\n LexiconDocument,\n LexiconError,\n LexiconIndexer,\n LexiconInteger,\n LexiconObject,\n LexiconParameters,\n LexiconPayload,\n LexiconPermissionSet,\n LexiconProcedure,\n LexiconQuery,\n LexiconRecord,\n LexiconRef,\n LexiconRefUnion,\n LexiconString,\n LexiconSubscription,\n LexiconToken,\n LexiconUnknown,\n} from '@atproto/lex-document'\nimport { l } from '@atproto/lex-schema'\nimport {\n RefResolver,\n ResolvedRef,\n getPublicIdentifiers,\n} from './ref-resolver.js'\nimport { isSafeIdentifier } from './ts-lang.js'\n\nexport type LexDefBuilderOptions = {\n lib?: string\n allowLegacyBlobs?: boolean\n pureAnnotations?: boolean\n}\n\n/**\n * Utility class to build a TypeScript source file from a lexicon document.\n */\nexport class LexDefBuilder {\n private readonly refResolver: RefResolver\n\n constructor(\n private readonly options: LexDefBuilderOptions,\n private readonly file: SourceFile,\n private readonly doc: LexiconDocument,\n indexer: LexiconIndexer,\n ) {\n this.refResolver = new RefResolver(doc, file, indexer)\n }\n\n private pure(code: string) {\n return this.options.pureAnnotations ? markPure(code) : code\n }\n\n async build() {\n this.file.addVariableStatement({\n declarationKind: VariableDeclarationKind.Const,\n declarations: [\n { name: '$nsid', initializer: JSON.stringify(this.doc.id) },\n ],\n })\n\n this.file.addExportDeclaration({\n namedExports: [{ name: '$nsid' }],\n })\n\n const defs = Object.keys(this.doc.defs)\n if (defs.length) {\n const moduleSpecifier = this.options?.lib ?? '@atproto/lex-schema'\n this.file\n .addImportDeclaration({ moduleSpecifier })\n .addNamedImports([{ name: 'l' }])\n\n for (const hash of defs) {\n await this.addDef(hash)\n }\n }\n }\n\n private addUtils(definitions: Record<string, undefined | string>) {\n const entries = Object.entries(definitions).filter(\n (e): e is [(typeof e)[0], NonNullable<(typeof e)[1]>] => e[1] != null,\n )\n if (entries.length) {\n this.file.addVariableStatement({\n isExported: true,\n declarationKind: VariableDeclarationKind.Const,\n declarations: entries.map(([name, initializer]) => ({\n name,\n initializer,\n })),\n })\n }\n }\n\n private async addDef(hash: string) {\n const def = Object.hasOwn(this.doc.defs, hash) ? this.doc.defs[hash] : null\n if (def == null) return\n\n switch (def.type) {\n case 'permission-set':\n return this.addPermissionSet(hash, def)\n case 'procedure':\n return this.addProcedure(hash, def)\n case 'query':\n return this.addQuery(hash, def)\n case 'subscription':\n return this.addSubscription(hash, def)\n case 'record':\n return this.addRecord(hash, def)\n case 'token':\n return this.addToken(hash, def)\n case 'object':\n return this.addObject(hash, def)\n case 'array':\n return this.addArray(hash, def)\n default:\n await this.addSchema(hash, def, {\n type: await this.compileContainedType(def),\n schema: await this.compileContainedSchema(def),\n validationUtils: true,\n })\n }\n }\n\n private async addPermissionSet(hash: string, def: LexiconPermissionSet) {\n const permission = def.permissions.map((def) => {\n const options = stringifyOptionalOptions(def, ['resource', 'type'])\n return this.pure(\n `l.permission(${JSON.stringify(def.resource)}, ${options})`,\n )\n })\n\n const options = stringifyOptionalOptions(def, [\n 'type',\n 'description',\n 'permissions',\n ])\n\n await this.addSchema(hash, def, {\n schema: this.pure(\n `l.permissionSet($nsid, [${permission.join(',')}], ${options})`,\n ),\n })\n }\n\n private async addProcedure(hash: string, def: LexiconProcedure) {\n if (hash !== 'main') {\n throw new Error(`Definition ${hash} cannot be of type ${def.type}`)\n }\n\n // @TODO Build the types instead of using an inferred type.\n\n const ref = await this.addSchema(hash, def, {\n schema: this.pure(`\n l.procedure(\n $nsid,\n ${await this.compileParamsSchema(def.parameters)},\n ${await this.compilePayload(def.input)},\n ${await this.compilePayload(def.output)},\n ${await this.compileErrors(def.errors)}\n )\n `),\n })\n\n this.addUtils({\n $params: this.pure(`${ref.varName}.parameters`),\n $input: this.pure(`${ref.varName}.input`),\n $output: this.pure(`${ref.varName}.output`),\n })\n\n const parametersTypeStmt = this.file.addTypeAlias({\n isExported: true,\n name: 'Params',\n type: `l.InferProcedureParameters<typeof ${ref.varName}>`,\n })\n\n addJsDoc(parametersTypeStmt, def.parameters)\n\n const inputTypeStmt = this.file.addTypeAlias({\n isExported: true,\n name: 'Input',\n type: `l.InferProcedureInputBody<typeof ${ref.varName}>`,\n })\n\n addJsDoc(inputTypeStmt, def.input)\n\n const outputTypeStmt = this.file.addTypeAlias({\n isExported: true,\n name: 'Output',\n type: `l.InferProcedureOutputBody<typeof ${ref.varName}>`,\n })\n\n addJsDoc(outputTypeStmt, def.output)\n }\n\n private async addQuery(hash: string, def: LexiconQuery) {\n if (hash !== 'main') {\n throw new Error(`Definition ${hash} cannot be of type ${def.type}`)\n }\n\n // @TODO Build the types instead of using an inferred type.\n\n const ref = await this.addSchema(hash, def, {\n schema: this.pure(`\n l.query(\n $nsid,\n ${await this.compileParamsSchema(def.parameters)},\n ${await this.compilePayload(def.output)},\n ${await this.compileErrors(def.errors)}\n )\n `),\n })\n\n this.addUtils({\n $params: `${ref.varName}.parameters`,\n $output: `${ref.varName}.output`,\n })\n\n this.file.addTypeAlias({\n isExported: true,\n name: 'Params',\n type: `l.InferQueryParameters<typeof ${ref.varName}>`,\n })\n\n this.file.addTypeAlias({\n isExported: true,\n name: 'Output',\n type: `l.InferQueryOutputBody<typeof ${ref.varName}>`,\n })\n }\n\n private async addSubscription(hash: string, def: LexiconSubscription) {\n if (hash !== 'main') {\n throw new Error(`Definition ${hash} cannot be of type ${def.type}`)\n }\n\n // @TODO Build the types instead of using an inferred type.\n\n const ref = await this.addSchema(hash, def, {\n schema: this.pure(`\n l.subscription(\n $nsid,\n ${await this.compileParamsSchema(def.parameters)},\n ${await this.compileBodySchema(def.message?.schema)},\n ${await this.compileErrors(def.errors)}\n )\n `),\n })\n\n this.addUtils({\n $params: `${ref.varName}.parameters`,\n $message: `${ref.varName}.message`,\n })\n\n this.file.addTypeAlias({\n isExported: true,\n name: 'Params',\n type: `l.InferSubscriptionParameters<typeof ${ref.varName}>`,\n })\n\n this.file.addTypeAlias({\n isExported: true,\n name: 'Message',\n type: `l.InferSubscriptionMessage<typeof ${ref.varName}>`,\n })\n }\n\n private async addRecord(hash: string, def: LexiconRecord) {\n if (hash !== 'main') {\n throw new Error(`Definition ${hash} cannot be of type ${def.type}`)\n }\n\n const key = JSON.stringify(def.key ?? 'any')\n const objectSchema = await this.compileObjectSchema(def.record)\n\n const properties = await this.compilePropertiesTypes(def.record)\n properties.unshift(`$type: ${JSON.stringify(l.$type(this.doc.id, hash))}`)\n\n await this.addSchema(hash, def, {\n type: `{ ${properties.join(';')} }`,\n schema: (ref) =>\n this.pure(\n `l.record<${key}, ${ref.typeName}>(${key}, $nsid, ${objectSchema})`,\n ),\n objectUtils: true,\n validationUtils: true,\n })\n }\n\n private async addObject(hash: string, def: LexiconObject) {\n const objectSchema = await this.compileObjectSchema(def)\n\n const properties = await this.compilePropertiesTypes(def)\n properties.unshift(`$type?: ${JSON.stringify(l.$type(this.doc.id, hash))}`)\n\n await this.addSchema(hash, def, {\n type: `{ ${properties.join(';')} }`,\n schema: (ref) =>\n this.pure(\n `l.typedObject<${ref.typeName}>($nsid, ${JSON.stringify(hash)}, ${objectSchema})`,\n ),\n objectUtils: true,\n validationUtils: true,\n })\n }\n\n private async addToken(hash: string, def: LexiconToken) {\n await this.addSchema(hash, def, {\n schema: this.pure(`l.token($nsid, ${JSON.stringify(hash)})`),\n type: JSON.stringify(l.$type(this.doc.id, hash)),\n validationUtils: true,\n })\n }\n\n private async addArray(hash: string, def: LexiconArray) {\n // @TODO It could be nice to expose the array item type as a separate type.\n // This was not done (yet) as there is no easy way to name it to avoid\n // collisions.\n\n const itemSchema = await this.compileContainedSchema(def.items)\n const options = stringifyOptionalOptions(def, [\n 'type',\n 'description',\n 'items',\n ])\n\n await this.addSchema(hash, def, {\n type: `(${await this.compileContainedType(def.items)})[]`,\n // @NOTE Not using compileArraySchema to allow specifying the generic\n // parameter to l.array<>.\n schema: (ref) =>\n this.pure(\n `l.array<${ref.typeName}[number]>(${itemSchema}, ${options})`,\n ),\n validationUtils: true,\n })\n }\n\n private async addSchema(\n hash: string,\n def: { description?: string },\n {\n type,\n schema,\n objectUtils,\n validationUtils,\n }: {\n type?: string | ((ref: ResolvedRef) => string)\n schema?: string | ((ref: ResolvedRef) => string)\n objectUtils?: boolean\n validationUtils?: boolean\n },\n ): Promise<ResolvedRef> {\n const ref = await this.refResolver.resolveLocal(hash)\n const pub = getPublicIdentifiers(hash)\n\n // Fool-proofing\n assert(isSafeIdentifier(ref.varName), 'Expected safe type identifier')\n assert(isSafeIdentifier(ref.typeName), 'Expected safe type identifier')\n assert(isSafeIdentifier(pub.typeName), 'Expected safe type identifier')\n\n if (type) {\n const typeStmt = this.file.addTypeAlias({\n name: ref.typeName,\n type: typeof type === 'function' ? type(ref) : type,\n })\n\n addJsDoc(typeStmt, def)\n\n this.file.addExportDeclaration({\n isTypeOnly: true,\n namedExports: [\n {\n name: ref.typeName,\n alias: ref.typeName === pub.typeName ? undefined : pub.typeName,\n },\n ],\n })\n }\n\n if (schema) {\n const constStmt = this.file.addVariableStatement({\n declarationKind: VariableDeclarationKind.Const,\n declarations: [\n {\n name: ref.varName,\n initializer: typeof schema === 'function' ? schema(ref) : schema,\n },\n ],\n })\n\n addJsDoc(constStmt, def)\n\n this.file.addExportDeclaration({\n namedExports: [\n {\n name: ref.varName,\n alias:\n ref.varName === pub.varName\n ? undefined\n : isSafeIdentifier(pub.varName)\n ? pub.varName\n : JSON.stringify(pub.varName),\n },\n ],\n })\n }\n\n if (hash === 'main' && objectUtils) {\n this.addUtils({\n $isTypeOf: markPure(`${ref.varName}.isTypeOf.bind(${ref.varName})`),\n $build: markPure(`${ref.varName}.build.bind(${ref.varName})`),\n $type: markPure(`${ref.varName}.$type`),\n })\n }\n\n if (hash === 'main' && validationUtils) {\n this.addUtils({\n $assert: markPure(`${ref.varName}.assert.bind(${ref.varName})`),\n $check: markPure(`${ref.varName}.check.bind(${ref.varName})`),\n $maybe: markPure(`${ref.varName}.maybe.bind(${ref.varName})`),\n $parse: markPure(`${ref.varName}.parse.bind(${ref.varName})`),\n $validate: markPure(`${ref.varName}.validate.bind(${ref.varName})`),\n })\n }\n\n return ref\n }\n\n private async compilePayload(def: LexiconPayload | undefined) {\n if (!def) return this.pure(`l.payload()`)\n\n const encodedEncoding = JSON.stringify(def.encoding)\n if (def.schema) {\n const bodySchema = await this.compileBodySchema(def.schema)\n return this.pure(`l.payload(${encodedEncoding}, ${bodySchema})`)\n } else {\n return this.pure(`l.payload(${encodedEncoding})`)\n }\n }\n\n private async compileBodySchema(\n def?: LexiconRef | LexiconRefUnion | LexiconObject,\n ): Promise<string> {\n if (!def) return 'undefined'\n if (def.type === 'object') return this.compileObjectSchema(def)\n return this.compileContainedSchema(def)\n }\n\n private async compileParamsSchema(def: undefined | LexiconParameters) {\n if (!def) return this.pure(`l.params({})`)\n\n const properties = await this.compilePropertiesSchemas(def)\n const options = stringifyOptionalOptions(def, [\n 'type',\n 'description',\n 'properties',\n ])\n return this.pure(`l.params({${properties.join(',')}}, ${options})`)\n }\n\n private async compileErrors(defs?: readonly LexiconError[]) {\n if (!defs?.length) return ''\n return JSON.stringify(defs.map((d) => d.name))\n }\n\n private async compileObjectSchema(def: LexiconObject): Promise<string> {\n const properties = await this.compilePropertiesSchemas(def)\n const options = stringifyOptionalOptions(def, [\n 'type',\n 'description',\n 'properties',\n ])\n return this.pure(`l.object({${properties.join(',')}}, ${options})`)\n }\n\n private async compilePropertiesSchemas(options: {\n properties: Record<string, LexiconArray | LexiconArrayItems>\n required?: readonly string[]\n }) {\n for (const prop of options.required || []) {\n if (!Object.hasOwn(options.properties, prop)) {\n throw new Error(`Required property \"${prop}\" not found in properties`)\n }\n }\n\n return Promise.all(\n Object.entries(options.properties).map(\n this.compilePropertyEntrySchema,\n this,\n ),\n )\n }\n\n private async compilePropertiesTypes(options: {\n properties: Record<string, LexiconArray | LexiconArrayItems>\n required?: readonly string[]\n nullable?: readonly string[]\n }) {\n return Promise.all(\n Object.entries(options.properties).map((entry) => {\n return this.compilePropertyEntryType(entry, options)\n }),\n )\n }\n\n private async compilePropertyEntrySchema([key, def]: [\n string,\n LexiconArray | LexiconArrayItems,\n ]) {\n const name = JSON.stringify(key)\n const schema = await this.compileContainedSchema(def)\n return `${name}:${schema}`\n }\n\n private async compilePropertyEntryType(\n [key, def]: [string, LexiconArray | LexiconArrayItems],\n options: {\n required?: readonly string[]\n nullable?: readonly string[]\n },\n ) {\n const isNullable = options.nullable?.includes(key)\n const isRequired = options.required?.includes(key)\n\n const optional = isRequired ? '' : '?'\n const append = isNullable ? ' | null' : ''\n\n const jsDoc = compileLeadingTrivia(def.description) || ''\n const name = JSON.stringify(key)\n const type = await this.compileContainedType(def)\n\n return `${jsDoc}${name}${optional}:${type}${append}`\n }\n\n private async compileContainedSchema(\n def: LexiconArray | LexiconArrayItems,\n ): Promise<string> {\n switch (def.type) {\n case 'unknown':\n return this.compileUnknownSchema(def)\n case 'boolean':\n return this.compileBooleanSchema(def)\n case 'integer':\n return this.compileIntegerSchema(def)\n case 'string':\n return this.compileStringSchema(def)\n case 'bytes':\n return this.compileBytesSchema(def)\n case 'blob':\n return this.compileBlobSchema(def)\n case 'cid-link':\n return this.compileCidLinkSchema(def)\n case 'ref':\n return this.compileRefSchema(def)\n case 'union':\n return this.compileRefUnionSchema(def)\n case 'array':\n return this.compileArraySchema(def)\n default:\n // @ts-expect-error\n throw new Error(`Unsupported def type: ${def.type}`)\n }\n }\n\n private async compileContainedType(\n def: LexiconArray | LexiconArrayItems,\n ): Promise<string> {\n switch (def.type) {\n case 'unknown':\n return this.compileUnknownType(def)\n case 'boolean':\n return this.compileBooleanType(def)\n case 'integer':\n return this.compileIntegerType(def)\n case 'string':\n return this.compileStringType(def)\n case 'bytes':\n return this.compileBytesType(def)\n case 'blob':\n return this.compileBlobType(def)\n case 'cid-link':\n return this.compileCidLinkType(def)\n case 'ref':\n return this.compileRefType(def)\n case 'union':\n return this.compileRefUnionType(def)\n case 'array':\n return this.compileArrayType(def)\n default:\n // @ts-expect-error\n throw new Error(`Unsupported def type: ${def.type}`)\n }\n }\n\n private async compileArraySchema(def: LexiconArray): Promise<string> {\n const itemSchema = await this.compileContainedSchema(def.items)\n const options = stringifyOptionalOptions(def, [\n 'type',\n 'description',\n 'items',\n ])\n return this.pure(`l.array(${itemSchema}, ${options})`)\n }\n\n private async compileArrayType(def: LexiconArray): Promise<string> {\n return `(${await this.compileContainedType(def.items)})[]`\n }\n\n private async compileUnknownSchema(_def: LexiconUnknown): Promise<string> {\n return this.pure(`l.unknownObject()`)\n }\n\n private async compileUnknownType(_def: LexiconUnknown): Promise<string> {\n return `l.UnknownObject`\n }\n\n private async compileBooleanSchema(def: LexiconBoolean): Promise<string> {\n if (hasConst(def)) return this.compileConstSchema(def)\n\n const options = stringifyOptionalOptions(def, ['type', 'description'])\n return this.pure(`l.boolean(${options})`)\n }\n\n private async compileBooleanType(def: LexiconBoolean): Promise<string> {\n if (hasConst(def)) return this.compileConstType(def)\n return 'boolean'\n }\n\n private async compileIntegerSchema(def: LexiconInteger): Promise<string> {\n if (hasConst(def)) {\n const schema = l.integer(def)\n assert(\n schema.check(def.const),\n `Integer const ${def.const} is out of bounds`,\n )\n }\n\n if (hasEnum(def)) {\n const schema = l.integer(def)\n for (const val of def.enum) {\n assert(schema.check(val), `Integer enum value ${val} is out of bounds`)\n }\n }\n\n if (hasConst(def)) return this.compileConstSchema(def)\n if (hasEnum(def)) return this.compileEnumSchema(def)\n\n const options = stringifyOptionalOptions(def, ['type', 'description'])\n return this.pure(`l.integer(${options})`)\n }\n\n private async compileIntegerType(def: LexiconInteger): Promise<string> {\n if (hasConst(def)) return this.compileConstType(def)\n if (hasEnum(def)) return this.compileEnumType(def)\n\n return 'number'\n }\n\n private async compileStringSchema(def: LexiconString): Promise<string> {\n if (hasConst(def)) {\n const schema = l.string(def)\n assert(\n schema.check(def.const),\n `String const \"${def.const}\" does not match format`,\n )\n } else if (hasEnum(def)) {\n const schema = l.string(def)\n for (const val of def.enum) {\n assert(\n schema.check(val),\n `String enum value \"${val}\" does not match format`,\n )\n }\n }\n\n if (hasConst(def)) return this.compileConstSchema(def)\n if (hasEnum(def)) return this.compileEnumSchema(def)\n\n const options = stringifyOptionalOptions(def, ['type', 'description'])\n return this.pure(`l.string(${options})`)\n }\n\n private async compileStringType(def: LexiconString): Promise<string> {\n if (hasConst(def)) return this.compileConstType(def)\n if (hasEnum(def)) return this.compileEnumType(def)\n\n switch (def.format) {\n case 'datetime':\n return 'l.Datetime'\n case 'uri':\n return 'l.Uri'\n case 'at-uri':\n return 'l.AtUri'\n case 'did':\n return 'l.Did'\n case 'handle':\n return 'l.Handle'\n case 'at-identifier':\n return 'l.AtIdentifier'\n case 'nsid':\n return 'l.Nsid'\n }\n\n if (def.knownValues?.length) {\n return (\n def.knownValues.map((v) => JSON.stringify(v)).join(' | ') +\n ' | l.UnknownString'\n )\n }\n\n return 'string'\n }\n\n private async compileBytesSchema(def: LexiconBytes): Promise<string> {\n const options = stringifyOptionalOptions(def, ['type', 'description'])\n return this.pure(`l.bytes(${options})`)\n }\n\n private async compileBytesType(_def: LexiconBytes): Promise<string> {\n return 'Uint8Array'\n }\n\n private async compileBlobSchema(def: LexiconBlob): Promise<string> {\n const opts = this.options.allowLegacyBlobs\n ? { ...def, allowLegacy: true }\n : def\n const options = stringifyOptionalOptions(opts, ['type', 'description'])\n return this.pure(`l.blob(${options})`)\n }\n\n private async compileBlobType(_def: LexiconBlob): Promise<string> {\n return this.options.allowLegacyBlobs\n ? 'l.BlobRef | l.LegacyBlobRef'\n : 'l.BlobRef'\n }\n\n private async compileCidLinkSchema(def: LexiconCid): Promise<string> {\n const options = stringifyOptionalOptions(def, ['type', 'description'])\n return this.pure(`l.cidLink(${options})`)\n }\n\n private async compileCidLinkType(_def: LexiconCid): Promise<string> {\n return 'l.CID'\n }\n\n private async compileRefSchema(def: LexiconRef): Promise<string> {\n const { varName, typeName } = await this.refResolver.resolve(def.ref)\n // @NOTE \"as any\" is needed in schemas with circular refs as TypeScript\n // cannot infer the type of a value that depends on its initializer type\n return this.pure(`l.ref<${typeName}>((() => ${varName}) as any)`)\n }\n\n private async compileRefType(def: LexiconRef): Promise<string> {\n const ref = await this.refResolver.resolve(def.ref)\n return ref.typeName\n }\n\n private async compileRefUnionSchema(def: LexiconRefUnion): Promise<string> {\n if (def.refs.length === 0 && def.closed) {\n return this.pure(`l.never()`)\n }\n\n const refs = await Promise.all(\n def.refs.map(async (ref: string) => {\n const { varName, typeName } = await this.refResolver.resolve(ref)\n // @NOTE \"as any\" is needed in schemas with circular refs as TypeScript\n // cannot infer the type of a value that depends on its initializer type\n return this.pure(`l.typedRef<${typeName}>((() => ${varName}) as any)`)\n }),\n )\n\n return this.pure(\n `l.typedUnion([${refs.join(',')}], ${def.closed ?? false})`,\n )\n }\n\n private async compileRefUnionType(def: LexiconRefUnion): Promise<string> {\n const types = await Promise.all(\n def.refs.map(async (ref) => {\n const { typeName } = await this.refResolver.resolve(ref)\n return `l.TypedRef<${typeName}>`\n }),\n )\n if (!def.closed) types.push('l.TypedObject')\n return types.join(' | ') || 'never'\n }\n\n private async compileConstSchema<\n T extends null | number | string | boolean,\n >(def: { const: T; enum?: readonly T[] }): Promise<string> {\n if (hasEnum(def) && !def.enum.includes(def.const)) {\n return this.pure(`l.never()`)\n }\n\n return this.pure(`l.literal(${JSON.stringify(def.const)})`)\n }\n\n private async compileConstType<\n T extends null | number | string | boolean,\n >(def: { const: T; enum?: readonly T[] }): Promise<string> {\n if (hasEnum(def) && !def.enum.includes(def.const)) {\n return 'never'\n }\n return JSON.stringify(def.const)\n }\n\n private async compileEnumSchema<T extends null | number | string>(def: {\n enum: readonly T[]\n }): Promise<string> {\n if (def.enum.length === 0) {\n return this.pure(`l.never()`)\n }\n if (def.enum.length === 1) {\n return this.pure(`l.literal(${JSON.stringify(def.enum[0])})`)\n }\n return this.pure(`l.enum(${JSON.stringify(def.enum)})`)\n }\n\n private async compileEnumType<T extends null | number | string>(def: {\n enum: readonly T[]\n }): Promise<string> {\n return def.enum.map((v) => JSON.stringify(v)).join(' | ') || 'never'\n }\n}\n\ntype ParsedDescription = {\n description: string\n deprecated: boolean | string\n}\n\nfunction parseDescription(description: string): ParsedDescription {\n if (/deprecated/i.test(description)) {\n const deprecationMatch = description.match(\n /(\\s*deprecated\\s*(?:--?|:)?\\s*([^-]*)(?:-+)?)/i,\n )\n if (deprecationMatch) {\n const [, match, deprecationNotice] = deprecationMatch\n return {\n description: description.replace(match, '').trim(),\n deprecated: deprecationNotice?.trim() || true,\n }\n } else {\n return {\n description: description.trim(),\n deprecated: true,\n }\n }\n }\n\n return {\n description: description.trim(),\n deprecated: false,\n }\n}\n\nfunction compileLeadingTrivia(description?: string) {\n if (!description) return undefined\n return `\\n\\n/**${compileJsDoc(description).replaceAll('\\n', '\\n * ')}\\n */\\n`\n}\n\nfunction addJsDoc(\n declaration: { addJsDoc: (text: string) => void },\n def?: { description?: string },\n) {\n if (def?.description) {\n declaration.addJsDoc(compileJsDoc(def.description))\n }\n}\n\nfunction compileJsDoc(description: string) {\n const parsed = parseDescription(description)\n return `\\n${parsed.description}${\n !parsed.deprecated\n ? ''\n : (parsed.description ? '\\n\\n' : '') +\n (parsed.deprecated === true\n ? '@deprecated'\n : `@deprecated ${parsed.deprecated}`)\n }`\n}\n\nfunction stringifyOptionalOptions<O extends Record<string, unknown>>(\n obj: O,\n omit?: (keyof O)[],\n) {\n const filtered = Object.entries(obj).filter(([k]) => !omit?.includes(k))\n return filtered.length ? JSON.stringify(Object.fromEntries(filtered)) : ''\n}\n\nfunction hasConst<T extends { const?: unknown }>(\n def: T,\n): def is T & { const: NonNullable<T['const']> } {\n return def.const != null\n}\n\nfunction hasEnum<T extends { enum?: readonly unknown[] }>(\n def: T,\n): def is T & { enum: unknown[] } {\n return def.enum != null\n}\n\nfunction markPure<T extends string>(v: T): `/*#__PURE__*/ ${T}` {\n return `/*#__PURE__*/ ${v}`\n}\n"]}
@@ -0,0 +1,11 @@
1
+ import { LexiconIterableIndexer } from '@atproto/lex-document';
2
+ export type LexiconDirectoryIndexerOptions = ReadLexiconsOptions;
3
+ export declare class LexiconDirectoryIndexer extends LexiconIterableIndexer {
4
+ constructor(options: LexiconDirectoryIndexerOptions);
5
+ }
6
+ type ReadLexiconsOptions = {
7
+ lexicons: string | string[];
8
+ ignoreErrors?: boolean;
9
+ };
10
+ export {};
11
+ //# sourceMappingURL=lexicon-directory-indexer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lexicon-directory-indexer.d.ts","sourceRoot":"","sources":["../src/lexicon-directory-indexer.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,sBAAsB,EAEvB,MAAM,uBAAuB,CAAA;AAE9B,MAAM,MAAM,8BAA8B,GAAG,mBAAmB,CAAA;AAEhE,qBAAa,uBAAwB,SAAQ,sBAAsB;gBACrD,OAAO,EAAE,8BAA8B;CAGpD;AAED,KAAK,mBAAmB,GAAG;IACzB,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAC3B,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LexiconDirectoryIndexer = void 0;
4
+ const promises_1 = require("node:fs/promises");
5
+ const node_path_1 = require("node:path");
6
+ const lex_document_1 = require("@atproto/lex-document");
7
+ class LexiconDirectoryIndexer extends lex_document_1.LexiconIterableIndexer {
8
+ constructor(options) {
9
+ super(readLexicons(options));
10
+ }
11
+ }
12
+ exports.LexiconDirectoryIndexer = LexiconDirectoryIndexer;
13
+ async function* readLexicons(options) {
14
+ const dirs = Array.isArray(options.lexicons)
15
+ ? options.lexicons
16
+ : [options.lexicons];
17
+ for (const dir of dirs) {
18
+ for await (const filePath of listFiles(dir)) {
19
+ if (filePath.endsWith('.json')) {
20
+ try {
21
+ const data = await (0, promises_1.readFile)(filePath, 'utf8');
22
+ yield lex_document_1.lexiconDocumentSchema.parse(JSON.parse(data));
23
+ }
24
+ catch (cause) {
25
+ const message = `Error parsing lexicon document ${filePath}`;
26
+ if (options.ignoreErrors)
27
+ console.error(`${message}:`, cause);
28
+ else
29
+ throw new Error(message, { cause });
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ async function* listFiles(dir) {
36
+ const dirents = await (0, promises_1.readdir)(dir, { withFileTypes: true }).catch((err) => {
37
+ if (err?.code === 'ENOENT')
38
+ return [];
39
+ throw err;
40
+ });
41
+ for (const dirent of dirents) {
42
+ const res = (0, node_path_1.join)(dir, dirent.name);
43
+ if (dirent.isDirectory()) {
44
+ yield* listFiles(res);
45
+ }
46
+ else if (dirent.isFile() || dirent.isSymbolicLink()) {
47
+ yield res;
48
+ }
49
+ }
50
+ }
51
+ //# sourceMappingURL=lexicon-directory-indexer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lexicon-directory-indexer.js","sourceRoot":"","sources":["../src/lexicon-directory-indexer.ts"],"names":[],"mappings":";;;AAAA,+CAAoD;AACpD,yCAAgC;AAChC,wDAI8B;AAI9B,MAAa,uBAAwB,SAAQ,qCAAsB;IACjE,YAAY,OAAuC;QACjD,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAA;IAC9B,CAAC;CACF;AAJD,0DAIC;AAOD,KAAK,SAAS,CAAC,CAAC,YAAY,CAC1B,OAA4B;IAE5B,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;QAC1C,CAAC,CAAC,OAAO,CAAC,QAAQ;QAClB,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;IACtB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5C,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC/B,IAAI,CAAC;oBACH,MAAM,IAAI,GAAG,MAAM,IAAA,mBAAQ,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;oBAC7C,MAAM,oCAAqB,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;gBACrD,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,MAAM,OAAO,GAAG,kCAAkC,QAAQ,EAAE,CAAA;oBAC5D,IAAI,OAAO,CAAC,YAAY;wBAAE,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,EAAE,KAAK,CAAC,CAAA;;wBACxD,MAAM,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;gBAC1C,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC;AAED,KAAK,SAAS,CAAC,CAAC,SAAS,CAAC,GAAW;IACnC,MAAM,OAAO,GAAG,MAAM,IAAA,kBAAO,EAAC,GAAG,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QACxE,IAAK,GAAW,EAAE,IAAI,KAAK,QAAQ;YAAE,OAAO,EAAE,CAAA;QAC9C,MAAM,GAAG,CAAA;IACX,CAAC,CAAC,CAAA;IACF,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,IAAA,gBAAI,EAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,CAAA;QAClC,IAAI,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC;YACzB,KAAK,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;QACvB,CAAC;aAAM,IAAI,MAAM,CAAC,MAAM,EAAE,IAAI,MAAM,CAAC,cAAc,EAAE,EAAE,CAAC;YACtD,MAAM,GAAG,CAAA;QACX,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { readFile, readdir } from 'node:fs/promises'\nimport { join } from 'node:path'\nimport {\n LexiconDocument,\n LexiconIterableIndexer,\n lexiconDocumentSchema,\n} from '@atproto/lex-document'\n\nexport type LexiconDirectoryIndexerOptions = ReadLexiconsOptions\n\nexport class LexiconDirectoryIndexer extends LexiconIterableIndexer {\n constructor(options: LexiconDirectoryIndexerOptions) {\n super(readLexicons(options))\n }\n}\n\ntype ReadLexiconsOptions = {\n lexicons: string | string[]\n ignoreErrors?: boolean\n}\n\nasync function* readLexicons(\n options: ReadLexiconsOptions,\n): AsyncGenerator<LexiconDocument, void, unknown> {\n const dirs = Array.isArray(options.lexicons)\n ? options.lexicons\n : [options.lexicons]\n for (const dir of dirs) {\n for await (const filePath of listFiles(dir)) {\n if (filePath.endsWith('.json')) {\n try {\n const data = await readFile(filePath, 'utf8')\n yield lexiconDocumentSchema.parse(JSON.parse(data))\n } catch (cause) {\n const message = `Error parsing lexicon document ${filePath}`\n if (options.ignoreErrors) console.error(`${message}:`, cause)\n else throw new Error(message, { cause })\n }\n }\n }\n }\n}\n\nasync function* listFiles(dir: string): AsyncGenerator<string> {\n const dirents = await readdir(dir, { withFileTypes: true }).catch((err) => {\n if ((err as any)?.code === 'ENOENT') return []\n throw err\n })\n for (const dirent of dirents) {\n const res = join(dir, dirent.name)\n if (dirent.isDirectory()) {\n yield* listFiles(res)\n } else if (dirent.isFile() || dirent.isSymbolicLink()) {\n yield res\n }\n }\n}\n"]}
@@ -0,0 +1,48 @@
1
+ import { SourceFile } from 'ts-morph';
2
+ import { LexiconDocument, LexiconIndexer } from '@atproto/lex-document';
3
+ export type ResolvedRef = {
4
+ varName: string;
5
+ typeName: string;
6
+ };
7
+ /**
8
+ * Utility class to resolve lexicon references to TypeScript identifiers,
9
+ * generating "import" statements as needed.
10
+ */
11
+ export declare class RefResolver {
12
+ #private;
13
+ private doc;
14
+ private file;
15
+ private indexer;
16
+ constructor(doc: LexiconDocument, file: SourceFile, indexer: LexiconIndexer);
17
+ readonly resolve: (ref: string) => Promise<ResolvedRef>;
18
+ private nextSafeDefinitionIdentifier;
19
+ /**
20
+ * @note The returned `typeName` and `varName` are *both* guaranteed to be
21
+ * valid TypeScript identifiers.
22
+ */
23
+ readonly resolveLocal: (hash: string) => Promise<ResolvedRef>;
24
+ /**
25
+ * @note Since this is a memoized function, and is used to generate the name
26
+ * of local variables, we should avoid returning different results for
27
+ * similar, but non strictly equal, inputs (eg. normalized / non-normalized).
28
+ * @see {@link resolve}
29
+ */
30
+ private readonly resolveExternal;
31
+ private getNsIdentifier;
32
+ private computeSafeNamespaceIdentifierFor;
33
+ private isConflictingIdentifier;
34
+ private conflictsWithKeywords;
35
+ private conflictsWithUtils;
36
+ private conflictsWithLocalDefs;
37
+ private conflictsWithLocalDeclarations;
38
+ private conflictsWithImports;
39
+ }
40
+ /**
41
+ * Generates predictable public identifiers for a given definition hash.
42
+ *
43
+ * @note The returned `typeName` is guaranteed to be a valid TypeScript
44
+ * identifier. `varName` may not be a valid identifier (eg. if the hash contains
45
+ * unsafe characters), and may need to be accessed using string indexing.
46
+ */
47
+ export declare function getPublicIdentifiers(hash: string): ResolvedRef;
48
+ //# sourceMappingURL=ref-resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ref-resolver.d.ts","sourceRoot":"","sources":["../src/ref-resolver.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AACrC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAUvE,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;;GAGG;AACH,qBAAa,WAAW;;IAEpB,OAAO,CAAC,GAAG;IACX,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,OAAO;gBAFP,GAAG,EAAE,eAAe,EACpB,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,cAAc;IAGjC,SAAgB,OAAO,QACT,MAAM,KAAG,OAAO,CAAC,WAAW,CAAC,CAW1C;IAGD,OAAO,CAAC,4BAA4B;IASpC;;;OAGG;IACH,SAAgB,YAAY,SACb,MAAM,KAAG,OAAO,CAAC,WAAW,CAAC,CAsD3C;IAED;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,eAAe,CA6B/B;IAED,OAAO,CAAC,eAAe;IAiBvB,OAAO,CAAC,iCAAiC;IAazC,OAAO,CAAC,uBAAuB;IAU/B,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,sBAAsB;IAkB9B,OAAO,CAAC,8BAA8B;IActC,OAAO,CAAC,oBAAoB;CAe7B;AAoBD;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAU9D"}
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RefResolver = void 0;
4
+ exports.getPublicIdentifiers = getPublicIdentifiers;
5
+ const tslib_1 = require("tslib");
6
+ const node_assert_1 = tslib_1.__importDefault(require("node:assert"));
7
+ const node_path_1 = require("node:path");
8
+ const ts_lang_js_1 = require("./ts-lang.js");
9
+ const util_js_1 = require("./util.js");
10
+ /**
11
+ * Utility class to resolve lexicon references to TypeScript identifiers,
12
+ * generating "import" statements as needed.
13
+ */
14
+ class RefResolver {
15
+ doc;
16
+ file;
17
+ indexer;
18
+ constructor(doc, file, indexer) {
19
+ this.doc = doc;
20
+ this.file = file;
21
+ this.indexer = indexer;
22
+ }
23
+ resolve = (0, util_js_1.memoize)(async (ref) => {
24
+ const [nsid, hash = 'main'] = ref.split('#');
25
+ if (nsid === '' || nsid === this.doc.id) {
26
+ return this.resolveLocal(hash);
27
+ }
28
+ else {
29
+ // @NOTE: Normalize (#main fragment) to ensure proper memoization
30
+ const fullRef = `${nsid}#${hash}`;
31
+ return this.resolveExternal(fullRef);
32
+ }
33
+ });
34
+ #defCounters = new Map();
35
+ nextSafeDefinitionIdentifier(safeIdentifier) {
36
+ const count = this.#defCounters.get(safeIdentifier) ?? 0;
37
+ this.#defCounters.set(safeIdentifier, count + 1);
38
+ // @NOTE We don't need to check against local declarations in the file here
39
+ // since we are using a naming system that should guarantee no other
40
+ // identifier has a <safeIdentifier>$<number> format.
41
+ return `${safeIdentifier}$${count}`;
42
+ }
43
+ /**
44
+ * @note The returned `typeName` and `varName` are *both* guaranteed to be
45
+ * valid TypeScript identifiers.
46
+ */
47
+ resolveLocal = (0, util_js_1.memoize)(async (hash) => {
48
+ const hashes = Object.keys(this.doc.defs);
49
+ if (!hashes.includes(hash)) {
50
+ throw new Error(`Definition ${hash} not found in ${this.doc.id}`);
51
+ }
52
+ // Because we are using predictable "public" identifiers for type names,
53
+ // we need to ensure there are no conflicts between different definitions
54
+ // in the same lexicon document.
55
+ //
56
+ // @NOTE It should be possible to implement a way to generate
57
+ // non-conflicting type names for all public (type) identifiers in a
58
+ // project. However, this would add a lot of complexity to the code
59
+ // generation process, and the likelihood of such conflicts happening in
60
+ // practice is very low, so we opt for a simpler approach of just throwing
61
+ // an error if a conflict is detected.
62
+ const pub = getPublicIdentifiers(hash);
63
+ for (const otherHash of hashes) {
64
+ if (otherHash === hash)
65
+ continue;
66
+ const otherPub = getPublicIdentifiers(otherHash);
67
+ if (otherPub.typeName === pub.typeName) {
68
+ throw new Error(`Conflicting type names for definitions #${hash} and #${otherHash} in ${this.doc.id}`);
69
+ }
70
+ }
71
+ // Try to keep and identifier that resembles the original hash as identifier
72
+ const safeIdentifier = asSafeDefinitionIdentifier(hash);
73
+ // If the safe identifier is not conflicting with other definition names,
74
+ // or reserved words, we can use it as-is. Otherwise, we need to generate
75
+ // a unique safe identifier.
76
+ const varName = safeIdentifier
77
+ ? !hashes.some((otherHash) => {
78
+ if (otherHash === hash)
79
+ return false;
80
+ const otherIdentifier = asSafeDefinitionIdentifier(otherHash);
81
+ return otherIdentifier === safeIdentifier;
82
+ })
83
+ ? // Safe identifier can be used as-is as it does not conflict with
84
+ // other definition names
85
+ safeIdentifier
86
+ : // In order to keep identifiers stable, we use the safe identifier
87
+ // as base, and append a counter to avoid conflicts
88
+ this.nextSafeDefinitionIdentifier(safeIdentifier)
89
+ : // hash only contained unsafe characters, generate a safe one
90
+ this.nextSafeDefinitionIdentifier('def');
91
+ const typeName = (0, util_js_1.ucFirst)(varName);
92
+ (0, node_assert_1.default)(varName !== typeName, 'Variable and type name should be different');
93
+ return { varName, typeName };
94
+ });
95
+ /**
96
+ * @note Since this is a memoized function, and is used to generate the name
97
+ * of local variables, we should avoid returning different results for
98
+ * similar, but non strictly equal, inputs (eg. normalized / non-normalized).
99
+ * @see {@link resolve}
100
+ */
101
+ resolveExternal = (0, util_js_1.memoize)(async (fullRef) => {
102
+ const [nsid, hash] = fullRef.split('#');
103
+ const moduleSpecifier = `${(0, util_js_1.asRelativePath)(this.file.getDirectoryPath(), (0, node_path_1.join)('/', ...nsid.split('.')))}.defs.js`;
104
+ // Lets first make sure the referenced lexicon exists
105
+ const srcDoc = await this.indexer.get(nsid);
106
+ const srcDef = Object.hasOwn(srcDoc.defs, hash) ? srcDoc.defs[hash] : null;
107
+ if (!srcDef) {
108
+ throw new Error(`Missing def "${hash}" in "${nsid}" (referenced from ${this.doc.id})`);
109
+ }
110
+ // import * as <nsIdentifier> from './<moduleSpecifier>'
111
+ const nsIdentifier = this.getNsIdentifier(nsid, moduleSpecifier);
112
+ const publicIds = getPublicIdentifiers(hash);
113
+ return {
114
+ varName: (0, ts_lang_js_1.isSafeIdentifier)(publicIds.varName)
115
+ ? `${nsIdentifier}.${publicIds.varName}`
116
+ : `${nsIdentifier}[${JSON.stringify(publicIds.varName)}]`,
117
+ typeName: `${nsIdentifier}.${publicIds.typeName}`,
118
+ };
119
+ });
120
+ getNsIdentifier(nsid, moduleSpecifier) {
121
+ const namespaceImportDeclaration = this.file.getImportDeclaration((imp) => !imp.isTypeOnly() &&
122
+ imp.getModuleSpecifierValue() === moduleSpecifier &&
123
+ imp.getNamespaceImport() != null) ||
124
+ this.file.addImportDeclaration({
125
+ moduleSpecifier,
126
+ namespaceImport: this.computeSafeNamespaceIdentifierFor(nsid),
127
+ });
128
+ return namespaceImportDeclaration.getNamespaceImport().getText();
129
+ }
130
+ #nsIdentifiersCounters = new Map();
131
+ computeSafeNamespaceIdentifierFor(nsid) {
132
+ const baseName = nsidToIdentifier(nsid) || 'NS';
133
+ let name = baseName;
134
+ while (this.isConflictingIdentifier(name)) {
135
+ const count = this.#nsIdentifiersCounters.get(baseName) ?? 0;
136
+ this.#nsIdentifiersCounters.set(baseName, count + 1);
137
+ name = `${baseName}$$${count}`;
138
+ }
139
+ return name;
140
+ }
141
+ isConflictingIdentifier(name) {
142
+ return (this.conflictsWithKeywords(name) ||
143
+ this.conflictsWithUtils(name) ||
144
+ this.conflictsWithLocalDefs(name) ||
145
+ this.conflictsWithLocalDeclarations(name) ||
146
+ this.conflictsWithImports(name));
147
+ }
148
+ conflictsWithKeywords(name) {
149
+ return (0, ts_lang_js_1.isReservedWord)(name);
150
+ }
151
+ conflictsWithUtils(name) {
152
+ // Do not allow "Main" as imported ns identifier since it has a special
153
+ // meaning in the context of lexicon definitions.
154
+ if (name === 'Main')
155
+ return true;
156
+ // When "useRecordExport" returns true, an export named "Record" will be
157
+ // used in addition to the hash named export. So we need to make sure both
158
+ // names are not conflicting with local variables.
159
+ if (name === 'Record')
160
+ return true;
161
+ // Utility functions generated for lexicon schemas are prefixed with "$"
162
+ return name.startsWith('$');
163
+ }
164
+ conflictsWithLocalDefs(name) {
165
+ return Object.keys(this.doc.defs).some((hash) => {
166
+ const identifier = (0, util_js_1.toCamelCase)(hash);
167
+ // A safe identifier will be generated, no risk of conflict.
168
+ if (!identifier)
169
+ return false;
170
+ // The imported name conflicts with a local definition name
171
+ if (identifier === name || `_${identifier}` === name)
172
+ return true;
173
+ // The imported name conflicts with the type name of a local definition
174
+ const typeName = (0, util_js_1.ucFirst)(identifier);
175
+ if (typeName === name || `_${typeName}` === name)
176
+ return true;
177
+ return false;
178
+ });
179
+ }
180
+ conflictsWithLocalDeclarations(name) {
181
+ return (this.file.getVariableDeclarations().some((v) => v.getName() === name) ||
182
+ this.file
183
+ .getVariableStatements()
184
+ .some((vs) => vs.getDeclarations().some((d) => d.getName() === name)) ||
185
+ this.file.getTypeAliases().some((t) => t.getName() === name) ||
186
+ this.file.getInterfaces().some((i) => i.getName() === name) ||
187
+ this.file.getClasses().some((c) => c.getName() === name) ||
188
+ this.file.getFunctions().some((f) => f.getName() === name) ||
189
+ this.file.getEnums().some((e) => e.getName() === name));
190
+ }
191
+ conflictsWithImports(name) {
192
+ return this.file.getImportDeclarations().some((imp) =>
193
+ // import name from '...'
194
+ imp.getDefaultImport()?.getText() === name ||
195
+ // import * as name from '...'
196
+ imp.getNamespaceImport()?.getText() === name ||
197
+ imp.getNamedImports().some((named) =>
198
+ // import { name } from '...'
199
+ // import { foo as name } from '...'
200
+ (named.getAliasNode()?.getText() ?? named.getName()) === name));
201
+ }
202
+ }
203
+ exports.RefResolver = RefResolver;
204
+ /**
205
+ * @see {@link https://atproto.com/specs/nsid NSID syntax spec}
206
+ */
207
+ function nsidToIdentifier(nsid) {
208
+ const parts = nsid.split('.');
209
+ // By default, try to keep only to the last two segments of the NSID as
210
+ // contextual information. If those do not form a safe identifier (typically
211
+ // because they start with a digit), try with more segments until we reach the
212
+ // full NSID.
213
+ for (let i = 2; i < parts.length; i++) {
214
+ const identifier = (0, util_js_1.toPascalCase)(parts.slice(-i).join('.'));
215
+ if ((0, ts_lang_js_1.isSafeIdentifier)(identifier))
216
+ return identifier;
217
+ }
218
+ return undefined;
219
+ }
220
+ /**
221
+ * Generates predictable public identifiers for a given definition hash.
222
+ *
223
+ * @note The returned `typeName` is guaranteed to be a valid TypeScript
224
+ * identifier. `varName` may not be a valid identifier (eg. if the hash contains
225
+ * unsafe characters), and may need to be accessed using string indexing.
226
+ */
227
+ function getPublicIdentifiers(hash) {
228
+ const varName = hash;
229
+ // @NOTE Type names *must* be valid TypeScript identifiers (this is because,
230
+ // unlike variable names, we cannot use string indexing to access exported
231
+ // types).
232
+ const typeName = (0, util_js_1.toPascalCase)(hash);
233
+ if (!typeName || varName === typeName || !(0, ts_lang_js_1.isSafeIdentifier)(typeName)) {
234
+ return { varName, typeName: `Def${typeName}` };
235
+ }
236
+ return { varName, typeName };
237
+ }
238
+ function asSafeDefinitionIdentifier(name) {
239
+ if ((0, ts_lang_js_1.isSafeIdentifier)(name) && (0, ts_lang_js_1.isSafeIdentifier)((0, util_js_1.ucFirst)(name)))
240
+ return name;
241
+ const camel = (0, util_js_1.toCamelCase)(name);
242
+ if ((0, ts_lang_js_1.isSafeIdentifier)(camel) && (0, ts_lang_js_1.isSafeIdentifier)((0, util_js_1.ucFirst)(camel)))
243
+ return camel;
244
+ return undefined;
245
+ }
246
+ //# sourceMappingURL=ref-resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ref-resolver.js","sourceRoot":"","sources":["../src/ref-resolver.ts"],"names":[],"mappings":";;;AA2RA,oDAUC;;AArSD,sEAAgC;AAChC,yCAAgC;AAGhC,6CAA+D;AAC/D,uCAMkB;AAOlB;;;GAGG;AACH,MAAa,WAAW;IAEZ;IACA;IACA;IAHV,YACU,GAAoB,EACpB,IAAgB,EAChB,OAAuB;QAFvB,QAAG,GAAH,GAAG,CAAiB;QACpB,SAAI,GAAJ,IAAI,CAAY;QAChB,YAAO,GAAP,OAAO,CAAgB;IAC9B,CAAC;IAEY,OAAO,GAAG,IAAA,iBAAO,EAC/B,KAAK,EAAE,GAAW,EAAwB,EAAE;QAC1C,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAE5C,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACxC,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;QAChC,CAAC;aAAM,CAAC;YACN,iEAAiE;YACjE,MAAM,OAAO,GAAG,GAAG,IAAI,IAAI,I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AAC,CAAC;QAAE,OAAO,KAAK,CAAA;IAC7E,OAAO,SAAS,CAAA;AAClB,CAAC","sourcesContent":["import assert from 'node:assert'\nimport { join } from 'node:path'\nimport { SourceFile } from 'ts-morph'\nimport { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'\nimport { isReservedWord, isSafeIdentifier } from './ts-lang.js'\nimport {\n asRelativePath,\n memoize,\n toCamelCase,\n toPascalCase,\n ucFirst,\n} from './util.js'\n\nexport type ResolvedRef = {\n varName: string\n typeName: string\n}\n\n/**\n * Utility class to resolve lexicon references to TypeScript identifiers,\n * generating \"import\" statements as needed.\n */\nexport class RefResolver {\n constructor(\n private doc: LexiconDocument,\n private file: SourceFile,\n private indexer: LexiconIndexer,\n ) {}\n\n public readonly resolve = memoize(\n async (ref: string): Promise<ResolvedRef> => {\n const [nsid, hash = 'main'] = ref.split('#')\n\n if (nsid === '' || nsid === this.doc.id) {\n return this.resolveLocal(hash)\n } else {\n // @NOTE: Normalize (#main fragment) to ensure proper memoization\n const fullRef = `${nsid}#${hash}`\n return this.resolveExternal(fullRef)\n }\n },\n )\n\n #defCounters = new Map<string, number>()\n private nextSafeDefinitionIdentifier(safeIdentifier: string) {\n const count = this.#defCounters.get(safeIdentifier) ?? 0\n this.#defCounters.set(safeIdentifier, count + 1)\n // @NOTE We don't need to check against local declarations in the file here\n // since we are using a naming system that should guarantee no other\n // identifier has a <safeIdentifier>$<number> format.\n return `${safeIdentifier}$${count}`\n }\n\n /**\n * @note The returned `typeName` and `varName` are *both* guaranteed to be\n * valid TypeScript identifiers.\n */\n public readonly resolveLocal = memoize(\n async (hash: string): Promise<ResolvedRef> => {\n const hashes = Object.keys(this.doc.defs)\n\n if (!hashes.includes(hash)) {\n throw new Error(`Definition ${hash} not found in ${this.doc.id}`)\n }\n\n // Because we are using predictable \"public\" identifiers for type names,\n // we need to ensure there are no conflicts between different definitions\n // in the same lexicon document.\n //\n // @NOTE It should be possible to implement a way to generate\n // non-conflicting type names for all public (type) identifiers in a\n // project. However, this would add a lot of complexity to the code\n // generation process, and the likelihood of such conflicts happening in\n // practice is very low, so we opt for a simpler approach of just throwing\n // an error if a conflict is detected.\n const pub = getPublicIdentifiers(hash)\n for (const otherHash of hashes) {\n if (otherHash === hash) continue\n const otherPub = getPublicIdentifiers(otherHash)\n if (otherPub.typeName === pub.typeName) {\n throw new Error(\n `Conflicting type names for definitions #${hash} and #${otherHash} in ${this.doc.id}`,\n )\n }\n }\n\n // Try to keep and identifier that resembles the original hash as identifier\n const safeIdentifier = asSafeDefinitionIdentifier(hash)\n\n // If the safe identifier is not conflicting with other definition names,\n // or reserved words, we can use it as-is. Otherwise, we need to generate\n // a unique safe identifier.\n const varName = safeIdentifier\n ? !hashes.some((otherHash) => {\n if (otherHash === hash) return false\n const otherIdentifier = asSafeDefinitionIdentifier(otherHash)\n return otherIdentifier === safeIdentifier\n })\n ? // Safe identifier can be used as-is as it does not conflict with\n // other definition names\n safeIdentifier\n : // In order to keep identifiers stable, we use the safe identifier\n // as base, and append a counter to avoid conflicts\n this.nextSafeDefinitionIdentifier(safeIdentifier)\n : // hash only contained unsafe characters, generate a safe one\n this.nextSafeDefinitionIdentifier('def')\n\n const typeName = ucFirst(varName)\n assert(varName !== typeName, 'Variable and type name should be different')\n\n return { varName, typeName }\n },\n )\n\n /**\n * @note Since this is a memoized function, and is used to generate the name\n * of local variables, we should avoid returning different results for\n * similar, but non strictly equal, inputs (eg. normalized / non-normalized).\n * @see {@link resolve}\n */\n private readonly resolveExternal = memoize(\n async (fullRef: string): Promise<ResolvedRef> => {\n const [nsid, hash] = fullRef.split('#')\n const moduleSpecifier = `${asRelativePath(\n this.file.getDirectoryPath(),\n join('/', ...nsid.split('.')),\n )}.defs.js`\n\n // Lets first make sure the referenced lexicon exists\n const srcDoc = await this.indexer.get(nsid)\n const srcDef = Object.hasOwn(srcDoc.defs, hash) ? srcDoc.defs[hash] : null\n if (!srcDef) {\n throw new Error(\n `Missing def \"${hash}\" in \"${nsid}\" (referenced from ${this.doc.id})`,\n )\n }\n\n // import * as <nsIdentifier> from './<moduleSpecifier>'\n const nsIdentifier = this.getNsIdentifier(nsid, moduleSpecifier)\n\n const publicIds = getPublicIdentifiers(hash)\n\n return {\n varName: isSafeIdentifier(publicIds.varName)\n ? `${nsIdentifier}.${publicIds.varName}`\n : `${nsIdentifier}[${JSON.stringify(publicIds.varName)}]`,\n typeName: `${nsIdentifier}.${publicIds.typeName}`,\n }\n },\n )\n\n private getNsIdentifier(nsid: string, moduleSpecifier: string) {\n const namespaceImportDeclaration =\n this.file.getImportDeclaration(\n (imp) =>\n !imp.isTypeOnly() &&\n imp.getModuleSpecifierValue() === moduleSpecifier &&\n imp.getNamespaceImport() != null,\n ) ||\n this.file.addImportDeclaration({\n moduleSpecifier,\n namespaceImport: this.computeSafeNamespaceIdentifierFor(nsid),\n })\n\n return namespaceImportDeclaration.getNamespaceImport()!.getText()\n }\n\n #nsIdentifiersCounters = new Map<string, number>()\n private computeSafeNamespaceIdentifierFor(nsid: string) {\n const baseName = nsidToIdentifier(nsid) || 'NS'\n\n let name = baseName\n while (this.isConflictingIdentifier(name)) {\n const count = this.#nsIdentifiersCounters.get(baseName) ?? 0\n this.#nsIdentifiersCounters.set(baseName, count + 1)\n name = `${baseName}$$${count}`\n }\n\n return name\n }\n\n private isConflictingIdentifier(name: string) {\n return (\n this.conflictsWithKeywords(name) ||\n this.conflictsWithUtils(name) ||\n this.conflictsWithLocalDefs(name) ||\n this.conflictsWithLocalDeclarations(name) ||\n this.conflictsWithImports(name)\n )\n }\n\n private conflictsWithKeywords(name: string) {\n return isReservedWord(name)\n }\n\n private conflictsWithUtils(name: string) {\n // Do not allow \"Main\" as imported ns identifier since it has a special\n // meaning in the context of lexicon definitions.\n if (name === 'Main') return true\n\n // When \"useRecordExport\" returns true, an export named \"Record\" will be\n // used in addition to the hash named export. So we need to make sure both\n // names are not conflicting with local variables.\n if (name === 'Record') return true\n\n // Utility functions generated for lexicon schemas are prefixed with \"$\"\n return name.startsWith('$')\n }\n\n private conflictsWithLocalDefs(name: string) {\n return Object.keys(this.doc.defs).some((hash) => {\n const identifier = toCamelCase(hash)\n\n // A safe identifier will be generated, no risk of conflict.\n if (!identifier) return false\n\n // The imported name conflicts with a local definition name\n if (identifier === name || `_${identifier}` === name) return true\n\n // The imported name conflicts with the type name of a local definition\n const typeName = ucFirst(identifier)\n if (typeName === name || `_${typeName}` === name) return true\n\n return false\n })\n }\n\n private conflictsWithLocalDeclarations(name: string) {\n return (\n this.file.getVariableDeclarations().some((v) => v.getName() === name) ||\n this.file\n .getVariableStatements()\n .some((vs) => vs.getDeclarations().some((d) => d.getName() === name)) ||\n this.file.getTypeAliases().some((t) => t.getName() === name) ||\n this.file.getInterfaces().some((i) => i.getName() === name) ||\n this.file.getClasses().some((c) => c.getName() === name) ||\n this.file.getFunctions().some((f) => f.getName() === name) ||\n this.file.getEnums().some((e) => e.getName() === name)\n )\n }\n\n private conflictsWithImports(name: string) {\n return this.file.getImportDeclarations().some(\n (imp) =>\n // import name from '...'\n imp.getDefaultImport()?.getText() === name ||\n // import * as name from '...'\n imp.getNamespaceImport()?.getText() === name ||\n imp.getNamedImports().some(\n (named) =>\n // import { name } from '...'\n // import { foo as name } from '...'\n (named.getAliasNode()?.getText() ?? named.getName()) === name,\n ),\n )\n }\n}\n\n/**\n * @see {@link https://atproto.com/specs/nsid NSID syntax spec}\n */\nfunction nsidToIdentifier(nsid: string) {\n const parts = nsid.split('.')\n\n // By default, try to keep only to the last two segments of the NSID as\n // contextual information. If those do not form a safe identifier (typically\n // because they start with a digit), try with more segments until we reach the\n // full NSID.\n for (let i = 2; i < parts.length; i++) {\n const identifier = toPascalCase(parts.slice(-i).join('.'))\n if (isSafeIdentifier(identifier)) return identifier\n }\n\n return undefined\n}\n\n/**\n * Generates predictable public identifiers for a given definition hash.\n *\n * @note The returned `typeName` is guaranteed to be a valid TypeScript\n * identifier. `varName` may not be a valid identifier (eg. if the hash contains\n * unsafe characters), and may need to be accessed using string indexing.\n */\nexport function getPublicIdentifiers(hash: string): ResolvedRef {\n const varName = hash\n // @NOTE Type names *must* be valid TypeScript identifiers (this is because,\n // unlike variable names, we cannot use string indexing to access exported\n // types).\n const typeName = toPascalCase(hash)\n if (!typeName || varName === typeName || !isSafeIdentifier(typeName)) {\n return { varName, typeName: `Def${typeName}` }\n }\n return { varName, typeName }\n}\n\nfunction asSafeDefinitionIdentifier(name: string) {\n if (isSafeIdentifier(name) && isSafeIdentifier(ucFirst(name))) return name\n const camel = toCamelCase(name)\n if (isSafeIdentifier(camel) && isSafeIdentifier(ucFirst(camel))) return camel\n return undefined\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export declare function isReservedWord(word: string): boolean;
2
+ export declare function isSafeIdentifier(name: string): boolean;
3
+ //# sourceMappingURL=ts-lang.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ts-lang.d.ts","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":"AA+HA,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,WAE1C;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,WAE5C"}