Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Apr 19, 2024
1 parent 84c1ca9 commit 59aadf1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
25 changes: 23 additions & 2 deletions packages/typespec-azure-core/src/rules/require-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Model,
Operation,
Program,
Union,
createRule,
getDiscriminatedTypes,
getDoc,
Expand Down Expand Up @@ -33,8 +34,25 @@ function isExcludedEnumType(program: Program, enumObj: Enum): boolean {
return true;
}
} else if (type.kind === "Union") {
// TODO: handle union types
let test = "best";
const discriminatorVariant = type.variants.get(discName.propertyName);
if (discriminatorVariant?.type === enumObj) {
return true;
}
}
}
return false;
}

/** Discriminator unions are self-documenting and don't need separate documentation. */
function isExcludedUnionType(program: Program, union: Union): boolean {
const discTypes = getDiscriminatedTypes(program);
for (const [type, discName] of discTypes) {
// FIXME: Detect when union is a discriminator
if (type.kind === "Union") {
const discriminatorVariant = type.variants.get(discName.propertyName);
if (discriminatorVariant?.type === union) {
return true;
}
}
}
return false;
Expand Down Expand Up @@ -119,6 +137,9 @@ export const requireDocumentation = createRule({
) {
return;
}
if (prop.type.kind === "Union" && isExcludedUnionType(context.program, prop.type)) {
return;
}
if (!getDoc(context.program, prop)) {
context.reportDiagnostic({
target: prop,
Expand Down
16 changes: 11 additions & 5 deletions packages/typespec-azure-core/test/rules/require-docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,20 @@ describe("typespec-azure-core: documentation-required rule", () => {
await tester
.expect(
`
union PetKind {
cat: "cat",
string,
}
@discriminator("kind")
union Pet {
cat: Cat,
@doc("Base Pet model")
model Pet {
kind: PetKind;
}
@doc("A Cat!")
model Cat {
kind: "cat";
@doc("A Merry Ol' Cat")
model Cat extends Pet {
kind: PetKind.cat,
}`
)
.toBeValid();
Expand Down

0 comments on commit 59aadf1

Please sign in to comment.