fork of hey-api/openapi-ts because I need some additional things

Merge branch 'main' into feat/name-dedup

Lubos 052341eb 19194051

+62 -46
+1
docs/partials/contributors-list.md
··· 60 60 - [Michiel Lankamp](https://github.com/mlankamp) 61 61 - [Mika Vilpas](https://github.com/mikavilpas) 62 62 - [Miklos](https://github.com/jumika) 63 + - [Nacho García](https://github.com/nachogarcia) 63 64 - [Nicolas Chaulet](https://github.com/nicolas-chaulet) 64 65 - [Nimo Beeren](https://github.com/nimobeeren) 65 66 - [Novak Antonijevic](https://github.com/NovakAnton)
+13 -4
packages/codegen-core/src/__tests__/exports.test.ts
··· 4 4 5 5 const constExports = [ 6 6 'debug', 7 + 'defaultExtensions', 8 + 'defaultNameConflictResolvers', 9 + 'File', 7 10 'fromRef', 8 11 'fromRefs', 9 12 'isNode', ··· 15 18 'Project', 16 19 'ref', 17 20 'refs', 21 + 'simpleNameConflictResolver', 18 22 'Symbol', 19 23 'symbolBrand', 24 + 'underscoreNameConflictResolver', 20 25 ]; 21 26 22 27 // Type-level test: will fail to compile if any type export is missing or renamed 23 28 export type _TypeExports = [ 24 29 index.AnalysisContext, 30 + index.BindingKind, 31 + index.ExportMember, 32 + index.ExportModule, 33 + index.Extensions, 25 34 index.File, 26 - index.FileIdentifier, 27 35 index.FileIn, 28 36 index.FromRef<any>, 29 37 index.FromRefs<any>, 30 38 index.IProject, 39 + index.ImportMember, 40 + index.ImportModule, 41 + index.Language, 42 + index.NameConflictResolvers, 31 43 index.Node, 32 44 index.Output, 33 - index.PlannedExport, 34 - index.PlannedImport, 35 - index.PlannedReexport, 36 45 index.Project, 37 46 index.ProjectRenderMeta, 38 47 index.Ref<any>,
+24 -24
packages/codegen-core/src/bindings.d.ts
··· 1 1 import type { File } from './files/file'; 2 2 import type { BindingKind } from './symbols/types'; 3 3 4 - export interface ImportSingle { 5 - /** Whether this import is type-only. */ 6 - isTypeOnly: boolean; 7 - /** Import flavor. */ 8 - kind: BindingKind; 9 - /** 10 - * The name this symbol will have locally in this file. 11 - * This is where aliasing is applied: 12 - * 13 - * import { Foo as Foo$2 } from "./x" 14 - * 15 - * localName === "Foo$2" 16 - */ 17 - localName: string; 18 - /** The exported name of the symbol in its source file. */ 19 - sourceName: string; 20 - } 21 - 22 - export interface ExportSingle { 4 + export interface ExportMember { 23 5 /** 24 6 * Name under which the symbol is exported in this file. 25 7 * ··· 36 18 sourceName: string; 37 19 } 38 20 39 - export type ExportGroup = Pick<ExportSingle, 'isTypeOnly'> & { 21 + export type ExportModule = Pick<ExportMember, 'isTypeOnly'> & { 40 22 /** Whether this module can export all symbols: `export * from 'module'`. */ 41 23 canExportAll: boolean; 42 - /** List of symbol exported from this module. */ 43 - exports: Array<ExportSingle>; 24 + /** Members exported from this module. */ 25 + exports: Array<ExportMember>; 44 26 /** Source file. */ 45 27 from: File; 46 28 /** Namespace export: `export * as ns from 'module'`. Mutually exclusive with `exports`. */ 47 29 namespaceExport?: string; 48 30 }; 49 31 50 - export type ImportGroup = Pick<ImportSingle, 'isTypeOnly'> & { 32 + export interface ImportMember { 33 + /** Whether this import is type-only. */ 34 + isTypeOnly: boolean; 35 + /** Import flavor. */ 36 + kind: BindingKind; 37 + /** 38 + * The name this symbol will have locally in this file. 39 + * This is where aliasing is applied: 40 + * 41 + * import { Foo as Foo$2 } from "./x" 42 + * 43 + * localName === "Foo$2" 44 + */ 45 + localName: string; 46 + /** The exported name of the symbol in its source file. */ 47 + sourceName: string; 48 + } 49 + 50 + export type ImportModule = Pick<ImportMember, 'isTypeOnly'> & { 51 51 /** Source file. */ 52 52 from: File; 53 53 /** List of symbols imported from this module. */ 54 - imports: Array<ImportSingle>; 54 + imports: Array<ImportMember>; 55 55 /** Namespace import: `import * as name from 'module'`. Mutually exclusive with `imports`. */ 56 56 namespaceImport?: string; 57 57 };
+7 -7
packages/codegen-core/src/files/file.ts
··· 1 - import type { ExportGroup, ImportGroup } from '../bindings'; 1 + import type { ExportModule, ImportModule } from '../bindings'; 2 2 import { fileBrand } from '../brands'; 3 3 import { debug } from '../debug'; 4 4 import type { Language } from '../languages/types'; ··· 12 12 /** 13 13 * Exports from this file. 14 14 */ 15 - private _exports: Array<ExportGroup> = []; 15 + private _exports: Array<ExportModule> = []; 16 16 /** 17 17 * File extension (e.g. `.ts`). 18 18 */ ··· 24 24 /** 25 25 * Imports to this file. 26 26 */ 27 - private _imports: Array<ImportGroup> = []; 27 + private _imports: Array<ImportModule> = []; 28 28 /** 29 29 * Language of the file. 30 30 */ ··· 71 71 /** 72 72 * Exports from this file. 73 73 */ 74 - get exports(): ReadonlyArray<ExportGroup> { 74 + get exports(): ReadonlyArray<ExportModule> { 75 75 return [...this._exports]; 76 76 } 77 77 ··· 103 103 /** 104 104 * Imports to this file. 105 105 */ 106 - get imports(): ReadonlyArray<ImportGroup> { 106 + get imports(): ReadonlyArray<ImportModule> { 107 107 return [...this._imports]; 108 108 } 109 109 ··· 154 154 /** 155 155 * Add an export group to the file. 156 156 */ 157 - addExport(group: ExportGroup): void { 157 + addExport(group: ExportModule): void { 158 158 this._exports.push(group); 159 159 } 160 160 161 161 /** 162 162 * Add an import group to the file. 163 163 */ 164 - addImport(group: ImportGroup): void { 164 + addImport(group: ImportModule): void { 165 165 this._imports.push(group); 166 166 } 167 167
+4 -4
packages/codegen-core/src/index.ts
··· 1 1 export type { 2 - ExportGroup, 3 - ExportSingle, 4 - ImportGroup, 5 - ImportSingle, 2 + ExportMember, 3 + ExportModule, 4 + ImportMember, 5 + ImportModule, 6 6 } from './bindings'; 7 7 export { nodeBrand, symbolBrand } from './brands'; 8 8 export { debug } from './debug';
+3 -3
packages/codegen-core/src/planner/planner.ts
··· 1 1 import path from 'node:path'; 2 2 3 - import type { ExportGroup, ImportGroup } from '../bindings'; 3 + import type { ExportModule, ImportModule } from '../bindings'; 4 4 import type { IProjectRenderMeta } from '../extensions'; 5 5 import type { File } from '../files/file'; 6 6 import type { IFileIn } from '../files/types'; ··· 174 174 }); 175 175 176 176 for (const [file, fileMap] of seenByFile) { 177 - const exports = new Map<File, ExportGroup>(); 177 + const exports = new Map<File, ExportModule>(); 178 178 for (const [, entry] of fileMap) { 179 179 const source = sourceFile.get(entry.symbol.id)!; 180 180 let exp = exports.get(source); ··· 285 285 }); 286 286 287 287 for (const [file, fileMap] of seenByFile) { 288 - const imports = new Map<File, ImportGroup>(); 288 + const imports = new Map<File, ImportModule>(); 289 289 for (const [, entry] of fileMap) { 290 290 const source = entry.dep.file!; 291 291 let imp = imports.get(source);
+7 -1
packages/openapi-ts/src/ir/context.ts
··· 1 - import { Project } from '@hey-api/codegen-core'; 1 + import { Project, simpleNameConflictResolver } from '@hey-api/codegen-core'; 2 2 3 3 import type { Package } from '~/config/utils/package'; 4 4 import { packageFactory } from '~/config/utils/package'; ··· 70 70 this.config = config; 71 71 this.gen = new Project({ 72 72 defaultFileName: 'index', 73 + defaultNameConflictResolver(args) { 74 + return simpleNameConflictResolver(args); 75 + }, 73 76 fileName: (base) => { 74 77 const name = buildName({ 75 78 config: config.output.fileName, ··· 82 85 return name === 'index' || name.endsWith(suffix) 83 86 ? name 84 87 : `${name}${suffix}`; 88 + }, 89 + nameConflictResolvers: { 90 + // TODO: allow overriding via config 85 91 }, 86 92 // TODO: allow overriding via config 87 93 renderers: [
+3 -3
packages/openapi-ts/src/ts-dsl/render/utils.ts
··· 1 1 import path from 'node:path'; 2 2 3 - import type { ExportGroup, File, ImportGroup } from '@hey-api/codegen-core'; 3 + import type { ExportModule, File, ImportModule } from '@hey-api/codegen-core'; 4 4 import ts from 'typescript'; 5 5 6 6 const printer = ts.createPrinter({ ··· 76 76 export type SortModule = string; 77 77 export type SortKey = [SortGroup, SortDistance, SortModule]; 78 78 79 - export type ModuleExport = Omit<ExportGroup, 'from'> & { 79 + export type ModuleExport = Omit<ExportModule, 'from'> & { 80 80 /** Module specifier for re-exports, e.g. `./foo`. */ 81 81 modulePath: string; 82 82 }; 83 83 84 - export type ModuleImport = Omit<ImportGroup, 'from'> & { 84 + export type ModuleImport = Omit<ImportModule, 'from'> & { 85 85 /** Module specifier for imports, e.g. `./foo`. */ 86 86 modulePath: string; 87 87 };