Skip to content

Commit

Permalink
[tcgc] add linter for empty client names (Azure#1066)
Browse files Browse the repository at this point in the history
fixes Azure#647

---------

Co-authored-by: iscai-msft <isabellavcai@gmail.com>
Co-authored-by: catalinaperalta <catalinaperaltah@hotmail.com>
  • Loading branch information
3 people authored and markcowl committed Jun 26, 2024
1 parent 7697abb commit 2271561
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/whitespace_client_name-2024-5-24-14-18-45.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Add linter for empty `@clientName` values
8 changes: 7 additions & 1 deletion packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
},
},
},
});

Expand Down
16 changes: 16 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 2271561

Please sign in to comment.