Skip to content

Commit 5770f84

Browse files
committed
feat: adopt ts-morph and flip implementation/generated code
1 parent 86eb729 commit 5770f84

File tree

5 files changed

+226
-113
lines changed

5 files changed

+226
-113
lines changed

packages/openapi-code-generator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"json5": "^2.2.3",
4242
"lodash": "^4.17.21",
4343
"source-map-support": "^0.5.21",
44+
"ts-morph": "^22.0.0",
4445
"tslib": "^2.6.2"
4546
},
4647
"files": [

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
@@ -68,7 +68,7 @@ export class ImportBuilder {
6868
}
6969

7070
private add(name: string, from: string, isAll: boolean): void {
71-
from = this.normalizeFrom(from)
71+
from = ImportBuilder.normalizeFrom(from, this.unit?.filename)
7272
const imports = (this.imports[from] =
7373
this.imports[from] ?? new Set<string>())
7474

@@ -79,13 +79,14 @@ export class ImportBuilder {
7979
}
8080
}
8181

82-
private normalizeFrom(from: string) {
82+
public static normalizeFrom(from: string, filename?: string) {
8383
if (from.endsWith(".ts")) {
8484
from = from.substring(0, from.length - ".ts".length)
8585
}
8686

87-
if (this.unit && from.startsWith("./")) {
88-
const unitDirname = path.dirname(this.unit.filename)
87+
// TODO: does this work on windows?
88+
if (filename && from.startsWith("./")) {
89+
const unitDirname = path.dirname(filename)
8990
const fromDirname = path.dirname(from)
9091

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

0 commit comments

Comments
 (0)