Skip to content

Commit

Permalink
Re-add unsafe_ exports as deprecated
Browse files Browse the repository at this point in the history
To smooth the transition for any packages relying on these exports.
  • Loading branch information
steverice committed Jan 23, 2025
1 parent 99a7948 commit 951ba9e
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 26 deletions.
10 changes: 8 additions & 2 deletions packages/compiler/src/experimental/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
export {
MutableType as unsafe_MutableType,
mutateSubgraph as unsafe_mutateSubgraph,
mutateSubgraphWithNamespace as unsafe_mutateSubgraphWithNamespace,
Mutator as unsafe_Mutator,
MutatorFilterFn as unsafe_MutatorFilterFn,
MutatorFlow as unsafe_MutatorFlow,
MutatorFn as unsafe_MutatorFn,
MutatorRecord as unsafe_MutatorRecord,
MutatorReplaceFn as unsafe_MutatorReplaceFn,
MutatorWithNamespace as unsafe_MutatorWithNamespace,
mutateSubgraph as unsafe_mutateSubgraph,
mutateSubgraphWithNamespace as unsafe_mutateSubgraphWithNamespace,
} from "./mutators.js";
export { Realm as unsafe_Realm } from "./realm.js";
export { $ as unsafe_$ } from "./typekit/index.js";

import { useStateMap, useStateSet } from "../utils/state-accessor.js";
/** @deprecated use `useStateMap` from `@typespec/compiler/utils` instead */
export const unsafe_useStateMap = useStateMap;
/** @deprecated use `useStateSet` from `@typespec/compiler/utils` instead */
export const unsafe_useStateSet = useStateSet;
22 changes: 16 additions & 6 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ import {
UnionVariant,
Value,
} from "../core/types.js";
import { setKey } from "./key.js";
import { useStateMap, useStateSet } from "../utils/index.js";
import { setKey } from "./key.js";

export { $encodedName, resolveEncodedName } from "./encoded-names.js";
export { serializeValueAsJson } from "./examples.js";
Expand Down Expand Up @@ -395,7 +395,9 @@ export const $format: FormatDecorator = (
export { getFormat };

// -- @pattern decorator ---------------------
const [getPatternData, setPatternData] = useStateMap<Type, PatternData>(createStateSymbol("patternValues"));
const [getPatternData, setPatternData] = useStateMap<Type, PatternData>(
createStateSymbol("patternValues"),
);

export interface PatternData {
readonly pattern: string;
Expand Down Expand Up @@ -690,7 +692,9 @@ export interface EncodeData {
type: Scalar;
}

const [getEncode, setEncodeData] = useStateMap<Scalar | ModelProperty, EncodeData>(createStateSymbol("encode"));
const [getEncode, setEncodeData] = useStateMap<Scalar | ModelProperty, EncodeData>(
createStateSymbol("encode"),
);
export const $encode: EncodeDecorator = (
context: DecoratorContext,
target: Scalar | ModelProperty,
Expand Down Expand Up @@ -943,7 +947,9 @@ export function getAllTags(

// -- @friendlyName decorator ---------------------

const [getFriendlyName, setFriendlyName] = useStateMap<Type, string>(createStateSymbol("friendlyNames"));
const [getFriendlyName, setFriendlyName] = useStateMap<Type, string>(
createStateSymbol("friendlyNames"),
);
export const $friendlyName: FriendlyNameDecorator = (
context: DecoratorContext,
target: Type,
Expand Down Expand Up @@ -1100,7 +1106,9 @@ export function getDeprecated(program: Program, type: Type): string | undefined
return getDeprecationDetails(program, type)?.message;
}

const [getOverloads, setOverloads] = useStateMap<Operation, Operation[]>(createStateSymbol("overloadedByKey"));
const [getOverloads, setOverloads] = useStateMap<Operation, Operation[]>(
createStateSymbol("overloadedByKey"),
);
const [getOverloadedOperation, setOverloadBase] = useStateMap<Operation, Operation>(
createStateSymbol("overloadsOperation"),
);
Expand Down Expand Up @@ -1334,7 +1342,9 @@ export function getExamples(
return getExamplesState(program, target) ?? [];
}

const [getOpExamplesState, setOpExamples] = useStateMap<Operation, OpExample[]>(createStateSymbol("opExamples"));
const [getOpExamplesState, setOpExamples] = useStateMap<Operation, OpExample[]>(
createStateSymbol("opExamples"),
);
export const $opExample: OpExampleDecorator = (
context: DecoratorContext,
target: Operation,
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/lib/visibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import {
normalizeVisibilityToLegacyLifecycleString,
} from "../core/visibility/lifecycle.js";
import { isMutableType, mutateSubgraph, Mutator, MutatorFlow } from "../experimental/mutators.js";
import { useStateMap } from "../utils/index.js";
import { isKey } from "./key.js";
import { filterModelPropertiesInPlace } from "./utils.js";
import { useStateMap } from "../utils/index.js";

// #region Legacy Visibility Utilities

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
// Be explicit about what get exported so we don't export utils that are not meant to be public.
// ---------------------------------------
export { DuplicateTracker } from "./duplicate-tracker.js";
export { useStateMap, useStateSet } from "./state-accessor.js";
export { Queue, TwoLevelMap, createRekeyableMap, deepClone, deepEquals } from "./misc.js";
export { useStateMap, useStateSet } from "./state-accessor.js";
4 changes: 1 addition & 3 deletions packages/compiler/src/utils/state-accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export function useStateMap<K extends Type, V>(
type StateSetGetter<K extends Type> = (program: Program, type: K) => boolean;
type StateSetSetter<K extends Type> = (program: Program, type: K) => void;

export function useStateSet<K extends Type>(
key: symbol,
): [StateSetGetter<K>, StateSetSetter<K>] {
export function useStateSet<K extends Type>(key: symbol): [StateSetGetter<K>, StateSetSetter<K>] {
const getter = (program: Program, target: K) => program.stateSet(key).has(target);
const setter = (program: Program, target: K) => program.stateSet(key).add(target);

Expand Down
2 changes: 1 addition & 1 deletion packages/events/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type ModelProperty, type Union, type UnionVariant } from "@typespec/compiler";
import { useStateMap, useStateSet } from "@typespec/compiler/utils";
import type {
ContentTypeDecorator,
DataDecorator,
EventsDecorator,
} from "../generated-defs/TypeSpec.Events.js";
import { EventsStateKeys } from "./lib.js";
import { useStateMap, useStateSet } from "@typespec/compiler/utils";

const [isEvents, setEvents] = useStateSet<Union>(EventsStateKeys.events);

Expand Down
4 changes: 4 additions & 0 deletions packages/events/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { unsafe_useStateMap } from "@typespec/compiler/experimental";

export { $lib } from "./lib.js";

export { $decorators, $onValidate } from "./tsp-index.js";

export { getContentType, isEventData, isEvents } from "./decorators.js";

unsafe_useStateMap(Symbol.for("foo"));
9 changes: 4 additions & 5 deletions packages/json-schema/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
typespecTypeToJson,
type Union,
} from "@typespec/compiler";
import { useStateMap, useStateSet } from "@typespec/compiler/utils";
import type { ValidatesRawJsonDecorator } from "../generated-defs/TypeSpec.JsonSchema.Private.js";
import type {
ContainsDecorator,
Expand All @@ -32,7 +33,6 @@ import type {
} from "../generated-defs/TypeSpec.JsonSchema.js";
import { JsonSchemaStateKeys } from "./lib.js";
import { createDataDecorator } from "./utils.js";
import { useStateMap, useStateSet } from "@typespec/compiler/utils";

/**
* TypeSpec Types that can create a json schmea declaration
Expand Down Expand Up @@ -247,10 +247,9 @@ export interface ExtensionRecord {
value: Type | unknown;
}

const [getExtensionsInternal, _, getExtensionsStateMap] = useStateMap<
Type,
ExtensionRecord[]
>(JsonSchemaStateKeys["JsonSchema.extension"]);
const [getExtensionsInternal, _, getExtensionsStateMap] = useStateMap<Type, ExtensionRecord[]>(
JsonSchemaStateKeys["JsonSchema.extension"],
);
/** {@inheritdoc ExtensionDecorator} */
export const $extension: ExtensionDecorator = (
context: DecoratorContext,
Expand Down
7 changes: 3 additions & 4 deletions packages/openapi/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,9 @@ function omitUndefined<T extends Record<string, unknown>>(data: T): T {
}

/** Get TagsMetadata set with `@tagMetadata` decorator */
const [getTagsMetadata, setTagsMetadata] = useStateMap<
Type,
{ [name: string]: TagMetadata }
>(OpenAPIKeys.tagsMetadata);
const [getTagsMetadata, setTagsMetadata] = useStateMap<Type, { [name: string]: TagMetadata }>(
OpenAPIKeys.tagsMetadata,
);

/**
* Decorator to add metadata to a tag associated with a namespace.
Expand Down
4 changes: 1 addition & 3 deletions packages/sse/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { useStateSet } from "@typespec/compiler/utils";
import type { TerminalEventDecorator } from "../generated-defs/TypeSpec.SSE.js";
import { SSEStateKeys } from "./lib.js";

const [isTerminalEvent, setTerminalEvent] = useStateSet<UnionVariant>(
SSEStateKeys.terminalEvent,
);
const [isTerminalEvent, setTerminalEvent] = useStateSet<UnionVariant>(SSEStateKeys.terminalEvent);

export const $terminalEventDecorator: TerminalEventDecorator = (context, target) => {
setTerminalEvent(context.program, target);
Expand Down

0 comments on commit 951ba9e

Please sign in to comment.