Skip to content

Commit

Permalink
Make createStateSymbol a common utility function for `@typespec/com…
Browse files Browse the repository at this point in the history
…piler` (#5750)

This commit addresses a suggestion from
c788082 to refactor `createStateSymbol`
into a shared utility function. This is not meant for use outside of
`@typespec/compiler`, but rather an internal utility.
  • Loading branch information
steverice authored Jan 25, 2025
1 parent 2a58296 commit d5aaf39
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/compiler"
---

Make `createStateSymbol` a common utility function for `@typespec/compiler`
5 changes: 1 addition & 4 deletions packages/compiler/src/core/deprecation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { createStateSymbol } from "../lib/utils.js";
import type { Program } from "./program.js";
import { BaseNode, Node, SyntaxKind, Type } from "./types.js";

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

const deprecatedKey = createStateSymbol("deprecated");

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/compiler/src/core/intrinsic-type-state.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Contains all intrinsic data setter or getter
// Anything that the TypeSpec check might should be here.

import { createStateSymbol } from "../lib/utils.js";
import type { Model, Type, Union } from "./index.js";
import type { Numeric } from "./numeric.js";
import type { Program } from "./program.js";

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

const stateKeys = {
minValues: createStateSymbol("minValues"),
maxValues: createStateSymbol("maxValues"),
Expand Down
5 changes: 1 addition & 4 deletions packages/compiler/src/core/visibility/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from "./lifecycle.js";

import type { VisibilityFilter as GeneratedVisibilityFilter } from "../../../generated-defs/TypeSpec.js";
import { createStateSymbol } from "../../lib/utils.js";
import { useStateMap, useStateSet } from "../../utils/index.js";

export { GeneratedVisibilityFilter };
Expand All @@ -38,10 +39,6 @@ export { GeneratedVisibilityFilter };
*/
type VisibilityModifiers = Map<Enum, Set<EnumMember>>;

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

/**
* The global visibility store.
*
Expand Down
5 changes: 1 addition & 4 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import {
} from "../core/types.js";
import { useStateMap, useStateSet } from "../utils/index.js";
import { setKey } from "./key.js";
import { createStateSymbol } from "./utils.js";

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

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

const [getSummary, setSummary] = useStateMap<Type, string>(createStateSymbol("summary"));
/**
* @summary attaches a documentation string. It is typically used to give a short, single-line
Expand Down
5 changes: 1 addition & 4 deletions packages/compiler/src/lib/encoded-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { parseMimeType } from "../core/mime-type.js";
import type { Program } from "../core/program.js";
import type { DecoratorContext, Enum, Model, Type, Union } from "../core/types.js";
import { DuplicateTracker, useStateMap } from "../utils/index.js";

function createStateSymbol(name: string) {
return Symbol.for(`TypeSpec.${name}`);
}
import { createStateSymbol } from "./utils.js";

const [getEncodedNamesMap, setEncodedNamesMap, getEncodedNamesStateMap] = useStateMap<
Type,
Expand Down
5 changes: 1 addition & 4 deletions packages/compiler/src/lib/key.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Program } from "../core/index.js";
import { ModelProperty, Type } from "../core/types.js";
import { useStateMap } from "../utils/index.js";

function createStateSymbol(name: string) {
return Symbol.for(`TypeSpec.${name}`);
}
import { createStateSymbol } from "./utils.js";

const [getKey, setKey] = useStateMap<Type, string>(createStateSymbol("key"));

Expand Down
5 changes: 1 addition & 4 deletions packages/compiler/src/lib/paging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
Operation,
Type,
} from "../core/types.js";
import { createStateSymbol } from "../lib/utils.js";
import { DuplicateTracker, useStateSet } from "../utils/index.js";
import { isNumericType, isStringType } from "./decorators.js";

Expand Down Expand Up @@ -341,10 +342,6 @@ function validatePagingOperation(program: Program, op: Operation) {
program.reportDiagnostics(diagnostics);
}

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

function createMarkerDecorator<T extends DecoratorFunction>(
key: string,
validate?: (...args: Parameters<T>) => boolean,
Expand Down
8 changes: 8 additions & 0 deletions packages/compiler/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ export function filterModelPropertiesInPlace(
}
}
}

/**
* Creates a unique symbol for storing state on objects
* @param name The name/description of the state
*/
export function createStateSymbol(name: string): symbol {
return Symbol.for(`TypeSpec.${name}`);
}
6 changes: 1 addition & 5 deletions packages/compiler/src/lib/visibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ import {
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 { createStateSymbol, filterModelPropertiesInPlace } from "./utils.js";

// #region Legacy Visibility Utilities

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

/**
* Takes a list of visibilities that possibly include both legacy visibility
* strings and visibility class members, and returns two lists containing only
Expand Down

0 comments on commit d5aaf39

Please sign in to comment.