Skip to content

Commit

Permalink
feat: automatically noexport .of(redacted())
Browse files Browse the repository at this point in the history
  • Loading branch information
sparecycles committed Sep 6, 2024
1 parent c15cfe1 commit b39dc82
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/core/schema/core/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ export function defineSchema<Ops extends SchematicOps<unknown>>(
*/
export function extractOps<O extends SchematicOps<unknown>>(
schema: Schema<OpsValueType<O>>,
): O {
return schema() as O;
): Partial<O> {
return schema() as Partial<O>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import {
executeOp,
} from "../../core/schema.js";

type RedactSchemaOps<T = unknown> = SchematicOps<T> & {
export type RedactOps<T = unknown> = SchematicOps<T> & {
redact(): void;
schema(): Schema<T>;
};

export function redactSchema<T = unknown>(schema: Schema<T>): Schema<T> {
return defineSchema<RedactSchemaOps<T>>({
return defineSchema<RedactOps<T>>({
merge(context) {
const merged = executeOp(schema, "merge", context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import {
executeOp,
isMatchingContext,
SchemaRenderContext,
extractOps,
} from "../../core/schema.js";
import { parseScopedIdentifier } from "../../core/scope.js";
import { expandTemplate, templateTrampoline } from "../../template.js";
import { RedactOps } from "./redact-schema.js";
import { stubSchema } from "./stub-schema.js";

type ReferenceOps<T = unknown> = SchematicOps<T> & {
Expand All @@ -43,12 +45,23 @@ export function referenceSchema<T = unknown>(
const schemaDefintion = defineSchema<ReferenceOps<T>>({
merge(context) {
const merged = executeOp(schema, "merge", context);
let mergedHint = hint;

if (merged && context.stub !== undefined) {
if (
typeof context.stub === "function" &&
extractOps<RedactOps<T>>(context.stub as Schema<T>).redact
) {
mergedHint = `${hint}@`;
}

context.scope.define(context, reference, context.stub);
}

return merged && referenceSchema(reference, { schema: merged, hint });
return (
merged &&
referenceSchema(reference, { schema: merged, hint: mergedHint })
);
},
async render(context) {
const value = context.scope.resolve(context, reference);
Expand Down Expand Up @@ -127,12 +140,23 @@ export function referenceSchema<T = unknown>(
return referenceSchema(`${reference}.@key`, { hint, schema });
},
of(definition: unknown) {
return templateTrampoline((context) =>
referenceSchema(reference, {
hint,
schema: expandTemplate(definition, context),
}),
);
return templateTrampoline((context) => {
let ofHint = hint;

const schema = expandTemplate(definition, context);

if (
typeof schema === "function" &&
extractOps<RedactOps<T>>(schema as Schema<T>).redact
) {
ofHint = `${hint}@`;
}

return referenceSchema(reference, {
hint: ofHint,
schema,
});
});
},
}[property]
);
Expand Down

0 comments on commit b39dc82

Please sign in to comment.