Skip to content

Commit

Permalink
Refactor to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder committed May 29, 2024
1 parent 49567ae commit 1722e1a
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions eng/tools/typespec-validation/src/rules/linter-ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { TsvHost } from "../tsv-host.js";
export class LinterRulesetRule implements Rule {
readonly name = "LinterRuleset";

readonly description =
"Ensures each spec includes the correct linter ruleset (data-plane or management-plane)";
readonly description = "Specs must select correct linter ruleset";

async execute(host: TsvHost, folder: string): Promise<RuleResult> {
let success = true;
Expand All @@ -24,9 +23,6 @@ export class LinterRulesetRule implements Rule {
config.options?.["@azure-tools/typespec-autorest"]?.["azure-resource-provider-folder"];
stdOutput += `azure-resource-provider-folder: ${JSON.stringify(rpFolder)}\n`;

const mainTspExists = await host.checkFileExists(join(folder, "main.tsp"));
const clientTspExists = await host.checkFileExists(join(folder, "client.tsp"));

const linterExtends = config.linter?.extends;
stdOutput += `linter.extends: ${JSON.stringify(linterExtends)}`;

Expand All @@ -35,20 +31,25 @@ export class LinterRulesetRule implements Rule {
requiredRuleset = "@azure-tools/typespec-azure-rulesets/resource-manager";
} else if (rpFolder?.trim()?.endsWith("data-plane")) {
requiredRuleset = "@azure-tools/typespec-azure-rulesets/data-plane";
} else if (clientTspExists && !mainTspExists) {
// Assume folders with no autorest setting, containing only "client.tsp" but no "main.tsp",
// are data-plane (e.g. HealthInsights.TrialMatcher)
requiredRuleset = "@azure-tools/typespec-azure-rulesets/data-plane";
} else {
// Cannot determine if spec is data-plane or resource-manager, so cannot know
// which linter ruleset is required.
success = false;
errorOutput +=
"tspconfig.yaml must define the following property:\n" +
"\n" +
"options:\n" +
' "@azure-tools/typespec-autorest":\n' +
' azure-resource-provider-folder: "data-plane" | "resource-manager"\n';
const clientTspExists = await host.checkFileExists(join(folder, "client.tsp"));
const mainTspExists = await host.checkFileExists(join(folder, "main.tsp"));

if (clientTspExists && !mainTspExists) {
// Assume folders with no autorest setting, containing only "client.tsp" but no "main.tsp",
// are data-plane (e.g. HealthInsights.TrialMatcher)
requiredRuleset = "@azure-tools/typespec-azure-rulesets/data-plane";
} else {
// Cannot determine if spec is data-plane or resource-manager, so cannot know
// which linter ruleset is required.
success = false;
errorOutput +=
"tspconfig.yaml must define the following property:\n" +
"\n" +
"options:\n" +
' "@azure-tools/typespec-autorest":\n' +
' azure-resource-provider-folder: "data-plane" | "resource-manager"\n';
}
}

if (requiredRuleset && !linterExtends?.includes(requiredRuleset)) {
Expand Down

0 comments on commit 1722e1a

Please sign in to comment.