Skip to content

Add rule options#10352

Open
timotheeguerin wants to merge 3 commits intomicrosoft:mainfrom
timotheeguerin:linter-rule-options
Open

Add rule options#10352
timotheeguerin wants to merge 3 commits intomicrosoft:mainfrom
timotheeguerin:linter-rule-options

Conversation

@timotheeguerin
Copy link
Copy Markdown
Member

@timotheeguerin timotheeguerin commented Apr 13, 2026

fix #10222

Add ability for linter rule to define options

Example

const myRule = createRule({
  name: "no-model-with-name",
  severity: "warning",
  description: "Bans models with a specific name",
  messages: { default: "This model name is not allowed" },
  defaultOptions: { bannedName: "Foo" },
  create(context) {
    return {
      model: (target) => {
        if (target.name === context.options.bannedName) {
          context.reportDiagnostic({ target });
        }
      },
    };
  },
});

Configuring options in tspconfig.yaml:

linter:
  enable:
    # Enable with default options
    "@typespec/my-lib/no-model-with-name": true
    # Enable with custom options
    "@typespec/my-lib/no-model-with-name":
      bannedName: "Bar"

@microsoft-github-policy-service microsoft-github-policy-service bot added the compiler:core Issues for @typespec/compiler label Apr 13, 2026
@microsoft-github-policy-service microsoft-github-policy-service bot added the meta:website TypeSpec.io updates label Apr 13, 2026
@azure-sdk
Copy link
Copy Markdown
Collaborator

azure-sdk commented Apr 13, 2026

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 13, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/compiler@10352

commit: 7efb666

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

All changed packages have been documented.

  • @typespec/compiler
Show changes

@typespec/compiler - feature ✏️

Add support for configurable options on linter rules,> ,> Linter rules can now define typed options with defaults using defaultOptions, and users can pass options when enabling rules in tspconfig.yaml or rulesets.,> ,> Defining a rule with options:,> ,> ts,> const myRule = createRule({,> name: "no-model-with-name",,> severity: "warning",,> description: "Bans models with a specific name",,> messages: { default: "This model name is not allowed" },,> defaultOptions: { bannedName: "Foo" },,> create(context) {,> return {,> model: (target) => {,> if (target.name === context.options.bannedName) {,> context.reportDiagnostic({ target });,> },> },,> };,> },,> });,> ,> ,> Configuring options in tspconfig.yaml:,> ,> yaml,> linter:,> enable:,> # Enable with default options,> "@typespec/my-lib/no-model-with-name": true,> # Enable with custom options,> "@typespec/my-lib/no-model-with-name":,> bannedName: "Bar",>

@timotheeguerin timotheeguerin marked this pull request as ready for review April 13, 2026 18:21
Copy link
Copy Markdown
Member

@iscai-msft iscai-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler meta:website TypeSpec.io updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[compiler] allow rules to accept parameter inputs

3 participants