@atproto/lex-builder 0.0.8 → 0.0.9
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.
- package/CHANGELOG.md +16 -0
- package/dist/lex-builder.d.ts +8 -0
- package/dist/lex-builder.d.ts.map +1 -1
- package/dist/lex-builder.js +14 -3
- package/dist/lex-builder.js.map +1 -1
- package/dist/ref-resolver.d.ts.map +1 -1
- package/dist/ref-resolver.js +6 -2
- package/dist/ref-resolver.js.map +1 -1
- package/dist/ts-lang.d.ts +9 -1
- package/dist/ts-lang.d.ts.map +1 -1
- package/dist/ts-lang.js +67 -40
- package/dist/ts-lang.js.map +1 -1
- package/dist/util.d.ts +1 -0
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +5 -0
- package/dist/util.js.map +1 -1
- package/package.json +3 -3
- package/src/lex-builder.ts +28 -4
- package/src/ref-resolver.ts +11 -2
- package/src/ts-lang.ts +81 -40
- package/src/util.ts +5 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @atproto/lex-builder
|
|
2
2
|
|
|
3
|
+
## 0.0.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4501](https://github.com/bluesky-social/atproto/pull/4501) [`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Better prevent use of conflicting identifiers in generated code
|
|
8
|
+
|
|
9
|
+
- [#4501](https://github.com/bluesky-social/atproto/pull/4501) [`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add an `indexFile` option that allows generating an "index.ts" file that re-exports every tld namespaces.
|
|
10
|
+
|
|
11
|
+
- [#4501](https://github.com/bluesky-social/atproto/pull/4501) [`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Ensure generated "import" identifiers start with a lower case letter
|
|
12
|
+
|
|
13
|
+
- [#4501](https://github.com/bluesky-social/atproto/pull/4501) [`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Avoid escaping export identifier when it is a known global
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [[`2f78893`](https://github.com/bluesky-social/atproto/commit/2f78893ace3bbf14d4bac36837820ddb46658c98)]:
|
|
16
|
+
- @atproto/lex-schema@0.0.7
|
|
17
|
+
- @atproto/lex-document@0.0.8
|
|
18
|
+
|
|
3
19
|
## 0.0.8
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/lex-builder.d.ts
CHANGED
|
@@ -3,6 +3,14 @@ import { FormatterOptions } from './formatter.js';
|
|
|
3
3
|
import { LexDefBuilderOptions } from './lex-def-builder.js';
|
|
4
4
|
import { LexiconDirectoryIndexerOptions } from './lexicon-directory-indexer.js';
|
|
5
5
|
export type LexBuilderOptions = LexDefBuilderOptions & {
|
|
6
|
+
/**
|
|
7
|
+
* Whether to generate an index file at the root exporting all top-level
|
|
8
|
+
* namespaces.
|
|
9
|
+
*
|
|
10
|
+
* @note This could theoretically cause name conflicts if a
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
indexFile?: boolean;
|
|
6
14
|
importExt?: string;
|
|
7
15
|
fileExt?: string;
|
|
8
16
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-builder.d.ts","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"lex-builder.d.ts","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAA;AAE7D,OAAO,EAAa,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAC5D,OAAO,EAAiB,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAC1E,OAAO,EAEL,8BAA8B,EAC/B,MAAM,gCAAgC,CAAA;AAGvC,MAAM,MAAM,iBAAiB,GAAG,oBAAoB,GAAG;IACrD;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,qBAAqB,GAAG,8BAA8B,GAChE,kBAAkB,CAAA;AAEpB,MAAM,MAAM,qBAAqB,GAAG,gBAAgB,GAAG;IACrD,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,qBAAa,UAAU;;IAOT,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,GAAE,iBAAsB;IAE5D,IAAI,OAAO,WAEV;IAED,IAAI,SAAS,WAEZ;IAEY,IAAI,CAAC,OAAO,EAAE,qBAAqB;IAkBnC,IAAI,CAAC,OAAO,EAAE,qBAAqB;IA4BhD,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,OAAO;YAID,gBAAgB;YAyDhB,cAAc;CAU7B"}
|
package/dist/lex-builder.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LexBuilder = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const node_assert_1 = tslib_1.__importDefault(require("node:assert"));
|
|
5
6
|
const promises_1 = require("node:fs/promises");
|
|
6
7
|
const node_path_1 = require("node:path");
|
|
7
8
|
const ts_morph_1 = require("ts-morph");
|
|
@@ -78,6 +79,18 @@ class LexBuilder {
|
|
|
78
79
|
}
|
|
79
80
|
async createExportTree(doc) {
|
|
80
81
|
const namespaces = doc.id.split('.');
|
|
82
|
+
if (this.options.indexFile) {
|
|
83
|
+
const indexFile = this.getFile(`/index${this.fileExt}`);
|
|
84
|
+
const tldNs = namespaces[0];
|
|
85
|
+
(0, node_assert_1.default)(tldNs !== 'index', 'The "indexFile" options cannot be used with namespaces using a ".index" tld.');
|
|
86
|
+
const tldNsSpecifier = `./${tldNs}${this.importExt}`;
|
|
87
|
+
if (!indexFile.getExportDeclaration(tldNsSpecifier)) {
|
|
88
|
+
indexFile.addExportDeclaration({
|
|
89
|
+
moduleSpecifier: tldNsSpecifier,
|
|
90
|
+
namespaceExport: (0, ts_lang_js_1.asNamespaceExport)(tldNs),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
81
94
|
// First create the parent namespaces
|
|
82
95
|
for (let i = 0; i < namespaces.length - 1; i++) {
|
|
83
96
|
const currentNs = namespaces[i];
|
|
@@ -89,9 +102,7 @@ class LexBuilder {
|
|
|
89
102
|
if (!dec) {
|
|
90
103
|
file.addExportDeclaration({
|
|
91
104
|
moduleSpecifier: childModuleSpecifier,
|
|
92
|
-
namespaceExport: (0, ts_lang_js_1.
|
|
93
|
-
? childNs
|
|
94
|
-
: JSON.stringify(childNs),
|
|
105
|
+
namespaceExport: (0, ts_lang_js_1.asNamespaceExport)(childNs),
|
|
95
106
|
});
|
|
96
107
|
}
|
|
97
108
|
}
|
package/dist/lex-builder.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-builder.js","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":";;;;AAAA,+CAA6D;AAC7D,yCAAyC;AACzC,uCAAmD;AAEnD,2CAA6D;AAC7D,+DAAuD;AACvD,iDAA4D;AAC5D,6DAA0E;AAC1E,iFAGuC;AACvC,6CAA+C;AAgB/C,MAAa,UAAU;IAOQ;IANpB,SAAS,GAAG,IAAI,GAAG,EAAU,CAAA;IAC7B,QAAQ,GAAG,IAAI,kBAAO,CAAC;QAC9B,q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I,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAM;QAC1E,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC","sourcesContent":["import { mkdir, rm, stat, writeFile } from 'node:fs/promises'\nimport { join, resolve } from 'node:path'\nimport { IndentationText, Project } from 'ts-morph'\nimport { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'\nimport { BuildFilterOptions, buildFilter } from './filter.js'\nimport { FilteredIndexer } from './filtered-indexer.js'\nimport { Formatter, FormatterOptions } from './formatter.js'\nimport { LexDefBuilder, LexDefBuilderOptions } from './lex-def-builder.js'\nimport {\n LexiconDirectoryIndexer,\n LexiconDirectoryIndexerOptions,\n} from './lexicon-directory-indexer.js'\nimport { isSafeIdentifier } from './ts-lang.js'\n\nexport type LexBuilderOptions = LexDefBuilderOptions & {\n importExt?: string\n fileExt?: string\n}\n\nexport type LexBuilderLoadOptions = LexiconDirectoryIndexerOptions &\n BuildFilterOptions\n\nexport type LexBuilderSaveOptions = FormatterOptions & {\n out: string\n clear?: boolean\n override?: boolean\n}\n\nexport class LexBuilder {\n readonly #imported = new Set<string>()\n readonly #project = new Project({\n useInMemoryFileSystem: true,\n manipulationSettings: { indentationText: IndentationText.TwoSpaces },\n })\n\n constructor(private readonly options: LexBuilderOptions = {}) {}\n\n get fileExt() {\n return this.options.fileExt ?? '.ts'\n }\n\n get importExt() {\n return this.options.importExt ?? '.js'\n }\n\n public async load(options: LexBuilderLoadOptions) {\n await using indexer = new FilteredIndexer(\n new LexiconDirectoryIndexer(options),\n buildFilter(options),\n )\n\n for await (const doc of indexer) {\n if (!this.#imported.has(doc.id)) {\n this.#imported.add(doc.id)\n } else {\n throw new Error(`Duplicate lexicon document id: ${doc.id}`)\n }\n\n await this.createDefsFile(doc, indexer)\n await this.createExportTree(doc)\n }\n }\n\n public async save(options: LexBuilderSaveOptions) {\n const files = this.#project.getSourceFiles()\n\n const destination = resolve(options.out)\n\n if (options.clear) {\n await rm(destination, { recursive: true, force: true })\n } else if (!options.override) {\n await Promise.all(\n files.map(async (f) =>\n assertNotFileExists(join(destination, f.getFilePath())),\n ),\n )\n }\n\n const formatter = new Formatter(options)\n\n await Promise.all(\n Array.from(files, async (file) => {\n const filePath = join(destination, file.getFilePath())\n const content = await formatter.format(file.getFullText())\n await mkdir(join(filePath, '..'), { recursive: true })\n await rm(filePath, { recursive: true, force: true })\n await writeFile(filePath, content, 'utf8')\n }),\n )\n }\n\n private createFile(path: string) {\n return this.#project.createSourceFile(path)\n }\n\n private getFile(path: string) {\n return this.#project.getSourceFile(path) || this.createFile(path)\n }\n\n private async createExportTree(doc: LexiconDocument) {\n const namespaces = doc.id.split('.')\n\n // First create the parent namespaces\n for (let i = 0; i < namespaces.length - 1; i++) {\n const currentNs = namespaces[i]\n const childNs = namespaces[i + 1]\n\n const path = join('/', ...namespaces.slice(0, i + 1))\n const file = this.getFile(`${path}${this.fileExt}`)\n\n const childModuleSpecifier = `./${currentNs}/${childNs}${this.importExt}`\n const dec = file.getExportDeclaration(childModuleSpecifier)\n if (!dec) {\n file.addExportDeclaration({\n moduleSpecifier: childModuleSpecifier,\n namespaceExport: isSafeIdentifier(childNs)\n ? childNs\n : JSON.stringify(childNs),\n })\n }\n }\n\n // The child file exports the schemas (as *)\n const path = join('/', ...namespaces)\n const file = this.getFile(`${path}${this.fileExt}`)\n\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n })\n\n // @NOTE Individual exports exports from the defs file might conflict with\n // child namespaces. For this reason, we also add a namespace export for the\n // defs (export * as $defs from './xyz.defs'). This is an escape hatch\n // allowing to still access the definitions if a hash get shadowed by a\n // child namespace.\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n namespaceExport: '$defs',\n })\n }\n\n private async createDefsFile(\n doc: LexiconDocument,\n indexer: LexiconIndexer,\n ): Promise<void> {\n const path = join('/', ...doc.id.split('.'))\n const file = this.createFile(`${path}.defs${this.fileExt}`)\n\n const fileBuilder = new LexDefBuilder(this.options, file, doc, indexer)\n await fileBuilder.build()\n }\n}\n\nasync function assertNotFileExists(file: string): Promise<void> {\n try {\n await stat(file)\n throw new Error(`File already exists: ${file}`)\n } catch (err) {\n if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return\n throw err\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"lex-builder.js","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":";;;;AAAA,sEAAgC;AAChC,+CAA6D;AAC7D,yCAAyC;AACzC,uCAAmD;AAEnD,2CAA6D;AAC7D,+DAAuD;AACvD,iDAA4D;AAC5D,6DAA0E;AAC1E,iFAGuC;AACvC,6CAAgD;AAwBhD,MAAa,UAAU;IAOQ;IANpB,SAAS,GAAG,IAAI,GAAG,EAAU,CAAA;IAC7B,QAAQ,GAAG,IAAI,kBAAO,CAAC;QAC9B,qBAAqB,EAAE,IAAI;QAC3B,oBAAoB,EAAE,EAAE,eAAe,EAAE,0BAAe,CAAC,SAAS,EAAE;KACrE,CAAC,CAAA;IAEF,YAA6B,UAA6B,EAAE;QAA/B,YAAO,GAAP,OAAO,CAAwB;IAAG,CAAC;IAEhE,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,KAAK,CAAA;IACtC,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,KAAK,CAAA;IACxC,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,OAA8B;;;YAC9C,MAAY,OAAO,0CAAG,IAAI,qCAAe,CACvC,IAAI,sDAAuB,CAAC,OAAO,CAAC,EACpC,IAAA,uBAAW,EAAC,OAAO,CAAC,CACrB,OAAA,CAAA;YAED,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;gBAChC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EA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ve } from 'node:path'\nimport { IndentationText, Project } from 'ts-morph'\nimport { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'\nimport { BuildFilterOptions, buildFilter } from './filter.js'\nimport { FilteredIndexer } from './filtered-indexer.js'\nimport { Formatter, FormatterOptions } from './formatter.js'\nimport { LexDefBuilder, LexDefBuilderOptions } from './lex-def-builder.js'\nimport {\n LexiconDirectoryIndexer,\n LexiconDirectoryIndexerOptions,\n} from './lexicon-directory-indexer.js'\nimport { asNamespaceExport } from './ts-lang.js'\n\nexport type LexBuilderOptions = LexDefBuilderOptions & {\n /**\n * Whether to generate an index file at the root exporting all top-level\n * namespaces.\n *\n * @note This could theoretically cause name conflicts if a\n * @default false\n */\n indexFile?: boolean\n importExt?: string\n fileExt?: string\n}\n\nexport type LexBuilderLoadOptions = LexiconDirectoryIndexerOptions &\n BuildFilterOptions\n\nexport type LexBuilderSaveOptions = FormatterOptions & {\n out: string\n clear?: boolean\n override?: boolean\n}\n\nexport class LexBuilder {\n readonly #imported = new Set<string>()\n readonly #project = new Project({\n useInMemoryFileSystem: true,\n manipulationSettings: { indentationText: IndentationText.TwoSpaces },\n })\n\n constructor(private readonly options: LexBuilderOptions = {}) {}\n\n get fileExt() {\n return this.options.fileExt ?? '.ts'\n }\n\n get importExt() {\n return this.options.importExt ?? '.js'\n }\n\n public async load(options: LexBuilderLoadOptions) {\n await using indexer = new FilteredIndexer(\n new LexiconDirectoryIndexer(options),\n buildFilter(options),\n )\n\n for await (const doc of indexer) {\n if (!this.#imported.has(doc.id)) {\n this.#imported.add(doc.id)\n } else {\n throw new Error(`Duplicate lexicon document id: ${doc.id}`)\n }\n\n await this.createDefsFile(doc, indexer)\n await this.createExportTree(doc)\n }\n }\n\n public async save(options: LexBuilderSaveOptions) {\n const files = this.#project.getSourceFiles()\n\n const destination = resolve(options.out)\n\n if (options.clear) {\n await rm(destination, { recursive: true, force: true })\n } else if (!options.override) {\n await Promise.all(\n files.map(async (f) =>\n assertNotFileExists(join(destination, f.getFilePath())),\n ),\n )\n }\n\n const formatter = new Formatter(options)\n\n await Promise.all(\n Array.from(files, async (file) => {\n const filePath = join(destination, file.getFilePath())\n const content = await formatter.format(file.getFullText())\n await mkdir(join(filePath, '..'), { recursive: true })\n await rm(filePath, { recursive: true, force: true })\n await writeFile(filePath, content, 'utf8')\n }),\n )\n }\n\n private createFile(path: string) {\n return this.#project.createSourceFile(path)\n }\n\n private getFile(path: string) {\n return this.#project.getSourceFile(path) || this.createFile(path)\n }\n\n private async createExportTree(doc: LexiconDocument) {\n const namespaces = doc.id.split('.')\n\n if (this.options.indexFile) {\n const indexFile = this.getFile(`/index${this.fileExt}`)\n\n const tldNs = namespaces[0]!\n assert(\n tldNs !== 'index',\n 'The \"indexFile\" options cannot be used with namespaces using a \".index\" tld.',\n )\n const tldNsSpecifier = `./${tldNs}${this.importExt}`\n if (!indexFile.getExportDeclaration(tldNsSpecifier)) {\n indexFile.addExportDeclaration({\n moduleSpecifier: tldNsSpecifier,\n namespaceExport: asNamespaceExport(tldNs),\n })\n }\n }\n\n // First create the parent namespaces\n for (let i = 0; i < namespaces.length - 1; i++) {\n const currentNs = namespaces[i]\n const childNs = namespaces[i + 1]\n\n const path = join('/', ...namespaces.slice(0, i + 1))\n const file = this.getFile(`${path}${this.fileExt}`)\n\n const childModuleSpecifier = `./${currentNs}/${childNs}${this.importExt}`\n const dec = file.getExportDeclaration(childModuleSpecifier)\n if (!dec) {\n file.addExportDeclaration({\n moduleSpecifier: childModuleSpecifier,\n namespaceExport: asNamespaceExport(childNs),\n })\n }\n }\n\n // The child file exports the schemas (as *)\n const path = join('/', ...namespaces)\n const file = this.getFile(`${path}${this.fileExt}`)\n\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n })\n\n // @NOTE Individual exports exports from the defs file might conflict with\n // child namespaces. For this reason, we also add a namespace export for the\n // defs (export * as $defs from './xyz.defs'). This is an escape hatch\n // allowing to still access the definitions if a hash get shadowed by a\n // child namespace.\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n namespaceExport: '$defs',\n })\n }\n\n private async createDefsFile(\n doc: LexiconDocument,\n indexer: LexiconIndexer,\n ): Promise<void> {\n const path = join('/', ...doc.id.split('.'))\n const file = this.createFile(`${path}.defs${this.fileExt}`)\n\n const fileBuilder = new LexDefBuilder(this.options, file, doc, indexer)\n await fileBuilder.build()\n }\n}\n\nasync function assertNotFileExists(file: string): Promise<void> {\n try {\n await stat(file)\n throw new Error(`File already exists: ${file}`)\n } catch (err) {\n if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return\n throw err\n }\n}\n"]}
|
|
@@ -1 +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;
|
|
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;AAWvE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,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;IACf,OAAO,CAAC,OAAO;gBAHP,GAAG,EAAE,eAAe,EACpB,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,kBAAkB;IAGrC,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"}
|
package/dist/ref-resolver.js
CHANGED
|
@@ -238,11 +238,15 @@ function getPublicIdentifiers(hash) {
|
|
|
238
238
|
return { varName, typeName };
|
|
239
239
|
}
|
|
240
240
|
function asSafeDefinitionIdentifier(name) {
|
|
241
|
-
if ((0,
|
|
241
|
+
if ((0, util_js_1.startsWithLowerLetter)(name) &&
|
|
242
|
+
(0, ts_lang_js_1.isSafeIdentifier)(name) &&
|
|
243
|
+
(0, ts_lang_js_1.isSafeIdentifier)((0, util_js_1.ucFirst)(name))) {
|
|
242
244
|
return name;
|
|
245
|
+
}
|
|
243
246
|
const camel = (0, util_js_1.toCamelCase)(name);
|
|
244
|
-
if ((0, ts_lang_js_1.isSafeIdentifier)(camel) && (0, ts_lang_js_1.isSafeIdentifier)((0, util_js_1.ucFirst)(camel)))
|
|
247
|
+
if ((0, ts_lang_js_1.isSafeIdentifier)(camel) && (0, ts_lang_js_1.isSafeIdentifier)((0, util_js_1.ucFirst)(camel))) {
|
|
245
248
|
return camel;
|
|
249
|
+
}
|
|
246
250
|
return undefined;
|
|
247
251
|
}
|
|
248
252
|
//# sourceMappingURL=ref-resolver.js.map
|
package/dist/ref-resolver.js.map
CHANGED
|
@@ -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,CAAC,cAAc,EAAE,KAAK,GAAG,CAAC,CAAC,CAAA;Q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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":";;;AAiSA,oDAUC;;AA3SD,sEAAgC;AAChC,yCAAgC;AAGhC,6CAA+D;AAC/D,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,cAAsB;QACzD,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA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ph'\nimport { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'\nimport { isReservedWord, isSafeIdentifier } from './ts-lang.js'\nimport {\n asRelativePath,\n memoize,\n startsWithLowerLetter,\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 (\n startsWithLowerLetter(name) &&\n isSafeIdentifier(name) &&\n isSafeIdentifier(ucFirst(name))\n ) {\n return name\n }\n const camel = toCamelCase(name)\n if (isSafeIdentifier(camel) && isSafeIdentifier(ucFirst(camel))) {\n return camel\n }\n return undefined\n}\n"]}
|
package/dist/ts-lang.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export declare function isJsKeyword(word: string): boolean;
|
|
2
|
+
export declare function isGlobalIdentifier(word: string): boolean;
|
|
1
3
|
export declare function isReservedWord(word: string): boolean;
|
|
2
|
-
|
|
4
|
+
type SafeIdentifierOptions = {
|
|
5
|
+
/** Defaults to `false` */
|
|
6
|
+
allowGlobal?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare function isSafeIdentifier(name: string, options?: SafeIdentifierOptions): boolean;
|
|
9
|
+
export declare function asNamespaceExport(name: string): string;
|
|
10
|
+
export {};
|
|
3
11
|
//# sourceMappingURL=ts-lang.d.ts.map
|
package/dist/ts-lang.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ts-lang.d.ts","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":"AA+
|
|
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;AAwDD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,WAO9C;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,WAE1C;AAED,KAAK,qBAAqB,GAAG;IAC3B,0BAA0B;IAC1B,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAED,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,qBAAqB,WAOhC;AAMD,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,UAI7C"}
|
package/dist/ts-lang.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isJsKeyword = isJsKeyword;
|
|
4
|
+
exports.isGlobalIdentifier = isGlobalIdentifier;
|
|
3
5
|
exports.isReservedWord = isReservedWord;
|
|
4
6
|
exports.isSafeIdentifier = isSafeIdentifier;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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,29 +82,20 @@ const RESERVED_WORDS = new Set([
|
|
|
77
82
|
'while',
|
|
78
83
|
'with',
|
|
79
84
|
'yield',
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
'
|
|
87
|
-
|
|
88
|
-
'
|
|
89
|
-
'
|
|
90
|
-
'
|
|
91
|
-
'
|
|
92
|
-
'
|
|
93
|
-
|
|
94
|
-
'Set',
|
|
95
|
-
'String',
|
|
96
|
-
'Symbol',
|
|
97
|
-
'console',
|
|
98
|
-
'document',
|
|
99
|
-
'global',
|
|
100
|
-
'globalThis',
|
|
101
|
-
'window',
|
|
102
|
-
// Test globals
|
|
85
|
+
]);
|
|
86
|
+
function isJsKeyword(word) {
|
|
87
|
+
return JS_KEYWORDS.has(word);
|
|
88
|
+
}
|
|
89
|
+
const WELL_KNOWN_GLOBALS = new Set([
|
|
90
|
+
// ESM
|
|
91
|
+
'import',
|
|
92
|
+
// CommonJS
|
|
93
|
+
'__dirname',
|
|
94
|
+
'__filename',
|
|
95
|
+
'require',
|
|
96
|
+
'module',
|
|
97
|
+
'exports',
|
|
98
|
+
// Jest
|
|
103
99
|
'afterAll',
|
|
104
100
|
'afterEach',
|
|
105
101
|
'assert',
|
|
@@ -109,30 +105,61 @@ const RESERVED_WORDS = new Set([
|
|
|
109
105
|
'expect',
|
|
110
106
|
'it',
|
|
111
107
|
'test',
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
'require',
|
|
116
|
-
'module',
|
|
117
|
-
'exports',
|
|
118
|
-
// TypeScript
|
|
119
|
-
'Record',
|
|
108
|
+
]);
|
|
109
|
+
const TYPE_SCRIPT_GLOBALS = new Set([
|
|
110
|
+
// Primitives
|
|
120
111
|
'any',
|
|
112
|
+
'bigint',
|
|
113
|
+
'boolean',
|
|
121
114
|
'declare',
|
|
122
115
|
'never',
|
|
116
|
+
'null',
|
|
123
117
|
'number',
|
|
124
118
|
'object',
|
|
125
119
|
'string',
|
|
126
120
|
'symbol',
|
|
121
|
+
'undefined',
|
|
127
122
|
'unknown',
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
'
|
|
123
|
+
'void',
|
|
124
|
+
// Utility types
|
|
125
|
+
'Record',
|
|
126
|
+
'Partial',
|
|
127
|
+
'Readonly',
|
|
128
|
+
'Pick',
|
|
129
|
+
'Omit',
|
|
130
|
+
'Exclude',
|
|
131
|
+
'Extract',
|
|
132
|
+
'InstanceType',
|
|
133
|
+
'ReturnType',
|
|
134
|
+
'Required',
|
|
135
|
+
'ThisType',
|
|
136
|
+
'Uppercase',
|
|
137
|
+
'Lowercase',
|
|
138
|
+
'Capitalize',
|
|
139
|
+
'Uncapitalize',
|
|
131
140
|
]);
|
|
141
|
+
function isGlobalIdentifier(word) {
|
|
142
|
+
return (
|
|
143
|
+
// Should cover most common globals
|
|
144
|
+
word in globalThis ||
|
|
145
|
+
WELL_KNOWN_GLOBALS.has(word) ||
|
|
146
|
+
TYPE_SCRIPT_GLOBALS.has(word));
|
|
147
|
+
}
|
|
132
148
|
function isReservedWord(word) {
|
|
133
|
-
return
|
|
149
|
+
return isJsKeyword(word) || isGlobalIdentifier(word);
|
|
150
|
+
}
|
|
151
|
+
function isSafeIdentifier(name, options) {
|
|
152
|
+
if (!options?.allowGlobal && isGlobalIdentifier(name)) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
return isValidJsIdentifier(name);
|
|
156
|
+
}
|
|
157
|
+
function isValidJsIdentifier(name) {
|
|
158
|
+
return !isJsKeyword(name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
134
159
|
}
|
|
135
|
-
function
|
|
136
|
-
return
|
|
160
|
+
function asNamespaceExport(name) {
|
|
161
|
+
return isSafeIdentifier(name, { allowGlobal: true })
|
|
162
|
+
? name
|
|
163
|
+
: JSON.stringify(name);
|
|
137
164
|
}
|
|
138
165
|
//# sourceMappingURL=ts-lang.js.map
|
package/dist/ts-lang.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ts-lang.js","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":";;AA+
|
|
1
|
+
{"version":3,"file":"ts-lang.js","sourceRoot":"","sources":["../src/ts-lang.ts"],"names":[],"mappings":";;AA+EA,kCAEC;AAwDD,gDAOC;AAED,wCAEC;AAOD,4CASC;AAMD,8CAIC;AA9KD;;;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,OAAO;IACP,OAAO;IACP,UAAU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;IACN,gBAAgB;IAChB,QAAQ;IACR,SAAS;IACT,UAAU;IACV,MAAM;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;IACL,mCAAmC;IACnC,IAAI,IAAI,UAAU;QAClB,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC;QAC5B,mBAAmB,CAAC,GAAG,CAAC,IAAI,CAAC,CAC9B,CAAA;AACH,CAAC;AAED,SAAgB,cAAc,CAAC,IAAY;IACzC,OAAO,WAAW,CAAC,IAAI,CAAC,IAAI,kBAAkB,CAAC,IAAI,CAAC,CAAA;AACtD,CAAC;AAOD,SAAgB,gBAAgB,CAC9B,IAAY,EACZ,OAA+B;IAE/B,IAAI,CAAC,OAAO,EAAE,WAAW,IAAI,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;QACtD,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAA;AAClC,CAAC;AAED,SAAS,mBAAmB,CAAC,IAAY;IACvC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACtE,CAAC;AAED,SAAgB,iBAAiB,CAAC,IAAY;IAC5C,OAAO,gBAAgB,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;QAClD,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;AAC1B,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\nconst WELL_KNOWN_GLOBALS = new Set([\n // ESM\n 'import',\n // CommonJS\n '__dirname',\n '__filename',\n 'require',\n 'module',\n 'exports',\n // Jest\n 'afterAll',\n 'afterEach',\n 'assert',\n 'beforeAll',\n 'beforeEach',\n 'describe',\n 'expect',\n 'it',\n 'test',\n])\n\nconst TYPE_SCRIPT_GLOBALS = new Set([\n // 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 // 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 (\n // Should cover most common globals\n word in globalThis ||\n WELL_KNOWN_GLOBALS.has(word) ||\n TYPE_SCRIPT_GLOBALS.has(word)\n )\n}\n\nexport function isReservedWord(word: string) {\n return isJsKeyword(word) || isGlobalIdentifier(word)\n}\n\ntype SafeIdentifierOptions = {\n /** Defaults to `false` */\n allowGlobal?: boolean\n}\n\nexport function isSafeIdentifier(\n name: string,\n options?: SafeIdentifierOptions,\n) {\n if (!options?.allowGlobal && isGlobalIdentifier(name)) {\n return false\n }\n\n return isValidJsIdentifier(name)\n}\n\nfunction isValidJsIdentifier(name: string) {\n return !isJsKeyword(name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n\nexport function asNamespaceExport(name: string) {\n return isSafeIdentifier(name, { allowGlobal: true })\n ? name\n : 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 startsWithLowerLetter(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;
|
package/dist/util.d.ts.map
CHANGED
|
@@ -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,qBAAqB,CAAC,GAAG,EAAE,MAAM,WAGhD;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.startsWithLowerLetter = startsWithLowerLetter;
|
|
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 startsWithLowerLetter(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;
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAEA,0BAWC;AAED,sDAGC;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,CAAC,CAAM,CAAA;AACT,CAAC;AAED,S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AE,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 startsWithLowerLetter(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.
|
|
3
|
+
"version": "0.0.9",
|
|
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.
|
|
43
|
-
"@atproto/lex-schema": "0.0.
|
|
42
|
+
"@atproto/lex-document": "0.0.8",
|
|
43
|
+
"@atproto/lex-schema": "0.0.7"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@ts-morph/common": "^0.28.0",
|
package/src/lex-builder.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
117
|
-
? childNs
|
|
118
|
-
: JSON.stringify(childNs),
|
|
142
|
+
namespaceExport: asNamespaceExport(childNs),
|
|
119
143
|
})
|
|
120
144
|
}
|
|
121
145
|
}
|
package/src/ref-resolver.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { isReservedWord, isSafeIdentifier } from './ts-lang.js'
|
|
|
6
6
|
import {
|
|
7
7
|
asRelativePath,
|
|
8
8
|
memoize,
|
|
9
|
+
startsWithLowerLetter,
|
|
9
10
|
toCamelCase,
|
|
10
11
|
toPascalCase,
|
|
11
12
|
ucFirst,
|
|
@@ -299,8 +300,16 @@ export function getPublicIdentifiers(hash: string): ResolvedRef {
|
|
|
299
300
|
}
|
|
300
301
|
|
|
301
302
|
function asSafeDefinitionIdentifier(name: string) {
|
|
302
|
-
if (
|
|
303
|
+
if (
|
|
304
|
+
startsWithLowerLetter(name) &&
|
|
305
|
+
isSafeIdentifier(name) &&
|
|
306
|
+
isSafeIdentifier(ucFirst(name))
|
|
307
|
+
) {
|
|
308
|
+
return name
|
|
309
|
+
}
|
|
303
310
|
const camel = toCamelCase(name)
|
|
304
|
-
if (isSafeIdentifier(camel) && isSafeIdentifier(ucFirst(camel)))
|
|
311
|
+
if (isSafeIdentifier(camel) && isSafeIdentifier(ucFirst(camel))) {
|
|
312
|
+
return camel
|
|
313
|
+
}
|
|
305
314
|
return undefined
|
|
306
315
|
}
|
package/src/ts-lang.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* JavaScript keywords
|
|
3
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar}
|
|
4
|
+
*/
|
|
5
|
+
const JS_KEYWORDS = new Set([
|
|
4
6
|
'abstract',
|
|
5
7
|
'arguments',
|
|
6
8
|
'as',
|
|
@@ -73,29 +75,22 @@ const RESERVED_WORDS = new Set([
|
|
|
73
75
|
'while',
|
|
74
76
|
'with',
|
|
75
77
|
'yield',
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
'
|
|
85
|
-
|
|
86
|
-
'
|
|
87
|
-
'
|
|
88
|
-
'
|
|
89
|
-
'
|
|
90
|
-
'
|
|
91
|
-
|
|
92
|
-
'Symbol',
|
|
93
|
-
'console',
|
|
94
|
-
'document',
|
|
95
|
-
'global',
|
|
96
|
-
'globalThis',
|
|
97
|
-
'window',
|
|
98
|
-
// Test globals
|
|
78
|
+
])
|
|
79
|
+
|
|
80
|
+
export function isJsKeyword(word: string) {
|
|
81
|
+
return JS_KEYWORDS.has(word)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const WELL_KNOWN_GLOBALS = new Set([
|
|
85
|
+
// ESM
|
|
86
|
+
'import',
|
|
87
|
+
// CommonJS
|
|
88
|
+
'__dirname',
|
|
89
|
+
'__filename',
|
|
90
|
+
'require',
|
|
91
|
+
'module',
|
|
92
|
+
'exports',
|
|
93
|
+
// Jest
|
|
99
94
|
'afterAll',
|
|
100
95
|
'afterEach',
|
|
101
96
|
'assert',
|
|
@@ -105,30 +100,76 @@ const RESERVED_WORDS = new Set([
|
|
|
105
100
|
'expect',
|
|
106
101
|
'it',
|
|
107
102
|
'test',
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
'module',
|
|
113
|
-
'exports',
|
|
114
|
-
// TypeScript
|
|
115
|
-
'Record',
|
|
103
|
+
])
|
|
104
|
+
|
|
105
|
+
const TYPE_SCRIPT_GLOBALS = new Set([
|
|
106
|
+
// Primitives
|
|
116
107
|
'any',
|
|
108
|
+
'bigint',
|
|
109
|
+
'boolean',
|
|
117
110
|
'declare',
|
|
118
111
|
'never',
|
|
112
|
+
'null',
|
|
119
113
|
'number',
|
|
120
114
|
'object',
|
|
121
115
|
'string',
|
|
122
116
|
'symbol',
|
|
117
|
+
'undefined',
|
|
123
118
|
'unknown',
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
'
|
|
119
|
+
'void',
|
|
120
|
+
// Utility types
|
|
121
|
+
'Record',
|
|
122
|
+
'Partial',
|
|
123
|
+
'Readonly',
|
|
124
|
+
'Pick',
|
|
125
|
+
'Omit',
|
|
126
|
+
'Exclude',
|
|
127
|
+
'Extract',
|
|
128
|
+
'InstanceType',
|
|
129
|
+
'ReturnType',
|
|
130
|
+
'Required',
|
|
131
|
+
'ThisType',
|
|
132
|
+
'Uppercase',
|
|
133
|
+
'Lowercase',
|
|
134
|
+
'Capitalize',
|
|
135
|
+
'Uncapitalize',
|
|
127
136
|
])
|
|
137
|
+
|
|
138
|
+
export function isGlobalIdentifier(word: string) {
|
|
139
|
+
return (
|
|
140
|
+
// Should cover most common globals
|
|
141
|
+
word in globalThis ||
|
|
142
|
+
WELL_KNOWN_GLOBALS.has(word) ||
|
|
143
|
+
TYPE_SCRIPT_GLOBALS.has(word)
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
128
147
|
export function isReservedWord(word: string) {
|
|
129
|
-
return
|
|
148
|
+
return isJsKeyword(word) || isGlobalIdentifier(word)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
type SafeIdentifierOptions = {
|
|
152
|
+
/** Defaults to `false` */
|
|
153
|
+
allowGlobal?: boolean
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function isSafeIdentifier(
|
|
157
|
+
name: string,
|
|
158
|
+
options?: SafeIdentifierOptions,
|
|
159
|
+
) {
|
|
160
|
+
if (!options?.allowGlobal && isGlobalIdentifier(name)) {
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return isValidJsIdentifier(name)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isValidJsIdentifier(name: string) {
|
|
168
|
+
return !isJsKeyword(name) && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)
|
|
130
169
|
}
|
|
131
170
|
|
|
132
|
-
export function
|
|
133
|
-
return
|
|
171
|
+
export function asNamespaceExport(name: string) {
|
|
172
|
+
return isSafeIdentifier(name, { allowGlobal: true })
|
|
173
|
+
? name
|
|
174
|
+
: JSON.stringify(name)
|
|
134
175
|
}
|
package/src/util.ts
CHANGED
|
@@ -13,6 +13,11 @@ export function memoize<T extends (arg: string) => NonNullable<unknown> | null>(
|
|
|
13
13
|
}) as T
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export function startsWithLowerLetter(str: string) {
|
|
17
|
+
const code = str.charCodeAt(0)
|
|
18
|
+
return code >= 97 && code <= 122 // 'a' to 'z'
|
|
19
|
+
}
|
|
20
|
+
|
|
16
21
|
export function ucFirst(str: string) {
|
|
17
22
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
18
23
|
}
|