Skip to content

Commit

Permalink
Export and use utility functions from lib/utils
Browse files Browse the repository at this point in the history
Export `createStateSymbol` from `lib/utils`, and export all of utils in `@typespec/compiler` so they can be used by other packages.

Additionally, replace duplicate definitions of `utils` functions in `@typespec/compiler` with imports from `utils`.
  • Loading branch information
steverice committed Jan 16, 2025
1 parent 2bd7bc0 commit 8aed1d5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/compiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { resolveCompilerOptions, ResolveCompilerOptionsOptions } from "./config/index.js";
export * from "./core/index.js";
export * from "./lib/decorators.js";
export * from "./lib/utils.js";
export * from "./server/index.js";
export type { PackageJson } from "./types/package-json.js";
import * as formatter from "./formatter/index.js";
Expand Down
22 changes: 6 additions & 16 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ import {
Value,
} from "../core/types.js";
import { setKey } from "./key.js";
import { useStateMap, useStateSet } from "./utils.js";
import {
createStateSymbol,
filterModelPropertiesInPlace,
useStateMap,
useStateSet,
} from "./utils.js";

export { $encodedName, resolveEncodedName } from "./encoded-names.js";
export { serializeValueAsJson } from "./examples.js";
Expand All @@ -121,10 +126,6 @@ function replaceTemplatedStringFromProperties(formatString: string, sourceObject
});
}

export function createStateSymbol(name: string) {
return Symbol.for(`TypeSpec.${name}`);
}

const [getSummary, setSummary] = useStateMap<Type, string>("summary");
/**
* @summary attaches a documentation string. It is typically used to give a short, single-line
Expand Down Expand Up @@ -810,17 +811,6 @@ function validateEncodeData(context: DecoratorContext, target: Type, encodeData:

export { getEncode };

export function filterModelPropertiesInPlace(
model: Model,
filter: (prop: ModelProperty) => boolean,
) {
for (const [key, prop] of model.properties) {
if (!filter(prop)) {
model.properties.delete(key);
}
}
}

// -- @withOptionalProperties decorator ---------------------

export const $withOptionalProperties: WithOptionalPropertiesDecorator = (
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Model, ModelProperty, Type } from "../core/types.js";
import { unsafe_useStateMap, unsafe_useStateSet } from "../experimental/state-accessor.js";

function createStateSymbol(name: string) {
export function createStateSymbol(name: string) {
return Symbol.for(`TypeSpec.${name}`);
}

Expand Down

0 comments on commit 8aed1d5

Please sign in to comment.