Skip to content

Commit 79d0450

Browse files
authored
Convert LSP generator to mts with strip types (#1436)
1 parent 88e95c9 commit 79d0450

File tree

5 files changed

+34
-68
lines changed

5 files changed

+34
-68
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ jobs:
247247

248248
- run: npx hereby generate
249249

250-
- run: node ./internal/lsp/lsproto/_generate/fetchModel.mjs
251-
- run: node ./internal/lsp/lsproto/_generate/generate.mjs
250+
- run: node --experimental-strip-types ./internal/lsp/lsproto/_generate/fetchModel.mts
251+
- run: node --experimental-strip-types ./internal/lsp/lsproto/_generate/generate.mts
252252

253253
- name: Regenerate fourslash tests and update failing test list
254254
run: npm run updatefailing
File renamed without changes.

internal/lsp/lsproto/_generate/generate.mjs renamed to internal/lsp/lsproto/_generate/generate.mts

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import fs from "node:fs";
55
import path from "node:path";
66
import url from "node:url";
77
import which from "which";
8-
9-
/**
10-
* @import { MetaModel, OrType, Type, Request, Notification } from "./metaModelSchema.mts"
11-
*/
12-
void 0;
8+
import type {
9+
MetaModel,
10+
Notification,
11+
OrType,
12+
Request,
13+
Type,
14+
} from "./metaModelSchema.mts";
1315

1416
const __filename = url.fileURLToPath(new URL(import.meta.url));
1517
const __dirname = path.dirname(__filename);
@@ -22,44 +24,30 @@ if (!fs.existsSync(metaModelPath)) {
2224
process.exit(1);
2325
}
2426

25-
/** @type {MetaModel} */
26-
const model = JSON.parse(fs.readFileSync(metaModelPath, "utf-8"));
27+
const model: MetaModel = JSON.parse(fs.readFileSync(metaModelPath, "utf-8"));
2728

28-
/**
29-
* Represents a type in our intermediate type system
30-
* @typedef {Object} GoType
31-
* @property {string} name - Name of the type in Go
32-
* @property {boolean} needsPointer - Whether this type should be used with a pointer
33-
*/
29+
interface GoType {
30+
name: string;
31+
needsPointer: boolean;
32+
}
3433

35-
/**
36-
* @typedef {Object} TypeInfo
37-
* @property {Map<string, GoType>} types - Map of type names to types
38-
* @property {Map<string, string>} literalTypes - Map from literal values to type names
39-
* @property {Map<string, {name: string, type: Type}[]>} unionTypes - Map of union type names to their component types
40-
*/
34+
interface TypeInfo {
35+
types: Map<string, GoType>;
36+
literalTypes: Map<string, string>;
37+
unionTypes: Map<string, { name: string; type: Type; }[]>;
38+
}
4139

42-
/**
43-
* @type {TypeInfo}
44-
*/
45-
const typeInfo = {
40+
const typeInfo: TypeInfo = {
4641
types: new Map(),
4742
literalTypes: new Map(),
4843
unionTypes: new Map(),
4944
};
5045

51-
/**
52-
* @param {string} s
53-
*/
54-
function titleCase(s) {
46+
function titleCase(s: string) {
5547
return s.charAt(0).toUpperCase() + s.slice(1);
5648
}
5749

58-
/**
59-
* @param {Type} type
60-
* @returns {GoType}
61-
*/
62-
function resolveType(type) {
50+
function resolveType(type: Type): GoType {
6351
switch (type.kind) {
6452
case "base":
6553
switch (type.name) {
@@ -161,11 +149,7 @@ function resolveType(type) {
161149
}
162150
}
163151

164-
/**
165-
* @param {OrType} orType
166-
* @returns {GoType}
167-
*/
168-
function handleOrType(orType) {
152+
function handleOrType(orType: OrType): GoType {
169153
const types = orType.items;
170154

171155
// Check for nullable types (OR with null)
@@ -280,15 +264,10 @@ function collectTypeDefinitions() {
280264
}
281265
}
282266

283-
/**
284-
* @param {string | undefined} s
285-
* @returns {string}
286-
*/
287-
function formatDocumentation(s) {
267+
function formatDocumentation(s: string | undefined): string {
288268
if (!s) return "";
289269

290-
/** @type {string[]} */
291-
let lines = [];
270+
let lines: string[] = [];
292271

293272
for (let line of s.split("\n")) {
294273
line = line.trimEnd();
@@ -316,36 +295,26 @@ function formatDocumentation(s) {
316295
return lines.length > 0 ? "// " + lines.join("\n// ") + "\n" : "";
317296
}
318297

319-
/**
320-
* @param {string} name
321-
*/
322-
function methodNameIdentifier(name) {
298+
function methodNameIdentifier(name: string) {
323299
return name.split("/").map(v => v === "$" ? "" : titleCase(v)).join("");
324300
}
325301

326302
/**
327303
* Generate the Go code
328304
*/
329305
function generateCode() {
330-
/** @type {string[]} */
331-
const parts = [];
306+
const parts: string[] = [];
332307

333-
/**
334-
* @param {string} s
335-
*/
336-
function write(s) {
308+
function write(s: string) {
337309
parts.push(s);
338310
}
339311

340-
/**
341-
* @param {string} s
342-
*/
343312
function writeLine(s = "") {
344313
parts.push(s + "\n");
345314
}
346315

347316
// File header
348-
writeLine("// Code generated by generate.mjs; DO NOT EDIT.");
317+
writeLine("// Code generated by generate.mts; DO NOT EDIT.");
349318
writeLine("");
350319
writeLine("package lsproto");
351320
writeLine("");
@@ -361,11 +330,7 @@ function generateCode() {
361330
writeLine("// Structures\n");
362331

363332
for (const structure of model.structures) {
364-
/**
365-
* @param {string} name
366-
* @param {boolean} includeDocumentation
367-
*/
368-
function generateStructFields(name, includeDocumentation) {
333+
function generateStructFields(name: string, includeDocumentation: boolean) {
369334
if (includeDocumentation) {
370335
write(formatDocumentation(structure.documentation));
371336
}
@@ -533,8 +498,7 @@ function generateCode() {
533498
writeLine("");
534499
}
535500

536-
/** @type {(Request | Notification)[]} */
537-
const requestsAndNotifications = [...model.requests, ...model.notifications];
501+
const requestsAndNotifications: (Request | Notification)[] = [...model.requests, ...model.notifications];
538502

539503
// Generate unmarshalParams function
540504
writeLine("func unmarshalParams(method Method, data []byte) (any, error) {");

internal/lsp/lsproto/_generate/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"strict": true,
88
"noEmit": true,
99
"allowImportingTsExtensions": true,
10+
"erasableSyntaxOnly": true,
11+
"verbatimModuleSyntax": true,
1012
"types": ["node"],
1113
"noUnusedLocals": true,
1214
"noUnusedParameters": true,

internal/lsp/lsproto/lsp_generated.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)