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

[TypeSpecValidation/LinterRuleset] Fail if deprecated rulesets detected #29270

Merged
merged 3 commits into from
May 30, 2024
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
33 changes: 32 additions & 1 deletion eng/tools/typespec-validation/src/rules/linter-ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { Rule } from "../rule.js";
import { RuleResult } from "../rule-result.js";
import { TsvHost } from "../tsv-host.js";

// Maps deprecated rulesets to the replacement rulesets
const deprecatedRulesets = new Map<string, string>([
["@azure-tools/typespec-azure-core/all", "@azure-tools/typespec-azure-rulesets/data-plane"],
[
"@azure-tools/typespec-azure-resource-manager/all",
"@azure-tools/typespec-azure-rulesets/resource-manager",
],
]);

export class LinterRulesetRule implements Rule {
readonly name = "LinterRuleset";

Expand Down Expand Up @@ -33,7 +42,7 @@ export class LinterRulesetRule implements Rule {
}
stdOutput += `files: ${JSON.stringify(files)}\n`;

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

let requiredRuleset = "";
Expand All @@ -57,6 +66,28 @@ export class LinterRulesetRule implements Rule {
' azure-resource-provider-folder: "data-plane" | "resource-manager"\n';
}

if (linterExtends) {
for (const ruleset of linterExtends) {
if (deprecatedRulesets.has(ruleset)) {
const newRuleset = deprecatedRulesets.get(ruleset);

success = false;
errorOutput +=
"tspconfig.yaml references the following ruleset which is deprecated:\n" +
"\n" +
"linter:\n" +
" extends:\n" +
` - "${ruleset}"\n` +
"\n" +
"It should be replaced with the following:\n" +
"\n" +
"linter:\n" +
" extends:\n" +
` - "${newRuleset}"`;
}
}
}

if (requiredRuleset && !linterExtends?.includes(requiredRuleset)) {
success = false;
errorOutput +=
Expand Down
31 changes: 31 additions & 0 deletions eng/tools/typespec-validation/test/linter-ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,35 @@ linter:
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder);
assert(!result.success);
});

it("fails with data-plane/old-and-new", async function () {
const host = new TsvTestHost();
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "data-plane"
linter:
extends:
- "@azure-tools/typespec-azure-core/all"
- "@azure-tools/typespec-azure-rulesets/data-plane"
`;
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder);
assert(!result.success);
});

it("fails with resource-manager/old-and-new", async function () {
const host = new TsvTestHost();
host.readTspConfig = async (_folder: string) => `
options:
"@azure-tools/typespec-autorest":
azure-resource-provider-folder: "resource-manager"
linter:
extends:
- "@azure-tools/typespec-azure-resource-manager/all"
- "@azure-tools/typespec-azure-rulesets/resource-manager"
`;
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder);

assert(!result.success);
});
});