diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bcd400a0bf..54555d8a12 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -247,8 +247,8 @@ jobs: - run: npx hereby generate - - run: node ./internal/lsp/lsproto/_generate/fetchModel.mjs - - run: node ./internal/lsp/lsproto/_generate/generate.mjs + - run: node --experimental-strip-types ./internal/lsp/lsproto/_generate/fetchModel.mts + - run: node --experimental-strip-types ./internal/lsp/lsproto/_generate/generate.mts - name: Regenerate fourslash tests and update failing test list run: npm run updatefailing diff --git a/internal/lsp/lsproto/_generate/fetchModel.mjs b/internal/lsp/lsproto/_generate/fetchModel.mts similarity index 100% rename from internal/lsp/lsproto/_generate/fetchModel.mjs rename to internal/lsp/lsproto/_generate/fetchModel.mts diff --git a/internal/lsp/lsproto/_generate/generate.mjs b/internal/lsp/lsproto/_generate/generate.mts similarity index 89% rename from internal/lsp/lsproto/_generate/generate.mjs rename to internal/lsp/lsproto/_generate/generate.mts index ba509e155b..5d5c25c260 100644 --- a/internal/lsp/lsproto/_generate/generate.mjs +++ b/internal/lsp/lsproto/_generate/generate.mts @@ -5,11 +5,13 @@ import fs from "node:fs"; import path from "node:path"; import url from "node:url"; import which from "which"; - -/** - * @import { MetaModel, OrType, Type, Request, Notification } from "./metaModelSchema.mts" - */ -void 0; +import type { + MetaModel, + Notification, + OrType, + Request, + Type, +} from "./metaModelSchema.mts"; const __filename = url.fileURLToPath(new URL(import.meta.url)); const __dirname = path.dirname(__filename); @@ -22,44 +24,30 @@ if (!fs.existsSync(metaModelPath)) { process.exit(1); } -/** @type {MetaModel} */ -const model = JSON.parse(fs.readFileSync(metaModelPath, "utf-8")); +const model: MetaModel = JSON.parse(fs.readFileSync(metaModelPath, "utf-8")); -/** - * Represents a type in our intermediate type system - * @typedef {Object} GoType - * @property {string} name - Name of the type in Go - * @property {boolean} needsPointer - Whether this type should be used with a pointer - */ +interface GoType { + name: string; + needsPointer: boolean; +} -/** - * @typedef {Object} TypeInfo - * @property {Map} types - Map of type names to types - * @property {Map} literalTypes - Map from literal values to type names - * @property {Map} unionTypes - Map of union type names to their component types - */ +interface TypeInfo { + types: Map; + literalTypes: Map; + unionTypes: Map; +} -/** - * @type {TypeInfo} - */ -const typeInfo = { +const typeInfo: TypeInfo = { types: new Map(), literalTypes: new Map(), unionTypes: new Map(), }; -/** - * @param {string} s - */ -function titleCase(s) { +function titleCase(s: string) { return s.charAt(0).toUpperCase() + s.slice(1); } -/** - * @param {Type} type - * @returns {GoType} - */ -function resolveType(type) { +function resolveType(type: Type): GoType { switch (type.kind) { case "base": switch (type.name) { @@ -161,11 +149,7 @@ function resolveType(type) { } } -/** - * @param {OrType} orType - * @returns {GoType} - */ -function handleOrType(orType) { +function handleOrType(orType: OrType): GoType { const types = orType.items; // Check for nullable types (OR with null) @@ -280,15 +264,10 @@ function collectTypeDefinitions() { } } -/** - * @param {string | undefined} s - * @returns {string} - */ -function formatDocumentation(s) { +function formatDocumentation(s: string | undefined): string { if (!s) return ""; - /** @type {string[]} */ - let lines = []; + let lines: string[] = []; for (let line of s.split("\n")) { line = line.trimEnd(); @@ -316,10 +295,7 @@ function formatDocumentation(s) { return lines.length > 0 ? "// " + lines.join("\n// ") + "\n" : ""; } -/** - * @param {string} name - */ -function methodNameIdentifier(name) { +function methodNameIdentifier(name: string) { return name.split("/").map(v => v === "$" ? "" : titleCase(v)).join(""); } @@ -327,25 +303,18 @@ function methodNameIdentifier(name) { * Generate the Go code */ function generateCode() { - /** @type {string[]} */ - const parts = []; + const parts: string[] = []; - /** - * @param {string} s - */ - function write(s) { + function write(s: string) { parts.push(s); } - /** - * @param {string} s - */ function writeLine(s = "") { parts.push(s + "\n"); } // File header - writeLine("// Code generated by generate.mjs; DO NOT EDIT."); + writeLine("// Code generated by generate.mts; DO NOT EDIT."); writeLine(""); writeLine("package lsproto"); writeLine(""); @@ -361,11 +330,7 @@ function generateCode() { writeLine("// Structures\n"); for (const structure of model.structures) { - /** - * @param {string} name - * @param {boolean} includeDocumentation - */ - function generateStructFields(name, includeDocumentation) { + function generateStructFields(name: string, includeDocumentation: boolean) { if (includeDocumentation) { write(formatDocumentation(structure.documentation)); } @@ -533,8 +498,7 @@ function generateCode() { writeLine(""); } - /** @type {(Request | Notification)[]} */ - const requestsAndNotifications = [...model.requests, ...model.notifications]; + const requestsAndNotifications: (Request | Notification)[] = [...model.requests, ...model.notifications]; // Generate unmarshalParams function writeLine("func unmarshalParams(method Method, data []byte) (any, error) {"); diff --git a/internal/lsp/lsproto/_generate/tsconfig.json b/internal/lsp/lsproto/_generate/tsconfig.json index 522ceaddec..1ed48f5ac3 100644 --- a/internal/lsp/lsproto/_generate/tsconfig.json +++ b/internal/lsp/lsproto/_generate/tsconfig.json @@ -7,6 +7,8 @@ "strict": true, "noEmit": true, "allowImportingTsExtensions": true, + "erasableSyntaxOnly": true, + "verbatimModuleSyntax": true, "types": ["node"], "noUnusedLocals": true, "noUnusedParameters": true, diff --git a/internal/lsp/lsproto/lsp_generated.go b/internal/lsp/lsproto/lsp_generated.go index c9a85898e1..b9ba4f1912 100644 --- a/internal/lsp/lsproto/lsp_generated.go +++ b/internal/lsp/lsproto/lsp_generated.go @@ -1,4 +1,4 @@ -// Code generated by generate.mjs; DO NOT EDIT. +// Code generated by generate.mts; DO NOT EDIT. package lsproto