Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ export type InternalConnectionPropertiesUnion = InternalConnectionPropertiesApiK

// @public
export enum KnownVersions {
_20240701Preview = "2024-07-01-preview"
Comment thread
MaryGao marked this conversation as resolved.
Versions20240701Preview = "2024-07-01-preview"
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,5 +747,5 @@ export function evaluationScheduleArrayDeserializer(result: Array<EvaluationSche
/** Azure AI API versions */
export enum KnownVersions {
/** Azure AI API version 2024-07-01-preview. */
_20240701Preview = "2024-07-01-preview",
Versions20240701Preview = "2024-07-01-preview",
}
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ export interface JobStatistics {

// @public
export enum KnownVersions {
_20230501170 = "2023-05-01.17.0"
Versions20230501170 = "2023-05-01.17.0"
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5715,7 +5715,7 @@ export function nodeVMExtensionArrayDeserializer(result: Array<NodeVMExtension>)
/** The Azure Batch service version. */
export enum KnownVersions {
/** API Version 2023-05-01.17.0 */
_20230501170 = "2023-05-01.17.0",
Versions20230501170 = "2023-05-01.17.0",
}

export type GetNodeFileResponse = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface FooOperationsOperations {

// @public
export enum KnownVersions {
_20220830 = "2022-08-30"
Versions20220830 = "2022-08-30"
}

// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
/** The Contoso Widget Manager service version. */
export enum KnownVersions {
/** Version 2022-08-31 */
_20220830 = "2022-08-30",
Versions20220830 = "2022-08-30",
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type ContinuablePage<TElement, TPage = TElement[]> = TPage & {

// @public
export enum KnownVersions {
_100 = "1.0.0"
Versions100 = "1.0.0"
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ export function nonReferencedModelDeserializer(item: any): NonReferencedModel {
/** The Contoso Widget Manager service version. */
export enum KnownVersions {
/** Version 2022-08-31 */
_100 = "1.0.0",
Versions100 = "1.0.0",
}
2 changes: 1 addition & 1 deletion packages/typespec-ts/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ const libDef = {
"prefix-adding-in-enum-member": {
severity: "warning",
messages: {
default: paramMessage`Enum member name ${"memberName"} is normalized to ${"normalizedName"} with "_" prefix.`
default: paramMessage`Enum member name ${"memberName"} is not a valid TypeScript identifier. It has been renamed to ${"normalizedName"} using the enum type name ${"enumTypeName"} as prefix.`
}
},
"default-value-object": {
Expand Down
36 changes: 24 additions & 12 deletions packages/typespec-ts/src/modular/emitModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,12 @@ export function buildEnumTypes(
name: `Known${normalizeModelName(context, type)}`,
isExported: true,
members: type.values.map((value) =>
emitEnumMember(context, value, reportMemberNameDiagnostic)
emitEnumMember(
context,
value,
normalizeName(type.name, NameType.Interface, true),
reportMemberNameDiagnostic
)
)
};

Expand Down Expand Up @@ -653,24 +658,31 @@ function getExtensibleEnumDescription(
function emitEnumMember(
context: SdkContext,
member: SdkEnumValueType,
enumTypeName?: string,
Comment thread
JialinHuang803 marked this conversation as resolved.
Outdated
reportMemberNameDiagnostic = false // if reportMemberNameDiagnostic is true, it will report diagnostic for enum member name
): EnumMemberStructure {
const normalizedMemberName = context.rlcOptions?.ignoreEnumMemberNameNormalize
let normalizedMemberName = context.rlcOptions?.ignoreEnumMemberNameNormalize
? fixLeadingNumber(member.name, NameType.EnumMemberName) // need to fix the leading number also for enum member
: normalizeName(member.name, NameType.EnumMemberName, true);
// If the member name starts with _ due to a leading digit (not because the original has _),
// replace the _ prefix with the enum type name for a more descriptive identifier
if (
reportMemberNameDiagnostic &&
normalizedMemberName.toLowerCase().startsWith("_") &&
!member.name.toLowerCase().startsWith("_")
!member.name.toLowerCase().startsWith("_") &&
enumTypeName
) {
reportDiagnostic(context.program, {
code: "prefix-adding-in-enum-member",
format: {
memberName: member.name,
normalizedName: normalizedMemberName
},
target: NoTarget
});
normalizedMemberName = enumTypeName + normalizedMemberName.slice(1);
if (reportMemberNameDiagnostic) {
reportDiagnostic(context.program, {
code: "prefix-adding-in-enum-member",
format: {
memberName: member.name,
normalizedName: normalizedMemberName,
enumTypeName
},
target: NoTarget
});
}
}
const memberStructure: EnumMemberStructure = {
kind: StructureKind.EnumMember,
Expand Down
13 changes: 9 additions & 4 deletions packages/typespec-ts/test/modularUnit/diagnostics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,28 @@ describe("Diagnostic reporting tests", () => {
}
});

it("number enum and not ignore warnings", async () => {
it("should rename numeric enum member using enum type name prefix and report a warning", async () => {
try {
await emitModularModelsFromTypeSpec(
`
union ExtensibleNum {
One: 1,
\`2\`: 2,
}
model Test {
color: 1 | 2;
num: ExtensibleNum;
}
op read(@body body: Test): void;
`,
{
mustEmptyDiagnostic: true
mustEmptyDiagnostic: true,
"experimental-extensible-enums": true
}
);
} catch (e: any) {
assert.strictEqual(
e[0].message,
'Enum member name 1 is normalized to _1 with "_" prefix.'
"Enum member name 2 is not a valid TypeScript identifier. It has been renamed to ExtensibleNum2 using the enum type name ExtensibleNum as prefix."
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ export enum KnownVersions {
/** 2024-07-01-preview */
PreviewVersion = "2024-07-01-preview",
/** 2024-07-01 */
_20240701 = "2024-07-01",
Versions20240701 = "2024-07-01",
/** 2024-08-01-preview */
_20240801Preview = "2024-08-01-preview",
Versions20240801Preview = "2024-08-01-preview",
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ export enum KnownExtensibleString {
/** _10Pascal */
_10Pascal = "_10Pascal",
/** 090 */
_090 = "090",
ExtensibleString090 = "090",
/** 10 */
_10 = "10",
ExtensibleString10 = "10",
/** 20 */
_20 = "20",
ExtensibleString20 = "20",
/** 1.0 */
_10 = "1.0",
Comment thread
MaryGao marked this conversation as resolved.
ExtensibleString10 = "1.0",
/** -2.0 */
"Item-1.0" = "-2.0",
}
Expand All @@ -150,11 +150,11 @@ export enum KnownExtensibleNumber {
/** 1 */
One = 1,
/** 2 */
_2 = 2,
ExtensibleNumber2 = 2,
/** -2.1 */
_21 = -2.1,
ExtensibleNumber21 = -2.1,
/** 3 */
_3 = 3,
ExtensibleNumber3 = 3,
}

/** Type of ExtensibleNumber */
Expand All @@ -167,6 +167,6 @@ export enum KnownVersions {
/** 2024-07-01 */
StableVersion = "2024-07-01",
/** 2024-08-01-preview */
_20240801Preview = "2024-08-01-preview",
Versions20240801Preview = "2024-08-01-preview",
}
```
Loading