@atproto/lex-builder 0.0.8 → 0.0.10

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.
@@ -1 +1 @@
1
- {"version":3,"file":"ref-resolver.js","sourceRoot":"","sources":["../src/ref-resolver.ts"],"names":[],"mappings":";;;AAgSA,oDAUC;;AA1SD,sEAAgC;AAChC,yCAAgC;AAGhC,6CAA+D;AAC/D,uCAMkB;AAWlB;;;GAGG;AACH,MAAa,WAAW;IAEZ;IACA;IACA;IACA;IAJV,YACU,GAAoB,EACpB,IAAgB,EAChB,OAAuB,EACvB,OAA2B;QAH3B,QAAG,GAAH,GAAG,CAAiB;QACpB,SAAI,GAAJ,IAAI,CAAY;QAChB,YAAO,GAAP,OAAO,CAAgB;QACvB,YAAO,GAAP,OAAO,CAAoB;IAClC,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,IAAI,EAAE,CAAA;YACjC,OAAO,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAA;QACtC,CAAC;IACH,CAAC,CACF,CAAA;IAED,YAAY,GAAG,IAAI,GAAG,EAAkB,CAAA;IAChC,4BAA4B,CAAC,cAAsB;QACzD,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;QACxD,IAAI,CAAC,YAAY,CAAC,GAAG,CA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AAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;QAC1D,IAAI,IAAA,6BAAgB,EAAC,UAAU,CAAC;YAAE,OAAO,UAAU,CAAA;IACrD,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAAC,IAAY;IAC/C,MAAM,OAAO,GAAG,IAAI,CAAA;IACpB,4EAA4E;IAC5E,0EAA0E;IAC1E,UAAU;IACV,MAAM,QAAQ,GAAG,IAAA,sBAAY,EAAC,IAAI,CAAC,CAAA;IACnC,IAAI,CAAC,QAAQ,IAAI,OAAO,KAAK,QAAQ,IAAI,CAAC,IAAA,6BAAgB,EAAC,QAAQ,CAAC,EAAE,CAAC;QACrE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,EAAE,EAAE,CAAA;IAChD,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;AAC9B,CAAC;AAED,SAAS,0BAA0B,CAAC,IAAY;IAC9C,IAAI,IAAA,6BAAgB,EAAC,IAAI,CAAC,IAAI,IAAA,6BAAgB,EAAC,IAAA,iBAAO,EAAC,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAA;IAC1E,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,IAAI,CAAC,CAAA;IAC/B,IAAI,IAAA,6BAAgB,EAAC,KAAK,CAAC,IAAI,IAAA,6BAAgB,EAAC,IAAA,iBAAO,EAAC,KAAK,CAAC,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 RefResolverOptions = {\n importExt?: string\n}\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 private options: RefResolverOptions,\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${this.options.importExt ?? '.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"]}
1
+ {"version":3,"file":"ref-resolver.js","sourceRoot":"","sources":["../src/ref-resolver.ts"],"names":[],"mappings":";;;AA2UA,oDAkBC;;AA7VD,sEAAgC;AAChC,yCAAgC;AAGhC,6CAKqB;AACrB,uCAOkB;AAWlB;;;GAGG;AACH,MAAa,WAAW;IAEZ;IACA;IACA;IACA;IAJV,YACU,GAAoB,EACpB,IAAgB,EAChB,OAAuB,EACvB,OAA2B;QAH3B,QAAG,GAAH,GAAG,CAAiB;QACpB,SAAI,GAAJ,IAAI,CAAY;QAChB,YAAO,GAAP,OAAO,CAAgB;QACvB,YAAO,GAAP,OAAO,CAAoB;IAClC,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,IAAI,EAAE,CAAA;YACjC,OAAO,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAA;QACtC,CAAC;IACH,CAAC,CACF,CAAA;IAED,YAAY,GAAG,IAAI,GAAG,EAAkB,CAAA;IAChC,4BAA4B,CAAC,IAAY;QAC/C,iDAAiD;QACjD,MAAM,QAAQ,GACZ,IAAA,yBA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IACtE,IAAI,QAAQ,GAAG,IAAA,sBAAY,EAAC,IAAI,CAAC,CAAA;IAEjC,IAAI,OAAO,KAAK,QAAQ,IAAI,CAAC,IAAA,gCAAmB,EAAC,QAAQ,CAAC,EAAE,CAAC;QAC3D,QAAQ,GAAG,SAAS,QAAQ,EAAE,CAAA;IAChC,CAAC;IAED,IAAA,qBAAM,EACJ,IAAA,gCAAmB,EAAC,QAAQ,CAAC,EAC7B,yDAAyD,IAAI,GAAG,CACjE,CAAA;IAED,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAA;AAC9B,CAAC;AAED,SAAS,0BAA0B,CAAC,IAAY;IAC9C,IACE,IAAA,yBAAe,EAAC,IAAI,CAAC;QACrB,IAAA,kCAAqB,EAAC,IAAI,CAAC;QAC3B,IAAA,kCAAqB,EAAC,IAAA,iBAAO,EAAC,IAAI,CAAC,CAAC,EACpC,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,IAAI,CAAC,CAAA;IAC/B,IAAI,IAAA,kCAAqB,EAAC,KAAK,CAAC,IAAI,IAAA,kCAAqB,EAAC,IAAA,iBAAO,EAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QAC1E,OAAO,KAAK,CAAA;IACd,CAAC;IACD,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 {\n isGlobalIdentifier,\n isJsKeyword,\n isSafeLocalIdentifier,\n isValidJsIdentifier,\n} from './ts-lang.js'\nimport {\n asRelativePath,\n memoize,\n startsWithLower,\n toCamelCase,\n toPascalCase,\n ucFirst,\n} from './util.js'\n\nexport type RefResolverOptions = {\n importExt?: string\n}\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 private options: RefResolverOptions,\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(name: string) {\n // use camelCase version of the hash as base name\n const nameSafe =\n startsWithLower(name) && isValidJsIdentifier(name)\n ? name\n : toCamelCase(name).replace(/^[0-9]+/g, '') || 'def'\n\n const count = this.#defCounters.get(nameSafe) ?? 0\n this.#defCounters.set(nameSafe, count + 1)\n\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 <nameSafe>$<number> format (\"$\" cannot appear in\n // hashes so only *we* are generating such identifiers).\n\n const identifier = `${nameSafe}$${count}`\n\n assert(\n isValidJsIdentifier(identifier),\n `Unable to generate safe identifier for: \"${name}\"`,\n )\n\n return identifier\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(hash)\n\n const typeName = ucFirst(varName)\n assert(isSafeLocalIdentifier(typeName), 'Expected safe type identifier')\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${this.options.importExt ?? '.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 const publicIds = getPublicIdentifiers(hash)\n\n if (!isValidJsIdentifier(publicIds.typeName)) {\n // If <typeName> is not a valid identifier, we cannot access the type\n // using dot notation (<nsIdentifier>.<typeName>). Note that, unlike js\n // variables, types cannot be accessed using string indexing (like:\n // <nsIdentifier>['<typeName>']) because it generates TypeScript errors:\n //\n // > \"Cannot use namespace '<nsIdentifier>' as a type.\"\n\n // Instead the generated code should look like:\n // import { \"<unsafeTypeName>\" as <safeIdentifier> } from './<moduleSpecifier>'\n\n // Because it requires more complex management of local variables names,\n // and we don't expect this to actually happen with properly designed\n // lexicons documents, we do not support this for now.\n\n throw new Error(\n 'Import of definitions with unsafe type names is not supported',\n )\n }\n\n // import * as <nsIdentifier> from './<moduleSpecifier>'\n const nsIdentifier = this.getNsIdentifier(nsid, moduleSpecifier)\n\n return {\n varName: isValidJsIdentifier(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 isJsKeyword(name) || isGlobalIdentifier(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 (isSafeLocalIdentifier(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\n // @NOTE we try to circumvent the issue of unsafe type names described in\n // `RefResolver.resolveExternal` by ensuring that type names are always safe\n // identifiers, even if it means changing them from the original hash.\n let typeName = toPascalCase(hash)\n\n if (varName === typeName || !isValidJsIdentifier(typeName)) {\n typeName = `TypeOf${typeName}`\n }\n\n assert(\n isValidJsIdentifier(typeName),\n `Unable to generate a predictable safe identifier for \"${hash}\"`,\n )\n\n return { varName, typeName }\n}\n\nfunction asSafeDefinitionIdentifier(name: string) {\n if (\n startsWithLower(name) &&\n isSafeLocalIdentifier(name) &&\n isSafeLocalIdentifier(ucFirst(name))\n ) {\n return name\n }\n const camel = toCamelCase(name)\n if (isSafeLocalIdentifier(camel) && isSafeLocalIdentifier(ucFirst(camel))) {\n return camel\n }\n return undefined\n}\n"]}
package/dist/ts-lang.d.ts CHANGED
@@ -1,3 +1,6 @@
1
- export declare function isReservedWord(word: string): boolean;
2
- export declare function isSafeIdentifier(name: string): boolean;
1
+ export declare function isJsKeyword(word: string): boolean;
2
+ export declare function isGlobalIdentifier(word: string): boolean;
3
+ export declare function isSafeLocalIdentifier(name: string): boolean;
4
+ export declare function isValidJsIdentifier(name: string): boolean;
5
+ export declare function asNamespaceExport(name: string): string;
3
6
  //# sourceMappingURL=ts-lang.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"ts-lang.d.ts","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":"AA+EA,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,WAEvC;AAkDD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,WAE9C;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,WAEjD;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,WAM/C;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,UAE7C"}
package/dist/ts-lang.js CHANGED
@@ -1,10 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isReservedWord = isReservedWord;
4
- exports.isSafeIdentifier = isSafeIdentifier;
5
- const RESERVED_WORDS = new Set([
6
- // JavaScript keywords
7
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar
3
+ exports.isJsKeyword = isJsKeyword;
4
+ exports.isGlobalIdentifier = isGlobalIdentifier;
5
+ exports.isSafeLocalIdentifier = isSafeLocalIdentifier;
6
+ exports.isValidJsIdentifier = isValidJsIdentifier;
7
+ exports.asNamespaceExport = asNamespaceExport;
8
+ /**
9
+ * JavaScript keywords
10
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar}
11
+ */
12
+ const JS_KEYWORDS = new Set([
8
13
  'abstract',
9
14
  'arguments',
10
15
  'as',
@@ -77,62 +82,69 @@ const RESERVED_WORDS = new Set([
77
82
  'while',
78
83
  'with',
79
84
  'yield',
80
- // Constructors and globals
81
- 'Array',
82
- 'Boolean',
83
- 'Buffer',
84
- 'Date',
85
- 'Error',
86
- 'Function',
87
- 'Infinity',
88
- 'JSON',
89
- 'Map',
90
- 'Math',
91
- 'NaN',
92
- 'Number',
93
- 'Object',
94
- 'Set',
95
- 'String',
96
- 'Symbol',
97
- 'console',
98
- 'document',
99
- 'global',
85
+ ]);
86
+ function isJsKeyword(word) {
87
+ return JS_KEYWORDS.has(word);
88
+ }
89
+ // Only important to list var/type names that are likely to be used in the
90
+ // generated code files.
91
+ const GLOBAL_IDENTIFIERS = new Set([
92
+ // import { l } from "@atproto/lex-schema"
93
+ 'l',
94
+ // JS Globals
95
+ 'self',
100
96
  'globalThis',
101
- 'window',
102
- // Test globals
103
- 'afterAll',
104
- 'afterEach',
105
- 'assert',
106
- 'beforeAll',
107
- 'beforeEach',
108
- 'describe',
109
- 'expect',
110
- 'it',
111
- 'test',
112
- // CommonJS globals
97
+ // ESM
98
+ 'import',
99
+ // CommonJS
113
100
  '__dirname',
114
101
  '__filename',
115
102
  'require',
116
103
  'module',
117
104
  'exports',
118
- // TypeScript
119
- 'Record',
105
+ // TS Primitives
120
106
  'any',
107
+ 'bigint',
108
+ 'boolean',
121
109
  'declare',
122
110
  'never',
111
+ 'null',
123
112
  'number',
124
113
  'object',
125
114
  'string',
126
115
  'symbol',
116
+ 'undefined',
127
117
  'unknown',
128
- // Future reserved words
129
- 'constructor',
130
- 'meta',
118
+ 'void',
119
+ // TS Utility types
120
+ 'Record',
121
+ 'Partial',
122
+ 'Readonly',
123
+ 'Pick',
124
+ 'Omit',
125
+ 'Exclude',
126
+ 'Extract',
127
+ 'InstanceType',
128
+ 'ReturnType',
129
+ 'Required',
130
+ 'ThisType',
131
+ 'Uppercase',
132
+ 'Lowercase',
133
+ 'Capitalize',
134
+ 'Uncapitalize',
131
135
  ]);
132
- function isReservedWord(word) {
133
- return RESERVED_WORDS.has(word);
136
+ function isGlobalIdentifier(word) {
137
+ return GLOBAL_IDENTIFIERS.has(word);
138
+ }
139
+ function isSafeLocalIdentifier(name) {
140
+ return !isGlobalIdentifier(name) && isValidJsIdentifier(name);
141
+ }
142
+ function isValidJsIdentifier(name) {
143
+ return (name.length > 0 &&
144
+ !isJsKeyword(name) &&
145
+ /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name));
134
146
  }
135
- function isSafeIdentifier(name) {
136
- return !isReservedWord(name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
147
+ function asNamespaceExport(name) {
148
+ return isValidJsIdentifier(name) ? name : JSON.stringify(name);
137
149
  }
138
150
  //# sourceMappingURL=ts-lang.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ts-lang.js","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":";;AA+HA,wCAEC;AAED,4CAEC;AArID,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC;IAC7B,sBAAsB;IACtB,oFAAoF;IACpF,UAAU;IACV,WAAW;IACX,IAAI;IACJ,OAAO;IACP,OAAO;IACP,SAAS;IACT,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,OAAO;IACP,OAAO;IACP,UAAU;IACV,UAAU;IACV,SAAS;IACT,QAAQ;IACR,IAAI;IACJ,QAAQ;IACR,MAAM;IACN,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,OAAO;IACP,OAAO;IACP,SAAS;IACT,OAAO;IACP,KAAK;IACL,MAAM;IACN,UAAU;IACV,KAAK;IACL,MAAM;IACN,IAAI;IACJ,YAAY;IACZ,QAAQ;IACR,IAAI;IACJ,YAAY;IACZ,KAAK;IACL,WAAW;IACX,KAAK;IACL,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,IAAI;IACJ,SAAS;IACT,SAAS;IACT,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,KAAK;IACL,OAAO;IACP,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,cAAc;IACd,MAAM;IACN,OAAO;IACP,QAAQ;IACR,WAAW;IACX,MAAM;IACN,KAAK;IACL,QAAQ;IACR,WAAW;IACX,OAAO;IACP,KAAK;IACL,MAAM;IACN,UAAU;IACV,OAAO;IACP,MAAM;IACN,OAAO;IACP,2BAA2B;IAC3B,OAAO;IACP,SAAS;IACT,QAAQ;IACR,MAAM;IACN,OAAO;IACP,UAAU;IACV,UAAU;IACV,MAAM;IACN,KAAK;IACL,MAAM;IACN,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,UAAU;IACV,QAAQ;IACR,YAAY;IACZ,QAAQ;IACR,eAAe;IACf,UAAU;IACV,WAAW;IACX,QAAQ;IACR,WAAW;IACX,YAAY;IACZ,UAAU;IACV,QAAQ;IACR,IAAI;IACJ,MAAM;IACN,mBAAmB;IACnB,WAAW;IACX,YAAY;IACZ,SAAS;IACT,QAAQ;IACR,SAAS;IACT,aAAa;IACb,QAAQ;IACR,KAAK;IACL,SAAS;IACT,OAAO;IACP,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,wBAAwB;IACxB,aAAa;IACb,MAAM;CACP,CAAC,CAAA;AACF,SAAgB,cAAc,CAAC,IAAY;IACzC,OAAO,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AACjC,CAAC;AAED,SAAgB,gBAAgB,CAAC,IAAY;IAC3C,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACzE,CAAC","sourcesContent":["const RESERVED_WORDS = new Set([\n // JavaScript keywords\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar\n 'abstract',\n 'arguments',\n 'as',\n 'async',\n 'await',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'from',\n 'function',\n 'get',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'of',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'set',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'undefined',\n 'using',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n // Constructors and globals\n 'Array',\n 'Boolean',\n 'Buffer',\n 'Date',\n 'Error',\n 'Function',\n 'Infinity',\n 'JSON',\n 'Map',\n 'Math',\n 'NaN',\n 'Number',\n 'Object',\n 'Set',\n 'String',\n 'Symbol',\n 'console',\n 'document',\n 'global',\n 'globalThis',\n 'window',\n // Test globals\n 'afterAll',\n 'afterEach',\n 'assert',\n 'beforeAll',\n 'beforeEach',\n 'describe',\n 'expect',\n 'it',\n 'test',\n // CommonJS globals\n '__dirname',\n '__filename',\n 'require',\n 'module',\n 'exports',\n // TypeScript\n 'Record',\n 'any',\n 'declare',\n 'never',\n 'number',\n 'object',\n 'string',\n 'symbol',\n 'unknown',\n // Future reserved words\n 'constructor',\n 'meta',\n])\nexport function isReservedWord(word: string) {\n return RESERVED_WORDS.has(word)\n}\n\nexport function isSafeIdentifier(name: string) {\n return !isReservedWord(name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n"]}
1
+ {"version":3,"file":"ts-lang.js","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":";;AA+EA,kCAEC;AAkDD,gDAEC;AAED,sDAEC;AAED,kDAMC;AAED,8CAEC;AArJD;;;GAGG;AACH,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC;IAC1B,UAAU;IACV,WAAW;IACX,IAAI;IACJ,OAAO;IACP,OAAO;IACP,SAAS;IACT,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,OA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M;IACN,MAAM;IACN,SAAS;IACT,SAAS;IACT,cAAc;IACd,YAAY;IACZ,UAAU;IACV,UAAU;IACV,WAAW;IACX,WAAW;IACX,YAAY;IACZ,cAAc;CACf,CAAC,CAAA;AAEF,SAAgB,kBAAkB,CAAC,IAAY;IAC7C,OAAO,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AACrC,CAAC;AAED,SAAgB,qBAAqB,CAAC,IAAY;IAChD,OAAO,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAA;AAC/D,CAAC;AAED,SAAgB,mBAAmB,CAAC,IAAY;IAC9C,OAAO,CACL,IAAI,CAAC,MAAM,GAAG,CAAC;QACf,CAAC,WAAW,CAAC,IAAI,CAAC;QAClB,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAA;AACH,CAAC;AAED,SAAgB,iBAAiB,CAAC,IAAY;IAC5C,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;AAChE,CAAC","sourcesContent":["/**\n * JavaScript keywords\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar}\n */\nconst JS_KEYWORDS = new Set([\n 'abstract',\n 'arguments',\n 'as',\n 'async',\n 'await',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'from',\n 'function',\n 'get',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'of',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'set',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'undefined',\n 'using',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n])\n\nexport function isJsKeyword(word: string) {\n return JS_KEYWORDS.has(word)\n}\n\n// Only important to list var/type names that are likely to be used in the\n// generated code files.\nconst GLOBAL_IDENTIFIERS = new Set([\n // import { l } from \"@atproto/lex-schema\"\n 'l',\n // JS Globals\n 'self',\n 'globalThis',\n // ESM\n 'import',\n // CommonJS\n '__dirname',\n '__filename',\n 'require',\n 'module',\n 'exports',\n // TS Primitives\n 'any',\n 'bigint',\n 'boolean',\n 'declare',\n 'never',\n 'null',\n 'number',\n 'object',\n 'string',\n 'symbol',\n 'undefined',\n 'unknown',\n 'void',\n // TS Utility types\n 'Record',\n 'Partial',\n 'Readonly',\n 'Pick',\n 'Omit',\n 'Exclude',\n 'Extract',\n 'InstanceType',\n 'ReturnType',\n 'Required',\n 'ThisType',\n 'Uppercase',\n 'Lowercase',\n 'Capitalize',\n 'Uncapitalize',\n])\n\nexport function isGlobalIdentifier(word: string) {\n return GLOBAL_IDENTIFIERS.has(word)\n}\n\nexport function isSafeLocalIdentifier(name: string) {\n return !isGlobalIdentifier(name) && isValidJsIdentifier(name)\n}\n\nexport function isValidJsIdentifier(name: string) {\n return (\n name.length > 0 &&\n !isJsKeyword(name) &&\n /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n )\n}\n\nexport function asNamespaceExport(name: string) {\n return isValidJsIdentifier(name) ? name : JSON.stringify(name)\n}\n"]}
package/dist/util.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare function memoize<T extends (arg: string) => NonNullable<unknown> | null>(fn: T): T;
2
+ export declare function startsWithLower(str: string): boolean;
2
3
  export declare function ucFirst(str: string): string;
3
4
  export declare function lcFirst(str: string): string;
4
5
  export declare function toPascalCase(str: string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAEA,wBAAgB,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,KAAK,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,EAC5E,EAAE,EAAE,CAAC,GACJ,CAAC,CASH;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,UAElC;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,UAElC;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAeD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,UAKtD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,WAG1C"}
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAEA,wBAAgB,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,KAAK,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,EAC5E,EAAE,EAAE,CAAC,GACJ,CAAC,CASH;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,WAG1C;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,UAElC;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,UAElC;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C;AAeD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,UAKtD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,WAG1C"}
package/dist/util.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.memoize = memoize;
4
+ exports.startsWithLower = startsWithLower;
4
5
  exports.ucFirst = ucFirst;
5
6
  exports.lcFirst = lcFirst;
6
7
  exports.toPascalCase = toPascalCase;
@@ -22,6 +23,10 @@ function memoize(fn) {
22
23
  return result;
23
24
  });
24
25
  }
26
+ function startsWithLower(str) {
27
+ const code = str.charCodeAt(0);
28
+ return code >= 97 && code <= 122; // 'a' to 'z'
29
+ }
25
30
  function ucFirst(str) {
26
31
  return str.charAt(0).toUpperCase() + str.slice(1);
27
32
  }
package/dist/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAEA,0BAWC;AAED,0BAEC;AAED,0BAEC;AAED,oCAEC;AAED,kCAEC;AAED,wCAEC;AAED,kCAEC;AAED,kCAEC;AAeD,wCAKC;AAED,0CAGC;AAlED,yCAAoC;AAEpC,SAAgB,OAAO,CACrB,EAAK;IAEL,MAAM,KAAK,GAAG,IAAI,GAAG,EAAuC,CAAA;IAC5D,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;QACtB,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC7B,IAAI,MAAM,KAAK,SAAS;YAAE,OAAO,MAAM,CAAA;QACvC,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C,CAAC,qBAAqB;SAC9D,OAAO,CAAC,oBAAoB,EAAE,OAAO,CAAC,CAAC,kCAAkC;SACzE,OAAO,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC,sCAAsC;SACrE,IAAI,EAAE,CAAA,CAAC,+BAA+B;IAEzC,OAAO,YAAY;QACjB,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,kBAAkB;QAC9C,CAAC,CAAC,EAAE,CAAA,CAAC,yCAAyC;AAClD,CAAC;AAED,SAAgB,cAAc,CAAC,IAAY,EAAE,EAAU;IACrD,MAAM,OAAO,GAAG,IAAA,oBAAQ,EAAC,IAAI,EAAE,EAAE,CAAC,CAAA;IAClC,OAAO,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC;QAC1D,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,KAAK,OAAO,EAAE,CAAA;AACpB,CAAC;AAED,SAAgB,eAAe,CAAC,GAAW;IACzC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;IAC9B,OAAO,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAA,CAAC,aAAa;AAC/C,CAAC","sourcesContent":["import { relative } from 'node:path'\n\nexport function memoize<T extends (arg: string) => NonNullable<unknown> | null>(\n fn: T,\n): T {\n const cache = new Map<string, NonNullable<unknown> | null>()\n return ((arg: string) => {\n const cached = cache.get(arg)\n if (cached !== undefined) return cached\n const result = fn(arg)\n cache.set(arg, result)\n return result\n }) as T\n}\n\nexport function ucFirst(str: string) {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nexport function lcFirst(str: string) {\n return str.charAt(0).toLowerCase() + str.slice(1)\n}\n\nexport function toPascalCase(str: string): string {\n return extractWords(str).map(toLowerCase).map(ucFirst).join('')\n}\n\nexport function toCamelCase(str: string): string {\n return lcFirst(toPascalCase(str))\n}\n\nexport function toConstantCase(str: string): string {\n return extractWords(str).map(toUpperCase).join('_')\n}\n\nexport function toLowerCase(str: string): string {\n return str.toLowerCase()\n}\n\nexport function toUpperCase(str: string): string {\n return str.toUpperCase()\n}\n\nfunction extractWords(str: string): string[] {\n const processedStr = str\n .replace(/([a-z0-9])([A-Z])/g, '$1 $2') // split camelCase\n .replace(/([A-Z])([A-Z][a-z])/g, '$1 $2') // split ALLCAPSWords\n .replace(/([0-9])([A-Za-z])/g, '$1 $2') // split number followed by letter\n .replace(/[^a-zA-Z0-9]+/g, ' ') // replace non-alphanumeric with space\n .trim() // trim leading/trailing spaces\n\n return processedStr\n ? processedStr.split(/\\s+/) // split by spaces\n : [] // Avoid returning [''] for empty strings\n}\n\nexport function asRelativePath(from: string, to: string) {\n const relPath = relative(from, to)\n return relPath.startsWith('./') || relPath.startsWith('../')\n ? relPath\n : `./${relPath}`\n}\n\nexport function startsWithDigit(str: string) {\n const code = str.charCodeAt(0)\n return code >= 48 && code <= 57 // '0' to '9'\n}\n"]}
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAEA,0BAWC;AAED,0CAGC;AAED,0BAEC;AAED,0BAEC;AAED,oCAEC;AAED,kCAEC;AAED,wCAEC;AAED,kCAEC;AAED,kCAEC;AAeD,wCAKC;AAED,0CAGC;AAvED,yCAAoC;AAEpC,SAAgB,OAAO,CACrB,EAAK;IAEL,MAAM,KAAK,GAAG,IAAI,GAAG,EAAuC,CAAA;IAC5D,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;QACtB,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC7B,IAAI,MAAM,KAAK,SAAS;YAAE,OAAO,MAAM,CAAA;QACvC,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAC,CAAA;QACtB,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QACtB,OAAO,MAAM,CAAA;IACf,CAA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ACzD,OAAO,CAAC,sBAAsB,EAAE,OAAO,CAAC,CAAC,qBAAqB;SAC9D,OAAO,CAAC,oBAAoB,EAAE,OAAO,CAAC,CAAC,kCAAkC;SACzE,OAAO,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC,sCAAsC;SACrE,IAAI,EAAE,CAAA,CAAC,+BAA+B;IAEzC,OAAO,YAAY;QACjB,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,kBAAkB;QAC9C,CAAC,CAAC,EAAE,CAAA,CAAC,yCAAyC;AAClD,CAAC;AAED,SAAgB,cAAc,CAAC,IAAY,EAAE,EAAU;IACrD,MAAM,OAAO,GAAG,IAAA,oBAAQ,EAAC,IAAI,EAAE,EAAE,CAAC,CAAA;IAClC,OAAO,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC;QAC1D,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,KAAK,OAAO,EAAE,CAAA;AACpB,CAAC;AAED,SAAgB,eAAe,CAAC,GAAW;IACzC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;IAC9B,OAAO,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAA,CAAC,aAAa;AAC/C,CAAC","sourcesContent":["import { relative } from 'node:path'\n\nexport function memoize<T extends (arg: string) => NonNullable<unknown> | null>(\n fn: T,\n): T {\n const cache = new Map<string, NonNullable<unknown> | null>()\n return ((arg: string) => {\n const cached = cache.get(arg)\n if (cached !== undefined) return cached\n const result = fn(arg)\n cache.set(arg, result)\n return result\n }) as T\n}\n\nexport function startsWithLower(str: string) {\n const code = str.charCodeAt(0)\n return code >= 97 && code <= 122 // 'a' to 'z'\n}\n\nexport function ucFirst(str: string) {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nexport function lcFirst(str: string) {\n return str.charAt(0).toLowerCase() + str.slice(1)\n}\n\nexport function toPascalCase(str: string): string {\n return extractWords(str).map(toLowerCase).map(ucFirst).join('')\n}\n\nexport function toCamelCase(str: string): string {\n return lcFirst(toPascalCase(str))\n}\n\nexport function toConstantCase(str: string): string {\n return extractWords(str).map(toUpperCase).join('_')\n}\n\nexport function toLowerCase(str: string): string {\n return str.toLowerCase()\n}\n\nexport function toUpperCase(str: string): string {\n return str.toUpperCase()\n}\n\nfunction extractWords(str: string): string[] {\n const processedStr = str\n .replace(/([a-z0-9])([A-Z])/g, '$1 $2') // split camelCase\n .replace(/([A-Z])([A-Z][a-z])/g, '$1 $2') // split ALLCAPSWords\n .replace(/([0-9])([A-Za-z])/g, '$1 $2') // split number followed by letter\n .replace(/[^a-zA-Z0-9]+/g, ' ') // replace non-alphanumeric with space\n .trim() // trim leading/trailing spaces\n\n return processedStr\n ? processedStr.split(/\\s+/) // split by spaces\n : [] // Avoid returning [''] for empty strings\n}\n\nexport function asRelativePath(from: string, to: string) {\n const relPath = relative(from, to)\n return relPath.startsWith('./') || relPath.startsWith('../')\n ? relPath\n : `./${relPath}`\n}\n\nexport function startsWithDigit(str: string) {\n const code = str.charCodeAt(0)\n return code >= 48 && code <= 57 // '0' to '9'\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/lex-builder",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "license": "MIT",
5
5
  "description": "TypeScript schema builder for AT Lexicons",
6
6
  "keywords": [
@@ -39,8 +39,8 @@
39
39
  "prettier": "^3.2.5",
40
40
  "ts-morph": "^27.0.0",
41
41
  "tslib": "^2.8.1",
42
- "@atproto/lex-document": "0.0.7",
43
- "@atproto/lex-schema": "0.0.6"
42
+ "@atproto/lex-document": "0.0.9",
43
+ "@atproto/lex-schema": "0.0.8"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@ts-morph/common": "^0.28.0",
@@ -1,3 +1,4 @@
1
+ import assert from 'node:assert'
1
2
  import { mkdir, rm, stat, writeFile } from 'node:fs/promises'
2
3
  import { join, resolve } from 'node:path'
3
4
  import { IndentationText, Project } from 'ts-morph'
@@ -10,9 +11,17 @@ import {
10
11
  LexiconDirectoryIndexer,
11
12
  LexiconDirectoryIndexerOptions,
12
13
  } from './lexicon-directory-indexer.js'
13
- import { isSafeIdentifier } from './ts-lang.js'
14
+ import { asNamespaceExport } from './ts-lang.js'
14
15
 
15
16
  export type LexBuilderOptions = LexDefBuilderOptions & {
17
+ /**
18
+ * Whether to generate an index file at the root exporting all top-level
19
+ * namespaces.
20
+ *
21
+ * @note This could theoretically cause name conflicts if a
22
+ * @default false
23
+ */
24
+ indexFile?: boolean
16
25
  importExt?: string
17
26
  fileExt?: string
18
27
  }
@@ -100,6 +109,23 @@ export class LexBuilder {
100
109
  private async createExportTree(doc: LexiconDocument) {
101
110
  const namespaces = doc.id.split('.')
102
111
 
112
+ if (this.options.indexFile) {
113
+ const indexFile = this.getFile(`/index${this.fileExt}`)
114
+
115
+ const tldNs = namespaces[0]!
116
+ assert(
117
+ tldNs !== 'index',
118
+ 'The "indexFile" options cannot be used with namespaces using a ".index" tld.',
119
+ )
120
+ const tldNsSpecifier = `./${tldNs}${this.importExt}`
121
+ if (!indexFile.getExportDeclaration(tldNsSpecifier)) {
122
+ indexFile.addExportDeclaration({
123
+ moduleSpecifier: tldNsSpecifier,
124
+ namespaceExport: asNamespaceExport(tldNs),
125
+ })
126
+ }
127
+ }
128
+
103
129
  // First create the parent namespaces
104
130
  for (let i = 0; i < namespaces.length - 1; i++) {
105
131
  const currentNs = namespaces[i]
@@ -113,9 +139,7 @@ export class LexBuilder {
113
139
  if (!dec) {
114
140
  file.addExportDeclaration({
115
141
  moduleSpecifier: childModuleSpecifier,
116
- namespaceExport: isSafeIdentifier(childNs)
117
- ? childNs
118
- : JSON.stringify(childNs),
142
+ namespaceExport: asNamespaceExport(childNs),
119
143
  })
120
144
  }
121
145
  }
@@ -1,4 +1,3 @@
1
- import assert from 'node:assert'
2
1
  import {
3
2
  JSDocStructure,
4
3
  OptionalKind,
@@ -37,7 +36,7 @@ import {
37
36
  ResolvedRef,
38
37
  getPublicIdentifiers,
39
38
  } from './ref-resolver.js'
40
- import { isSafeIdentifier } from './ts-lang.js'
39
+ import { asNamespaceExport } from './ts-lang.js'
41
40
 
42
41
  export type LexDefBuilderOptions = RefResolverOptions & {
43
42
  lib?: string
@@ -378,11 +377,6 @@ export class LexDefBuilder {
378
377
  const ref = await this.refResolver.resolveLocal(hash)
379
378
  const pub = getPublicIdentifiers(hash)
380
379
 
381
- // Fool-proofing
382
- assert(isSafeIdentifier(ref.varName), 'Expected safe type identifier')
383
- assert(isSafeIdentifier(ref.typeName), 'Expected safe type identifier')
384
- assert(isSafeIdentifier(pub.typeName), 'Expected safe type identifier')
385
-
386
380
  if (type) {
387
381
  this.file.addTypeAlias({
388
382
  name: ref.typeName,
@@ -395,7 +389,10 @@ export class LexDefBuilder {
395
389
  namedExports: [
396
390
  {
397
391
  name: ref.typeName,
398
- alias: ref.typeName === pub.typeName ? undefined : pub.typeName,
392
+ alias:
393
+ ref.typeName === pub.typeName
394
+ ? undefined
395
+ : asNamespaceExport(pub.typeName),
399
396
  },
400
397
  ],
401
398
  })
@@ -420,9 +417,7 @@ export class LexDefBuilder {
420
417
  alias:
421
418
  ref.varName === pub.varName
422
419
  ? undefined
423
- : isSafeIdentifier(pub.varName)
424
- ? pub.varName
425
- : JSON.stringify(pub.varName),
420
+ : asNamespaceExport(pub.varName),
426
421
  },
427
422
  ],
428
423
  })
@@ -2,10 +2,16 @@ import assert from 'node:assert'
2
2
  import { join } from 'node:path'
3
3
  import { SourceFile } from 'ts-morph'
4
4
  import { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'
5
- import { isReservedWord, isSafeIdentifier } from './ts-lang.js'
5
+ import {
6
+ isGlobalIdentifier,
7
+ isJsKeyword,
8
+ isSafeLocalIdentifier,
9
+ isValidJsIdentifier,
10
+ } from './ts-lang.js'
6
11
  import {
7
12
  asRelativePath,
8
13
  memoize,
14
+ startsWithLower,
9
15
  toCamelCase,
10
16
  toPascalCase,
11
17
  ucFirst,
@@ -47,13 +53,29 @@ export class RefResolver {
47
53
  )
48
54
 
49
55
  #defCounters = new Map<string, number>()
50
- private nextSafeDefinitionIdentifier(safeIdentifier: string) {
51
- const count = this.#defCounters.get(safeIdentifier) ?? 0
52
- this.#defCounters.set(safeIdentifier, count + 1)
56
+ private nextSafeDefinitionIdentifier(name: string) {
57
+ // use camelCase version of the hash as base name
58
+ const nameSafe =
59
+ startsWithLower(name) && isValidJsIdentifier(name)
60
+ ? name
61
+ : toCamelCase(name).replace(/^[0-9]+/g, '') || 'def'
62
+
63
+ const count = this.#defCounters.get(nameSafe) ?? 0
64
+ this.#defCounters.set(nameSafe, count + 1)
65
+
53
66
  // @NOTE We don't need to check against local declarations in the file here
54
67
  // since we are using a naming system that should guarantee no other
55
- // identifier has a <safeIdentifier>$<number> format.
56
- return `${safeIdentifier}$${count}`
68
+ // identifier has a <nameSafe>$<number> format ("$" cannot appear in
69
+ // hashes so only *we* are generating such identifiers).
70
+
71
+ const identifier = `${nameSafe}$${count}`
72
+
73
+ assert(
74
+ isValidJsIdentifier(identifier),
75
+ `Unable to generate safe identifier for: "${name}"`,
76
+ )
77
+
78
+ return identifier
57
79
  }
58
80
 
59
81
  /**
@@ -108,9 +130,10 @@ export class RefResolver {
108
130
  // as base, and append a counter to avoid conflicts
109
131
  this.nextSafeDefinitionIdentifier(safeIdentifier)
110
132
  : // hash only contained unsafe characters, generate a safe one
111
- this.nextSafeDefinitionIdentifier('def')
133
+ this.nextSafeDefinitionIdentifier(hash)
112
134
 
113
135
  const typeName = ucFirst(varName)
136
+ assert(isSafeLocalIdentifier(typeName), 'Expected safe type identifier')
114
137
  assert(varName !== typeName, 'Variable and type name should be different')
115
138
 
116
139
  return { varName, typeName }
@@ -140,13 +163,33 @@ export class RefResolver {
140
163
  )
141
164
  }
142
165
 
166
+ const publicIds = getPublicIdentifiers(hash)
167
+
168
+ if (!isValidJsIdentifier(publicIds.typeName)) {
169
+ // If <typeName> is not a valid identifier, we cannot access the type
170
+ // using dot notation (<nsIdentifier>.<typeName>). Note that, unlike js
171
+ // variables, types cannot be accessed using string indexing (like:
172
+ // <nsIdentifier>['<typeName>']) because it generates TypeScript errors:
173
+ //
174
+ // > "Cannot use namespace '<nsIdentifier>' as a type."
175
+
176
+ // Instead the generated code should look like:
177
+ // import { "<unsafeTypeName>" as <safeIdentifier> } from './<moduleSpecifier>'
178
+
179
+ // Because it requires more complex management of local variables names,
180
+ // and we don't expect this to actually happen with properly designed
181
+ // lexicons documents, we do not support this for now.
182
+
183
+ throw new Error(
184
+ 'Import of definitions with unsafe type names is not supported',
185
+ )
186
+ }
187
+
143
188
  // import * as <nsIdentifier> from './<moduleSpecifier>'
144
189
  const nsIdentifier = this.getNsIdentifier(nsid, moduleSpecifier)
145
190
 
146
- const publicIds = getPublicIdentifiers(hash)
147
-
148
191
  return {
149
- varName: isSafeIdentifier(publicIds.varName)
192
+ varName: isValidJsIdentifier(publicIds.varName)
150
193
  ? `${nsIdentifier}.${publicIds.varName}`
151
194
  : `${nsIdentifier}[${JSON.stringify(publicIds.varName)}]`,
152
195
  typeName: `${nsIdentifier}.${publicIds.typeName}`,
@@ -195,7 +238,7 @@ export class RefResolver {
195
238
  }
196
239
 
197
240
  private conflictsWithKeywords(name: string) {
198
- return isReservedWord(name)
241
+ return isJsKeyword(name) || isGlobalIdentifier(name)
199
242
  }
200
243
 
201
244
  private conflictsWithUtils(name: string) {
@@ -273,7 +316,7 @@ function nsidToIdentifier(nsid: string) {
273
316
  // full NSID.
274
317
  for (let i = 2; i < parts.length; i++) {
275
318
  const identifier = toPascalCase(parts.slice(-i).join('.'))
276
- if (isSafeIdentifier(identifier)) return identifier
319
+ if (isSafeLocalIdentifier(identifier)) return identifier
277
320
  }
278
321
 
279
322
  return undefined
@@ -288,19 +331,35 @@ function nsidToIdentifier(nsid: string) {
288
331
  */
289
332
  export function getPublicIdentifiers(hash: string): ResolvedRef {
290
333
  const varName = hash
291
- // @NOTE Type names *must* be valid TypeScript identifiers (this is because,
292
- // unlike variable names, we cannot use string indexing to access exported
293
- // types).
294
- const typeName = toPascalCase(hash)
295
- if (!typeName || varName === typeName || !isSafeIdentifier(typeName)) {
296
- return { varName, typeName: `Def${typeName}` }
334
+
335
+ // @NOTE we try to circumvent the issue of unsafe type names described in
336
+ // `RefResolver.resolveExternal` by ensuring that type names are always safe
337
+ // identifiers, even if it means changing them from the original hash.
338
+ let typeName = toPascalCase(hash)
339
+
340
+ if (varName === typeName || !isValidJsIdentifier(typeName)) {
341
+ typeName = `TypeOf${typeName}`
297
342
  }
343
+
344
+ assert(
345
+ isValidJsIdentifier(typeName),
346
+ `Unable to generate a predictable safe identifier for "${hash}"`,
347
+ )
348
+
298
349
  return { varName, typeName }
299
350
  }
300
351
 
301
352
  function asSafeDefinitionIdentifier(name: string) {
302
- if (isSafeIdentifier(name) && isSafeIdentifier(ucFirst(name))) return name
353
+ if (
354
+ startsWithLower(name) &&
355
+ isSafeLocalIdentifier(name) &&
356
+ isSafeLocalIdentifier(ucFirst(name))
357
+ ) {
358
+ return name
359
+ }
303
360
  const camel = toCamelCase(name)
304
- if (isSafeIdentifier(camel) && isSafeIdentifier(ucFirst(camel))) return camel
361
+ if (isSafeLocalIdentifier(camel) && isSafeLocalIdentifier(ucFirst(camel))) {
362
+ return camel
363
+ }
305
364
  return undefined
306
365
  }