Skip to content

Instantly share code, notes, and snippets.

@leonidaz
Created October 1, 2026 01:51
Show Gist options
  • Select an option

  • Save leonidaz/a09cfe652d708cda59e20a112e879307 to your computer and use it in GitHub Desktop.

Select an option

Save leonidaz/a09cfe652d708cda59e20a112e879307 to your computer and use it in GitHub Desktop.
TypeScript 7 VS Code extension: no closing JSX tag in content-mapped files (tsc --lsp provides it)

TypeScript 7 VS Code extension: no closing JSX tag in content-mapped files

.foo files contain TSX; mapper.mjs is a 47-line identity content mapper that returns them unchanged as .tsx (one verbatim span, all features). No framework, Volar, or tsserver plugin is involved.

Verified with [email protected], the TypeScript 7 extension 1.0.1, VS Code 1.140.0, and Node.js 24.18.0 on macOS 26.6.2 x64.

Setup

npm install --ignore-scripts
npm run setup   # writes node_modules/identity-foo-mapper/package.json pointing at mapper.mjs

1. The language server answers for the content-mapped file

npm run ask

ask.mjs sends tsc --lsp the request the extension sends when > is typed (textDocument/_vs_onAutoInsert), right after <b> in card.foo and in control.tsx (same text: export const card = <div><b></div>;):

card.foo: {"_vs_textEditFormat":2,"_vs_textEdit":{"range":{"start":{"line":0,"character":28},"end":{"line":0,"character":28}},"newText":"$0</b>"}}
control.tsx: {"_vs_textEditFormat":2,"_vs_textEdit":{"range":{"start":{"line":0,"character":28},"end":{"line":0,"character":28}},"newText":"$0</b>"}}

2. The extension does not ask for it

Open this folder in VS Code with the TypeScript 7 extension, "js/ts.experimental.useTsgo": true, and "js/ts.tsdk.path" pointing at this folder's node_modules/typescript (the 7.1 nightly; the extension's bundled 7.0.2 has no content mappers). Trust the workspace.

File Hover on card Type <b> between <div> and </div>
type-here.tsx const card: any <div><b></b></div>
type-here.foo const card: any <div><b></div>, no closing tag

With the server log at its default verbosity, typing in type-here.tsx produces one textDocument/_vs_onAutoInsert request; typing in type-here.foo produces none, although the extension sends hover, diagnostic, code action, inlay hint and semantic token requests for that file.

// Asks `tsc --lsp` for the closing tag after `<b>` in card.foo (content-mapped)
// and in control.tsx (plain TSX), the request the TypeScript 7 VS Code extension
// sends when `>` is typed (`textDocument/_vs_onAutoInsert`).
import { spawn } from "node:child_process";
import { readFileSync } from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";
const root = fileURLToPath(new URL(".", import.meta.url));
const tsc = fileURLToPath(new URL("./node_modules/typescript/bin/tsc", import.meta.url));
const server = spawn(process.execPath, [tsc, "--lsp", "--stdio"], { cwd: root, stdio: ["pipe", "pipe", "inherit"] });
let nextId = 1;
const pending = new Map();
let buffered = Buffer.alloc(0);
const send = (message) => {
const body = Buffer.from(JSON.stringify({ jsonrpc: "2.0", ...message }));
server.stdin.write(`Content-Length: ${body.length}\r\n\r\n`);
server.stdin.write(body);
};
const request = (method, params) =>
new Promise((resolve) => {
const id = nextId++;
pending.set(id, resolve);
send({ id, method, params });
});
server.stdout.on("data", (chunk) => {
buffered = Buffer.concat([buffered, chunk]);
while (true) {
const headerEnd = buffered.indexOf("\r\n\r\n");
if (headerEnd < 0) return;
const length = Number(/Content-Length: (\d+)/i.exec(buffered.subarray(0, headerEnd).toString())[1]);
if (buffered.length < headerEnd + 4 + length) return;
const message = JSON.parse(buffered.subarray(headerEnd + 4, headerEnd + 4 + length).toString());
buffered = buffered.subarray(headerEnd + 4 + length);
if (message.method && message.id !== undefined) {
// Server-to-client requests: configuration, capability registration, progress.
const result = message.method === "workspace/configuration" ? message.params.items.map(() => null) : null;
send({ id: message.id, result });
} else if (message.id !== undefined && pending.has(message.id)) {
pending.get(message.id)(message.result ?? message.error);
pending.delete(message.id);
}
}
});
const uri = (file) => pathToFileURL(`${root}${file}`).href;
await request("initialize", {
processId: process.pid,
rootUri: pathToFileURL(root).href,
workspaceFolders: [{ uri: pathToFileURL(root).href, name: "repro" }],
capabilities: {
workspace: { configuration: true, workspaceFolders: true },
textDocument: {
synchronization: { dynamicRegistration: true },
diagnostic: { dynamicRegistration: true },
},
},
initializationOptions: { runExternalCode: true },
});
send({ method: "initialized", params: {} });
for (const [file, languageId] of [["card.foo", "plaintext"], ["control.tsx", "typescriptreact"]]) {
const text = readFileSync(`${root}${file}`, "utf8");
send({ method: "textDocument/didOpen", params: { textDocument: { uri: uri(file), languageId, version: 1, text } } });
// Wait until the project (and the mapper) has loaded the file.
await request("textDocument/diagnostic", { textDocument: { uri: uri(file) } });
const character = text.indexOf("<b>") + "<b>".length;
const response = await request("textDocument/_vs_onAutoInsert", {
_vs_textDocument: { uri: uri(file) },
_vs_position: { line: 0, character },
_vs_ch: ">",
});
console.log(`${file}: ${JSON.stringify(response)}`);
}
await request("shutdown");
send({ method: "exit" });
export const card = <div><b></div>;
export const card = <div><b></div>;
// Identity content mapper: .foo contains TSX, returned without changes.
// No TSRX, Volar, or framework dependencies.
let buffered = Buffer.alloc(0);
process.stdin.on("data", (chunk) => {
buffered = Buffer.concat([buffered, chunk]);
while (true) {
const headerEnd = buffered.indexOf("\r\n\r\n");
if (headerEnd < 0) return;
const header = buffered.subarray(0, headerEnd).toString("ascii");
const match = /^Content-Length:\s*(\d+)\s*$/im.exec(header);
if (!match) throw new Error("Missing Content-Length");
const end = headerEnd + 4 + Number(match[1]);
if (buffered.length < end) return;
const request = JSON.parse(buffered.subarray(headerEnd + 4, end).toString("utf8"));
buffered = buffered.subarray(end);
if (request.id === undefined || request.id === null) continue;
let result;
switch (request.method) {
case "initialize":
result = { positionEncoding: "utf-16", diagnosticSource: "foo" };
break;
case "openProject":
result = {};
break;
case "closeProject":
result = null;
break;
case "transform": {
const text = request.params.content;
result = {
text,
extension: ".tsx",
mappings: [[0, text.length, 0, text.length, 0]],
};
break;
}
default:
throw new Error(`Unexpected request: ${request.method}`);
}
const body = Buffer.from(JSON.stringify({ jsonrpc: "2.0", id: request.id, result }));
process.stdout.write(`Content-Length: ${body.length}\r\n\r\n`);
process.stdout.write(body);
}
});
process.stdin.on("end", () => process.exit(0));
{
"name": "typescript-content-mapper-auto-insert-repro",
"private": true,
"type": "module",
"scripts": {
"setup": "node setup.mjs",
"ask": "node ask.mjs"
},
"devDependencies": {
"typescript": "7.1.0-dev.20260930.4"
}
}
import { mkdirSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
// TypeScript resolves the local mapper package and starts this Node script.
const directory = new URL("./node_modules/identity-foo-mapper/", import.meta.url);
mkdirSync(directory, { recursive: true });
writeFileSync(new URL("package.json", directory), JSON.stringify({
name: "identity-foo-mapper",
version: "1.0.0",
type: "module",
typescript: {
contentMapper: {
exec: [process.execPath, fileURLToPath(new URL("./mapper.mjs", import.meta.url))],
compilerOptions: [],
},
},
}, null, 2) + "\n");
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"jsx": "preserve",
"strict": true,
"noEmit": true,
"types": []
},
"contentMappers": [
{
"package": "identity-foo-mapper",
"extensions": [
".foo"
]
}
],
"include": [
"card.foo",
"control.tsx",
"type-here.foo",
"type-here.tsx"
]
}
export const card = <div></div>;
export const card = <div></div>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment