-
Notifications
You must be signed in to change notification settings - Fork 390
Support defining a linter ruleset in a yaml file #11851
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
Merged
Timothee Guerin (timotheeguerin)
merged 8 commits into
microsoft:main
from
timotheeguerin:linter-ruleset-file
Sep 6, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0119ad9
feat(compiler): allow defining a linter ruleset in a yaml file
timotheeguerin 6fbaf3f
Fix test description for path resolution
timotheeguerin 280019e
test: assert ruleset file diagnostics point at the ruleset file path
timotheeguerin 5fb71eb
Merge branch 'main' into linter-ruleset-file
timotheeguerin 7eddd8c
fix(compiler): locate ruleset file diagnostics on the extends entry t…
timotheeguerin 4b64f80
Merge remote-tracking branch 'upstream/main' into linter-ruleset-file
timotheeguerin d224403
test: cover re-enabling a rule that an extended ruleset file turned off
timotheeguerin b168c0f
fix(compiler): disallow file: ruleset refs in libraries and locate co…
timotheeguerin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Add support for defining a linter ruleset in a standalone yaml file and referencing it with the `file:` prefix in `linter.extends`. This lets a repository share and version a set of linter rules without depending on a library release. | ||
|
|
||
| ```yaml | ||
| # tspconfig.yaml | ||
| linter: | ||
| extends: | ||
| - "file:../common-rules.yaml" | ||
| ``` | ||
|
|
||
| ```yaml | ||
| # common-rules.yaml | ||
| extends: | ||
| - "@typespec/best-practices/recommended" | ||
| enable: | ||
| "@typespec/best-practices/new-rule": true | ||
| disable: | ||
| "@typespec/best-practices/foo": "This rule is too strict for this repository" | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { doIO } from "../utils/io.js"; | ||
| import { parseYaml } from "../yaml/parser.js"; | ||
| import type { YamlScript } from "../yaml/types.js"; | ||
| import { createJSONSchemaValidator } from "./schema-validator.js"; | ||
| import type { | ||
| Diagnostic, | ||
| DiagnosticTarget, | ||
| JSONSchemaType, | ||
| LinterRuleSet, | ||
| SystemHost, | ||
| } from "./types.js"; | ||
| import { NoTarget } from "./types.js"; | ||
|
|
||
| /** Prefix used in `extends` to reference a ruleset defined in a yaml file. */ | ||
| export const linterRuleSetFilePrefix = "file:"; | ||
|
|
||
| export const LinterRuleSetFileJsonSchema: JSONSchemaType<LinterRuleSet> = { | ||
| type: "object", | ||
| additionalProperties: false, | ||
| required: [], | ||
| properties: { | ||
| extends: { | ||
| type: "array", | ||
| nullable: true, | ||
| items: { type: "string" }, | ||
| }, | ||
| enable: { | ||
| type: "object", | ||
| required: [], | ||
| nullable: true, | ||
| additionalProperties: { | ||
| oneOf: [{ type: "boolean" }, { type: "object" }], | ||
| }, | ||
| }, | ||
| disable: { | ||
| type: "object", | ||
| required: [], | ||
| nullable: true, | ||
| additionalProperties: { type: "string" }, | ||
| }, | ||
| }, | ||
| } as any; // ajv type system doesn't like the string templates | ||
|
|
||
| const ruleSetFileValidator = createJSONSchemaValidator(LinterRuleSetFileJsonSchema); | ||
|
|
||
| /** A linter ruleset loaded from a yaml file. */ | ||
| export interface LoadedLinterRuleSetFile { | ||
| readonly ruleSet: LinterRuleSet; | ||
| /** Parsed yaml, used to locate diagnostics reported for entries of this ruleset. */ | ||
| readonly script: YamlScript; | ||
| } | ||
|
|
||
| /** | ||
| * Load a linter ruleset defined in a yaml file. | ||
| * @param host Host used to read the file. | ||
| * @param path Absolute path to the yaml file. | ||
| * @param target Target to report the file loading errors on. Typically the reference that led to this file. | ||
| */ | ||
| export async function loadLinterRuleSetFile( | ||
| host: SystemHost, | ||
| path: string, | ||
| target: DiagnosticTarget | typeof NoTarget = NoTarget, | ||
| ): Promise<[LoadedLinterRuleSetFile | undefined, readonly Diagnostic[]]> { | ||
| const diagnostics: Diagnostic[] = []; | ||
| const reportDiagnostic = (d: Diagnostic) => diagnostics.push(d); | ||
| const file = await doIO(host.readFile, path, reportDiagnostic, { diagnosticTarget: target }); | ||
| if (file === undefined) { | ||
| return [undefined, diagnostics]; | ||
| } | ||
|
|
||
| const [script, yamlDiagnostics] = parseYaml(file); | ||
| diagnostics.push(...yamlDiagnostics); | ||
| if (yamlDiagnostics.some((d) => d.severity === "error")) { | ||
| return [undefined, diagnostics]; | ||
| } | ||
|
|
||
| // An empty yaml file is a valid, empty, ruleset. | ||
| const data = script.value ?? {}; | ||
| const validationDiagnostics = ruleSetFileValidator.validate(data, script); | ||
| diagnostics.push(...validationDiagnostics); | ||
| if (validationDiagnostics.some((d) => d.severity === "error")) { | ||
| return [undefined, diagnostics]; | ||
| } | ||
|
|
||
| return [{ ruleSet: data as LinterRuleSet, script }, diagnostics]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.