Skip to content

Commit 344546b

Browse files
committed
feat: adopt ts-morph and flip implementation/generated code
1 parent 89d8850 commit 344546b

File tree

5 files changed

+234
-112
lines changed

5 files changed

+234
-112
lines changed

packages/openapi-code-generator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"json5": "^2.2.3",
5353
"lodash": "^4.17.21",
5454
"source-map-support": "^0.5.21",
55+
"ts-morph": "^22.0.0",
5556
"tslib": "^2.6.3",
5657
"zod": "^3.23.8"
5758
},

packages/openapi-code-generator/src/typescript/common/compilation-units.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class CompilationUnit {
1515
readonly filename: string,
1616
readonly imports: ImportBuilder | undefined,
1717
readonly code: string,
18+
readonly isAutogenerated: boolean = true,
1819
) {}
1920

2021
hasCode() {
@@ -26,7 +27,7 @@ export class CompilationUnit {
2627
includeHeader = true,
2728
}: {allowUnusedImports: boolean; includeHeader?: boolean}): string {
2829
return [
29-
includeHeader ? FILE_HEADER : "",
30+
includeHeader && this.isAutogenerated ? FILE_HEADER : "",
3031
this.imports
3132
? this.imports.toString(allowUnusedImports ? "" : this.code)
3233
: "",

packages/openapi-code-generator/src/typescript/common/import-builder.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class ImportBuilder {
7676

7777
private add(name: string, from: string, isAll: boolean): void {
7878
// biome-ignore lint/style/noParameterAssign: <explanation>
79-
from = this.normalizeFrom(from)
79+
from = ImportBuilder.normalizeFrom(from, this.unit?.filename)
8080
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
8181
const imports = (this.imports[from] =
8282
this.imports[from] ?? new Set<string>())
@@ -88,14 +88,15 @@ export class ImportBuilder {
8888
}
8989
}
9090

91-
private normalizeFrom(from: string) {
91+
public static normalizeFrom(from: string, filename?: string) {
9292
if (from.endsWith(".ts")) {
9393
// biome-ignore lint/style/noParameterAssign: <explanation>
9494
from = from.substring(0, from.length - ".ts".length)
9595
}
9696

97-
if (this.unit && from.startsWith("./")) {
98-
const unitDirname = path.dirname(this.unit.filename)
97+
// TODO: does this work on windows?
98+
if (filename && from.startsWith("./")) {
99+
const unitDirname = path.dirname(filename)
99100
const fromDirname = path.dirname(from)
100101

101102
const relative = path.relative(unitDirname, fromDirname)

0 commit comments

Comments
 (0)