Skip to content

Commit a89147a

Browse files
visibility: apply name template to inner mutations in Update transform (#8676)
This was an oversight. The PR corrects the construction of the mutators to pass the name template through the inner mutator and adds tests for regression. Closes #8653
1 parent 4a68b20 commit a89147a

File tree

3 files changed

+61
-46
lines changed

3 files changed

+61
-46
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/compiler"
5+
---
6+
7+
Addressed an issue where applying the `Update` visibility transform would not correctly rename nested models.

packages/compiler/src/lib/visibility.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ export const $withLifecycleUpdate: WithLifecycleUpdateDecorator = (
480480
any: new Set([lifecycle.members.get("Create")!, lifecycle.members.get("Update")!]),
481481
};
482482

483-
const createOrUpdateMutator = createVisibilityFilterMutator(lifecycleCreateOrUpdate);
483+
const createOrUpdateMutator = createVisibilityFilterMutator(lifecycleCreateOrUpdate, {
484+
nameTemplate,
485+
});
484486

485487
mutator = createVisibilityFilterMutator(lifecycleUpdate, {
486488
recur: createOrUpdateMutator,

packages/compiler/test/visibility.test.ts

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe("compiler: visibility core", () => {
204204
removeVisibilityModifiers(runner.program, x, [Create]);
205205
clearVisibilityModifiersForClass(runner.program, x, Lifecycle);
206206

207-
ok(runner.program.diagnostics.length === 3);
207+
strictEqual(runner.program.diagnostics.length, 3);
208208

209209
expectDiagnostics(runner.program.diagnostics, [
210210
{
@@ -737,31 +737,33 @@ describe("compiler: visibility core", () => {
737737
@invisible(Lifecycle)
738738
invisible: string;
739739
740-
nested: {
741-
@visibility(${Lifecycle.Read})
742-
r: string;
740+
nested: Nested;
741+
}
743742
744-
cru: string;
743+
model Nested {
744+
@visibility(${Lifecycle.Read})
745+
r: string;
745746
746-
@visibility(${Lifecycle.Create}, ${Lifecycle.Read})
747-
cr: string;
747+
cru: string;
748748
749-
@visibility(${Lifecycle.Create}, ${Lifecycle.Update})
750-
cu: string;
749+
@visibility(${Lifecycle.Create}, ${Lifecycle.Read})
750+
cr: string;
751751
752-
@visibility(${Lifecycle.Create})
753-
c: string;
752+
@visibility(${Lifecycle.Create}, ${Lifecycle.Update})
753+
cu: string;
754754
755-
@visibility(${Lifecycle.Update}, ${Lifecycle.Read})
756-
ru: string;
755+
@visibility(${Lifecycle.Create})
756+
c: string;
757757
758-
@visibility(${Lifecycle.Update})
759-
u: string;
758+
@visibility(${Lifecycle.Update}, ${Lifecycle.Read})
759+
ru: string;
760760
761-
@invisible(Lifecycle)
762-
invisible: string;
763-
};
764-
}
761+
@visibility(${Lifecycle.Update})
762+
u: string;
763+
764+
@invisible(Lifecycle)
765+
invisible: string;
766+
};
765767
766768
// This ensures the transforms are non-side-effecting.
767769
model ReadExample is Read<Example>;
@@ -888,7 +890,7 @@ describe("compiler: visibility core", () => {
888890

889891
const A = ref.type;
890892

891-
ok(A.kind === "Model");
893+
strictEqual(A.kind, "Model");
892894

893895
const a = A.properties.get("a");
894896
const b = A.properties.get("b");
@@ -1011,14 +1013,14 @@ describe("compiler: visibility core", () => {
10111013
const dataA = DataA.properties.get("data_a")!;
10121014
const dataB = DataB.properties.get("data_b")!;
10131015

1014-
ok(dataA.type.kind === "Model");
1015-
ok(dataB.type.kind === "Model");
1016+
strictEqual(dataA.type.kind, "Model");
1017+
strictEqual(dataB.type.kind, "Model");
10161018

10171019
const FooA = dataA.type as Model;
10181020
const FooB = dataB.type as Model;
10191021

1020-
ok(FooA.name === "FooA");
1021-
ok(FooB.name === "FooB");
1022+
strictEqual(FooA.name, "FooA");
1023+
strictEqual(FooB.name, "FooB");
10221024

10231025
ok(FooA.properties.has("foo_a"));
10241026
ok(!FooA.properties.has("foo_b"));
@@ -1070,8 +1072,8 @@ describe("compiler: visibility core", () => {
10701072
ok(a);
10711073
ok(b);
10721074

1073-
ok(a.type.kind === "Model");
1074-
ok(b.type.kind === "Model");
1075+
strictEqual(a.type.kind, "Model");
1076+
strictEqual(b.type.kind, "Model");
10751077

10761078
const A = a.type as Model;
10771079
const B = b.type as Model;
@@ -1091,20 +1093,20 @@ describe("compiler: visibility core", () => {
10911093
ok(aC);
10921094
ok(bC);
10931095

1094-
ok(aC.type === bC.type);
1096+
strictEqual(aC.type, bC.type);
10951097

10961098
let C = aC.type as Model;
10971099

1098-
ok(C.kind === "Model");
1099-
ok(C.name === "ReadC");
1100+
strictEqual(C.kind, "Model");
1101+
strictEqual(C.name, "ReadC");
11001102

11011103
ok(!C.properties.has("invisible"));
11021104
ok(C.properties.has("c"));
11031105

11041106
C = bC.type as Model;
11051107

1106-
ok(C.kind === "Model");
1107-
ok(C.name === "ReadC");
1108+
strictEqual(C.kind, "Model");
1109+
strictEqual(C.name, "ReadC");
11081110

11091111
ok(!C.properties.has("invisible"));
11101112
ok(C.properties.has("c"));
@@ -1135,7 +1137,7 @@ describe("compiler: visibility core", () => {
11351137

11361138
strictEqual(aB.kind, "Model");
11371139

1138-
ok(aB === B);
1140+
strictEqual(aB, B);
11391141
});
11401142

11411143
it("correctly transforms arrays and records", async () => {
@@ -1166,22 +1168,22 @@ describe("compiler: visibility core", () => {
11661168
const arrayType = array.type;
11671169
const recordType = record.type;
11681170

1169-
ok(arrayType.kind === "Model");
1170-
ok(recordType.kind === "Model");
1171+
strictEqual(arrayType.kind, "Model");
1172+
strictEqual(recordType.kind, "Model");
11711173

11721174
ok($(runner.program).array.is(arrayType));
11731175
ok($(runner.program).record.is(recordType));
11741176

11751177
const arrayA = (arrayType as Model).indexer!.value as Model;
11761178
const recordA = (recordType as Model).indexer!.value as Model;
11771179

1178-
ok(arrayA.kind === "Model");
1179-
ok(recordA.kind === "Model");
1180+
strictEqual(arrayA.kind, "Model");
1181+
strictEqual(recordA.kind, "Model");
11801182

1181-
ok(arrayA.name === "ATransform");
1182-
ok(recordA.name === "ATransform");
1183+
strictEqual(arrayA.name, "ATransform");
1184+
strictEqual(recordA.name, "ATransform");
11831185

1184-
ok(arrayA === recordA);
1186+
strictEqual(arrayA, recordA);
11851187

11861188
ok(arrayA.properties.has("a"));
11871189
ok(!arrayA.properties.has("invisible"));
@@ -1219,8 +1221,8 @@ describe("compiler: visibility core", () => {
12191221
const arrType = arr.type;
12201222
const recType = rec.type;
12211223

1222-
ok(arrType.kind === "Model");
1223-
ok(recType.kind === "Model");
1224+
strictEqual(arrType.kind, "Model");
1225+
strictEqual(recType.kind, "Model");
12241226

12251227
ok($(runner.program).array.is(arrType));
12261228
ok($(runner.program).record.is(recType));
@@ -1293,7 +1295,8 @@ function validateCreateOrUpdateTransform(
12931295
const nested = Result.properties.get("nested");
12941296

12951297
ok(nested);
1296-
ok(nested.type.kind === "Model");
1298+
strictEqual(nested.type.kind, "Model");
1299+
strictEqual(nested.type.name, "CreateOrUpdateNested");
12971300

12981301
const nestedProps = getProperties(nested.type);
12991302

@@ -1346,7 +1349,8 @@ function validateUpdateTransform(
13461349
const nested = Result.properties.get("nested");
13471350

13481351
ok(nested);
1349-
ok(nested.type.kind === "Model");
1352+
strictEqual(nested.type.kind, "Model");
1353+
strictEqual(nested.type.name, "UpdateNested");
13501354

13511355
// Nested properties work differently in Lifecycle Update transforms, requiring nested create-only properties to
13521356
// additionally be visible
@@ -1401,7 +1405,8 @@ function validateCreateTransform(
14011405
const nested = Result.properties.get("nested");
14021406

14031407
ok(nested);
1404-
ok(nested.type.kind === "Model");
1408+
strictEqual(nested.type.kind, "Model");
1409+
strictEqual(nested.type.name, "CreateNested");
14051410

14061411
const nestedProps = getProperties(nested.type);
14071412

@@ -1455,7 +1460,8 @@ function validateReadTransform(
14551460
const nested = Result.properties.get("nested");
14561461

14571462
ok(nested);
1458-
ok(nested.type.kind === "Model");
1463+
strictEqual(nested.type.kind, "Model");
1464+
strictEqual(nested.type.name, "ReadNested");
14591465

14601466
const nestedProps = getProperties(nested.type);
14611467

0 commit comments

Comments
 (0)