From b0884de93cd6f904843f47c55f03fd7a58779f8e Mon Sep 17 00:00:00 2001 From: tadelesh Date: Wed, 13 Mar 2024 11:48:55 +0800 Subject: [PATCH 1/4] prevent carry over for `@clientName` --- .../src/decorators.ts | 16 +++++ .../test/decorators.test.ts | 65 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/packages/typespec-client-generator-core/src/decorators.ts b/packages/typespec-client-generator-core/src/decorators.ts index 279975806f..f37292433e 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -1,5 +1,9 @@ import { + AugmentDecoratorStatementNode, + DecoratedType, + Decorator, DecoratorContext, + DecoratorExpressionNode, DecoratorFunction, EmitContext, Enum, @@ -16,6 +20,7 @@ import { Union, getNamespaceFullName, getProjectedName, + ignoreDiagnostics, isService, isTemplateDeclaration, isTemplateDeclarationOrInstance, @@ -818,6 +823,17 @@ export function $clientName( value: string, scope?: LanguageScopes ) { + 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); } diff --git a/packages/typespec-client-generator-core/test/decorators.test.ts b/packages/typespec-client-generator-core/test/decorators.test.ts index 592e0fe0eb..e31fa3b73f 100644 --- a/packages/typespec-client-generator-core/test/decorators.test.ts +++ b/packages/typespec-client-generator-core/test/decorators.test.ts @@ -13,6 +13,7 @@ import { beforeEach, describe, it } from "vitest"; import { getAccess, getClient, + getClientNameOverride, getOperationGroup, getUsage, listClients, @@ -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); + }); + }); }); From 51c00ead73113dca1cecbb15c63fbf123419f545 Mon Sep 17 00:00:00 2001 From: tadelesh Date: Wed, 13 Mar 2024 14:32:34 +0800 Subject: [PATCH 2/4] fix test and format --- .../src/decorators.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/typespec-client-generator-core/src/decorators.ts b/packages/typespec-client-generator-core/src/decorators.ts index f37292433e..159fa505c7 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -1,7 +1,5 @@ import { AugmentDecoratorStatementNode, - DecoratedType, - Decorator, DecoratorContext, DecoratorExpressionNode, DecoratorFunction, @@ -823,14 +821,22 @@ export function $clientName( value: string, scope?: LanguageScopes ) { - if ((context.decoratorTarget as Node).kind === SyntaxKind.AugmentDecoratorStatement) { - if (ignoreDiagnostics(context.program.checker.resolveTypeReference((context.decoratorTarget as AugmentDecoratorStatementNode).targetType)) !== entity) { - return; + if (entity.kind === "Model" || entity.kind === "Operation") { + 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; + if ((context.decoratorTarget as Node).kind === SyntaxKind.DecoratorExpression) { + if ((context.decoratorTarget as DecoratorExpressionNode).parent !== entity.node) { + return; + } } } From 96929acf1545e9711cde5d0892929558887ce43e Mon Sep 17 00:00:00 2001 From: tadelesh Date: Wed, 13 Mar 2024 14:34:44 +0800 Subject: [PATCH 3/4] changelog --- .chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md diff --git a/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md b/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md new file mode 100644 index 0000000000..32d6f4fa65 --- /dev/null +++ b/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +prevent carry over for @clientName \ No newline at end of file From 46026cf5d0374e37aefb3f4ff6123a1ff788795c Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 13 Mar 2024 11:49:57 -0400 Subject: [PATCH 4/4] implement tim's comments --- .chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md | 2 +- packages/typespec-client-generator-core/src/decorators.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md b/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md index 32d6f4fa65..aca50ecb89 100644 --- a/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md +++ b/.chronus/changes/prevent-carry-over-2024-2-13-14-34-30.md @@ -4,4 +4,4 @@ packages: - "@azure-tools/typespec-client-generator-core" --- -prevent carry over for @clientName \ No newline at end of file +prevent carry over for `@clientName` \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/decorators.ts b/packages/typespec-client-generator-core/src/decorators.ts index 159fa505c7..32a3c7f7bf 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -821,6 +821,8 @@ 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") { if ((context.decoratorTarget as Node).kind === SyntaxKind.AugmentDecoratorStatement) { if (