Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enum member references to emitter framework #2137

Merged
merged 3 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Emitter Framework: add support for emitting enum member references.",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/json-schema",
"comment": "Add support for enum member references.",
"type": "none"
}
],
"packageName": "@typespec/json-schema"
}
2 changes: 2 additions & 0 deletions packages/compiler/src/emitter-framework/asset-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export function createAssetEmitter<T, TOptions extends object>(
emitTypeReference(target): EmitEntity<T> {
if (target.kind === "ModelProperty") {
return invokeTypeEmitter("modelPropertyReference", target);
} else if (target.kind === "EnumMember") {
return invokeTypeEmitter("enumMemberReference", target);
}

incomingReferenceContext = context.referenceContext ?? null;
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler/src/emitter-framework/type-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ export class TypeEmitter<T, TOptions extends object = Record<string, never>> {
return this.emitter.emitTypeReference(property.type);
}

/**
* Emit an enum member reference (e.g. as created by the `SomeEnum.member` syntax
* in TypeSpec). By default, this will emit nothing.
*
* @param property the enum member
*/
enumMemberReference(member: EnumMember): EmitterOutput<T> {
return this.emitter.result.none();
}

arrayDeclaration(array: Model, name: string, elementType: Type): EmitterOutput<T> {
this.emitter.emitType(array.indexer!.value);
return this.emitter.result.none();
Expand Down
19 changes: 18 additions & 1 deletion packages/compiler/test/emitter-framework/emitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ union UnionDecl {
x: int32;
y: string;
}

enum MyEnum {
a: "hi";
b: "bye";
Expand Down Expand Up @@ -275,6 +276,22 @@ describe("emitter-framework: typescript emitter", () => {
assert.match(contents, /x: \[string, number\]/);
});

it("emits enum member references", async () => {
const contents = await emitTypeSpecToTs(`
enum MyEnum {
a: "hi";
b: "bye";
}

model EnumReference {
prop: MyEnum.a;
prop2: MyEnum.b;
}
`);
assert.match(contents, /prop: MyEnum.a/);
assert.match(contents, /prop2: MyEnum.b/);
});

it("emits scalars", async () => {
class TestEmitter extends CodeTypeEmitter {
scalarDeclaration(scalar: Scalar, name: string): EmitterOutput<string> {
Expand Down Expand Up @@ -381,7 +398,7 @@ describe("emitter-framework: typescript emitter", () => {
"UnionDecl.ts",
"MyInterface.ts",
].forEach((file) => {
assert(files.has(file));
assert(files.has(file), `emits ${file}`);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export class TypeScriptInterfaceEmitter extends CodeTypeEmitter {
`;
}

enumMemberReference(member: EnumMember): EmitterOutput<string> {
return `${this.emitter.emitDeclarationName(member.enum)}.${member.name}`;
}

unionDeclaration(union: Union, name: string): EmitterOutput<string> {
return this.emitter.result.declaration(
name,
Expand Down
13 changes: 13 additions & 0 deletions packages/json-schema/src/json-schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BooleanLiteral,
emitFile,
Enum,
EnumMember,
getDeprecated,
getDirectoryPath,
getDoc,
Expand Down Expand Up @@ -195,6 +196,18 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
return this.#createDeclaration(en, name, withConstraints);
}

enumMemberReference(member: EnumMember): EmitterOutput<Record<string, any>> {
// would like to dispatch to the same `literal` codepaths but enum members aren't literal types
switch (typeof member.value) {
case "undefined":
return { type: "string", const: member.name };
case "string":
return { type: "string", const: member.value };
case "number":
return { type: "number", const: member.value };
}
}

unionDeclaration(union: Union, name: string): EmitterOutput<object> {
const withConstraints = new ObjectBuilder({
$schema: "https://json-schema.org/draft/2020-12/schema",
Expand Down
20 changes: 20 additions & 0 deletions packages/json-schema/test/enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,24 @@ describe("emitting enums", () => {
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo["x-foo"], "foo");
});

it("handles enum member refs", async () => {
const schemas = await emitSchema(`
enum Foo {
a: "hi";
b: 2;
c;
}

model Bar {
a: Foo.a;
b: Foo.b;
c: Foo.c;
}
`);
const Bar = schemas["Bar.json"];
assert.deepStrictEqual(Bar.properties.a, { type: "string", const: "hi" });
assert.deepStrictEqual(Bar.properties.b, { type: "number", const: 2 });
assert.deepStrictEqual(Bar.properties.c, { type: "string", const: "c" });
});
});