Skip to content

Commit

Permalink
Azure Core: Enable no-enum rule by default in all ruleset (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Mar 22, 2024
1 parent 2223654 commit 8046715
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/enable-no-enum-rule-2024-2-21-16-42-36.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@azure-tools/typespec-azure-core"
---

Enable `no-enum` rule by default in `all` ruleset
2 changes: 1 addition & 1 deletion packages/typespec-azure-core/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const $linter = defineLinter({
true,
[`@azure-tools/typespec-azure-core/${useStandardNames.name}`]: true,
[`@azure-tools/typespec-azure-core/${friendlyNameRule.name}`]: true,
[`@azure-tools/typespec-azure-core/${noEnumRule.name}`]: false,
[`@azure-tools/typespec-azure-core/${noEnumRule.name}`]: true,
},
extends: ["@typespec/http/all"],
},
Expand Down
4 changes: 3 additions & 1 deletion packages/typespec-azure-core/src/rules/no-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export const noEnumRule = createRule({
create(context) {
return {
enum: (en: Enum) => {
if (getVersionsForEnum(context.program, en).length > 0) {
const [_, versions] = getVersionsForEnum(context.program, en);
if (versions !== undefined && versions.getVersions()[0].enumMember.enum === en) {
return;
}

context.reportDiagnostic({
format: { enumName: en.name },
target: en,
Expand Down
22 changes: 22 additions & 0 deletions packages/typespec-azure-core/test/rules/no-enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("typespec-azure-core: no-enum rule", () => {
},
]);
});

it("allows the version enum", async () => {
await tester
.expect(
Expand All @@ -46,6 +47,27 @@ describe("typespec-azure-core: no-enum rule", () => {
.toBeValid();
});

it("emit warning about other enums in versioned service", async () => {
await tester
.expect(
`
@service
@versioned(Versions)
namespace Foo;
enum Versions {
v1, v2
}
enum Bar { a, b}
`
)
.toEmitDiagnostics([
{
code: "@azure-tools/typespec-azure-core/no-enum",
},
]);
});

describe("codefix", () => {
it("codefix simple enum", async () => {
await tester
Expand Down

0 comments on commit 8046715

Please sign in to comment.