Skip to content

Commit

Permalink
General projected name fix (#1719)
Browse files Browse the repository at this point in the history
* Allow projectedNames to work with other projections

* Updating changelogs
  • Loading branch information
markcowl authored Mar 20, 2023
1 parent 83d8f39 commit c2e1247
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Allow projectedNames helpers to work with previous projections",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
10 changes: 7 additions & 3 deletions packages/compiler/core/helpers/projected-names-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export function createProjectedNameProgram(program: Program, target: string): Pr
};

function getProjectedName(type: Type & { name: string }): string {
const baseType = findTypeInProjector(projectedProgram.projector.parentProjector, type);
const baseType = findTypeInProjector(projectedProgram.projector, type);
const projectedType = projectedProgram.projector.projectedTypes.get(baseType);
if (projectedType === undefined || !("name" in projectedType)) {
return baseType.name;
if (
projectedType === undefined ||
!("name" in projectedType) ||
projectedType.name === baseType.name
) {
return type.name;
}
return projectedType.name as string;
}
Expand Down
31 changes: 31 additions & 0 deletions packages/compiler/test/projected-names.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,35 @@ describe("compiler: projected-names", () => {
strictEqual(expireAtProjected?.name, "actualNameAtThisVersion");
strictEqual(jsonView.getProjectedName(expireAtProjected), "actualNameAtThisVersion");
});

it("projectedName overrides a previous renaming", async () => {
const { expireAt, renamedProperty } = (await runner.compile(`
model Foo {
@projectedName("json", "jsonExpireAt") @test expireAt: int32;
@projectedName("json", "jsonRenamedProperty") @test renamedProperty: string;
}
#suppress "projections-are-experimental"
projection Foo#v {
to {
self::renameProperty("expireAt", "actualNameAtThisVersion");
}
}
`)) as { expireAt: ModelProperty; renamedProperty: ModelProperty };
const updatedProgram = projectProgram(runner.program, [
{
projectionName: "v",
arguments: [],
},
]);
const jsonView = createProjectedNameProgram(updatedProgram, "json");
const expireAtProjected = updatedProgram.projector.projectedTypes.get(expireAt);
const renamedProjected = updatedProgram.projector.projectedTypes.get(renamedProperty);
strictEqual(expireAtProjected?.kind, "ModelProperty" as const);
strictEqual(expireAtProjected?.name, "actualNameAtThisVersion");
strictEqual(renamedProjected?.kind, "ModelProperty" as const);
strictEqual(renamedProjected?.name, "renamedProperty");
strictEqual(jsonView.getProjectedName(expireAtProjected), "jsonExpireAt");
strictEqual(jsonView.getProjectedName(renamedProjected), "jsonRenamedProperty");
});
});

0 comments on commit c2e1247

Please sign in to comment.