Skip to content

Commit

Permalink
[core] Fix enum-driven visibility consistency issues when projections…
Browse files Browse the repository at this point in the history
… are used (#5525)

- Changed `getLifecycleVisibilityEnum` to honor projections when
`program` is a `ProjectedProgram`.
- Taught `Projector` to project `EnumValue` values only, so that they
remain consistent with projected `Enum` and `EnumMember` references. If
the projection of the underlying `EnumMember` does not result in another
`EnumMember` (i.e. it projects to some other type, which I think should
be rare, but possible according to the type system), the projector gives
up and returns the original enum member.

A more complex solution would try to project all values, but this could
damage the scalar hierarchy and there is an open issue to consider
removing projections in favor of mutators in the future.

Closes #5450
Closes #5461

---------

Co-authored-by: Will Temple <[email protected]>
  • Loading branch information
witemple-msft and willmtemple authored Jan 8, 2025
1 parent 1536482 commit abd3664
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Enum-driven visibility decorators and projections now interact correctly.

Projections now project EnumValue values to preserve consistency with projected Enum/EnumMember types using a best-effort
strategy.
27 changes: 24 additions & 3 deletions packages/compiler/src/core/projector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,28 @@ export function createProjector(
type: Type | Value | IndeterminateEntity,
): Type | Value | IndeterminateEntity {
if (isValue(type)) {
return type;
if (type.valueKind === "EnumValue") {
// This is a hack. We project the enum itself, so we need to project enum values in order to prevent incoherence
// between projections that reference enum values and the enum/members themselves. If we project the enummember
// in the value and it results in something other than an enum member, we will give up and just return the value
// as is. Otherwise we'll return the projected member.

const projectedType = projectType(type.type);
const projectedMember = projectType(type.value);

if (projectedMember.kind === "EnumMember") {
return {
entityKind: "Value",
valueKind: "EnumValue",
type: projectedType,
value: projectedMember,
};
} else {
return type;
}
} else {
return type;
}
}
if (type.entityKind === "Indeterminate") {
return { entityKind: "Indeterminate", type: projectType(type.type) as any };
Expand Down Expand Up @@ -571,8 +592,8 @@ export function createProjector(
const args: DecoratorArgument[] = [];
for (const arg of dec.args) {
const jsValue =
typeof arg.jsValue === "object" && arg.jsValue !== null && "kind" in arg.jsValue
? projectType(arg.jsValue as any)
typeof arg.jsValue === "object" && arg.jsValue !== null && "entityKind" in arg.jsValue
? projectType(arg.jsValue as Type | Value)
: arg.jsValue;
args.push({ ...arg, value: projectType(arg.value), jsValue });
}
Expand Down
18 changes: 16 additions & 2 deletions packages/compiler/src/core/visibility/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { compilerAssert } from "../diagnostics.js";
import type { Program } from "../program.js";
import { isProjectedProgram } from "../projected-program.js";
import type { Enum, EnumMember } from "../types.js";

/**
Expand Down Expand Up @@ -30,9 +31,22 @@ export function getLifecycleVisibilityEnum(program: Program): Enum {

compilerAssert(type!.kind === "Enum", "Expected `TypeSpec.Visibility.Lifecycle` to be an enum");

LIFECYCLE_ENUM_CACHE.set(program, type);
if (isProjectedProgram(program)) {
const projectedType = program.projector.projectType(type);

return type;
compilerAssert(
projectedType.entityKind === "Type" && projectedType.kind === "Enum",
"Expected `TypeSpec.Visibility.Lifecycle` to be an Enum (projected)",
);

LIFECYCLE_ENUM_CACHE.set(program, projectedType);

return projectedType;
} else {
LIFECYCLE_ENUM_CACHE.set(program, type);

return type;
}
}

/**
Expand Down
95 changes: 95 additions & 0 deletions packages/compiler/test/visibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isVisible,
Model,
ModelProperty,
projectProgram,
removeVisibilityModifiers,
resetVisibilityModifiersForClass,
sealVisibilityModifiers,
Expand Down Expand Up @@ -1010,7 +1011,101 @@ describe("compiler: visibility core", () => {
strictEqual(idRead.type, idReadCreate.type);
});
});

describe("projection compatibility", () => {
it("allows @defaultVisibility to be used on enums that are projected", async () => {
const { Example, Bar } = (await runner.compile(`
@defaultVisibility(Example.A)
@test enum Example {
A,
B,
}
@test
model Bar {
@visibility(Example.B)
x: string;
}
#suppress "projections-are-experimental"
projection Bar#test {
to {
}
}
`)) as { Example: Enum; Bar: Model };

const projected = projectProgram(runner.program, [
{
projectionName: "test",
arguments: [],
},
]);

const visibility = getVisibilityForClass(runner.program, Bar.properties.get("x")!, Example);

strictEqual(visibility.size, 1);
ok(visibility.has(Example.members.get("B")!));

const ProjectedBar = projected.projector.projectType(Bar) as Model;
const ProjectedExample = projected.projector.projectType(Example) as Enum;

const projectedX = ProjectedBar.properties.get("x")!;

strictEqual(projectedX.decorators.length, 1);

const projectedVisibility = getVisibilityForClass(projected, projectedX, ProjectedExample);

strictEqual(projectedVisibility.size, 1);
ok(projectedVisibility.has(ProjectedExample.members.get("B")!));
});

it("correctly makes projected properties invisible", async () => {
const { Example } = (await runner.compile(`
@test
model Example {
@invisible(Lifecycle)
x: string;
}
#suppress "projections-are-experimental"
projection Example#test {
to {
}
}
`)) as { Example: Model };

const projected = projectProgram(runner.program, [
{
projectionName: "test",
arguments: [],
},
]);

const visibility = getVisibilityForClass(
runner.program,
Example.properties.get("x")!,
getLifecycleVisibilityEnum(runner.program),
);

strictEqual(visibility.size, 0);

const projectedExample = projected.projector.projectType(Example) as Model;

const projectedX = projectedExample.properties.get("x")!;

strictEqual(projectedX.decorators.length, 1);

const projectedVisibility = getVisibilityForClass(
projected,
projectedX,
getLifecycleVisibilityEnum(projected),
);

strictEqual(projectedVisibility.size, 0);
});
});
});

function validateCreateOrUpdateTransform(
props: {
c: ModelProperty | undefined;
Expand Down

0 comments on commit abd3664

Please sign in to comment.