Skip to content

Commit

Permalink
Refactor logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Feb 28, 2024
1 parent e7b007c commit 889233c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
43 changes: 15 additions & 28 deletions packages/typespec-azure-resource-manager/src/rules/arm-no-record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Model, SemanticNodeListener, createRule } from "@typespec/compiler";
import { DiagnosticTarget, Model, SemanticNodeListener, createRule } from "@typespec/compiler";
import { getArmResources } from "../resource.js";

export const armNoRecordRule = createRule({
Expand All @@ -15,36 +15,23 @@ export const armNoRecordRule = createRule({
},
create(context): SemanticNodeListener {
return {
root: (program) => {
function isRecordType(model: Model): boolean {
return model.name === "Record";
}

function checkModel(model: Model) {
if (model.baseModel !== undefined && isRecordType(model.baseModel)) {
context.reportDiagnostic({
code: "arm-no-record",
target: model,
messageId: "extends",
});
}
if (model.sourceModel !== undefined && isRecordType(model.sourceModel)) {
root: (_) => {
function checkModel(model: Model, target: DiagnosticTarget, kind?: "extends" | "is") {
if (model.name === "Record") {
context.reportDiagnostic({
code: "arm-no-record",
target: model,
messageId: "is",
target: target,
messageId: kind || "default",
});
} else if (model.baseModel !== undefined) {
checkModel(model.baseModel, model, "extends");
} else if (model.sourceModel !== undefined) {
checkModel(model.sourceModel, model, "is");
}
for (const prop of model.properties.values()) {
if (prop.type.kind === "Model") {
if (isRecordType(prop.type)) {
context.reportDiagnostic({
code: "arm-no-record",
target: prop,
messageId: "default",
});
} else {
checkModel(prop.type);
if (model?.properties !== undefined) {
for (const prop of model.properties.values()) {
if (prop.type.kind === "Model") {
checkModel(prop.type, prop);
}
}
}
Expand All @@ -53,7 +40,7 @@ export const armNoRecordRule = createRule({
// ensure only ARM resources and models they touch are checked
const resources = getArmResources(context.program);
for (const resource of resources) {
checkModel(resource.typespecType);
checkModel(resource.typespecType, resource.typespecType);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,6 @@ it("emits a diagnostic if a deeply aliased model use Record type", async () => {
.toEmitDiagnostics({
code: "@azure-tools/typespec-azure-resource-manager/arm-no-record",
message:
"Model properties or operation parameters should not be of type Record. ARM requires Resource provider teams to define types explicitly.",
"Models should not equate to type Record. ARM requires Resource provider teams to define types explicitly.",
});
});

0 comments on commit 889233c

Please sign in to comment.