Skip to content

Commit 5088996

Browse files
committed
Clean up vendored schema compiler helpers
1 parent 6de35aa commit 5088996

9 files changed

Lines changed: 23 additions & 33 deletions

File tree

packages/core/sdk/src/vendor/json-schema-to-typescript/lodash.ts renamed to packages/core/sdk/src/vendor/json-schema-to-typescript/compat.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { Struct } from "effect";
2+
3+
// Keep these helpers deliberately small and local. They preserve only the
4+
// upstream helper behavior this vendored compiler actually uses.
15
export const isPlainObject = (value: unknown): value is Record<string, unknown> => {
26
if (value === null || typeof value !== "object") return false;
37
const prototype = Object.getPrototypeOf(value);
@@ -6,8 +10,6 @@ export const isPlainObject = (value: unknown): value is Record<string, unknown>
610

711
export const cloneDeep = <T>(value: T): T => structuredClone(value);
812

9-
export const endsWith = (value: string, suffix: string): boolean => value.endsWith(suffix);
10-
1113
export const merge = <T extends Record<string, unknown>>(
1214
target: T,
1315
...sources: ReadonlyArray<Record<string, unknown> | undefined>
@@ -36,13 +38,6 @@ export const findKey = <T>(
3638
return undefined;
3739
};
3840

39-
export const includes = <T>(array: ReadonlyArray<T>, value: T): boolean => array.includes(value);
40-
41-
export const map = <T, U>(
42-
object: Record<string, T> | undefined,
43-
iteratee: (value: T, key: string) => U,
44-
): U[] => (object ? Object.entries(object).map(([key, value]) => iteratee(value, key)) : []);
45-
4641
export const memoize = <F extends (arg: any, ...rest: any[]) => any>(fn: F): F => {
4742
const cache = new Map<Parameters<F>[0], ReturnType<F>>();
4843
return ((arg: Parameters<F>[0], ...rest: unknown[]) => {
@@ -53,12 +48,8 @@ export const memoize = <F extends (arg: any, ...rest: any[]) => any>(fn: F): F =
5348
}) as F;
5449
};
5550

56-
export const omit = <T extends object, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K> => {
57-
const omitted = new Set<PropertyKey>(keys);
58-
return Object.fromEntries(
59-
Object.entries(object).filter(([key]) => !omitted.has(key)),
60-
) as Omit<T, K>;
61-
};
51+
export const omit = <T extends object, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K> =>
52+
Struct.omit(object, keys);
6253

6354
export const uniqBy = <T>(items: ReadonlyArray<T>, iteratee: (value: T) => string): T[] => {
6455
const seen = new Set<string>();
@@ -75,7 +66,5 @@ export const uniqBy = <T>(items: ReadonlyArray<T>, iteratee: (value: T) => strin
7566
export const deburr = (value: string): string =>
7667
value.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
7768

78-
export const trim = (value: string): string => value.trim();
79-
8069
export const upperFirst = (value: string): string =>
8170
value.length === 0 ? value : value[0]!.toUpperCase() + value.slice(1);

packages/core/sdk/src/vendor/json-schema-to-typescript/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memoize, omit } from "./lodash";
1+
import { memoize, omit } from "./compat";
22
import { DEFAULT_OPTIONS, type Options } from "./index";
33
import { hasComment, hasStandaloneName, T_ANY, T_UNKNOWN } from "./types/AST";
44
import type {

packages/core/sdk/src/vendor/json-schema-to-typescript/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { readFileSync } from "fs";
44
import type { JSONSchema4 } from "./types/json-schema";
55
import type { ParserOptions as $RefOptions } from "@apidevtools/json-schema-ref-parser";
6-
import { cloneDeep, endsWith, merge } from "./lodash";
6+
import { cloneDeep, merge } from "./compat";
77
import { dirname } from "path";
88
import { format } from "./formatter";
99
import { generate } from "./generator";
@@ -172,7 +172,7 @@ export async function compile(
172172
}
173173

174174
// normalize options
175-
if (!endsWith(_options.cwd, "/")) {
175+
if (!_options.cwd.endsWith("/")) {
176176
_options.cwd += "/";
177177
}
178178

packages/core/sdk/src/vendor/json-schema-to-typescript/linker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Parent } from "./types/JSONSchema";
22
import type { JSONSchema, LinkedJSONSchema } from "./types/JSONSchema";
3-
import { isPlainObject } from "./lodash";
3+
import { isPlainObject } from "./compat";
44
import type { JSONSchema4Type } from "./types/json-schema";
55

66
/**

packages/core/sdk/src/vendor/json-schema-to-typescript/optimizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { uniqBy } from "./lodash";
1+
import { uniqBy } from "./compat";
22
import type { Options } from ".";
33
import { generateType } from "./generator";
44
import { T_ANY, T_UNKNOWN } from "./types/AST";

packages/core/sdk/src/vendor/json-schema-to-typescript/parser.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JSONSchema4Type, JSONSchema4TypeName } from "./types/json-schema";
2-
import { findKey, includes, isPlainObject, map, memoize, omit } from "./lodash";
2+
import { findKey, isPlainObject, memoize, omit } from "./compat";
33
import { format } from "util";
44
import type { Options } from "./";
55
import { applySchemaTyping } from "./applySchemaTyping";
@@ -428,10 +428,11 @@ function parseSchema(
428428
usedNames: UsedNames,
429429
parentSchemaName: string,
430430
): TInterfaceParam[] {
431-
let asts: TInterfaceParam[] = map(schema.properties, (value, key: string) => ({
431+
const required = new Set(schema.required ?? []);
432+
let asts: TInterfaceParam[] = Object.entries(schema.properties ?? {}).map(([key, value]) => ({
432433
ast: parse(value, options, key, processed, usedNames),
433434
isPatternProperty: false,
434-
isRequired: includes(schema.required || [], key),
435+
isRequired: required.has(key),
435436
isUnreachableDefinition: false,
436437
keyName: key,
437438
}));
@@ -445,15 +446,15 @@ function parseSchema(
445446
!schema.additionalProperties && Object.keys(schema.patternProperties).length === 1;
446447

447448
asts = asts.concat(
448-
map(schema.patternProperties, (value, key: string) => {
449+
Object.entries(schema.patternProperties).map(([key, value]) => {
449450
const ast = parse(value, options, key, processed, usedNames);
450451
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema definition
451452
via the \`patternProperty\` "${key.replace("*/", "*\\/")}".`;
452453
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment;
453454
return {
454455
ast,
455456
isPatternProperty: !singlePatternProperty,
456-
isRequired: singlePatternProperty || includes(schema.required || [], key),
457+
isRequired: singlePatternProperty || required.has(key),
457458
isUnreachableDefinition: false,
458459
keyName: singlePatternProperty ? "[k: string]" : key,
459460
};
@@ -463,15 +464,15 @@ via the \`patternProperty\` "${key.replace("*/", "*\\/")}".`;
463464

464465
if (options.unreachableDefinitions) {
465466
asts = asts.concat(
466-
map(schema.$defs, (value, key: string) => {
467+
Object.entries(schema.$defs ?? {}).map(([key, value]) => {
467468
const ast = parse(value, options, key, processed, usedNames);
468469
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema
469470
via the \`definition\` "${key}".`;
470471
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment;
471472
return {
472473
ast,
473474
isPatternProperty: false,
474-
isRequired: includes(schema.required || [], key),
475+
isRequired: required.has(key),
475476
isUnreachableDefinition: true,
476477
keyName: key,
477478
};

packages/core/sdk/src/vendor/json-schema-to-typescript/types/JSONSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JSONSchema4, JSONSchema4Type, JSONSchema4TypeName } from "./json-schema";
2-
import { isPlainObject, memoize } from "../lodash";
2+
import { isPlainObject, memoize } from "../compat";
33

44
export type SchemaType =
55
| "ALL_OF"

packages/core/sdk/src/vendor/json-schema-to-typescript/typesOfSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isPlainObject } from "./lodash";
1+
import { isPlainObject } from "./compat";
22
import { isCompound } from "./types/JSONSchema";
33
import type { JSONSchema, SchemaType } from "./types/JSONSchema";
44

packages/core/sdk/src/vendor/json-schema-to-typescript/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deburr, isPlainObject, trim, upperFirst } from "./lodash";
1+
import { deburr, isPlainObject, upperFirst } from "./compat";
22
import { basename, dirname, extname, normalize, sep, posix } from "path";
33
import {
44
Intersection,
@@ -204,7 +204,7 @@ export function toSafeString(string: string): string {
204204
// uppercase letters after digits, dollars
205205
.replace(/([\d$]+[a-zA-Z])/g, (match) => match.toUpperCase())
206206
// uppercase first letter after whitespace
207-
.replace(/\s+([a-zA-Z])/g, (match) => trim(match.toUpperCase()))
207+
.replace(/\s+([a-zA-Z])/g, (match) => match.toUpperCase().trim())
208208
// remove remaining whitespace
209209
.replace(/\s/g, ""),
210210
);

0 commit comments

Comments
 (0)