Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Commit 1a6eceb

Browse files
authored
Generate TS API from Go source (#4915)
1 parent 65535b2 commit 1a6eceb

16 files changed

Lines changed: 2553 additions & 926 deletions

File tree

.github/copilot-instructions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ npx hereby format # Format the code
3737
```
3838
</critical>
3939

40+
If you are writing or testing TS API features (eg, code in _packages/native-preview/src/api/async/api.ts), additionally, you need to run
41+
```sh
42+
npx hereby test:api
43+
```
44+
which is not run as part of the primary suite.
45+
4046
## Compiler Features, Fixes, and Tests
4147

4248
When fixing a bug or implementing a new feature, at least one minimal test case should always be added in advance to verify the fix.

Herebyfile.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ export const generateAST = task({
577577
run: () => $`node --experimental-strip-types --no-warnings ./_scripts/generate.ts`,
578578
});
579579

580+
export const generateAPI = task({
581+
name: "generate:api",
582+
description: "Generates API files from internal/api/proto.go and internal/api/session.go.",
583+
run: () => $`go -C ./_tools run ./gen-proto ../internal/api/proto.go ../_packages/native-preview/src/api/proto.generated.ts`,
584+
});
585+
580586
// ── Vendored npm dependencies ───────────────────────────────────
581587

582588
const vendorJsonrpcDir = "_packages/native-preview/vendor/vscode-jsonrpc";
@@ -834,6 +840,7 @@ export const buildAPI = task({
834840
export const buildAPITests = task({
835841
name: "build:api:test",
836842
description: "Builds the @typescript/native-preview JS API tests.",
843+
dependencies: [generateEnums, generateAPI],
837844
run: async () => {
838845
await $`npm run -w @typescript/native-preview build:test`;
839846
},

_packages/native-preview/src/api/async/api.ts

Lines changed: 232 additions & 162 deletions
Large diffs are not rendered by default.

_packages/native-preview/src/api/async/client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import {
2121
isSpawnOptions,
2222
resolveExePath,
2323
} from "../options.ts";
24+
import type {
25+
APIMethodInfo,
26+
SourceFileResponseMethod,
27+
} from "../proto.ts";
2428
import {
2529
combineTimingInfo,
2630
disabledServerTimingInfo,
@@ -154,15 +158,15 @@ export class Client {
154158
}
155159
}
156160

157-
async apiRequest<T>(method: string, params?: unknown): Promise<T> {
161+
async apiRequest<K extends keyof APIMethodInfo>(method: K, params: APIMethodInfo[K]["params"]): Promise<APIMethodInfo[K]["result"]> {
158162
if (!this.connected) {
159163
await this.connect();
160164
}
161165
if (!this.connection) {
162166
throw new Error("Connection not established");
163167
}
164168

165-
const requestType = new RequestType<unknown, T, void>(method);
169+
const requestType = new RequestType<unknown, APIMethodInfo[K]["result"], void>(method);
166170
if (!this.timing) {
167171
return this.connection.sendRequest(requestType, params);
168172
}
@@ -186,8 +190,8 @@ export class Client {
186190
return result;
187191
}
188192

189-
async apiRequestBinary(method: string, params?: unknown): Promise<Uint8Array | undefined> {
190-
const response = await this.apiRequest<{ data: string; } | null>(method, params);
193+
async apiRequestBinary<K extends SourceFileResponseMethod>(method: K, params: APIMethodInfo[K]["params"]): Promise<Uint8Array | undefined> {
194+
const response = await this.apiRequest(method, params);
191195
if (!response) return undefined;
192196
const buffer = Buffer.from(response.data, "base64");
193197
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);

_packages/native-preview/src/api/compilerOptions.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)