-
Notifications
You must be signed in to change notification settings - Fork 376
feat(tspd): auto-generate linter rule and diagnostic reference pages #11221
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14d52c9
feat(tspd): auto-generate linter rule and diagnostic reference pages
87bfff5
Merge branch 'main' into tadelesh/auto-gen-rule-diagnostic-docs
tadelesh 3db903d
refactor(tspd): use docs field with FileRef for reference docs
6fcb0e3
feat(compiler): auto-generate reference doc url for diagnostics and r…
d6180ef
feat(compiler): show diagnostic and rule docs on hover in the languag…
597378b
Merge remote-tracking branch 'origin/main' into tadelesh/auto-gen-rul…
69c49cd
fix(compiler): harden diagnostic documentation links and hover
92d0f17
chore: split reference-doc url auto-generation and language server ho…
5d447dd
refactor(tspd): resolve rule/diagnostic docs with the async file API …
f614bf9
Merge branch 'main' into tadelesh/auto-gen-rule-diagnostic-docs
tadelesh 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
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,7 @@ | ||
| --- | ||
| changeKind: internal | ||
| packages: | ||
| - "@typespec/openapi3" | ||
| --- | ||
|
|
||
| Provide extended documentation for several diagnostics (`path-query`, `duplicate-header`, `inline-cycle`, `invalid-schema`, `invalid-server-variable`, `union-null`) via co-located markdown files. |
19 changes: 19 additions & 0 deletions
19
.chronus/changes/rule-diagnostic-docs-compiler-2026-6-9.md
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,19 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/compiler" | ||
| --- | ||
|
|
||
| Add a `docs` field to linter rule and diagnostic definitions to provide extended reference documentation. The value can be an inline markdown string or a `FileRef` created with `fileRef.fromPackageRoot("src/rules/my-rule.md")`, which is read lazily by tooling so it stays safe to bundle for the browser. | ||
|
|
||
| ```ts | ||
| export const myRule = createRule({ | ||
| name: "my-rule", | ||
| severity: "warning", | ||
| description: "Short description.", | ||
| docs: fileRef.fromPackageRoot("src/rules/my-rule.md"), | ||
| messages: { | ||
| /* ... */ | ||
| }, | ||
| }); | ||
| ``` |
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,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/tspd" | ||
| --- | ||
|
|
||
| `tspd doc` now generates a documentation page per linter rule (`reference/rules/<name>.md`) and per diagnostic (`reference/diagnostics/<code>.md`), sourced from the `docs` field on the rule and diagnostic definitions. A `documentation-missing` warning is reported for any linter rule or diagnostic that does not provide documentation. |
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,7 @@ | ||
| --- | ||
| changeKind: internal | ||
| packages: | ||
| - "@typespec/http" | ||
| --- | ||
|
|
||
| Provide extended documentation for the `op-reference-container-route` linter rule via a co-located markdown file. |
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,31 @@ | ||
| /** | ||
| * A lazy reference to a file on disk. Unlike embedding the file content directly, a | ||
| * `FileRef` only describes *where* the content lives; it is resolved (read) by tooling | ||
| * such as `tspd` when needed. Because it does not read the file at definition time, it is | ||
| * safe to include in code that is bundled for the browser (e.g. the playground). | ||
| */ | ||
| export interface FileRef { | ||
| readonly kind: "file-ref"; | ||
| /** Path to the file, relative to the package root. */ | ||
| readonly path: string; | ||
| } | ||
|
|
||
| export const fileRef = { | ||
| /** | ||
| * Create a {@link FileRef} pointing to a file relative to the package root (the directory | ||
| * containing the library's `package.json`). | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * docs: fileRef.fromPackageRoot("src/rules/my-rule.md"), | ||
| * ``` | ||
| */ | ||
| fromPackageRoot(path: string): FileRef { | ||
| return { kind: "file-ref", path }; | ||
| }, | ||
| }; | ||
|
|
||
| /** Type guard for {@link FileRef}. */ | ||
| export function isFileRef(value: unknown): value is FileRef { | ||
| return typeof value === "object" && value !== null && (value as FileRef).kind === "file-ref"; | ||
| } |
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,38 @@ | ||
| When referencing an operation with `op is`, only the data on the operation itself is carried over; anything on the parent container is lost. | ||
| This results in unexpected behavior where information is lost. | ||
| As a best practice the route should be provided on the operation itself. | ||
|
|
||
| #### ❌ Incorrect | ||
|
|
||
| ```tsp | ||
| namespace Library { | ||
| @route("/pets") | ||
| interface Pets { | ||
| @route("/read") read(): string; | ||
| } | ||
| } | ||
|
|
||
| @service | ||
| namespace Service { | ||
| interface PetStore { | ||
| readPet is Library.Pets.read; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### ✅ Correct | ||
|
|
||
| ```tsp | ||
| namespace Library { | ||
| interface Pets { | ||
| @route("/pets/read") read(): string; | ||
| } | ||
| } | ||
|
|
||
| @service | ||
| namespace Service { | ||
| interface PetStore { | ||
| readPet is Library.Pets.read; | ||
| } | ||
| } | ||
| ``` |
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,22 @@ | ||
| This diagnostic is issued when a response header is defined more than once for a response of a specific status code. | ||
|
|
||
| To fix this issue, ensure that each response header is defined only once for each status code. | ||
|
|
||
| ### Example | ||
|
|
||
| ```yaml | ||
| responses: | ||
| "200": | ||
| description: Successful response | ||
| headers: | ||
| X-Rate-Limit: | ||
| description: The number of allowed requests in the current period | ||
| schema: | ||
| type: integer | ||
| X-Rate-Limit: | ||
| description: The number of allowed requests in the current period | ||
| schema: | ||
| type: integer | ||
| ``` | ||
|
|
||
| In this example, the `X-Rate-Limit` header is defined twice for the `200` status code. To fix this issue, remove the duplicate header definition. |
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,19 @@ | ||
| This diagnostic is issued when a cyclic reference is detected within inline schemas. | ||
|
|
||
| To fix this issue, refactor the schemas to remove the cyclic reference. | ||
|
|
||
| ### Example | ||
|
|
||
| ```yaml | ||
| components: | ||
| schemas: | ||
| Node: | ||
| type: object | ||
| properties: | ||
| value: | ||
| type: string | ||
| next: | ||
| $ref: "#/components/schemas/Node" | ||
| ``` | ||
|
|
||
| In this example, the `Node` schema references itself, creating a cyclic reference. To fix this issue, refactor the schema to remove the cyclic reference. |
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,20 @@ | ||
| This diagnostic is issued when a schema is invalid according to the OpenAPI v3 specification. | ||
|
|
||
| To fix this issue, review your TypeSpec definitions to ensure they map to valid OpenAPI schemas. | ||
|
|
||
| ### Example | ||
|
|
||
| ```yaml | ||
| components: | ||
| schemas: | ||
| User: | ||
| type: object | ||
| properties: | ||
| id: | ||
| type: string | ||
| age: | ||
| type: integer | ||
| format: "int" # Invalid format | ||
| ``` | ||
|
|
||
| In this example, the `format` value for the `age` property is invalid. To fix this issue, provide a valid format value such as `int32` or `int64`. |
14 changes: 14 additions & 0 deletions
14
packages/openapi3/src/diagnostics/invalid-server-variable.md
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,14 @@ | ||
| This diagnostic is issued when a variable in the `@server` decorator is not defined as a string type. | ||
| Since server variables are substituted into the server URL which is a string, all variables must have string values. | ||
|
|
||
| To fix this issue, make sure all server variables are of a type that is assignable to `string`. | ||
|
|
||
| ### Example | ||
|
|
||
| ```typespec | ||
| @server("{protocol}://{host}/api/{version}", "Custom endpoint", { | ||
| protocol: "http" | "https", | ||
| host: string, | ||
| version: 1, // Should be a string: "1" | ||
| }) | ||
| ``` |
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,26 @@ | ||
| This diagnostic is issued when the OpenAPI emitter finds an `@route` decorator that specifies a path that contains a query parameter. This is not permitted by the OpenAPI v3 specification, which requires query parameters to be defined separately. | ||
|
|
||
| To fix this issue, redesign the API to only use paths without query parameters, and define query parameters using the `@query` decorator. | ||
|
|
||
| ### Example | ||
|
|
||
| Instead of: | ||
|
|
||
| ```typespec | ||
| @route("/users?filter={filter}") | ||
| op getUsers(filter: string): User[]; | ||
| ``` | ||
|
|
||
| Use: | ||
|
|
||
| ```typespec | ||
| @route("/users") | ||
| op getUsers(@query filter?: string): User[]; | ||
| ``` | ||
|
|
||
| Alternatively, you can leverage TypeSpec's support for URI templates: | ||
|
|
||
| ```typespec | ||
| @route("/users{?filter}") | ||
| op getUsers(filter?: string): User[]; | ||
| ``` |
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,3 @@ | ||
| This diagnostic is issued when the result of model composition is effectively a `null` schema which cannot be represented in OpenAPI. | ||
|
|
||
| To fix this issue, review your model compositions to ensure they produce valid schemas with actual properties or types. |
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.