Skip to content

Commit f543124

Browse files
committed
fix: typecheck in worker, stop using symbol
1 parent 275e2cc commit f543124

6 files changed

Lines changed: 87 additions & 16 deletions

File tree

e2e/openapi.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ paths:
4343
responses:
4444
200:
4545
$ref: '#/components/responses/GetHeaders'
46-
# TODO: add tests for enum handling and open/closed
4746
/validation/numbers/random-number:
4847
get:
4948
tags:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {Piscina} from "piscina"
2+
3+
import path from "node:path"
4+
import {filename} from "./typescript-compiler-worker.test-utils"
5+
6+
let pool: Piscina | null = null
7+
8+
export function startWorkerPool() {
9+
if (pool) {
10+
throw new Error("already started")
11+
}
12+
13+
pool = new Piscina({
14+
filename: path.resolve(__dirname, "./workerWrapper.js"),
15+
workerData: {fullpath: filename},
16+
concurrentTasksPerWorker: 1,
17+
maxThreads: 4,
18+
minThreads: 4,
19+
idleTimeout: 5_000,
20+
})
21+
}
22+
23+
export function stopWorkerPool() {
24+
if (!pool) {
25+
throw new Error("worker pool not started")
26+
}
27+
28+
pool.destroy()
29+
pool = null
30+
}
31+
32+
export async function typecheckInWorker(
33+
compilationUnits: {filename: string; content: string}[],
34+
) {
35+
if (!pool) {
36+
throw new Error("worker pool not started")
37+
}
38+
39+
await pool.run(compilationUnits)
40+
}

packages/openapi-code-generator/src/test/typescript-compiler-worker.test-utils.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import fs from "node:fs"
22
import path from "node:path"
33
import ts from "typescript"
4-
import type {CompilationUnit} from "../typescript/common/compilation-units"
54
import {TypescriptFormatterBiome} from "../typescript/common/typescript-formatter.biome"
65

7-
export async function typecheck(compilationUnits: CompilationUnit[]) {
8-
const formatter = await TypescriptFormatterBiome.createNodeFormatter()
6+
export const filename = path.resolve(__filename)
7+
8+
const whenFormatter = TypescriptFormatterBiome.createNodeFormatter()
9+
10+
export default async function typecheck(
11+
compilationUnits: {filename: string; content: string}[],
12+
) {
13+
const formatter = await whenFormatter
914

1015
const files: Record<string, string> = {}
1116

1217
for (const unit of compilationUnits) {
13-
const formatted = await formatter.format(
14-
unit.filename,
15-
unit.getRawFileContent({
16-
allowUnusedImports: false,
17-
includeHeader: false,
18-
}),
19-
)
18+
const formatted = await formatter.format(unit.filename, unit.content)
2019

2120
if (formatted.err) {
2221
throw formatted.err
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const tsx = require("tsx/cjs/api")
2+
const {workerData} = require("node:worker_threads")
3+
4+
module.exports = tsx.require(workerData.fullpath, __filename)

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
import {describe, expect, it} from "@jest/globals"
1+
import {describe, expect, it, jest} from "@jest/globals"
22
import type {CompilerOptions} from "../../core/loaders/tsconfig.loader"
33
import {testVersions, unitTestInput} from "../../test/input.test-utils"
44
import {ImportBuilder} from "./import-builder"
55
import {TypeBuilder, type TypeBuilderConfig} from "./type-builder"
66
import {TypescriptFormatterBiome} from "./typescript-formatter.biome"
77

8-
import {typecheck} from "../../test/typescript.test-utils"
8+
import {
9+
startWorkerPool,
10+
stopWorkerPool,
11+
typecheckInWorker,
12+
} from "../../test/typescript-compiler-worker-pool.test-utils"
913
import {CompilationUnit} from "./compilation-units"
1014

1115
describe.each(testVersions)(
1216
"%s - typescript/common/type-builder",
1317
(version) => {
18+
beforeAll(() => {
19+
startWorkerPool()
20+
})
21+
22+
afterAll(() => {
23+
stopWorkerPool()
24+
})
25+
1426
it("can build a type for a simple object correctly", async () => {
1527
const {code, types} = await getActual("components/schemas/SimpleObject")
1628

@@ -460,7 +472,22 @@ describe.each(testVersions)(
460472
)
461473
const types = builder.toCompilationUnit()
462474

463-
await typecheck([usage, builder.toCompilationUnit()])
475+
await typecheckInWorker([
476+
{
477+
filename: usage.filename,
478+
content: usage.getRawFileContent({
479+
allowUnusedImports: false,
480+
includeHeader: false,
481+
}),
482+
},
483+
{
484+
filename: types.filename,
485+
content: types.getRawFileContent({
486+
allowUnusedImports: false,
487+
includeHeader: false,
488+
}),
489+
},
490+
])
464491

465492
return {
466493
code: (

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import {buildExport} from "./typescript-common"
1919

2020
const staticTypes = {
2121
EmptyObject: "export type EmptyObject = { [key: string]: never }",
22+
// TODO: results in TS4023: Exported variable '<schema>' has or is using name '_brand' from external module "<path>" but cannot be named.
23+
// Brand: "export const _brand = Symbol.for('brand')",
2224
UnknownEnumStringValue:
23-
"export type UnknownEnumStringValue = (string & {_: 'unknown enum string value'})",
25+
"export type UnknownEnumStringValue = (string & {_brand: 'unknown enum string value'})",
2426
UnknownEnumNumberValue:
25-
"export type UnknownEnumNumberValue = (number & {_: 'unknown enum number value'})",
27+
"export type UnknownEnumNumberValue = (number & {_brand: 'unknown enum number value'})",
2628
}
2729

2830
type StaticType = keyof typeof staticTypes

0 commit comments

Comments
 (0)