ProvenPin Install

magic-string sourcemap edits: working versions (tested 2026-10-15)

Overwrite, prependLeft, append and remove ranges in a source string with magic-string, generate a hires source map and verify positions with source-map-js SourceMapConsumer.originalPositionFor offline.

passed3 passed · 0 failed2026-10-15 22:58 UTC
Status
passed
Last passed
2026-10-15 22:58 UTC
Tests
3 / 3
Language
TypeScript
Runtime
node v22.23.3
pinned versions4
magic-string@1.4.3source-map-js@1.2.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false
npm test
test environment
node v22.23.3
linux 6.17.0-1022-azure
x64
runner: github-actions 38820570375

The code

Exactly the files that passed. Your agent can pull them with get_recipe("magic-string-sourcemap-edits") (install), or see them on GitHub.

package.json
{
  "name": "magic-string-sourcemap-edits",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {
    "magic-string": "1.4.3",
    "source-map-js": "1.2.2"
  },
  "devDependencies": {
    "@types/node": "26.6.5",
    "tsx": "4.23.15"
  }
}
src/transform.ts
import MagicString from "magic-string";

export type TransformResult = {
  code: string;
  map: ReturnType<MagicString["generateMap"]>;
};

export function transform(input: string): TransformResult {
  const magic = new MagicString(input);

  const worldStart = input.indexOf('"world"');
  magic.overwrite(worldStart, worldStart + '"world"'.length, '"provenpin"');

  const greetingStart = input.indexOf("greeting");
  magic.prependLeft(greetingStart, "/* left */");

  const unusedStart = input.indexOf("// unused");
  magic.remove(unusedStart, unusedStart + "// unused".length);

  magic.append("\nexport const version = 1;\n");

  const map = magic.generateMap({
    source: "input.ts",
    file: "output.ts",
    hires: true,
    includeContent: true,
  });

  return { code: magic.toString(), map };
}
test/transform.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { SourceMapConsumer } from "source-map-js";
import { transform } from "../src/transform.ts";

const input = [
  'const greeting = "hello";',
  "// unused",
  'const target = "world";',
  "console.log(greeting, target);",
  "",
].join("\n");

function locate(text: string, needle: string): { line: number; column: number } {
  const index = text.indexOf(needle);
  assert.notEqual(index, -1, `expected to find ${needle}`);
  const before = text.slice(0, index);
  const lines = before.split("\n");
  return { line: lines.length, column: lines[lines.length - 1].length };
}

test("applies overwrite, prepend, remove and append edits", () => {
  const { code } = transform(input);
  assert.ok(code.includes('const /* left */greeting = "hello";'));
  assert.ok(code.includes('const target = "provenpin";'));
  assert.ok(!code.includes("// unused"));
  assert.ok(code.includes("export const version = 1;"));
});

test("hires sourcemap maps generated positions back to the original", () => {
  const { code, map } = transform(input);
  const consumer = new SourceMapConsumer(map.toString());

  const generated = locate(code, "console.log(greeting, target)");
  const original = locate(input, "console.log(greeting, target)");
  const mapped = consumer.originalPositionFor(generated);

  assert.equal(mapped.source, "input.ts");
  assert.equal(mapped.line, original.line);
  assert.equal(mapped.column, original.column);

  const editedGenerated = locate(code, "const target =");
  const editedOriginal = locate(input, "const target =");
  const editedMapped = consumer.originalPositionFor(editedGenerated);

  assert.equal(editedMapped.line, editedOriginal.line);
  assert.equal(editedMapped.column, editedOriginal.column);
});

test("embedded source content is preserved in the map", () => {
  const { map } = transform(input);
  const raw = JSON.parse(map.toString()) as {
    file: string;
    sources: string[];
    sourcesContent: string[];
  };

  assert.equal(raw.file, "output.ts");
  assert.equal(raw.sources[0], "input.ts");
  assert.equal(raw.sourcesContent[0], input);
});

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.