Skip to content

Commit 052341e

Browse files
committed
Merge branch 'main' into feat/name-dedup
2 parents 1919405 + 0804fb8 commit 052341e

File tree

8 files changed

+62
-46
lines changed

8 files changed

+62
-46
lines changed

docs/partials/contributors-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
- [Michiel Lankamp](https://github.com/mlankamp)
6161
- [Mika Vilpas](https://github.com/mikavilpas)
6262
- [Miklos](https://github.com/jumika)
63+
- [Nacho García](https://github.com/nachogarcia)
6364
- [Nicolas Chaulet](https://github.com/nicolas-chaulet)
6465
- [Nimo Beeren](https://github.com/nimobeeren)
6566
- [Novak Antonijevic](https://github.com/NovakAnton)

packages/codegen-core/src/__tests__/exports.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import * as index from '../index';
44

55
const constExports = [
66
'debug',
7+
'defaultExtensions',
8+
'defaultNameConflictResolvers',
9+
'File',
710
'fromRef',
811
'fromRefs',
912
'isNode',
@@ -15,24 +18,30 @@ const constExports = [
1518
'Project',
1619
'ref',
1720
'refs',
21+
'simpleNameConflictResolver',
1822
'Symbol',
1923
'symbolBrand',
24+
'underscoreNameConflictResolver',
2025
];
2126

2227
// Type-level test: will fail to compile if any type export is missing or renamed
2328
export type _TypeExports = [
2429
index.AnalysisContext,
30+
index.BindingKind,
31+
index.ExportMember,
32+
index.ExportModule,
33+
index.Extensions,
2534
index.File,
26-
index.FileIdentifier,
2735
index.FileIn,
2836
index.FromRef<any>,
2937
index.FromRefs<any>,
3038
index.IProject,
39+
index.ImportMember,
40+
index.ImportModule,
41+
index.Language,
42+
index.NameConflictResolvers,
3143
index.Node,
3244
index.Output,
33-
index.PlannedExport,
34-
index.PlannedImport,
35-
index.PlannedReexport,
3645
index.Project,
3746
index.ProjectRenderMeta,
3847
index.Ref<any>,
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
import type { File } from './files/file';
22
import type { BindingKind } from './symbols/types';
33

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 {
235
/**
246
* Name under which the symbol is exported in this file.
257
*
@@ -36,22 +18,40 @@ export interface ExportSingle {
3618
sourceName: string;
3719
}
3820

39-
export type ExportGroup = Pick<ExportSingle, 'isTypeOnly'> & {
21+
export type ExportModule = Pick<ExportMember, 'isTypeOnly'> & {
4022
/** Whether this module can export all symbols: `export * from 'module'`. */
4123
canExportAll: boolean;
42-
/** List of symbol exported from this module. */
43-
exports: Array<ExportSingle>;
24+
/** Members exported from this module. */
25+
exports: Array<ExportMember>;
4426
/** Source file. */
4527
from: File;
4628
/** Namespace export: `export * as ns from 'module'`. Mutually exclusive with `exports`. */
4729
namespaceExport?: string;
4830
};
4931

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'> & {
5151
/** Source file. */
5252
from: File;
5353
/** List of symbols imported from this module. */
54-
imports: Array<ImportSingle>;
54+
imports: Array<ImportMember>;
5555
/** Namespace import: `import * as name from 'module'`. Mutually exclusive with `imports`. */
5656
namespaceImport?: string;
5757
};

packages/codegen-core/src/files/file.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ExportGroup, ImportGroup } from '../bindings';
1+
import type { ExportModule, ImportModule } from '../bindings';
22
import { fileBrand } from '../brands';
33
import { debug } from '../debug';
44
import type { Language } from '../languages/types';
@@ -12,7 +12,7 @@ export class File {
1212
/**
1313
* Exports from this file.
1414
*/
15-
private _exports: Array<ExportGroup> = [];
15+
private _exports: Array<ExportModule> = [];
1616
/**
1717
* File extension (e.g. `.ts`).
1818
*/
@@ -24,7 +24,7 @@ export class File {
2424
/**
2525
* Imports to this file.
2626
*/
27-
private _imports: Array<ImportGroup> = [];
27+
private _imports: Array<ImportModule> = [];
2828
/**
2929
* Language of the file.
3030
*/
@@ -71,7 +71,7 @@ export class File {
7171
/**
7272
* Exports from this file.
7373
*/
74-
get exports(): ReadonlyArray<ExportGroup> {
74+
get exports(): ReadonlyArray<ExportModule> {
7575
return [...this._exports];
7676
}
7777

@@ -103,7 +103,7 @@ export class File {
103103
/**
104104
* Imports to this file.
105105
*/
106-
get imports(): ReadonlyArray<ImportGroup> {
106+
get imports(): ReadonlyArray<ImportModule> {
107107
return [...this._imports];
108108
}
109109

@@ -154,14 +154,14 @@ export class File {
154154
/**
155155
* Add an export group to the file.
156156
*/
157-
addExport(group: ExportGroup): void {
157+
addExport(group: ExportModule): void {
158158
this._exports.push(group);
159159
}
160160

161161
/**
162162
* Add an import group to the file.
163163
*/
164-
addImport(group: ImportGroup): void {
164+
addImport(group: ImportModule): void {
165165
this._imports.push(group);
166166
}
167167

packages/codegen-core/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export type {
2-
ExportGroup,
3-
ExportSingle,
4-
ImportGroup,
5-
ImportSingle,
2+
ExportMember,
3+
ExportModule,
4+
ImportMember,
5+
ImportModule,
66
} from './bindings';
77
export { nodeBrand, symbolBrand } from './brands';
88
export { debug } from './debug';

packages/codegen-core/src/planner/planner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22

3-
import type { ExportGroup, ImportGroup } from '../bindings';
3+
import type { ExportModule, ImportModule } from '../bindings';
44
import type { IProjectRenderMeta } from '../extensions';
55
import type { File } from '../files/file';
66
import type { IFileIn } from '../files/types';
@@ -174,7 +174,7 @@ export class Planner {
174174
});
175175

176176
for (const [file, fileMap] of seenByFile) {
177-
const exports = new Map<File, ExportGroup>();
177+
const exports = new Map<File, ExportModule>();
178178
for (const [, entry] of fileMap) {
179179
const source = sourceFile.get(entry.symbol.id)!;
180180
let exp = exports.get(source);
@@ -285,7 +285,7 @@ export class Planner {
285285
});
286286

287287
for (const [file, fileMap] of seenByFile) {
288-
const imports = new Map<File, ImportGroup>();
288+
const imports = new Map<File, ImportModule>();
289289
for (const [, entry] of fileMap) {
290290
const source = entry.dep.file!;
291291
let imp = imports.get(source);

packages/openapi-ts/src/ir/context.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Project } from '@hey-api/codegen-core';
1+
import { Project, simpleNameConflictResolver } from '@hey-api/codegen-core';
22

33
import type { Package } from '~/config/utils/package';
44
import { packageFactory } from '~/config/utils/package';
@@ -70,6 +70,9 @@ export class Context<Spec extends Record<string, any> = any> {
7070
this.config = config;
7171
this.gen = new Project({
7272
defaultFileName: 'index',
73+
defaultNameConflictResolver(args) {
74+
return simpleNameConflictResolver(args);
75+
},
7376
fileName: (base) => {
7477
const name = buildName({
7578
config: config.output.fileName,
@@ -83,6 +86,9 @@ export class Context<Spec extends Record<string, any> = any> {
8386
? name
8487
: `${name}${suffix}`;
8588
},
89+
nameConflictResolvers: {
90+
// TODO: allow overriding via config
91+
},
8692
// TODO: allow overriding via config
8793
renderers: [
8894
new TypeScriptRenderer({

packages/openapi-ts/src/ts-dsl/render/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22

3-
import type { ExportGroup, File, ImportGroup } from '@hey-api/codegen-core';
3+
import type { ExportModule, File, ImportModule } from '@hey-api/codegen-core';
44
import ts from 'typescript';
55

66
const printer = ts.createPrinter({
@@ -76,12 +76,12 @@ export type SortDistance = number;
7676
export type SortModule = string;
7777
export type SortKey = [SortGroup, SortDistance, SortModule];
7878

79-
export type ModuleExport = Omit<ExportGroup, 'from'> & {
79+
export type ModuleExport = Omit<ExportModule, 'from'> & {
8080
/** Module specifier for re-exports, e.g. `./foo`. */
8181
modulePath: string;
8282
};
8383

84-
export type ModuleImport = Omit<ImportGroup, 'from'> & {
84+
export type ModuleImport = Omit<ImportModule, 'from'> & {
8585
/** Module specifier for imports, e.g. `./foo`. */
8686
modulePath: string;
8787
};

0 commit comments

Comments
 (0)