diff --git a/.chronus/changes/whitespace_client_name-2024-5-24-14-18-45.md b/.chronus/changes/whitespace_client_name-2024-5-24-14-18-45.md new file mode 100644 index 0000000000..5746fc36b6 --- /dev/null +++ b/.chronus/changes/whitespace_client_name-2024-5-24-14-18-45.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +Add linter for empty `@clientName` values \ 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 d3786b8a0d..3ceff1c721 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -977,7 +977,13 @@ export function $clientName( } } } - + if (value.trim() === "") { + reportDiagnostic(context.program, { + code: "empty-client-name", + format: {}, + target: entity, + }); + } setScopedDecoratorData(context, $clientName, clientNameKey, entity, value, scope); } diff --git a/packages/typespec-client-generator-core/src/lib.ts b/packages/typespec-client-generator-core/src/lib.ts index 4976ee4f3b..fbca5ba653 100644 --- a/packages/typespec-client-generator-core/src/lib.ts +++ b/packages/typespec-client-generator-core/src/lib.ts @@ -147,6 +147,12 @@ export const $lib = createTypeSpecLibrary({ default: paramMessage`Can not find name for your emitter, please check your emitter name.`, }, }, + "empty-client-name": { + severity: "warning", + messages: { + default: `Cannot pass an empty value to the @clientName decorator`, + }, + }, }, }); diff --git a/packages/typespec-client-generator-core/test/decorators.test.ts b/packages/typespec-client-generator-core/test/decorators.test.ts index a0e2bbfa18..2e86e17e00 100644 --- a/packages/typespec-client-generator-core/test/decorators.test.ts +++ b/packages/typespec-client-generator-core/test/decorators.test.ts @@ -2684,6 +2684,22 @@ describe("typespec-client-generator-core: decorators", () => { "body" ); }); + it("empty client name", async () => { + const diagnostics = await runner.diagnose(` + @service({}) + namespace MyService; + + @clientName(" ") + model Test { + id: string; + prop: string; + } + `); + + expectDiagnostics(diagnostics, { + code: "@azure-tools/typespec-client-generator-core/empty-client-name", + }); + }); }); describe("versioning projection", () => {