Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Azure/typespec-azure
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ab3bb72246f672306a37ce0fa79155f141b192cd
Choose a base ref
..
head repository: Azure/typespec-azure
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c97ea2de753287c1bf064f97ee29ac5b7248c41e
Choose a head ref
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
7 changes: 7 additions & 0 deletions .chronus/changes/enum_fixed-2024-2-21-15-54-49.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: breaking
packages:
- "@azure-tools/typespec-client-generator-core"
---

enums are always fixed after we switch to use union to represent extensible enum
2 changes: 1 addition & 1 deletion packages/typespec-azure-core/src/linter.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
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
@@ -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,
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
@@ -31,6 +31,7 @@ describe("typespec-azure-core: no-enum rule", () => {
},
]);
});

it("allows the version enum", async () => {
await tester
.expect(
@@ -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
9 changes: 2 additions & 7 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
UnionEnum,
getLroMetadata,
getUnionAsEnum,
isFixed,
} from "@azure-tools/typespec-azure-core";
import { UnionEnum, getLroMetadata, getUnionAsEnum } from "@azure-tools/typespec-azure-core";
import {
BooleanLiteral,
BytesKnownEncoding,
@@ -644,7 +639,7 @@ export function getSdkEnum(context: TCGCContext, type: Enum, operation?: Operati
details: docWrapper.details,
valueType: getSdkEnumValueType(context, type.members.values()),
values: [],
isFixed: isFixed(context.program, type),
isFixed: true, // enums are always fixed after we switch to use union to represent extensible enum
isFlags: false,
usage: UsageFlags.None, // We will add usage as we loop through the operations
access: undefined, // Dummy value until we update models map
6 changes: 3 additions & 3 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
@@ -805,7 +805,7 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(runner.context.experimental_sdkPackage.models.length, 1);
strictEqual(runner.context.experimental_sdkPackage.enums.length, 1);
const sdkType = runner.context.experimental_sdkPackage.enums[0];
strictEqual(sdkType.isFixed, false);
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "DaysOfWeekExtensibleEnum");
strictEqual(sdkType.valueType.kind, "string");
strictEqual(sdkType.usage & UsageFlags.Versioning, 0); // not a versioning enum
@@ -856,7 +856,7 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(runner.context.experimental_sdkPackage.models.length, 1);
strictEqual(runner.context.experimental_sdkPackage.enums.length, 1);
const sdkType = runner.context.experimental_sdkPackage.enums[0];
strictEqual(sdkType.isFixed, false);
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "Integers");
strictEqual(sdkType.valueType.kind, "int32");
const values = sdkType.values;
@@ -890,7 +890,7 @@ describe("typespec-client-generator-core: types", () => {

const sdkType = runner.context.experimental_sdkPackage.enums[0];
ok(sdkType);
strictEqual(sdkType.isFixed, false);
strictEqual(sdkType.isFixed, true);
strictEqual(sdkType.name, "Floats");
strictEqual(sdkType.valueType.kind, "float32");
const values = sdkType.values;