Skip to content

Commit

Permalink
Fix regressions during refactor of state accessor (#4471)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Sep 18, 2024
1 parent 8df0a9f commit 94ff9d6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
9 changes: 9 additions & 0 deletions .chronus/changes/fix-invalid-or-2024-8-18-21-5-24.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: internal
packages:
- "@typespec/compiler"
- "@typespec/rest"
---

Fix regressions during refactor of state accessor
4 changes: 2 additions & 2 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,8 @@ export function isKey(program: Program, property: ModelProperty) {
return getKey(program, property) !== undefined;
}

export function getKeyName(program: Program, property: ModelProperty): string {
return getKey(program, property) || property.name;
export function getKeyName(program: Program, property: ModelProperty): string | undefined {
return getKey(program, property);
}

export const $withDefaultKeyVisibility: WithDefaultKeyVisibilityDecorator = (
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Service extends ServiceDetails {
}

const [getService, setService, getServiceMap] = useStateMap<Namespace, Service>(
"@typespec/compiler.services",
Symbol.for("@typespec/compiler.services"),
);

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/compiler/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ function createStateSymbol(name: string) {
return Symbol.for(`TypeSpec.${name}`);
}

export function useStateMap<K extends Type, V>(key: string) {
return unsafe_useStateMap<K, V>(createStateSymbol(key));
export function useStateMap<K extends Type, V>(key: string | symbol) {
return unsafe_useStateMap<K, V>(typeof key === "string" ? createStateSymbol(key) : key);
}

export function useStateSet<K extends Type>(key: string) {
return unsafe_useStateSet<K>(createStateSymbol(key));
export function useStateSet<K extends Type>(key: string | symbol) {
return unsafe_useStateSet<K>(typeof key === "string" ? createStateSymbol(key) : key);
}
11 changes: 11 additions & 0 deletions packages/compiler/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,17 @@ describe("compiler: built-in decorators", () => {
strictEqual(getKeyName(runner.program, prop), "alternateName");
});

it("getKeyName returns undefined if used on property not annotated with @key", async () => {
const { prop } = await runner.compile(
`model M {
@test prop: string;
}`,
);

strictEqual(prop.kind, "ModelProperty" as const);
strictEqual(getKeyName(runner.program, prop), undefined);
});

it("emits diagnostic when key property is marked as optional", async () => {
const diagnostics = await runner.diagnose(
`model M {
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function cloneKeyProperties(context: DecoratorContext, target: Model, resourceTy
const resourceKey = getResourceTypeKey(program, resourceType);
if (resourceKey) {
const { keyProperty } = resourceKey;
const keyName = getKeyName(program, keyProperty);
const keyName = getKeyName(program, keyProperty)!;

const decorators = [
// Filter out the @visibility decorator because it might affect metadata
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function checkForDuplicateResourceKeyNames(program: Program) {
while (currentType) {
const resourceKey = getResourceTypeKey(program, currentType);
if (resourceKey) {
const keyName = getKeyName(program, resourceKey.keyProperty);
const keyName = getKeyName(program, resourceKey.keyProperty)!;
keyProperties.track(keyName, resourceKey);
}

Expand Down

0 comments on commit 94ff9d6

Please sign in to comment.