MCPcopy Index your code
hub / github.com/github/copilot-sdk / rewrite

Function rewrite

scripts/codegen/typescript.ts:276–333  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

274 delete root.$defs;
275
276 const rewrite = (value: unknown): unknown => {
277 if (Array.isArray(value)) {
278 return value.map(rewrite);
279 }
280 if (!value || typeof value !== "object") {
281 return value;
282 }
283
284 const rewritten = Object.fromEntries(
285 Object.entries(value as Record<string, unknown>).map(([key, child]) => [key, rewrite(child)])
286 ) as Record<string, unknown>;
287
288 // The TypeScript codegen doesn't distinguish opaque JSON from any
289 // other unconstrained value, so drop the marker before feeding the
290 // schema to json-schema-to-typescript. C# codegen reads the marker
291 // from its own (un-normalized) view of the schema and emits
292 // `JsonElement` instead.
293 stripOpaqueJsonMarker(rewritten);
294
295 const enumValueDescriptions = getEnumValueDescriptions(rewritten as JSONSchema7);
296 if (enumValueDescriptions && Array.isArray(rewritten.enum) && rewritten.enum.every((entry) => typeof entry === "string")) {
297 rewritten.tsType = (rewritten.enum as string[])
298 .map((entry) => {
299 const comment = enumValueDescriptions[entry];
300 const literal = JSON.stringify(entry);
301 return comment ? `${tsDocCommentText(comment)}\n| ${literal}` : `| ${literal}`;
302 })
303 .join("\n");
304 delete rewritten.type;
305 delete rewritten.enum;
306 delete rewritten["x-enumDescriptions"];
307 }
308
309 if (typeof rewritten.$ref === "string") {
310 const externalRef = parseExternalSchemaRef(rewritten.$ref);
311 if (externalRef && EXTERNAL_SCHEMA_TS_IMPORT[externalRef.schemaFile]) {
312 rewritten.tsType = externalRef.definitionName;
313 for (const key of Object.keys(rewritten)) {
314 if (key !== "tsType") {
315 delete rewritten[key];
316 }
317 }
318 } else if (rewritten.$ref.startsWith("#/$defs/")) {
319 const definitionName = rewritten.$ref.slice("#/$defs/".length);
320 rewritten.$ref = `#/definitions/${draftDefinitionAliases.get(definitionName) ?? definitionName}`;
321 }
322 // json-schema-to-typescript treats sibling keywords alongside $ref as a
323 // new inline type instead of reusing the referenced definition. Strip
324 // siblings so that $ref-only objects compile to a single shared type.
325 if ("$ref" in rewritten) {
326 for (const key of Object.keys(rewritten)) {
327 if (key !== "$ref") delete rewritten[key];
328 }
329 }
330 }
331
332 return rewritten;
333 };

Callers 1

Calls 6

stripOpaqueJsonMarkerFunction · 0.85
getEnumValueDescriptionsFunction · 0.85
tsDocCommentTextFunction · 0.85
parseExternalSchemaRefFunction · 0.85
joinMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…