Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent carry over for @clientName #412

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

prevent carry over for `@clientName`
24 changes: 24 additions & 0 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
AugmentDecoratorStatementNode,
DecoratorContext,
DecoratorExpressionNode,
DecoratorFunction,
EmitContext,
Enum,
Expand All @@ -16,6 +18,7 @@ import {
Union,
getNamespaceFullName,
getProjectedName,
ignoreDiagnostics,
isService,
isTemplateDeclaration,
isTemplateDeclarationOrInstance,
Expand Down Expand Up @@ -818,6 +821,27 @@ export function $clientName(
value: string,
scope?: LanguageScopes
) {
// workaround for current lack of functionality in compiler
// https://github.com/microsoft/typespec/issues/2717
if (entity.kind === "Model" || entity.kind === "Operation") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a reference to this issue microsoft/typespec#2717

that this is a workaround for the lack of functionality in the compiler.

if ((context.decoratorTarget as Node).kind === SyntaxKind.AugmentDecoratorStatement) {
if (
ignoreDiagnostics(
context.program.checker.resolveTypeReference(
(context.decoratorTarget as AugmentDecoratorStatementNode).targetType
)
) !== entity
) {
return;
}
}
if ((context.decoratorTarget as Node).kind === SyntaxKind.DecoratorExpression) {
if ((context.decoratorTarget as DecoratorExpressionNode).parent !== entity.node) {
return;
}
}
}

setScopedDecoratorData(context, $clientName, clientNameKey, entity, value, scope);
}

Expand Down
65 changes: 65 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { beforeEach, describe, it } from "vitest";
import {
getAccess,
getClient,
getClientNameOverride,
getOperationGroup,
getUsage,
listClients,
Expand Down Expand Up @@ -2350,4 +2351,68 @@ describe("typespec-client-generator-core: decorators", () => {
});
});
});

describe("@clientName", () => {
it("carry over", async () => {
const { Test1, Test2, func1, func2 } = (await runner.compile(`
@service({})
@test namespace MyService {
@test
@clientName("Test1Rename")
model Test1{}

@test
model Test2 is Test1{}

@test
@route("/func1")
@clientName("func1Rename")
op func1(): void;

@test
@route("/func2")
op func2 is func1;
}
`)) as { Test1: Model; Test2: Model; func1: Operation; func2: Operation };

strictEqual(getClientNameOverride(runner.context, Test1), "Test1Rename");
strictEqual(getClientNameOverride(runner.context, Test2), undefined);
strictEqual(getClientNameOverride(runner.context, func1), "func1Rename");
strictEqual(getClientNameOverride(runner.context, func2), undefined);
});

it("augment carry over", async () => {
const { Test1, Test2, func1, func2 } = (await runner.compileWithCustomization(
`
@service({})
@test namespace MyService {
@test
model Test1{}

@test
model Test2 is Test1{}

@test
@route("/func1")
op func1(): void;

@test
@route("/func2")
op func2 is func1;
}
`,
`
namespace Customizations;

@@clientName(MyService.Test1, "Test1Rename");
@@clientName(MyService.func1, "func1Rename");
`
)) as { Test1: Model; Test2: Model; func1: Operation; func2: Operation };

strictEqual(getClientNameOverride(runner.context, Test1), "Test1Rename");
strictEqual(getClientNameOverride(runner.context, Test2), undefined);
strictEqual(getClientNameOverride(runner.context, func1), "func1Rename");
strictEqual(getClientNameOverride(runner.context, func2), undefined);
});
});
});
Loading