-
Notifications
You must be signed in to change notification settings - Fork 82
Add C# nameing convention linter rules - batch1 #4867
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
Open
haiyuazhang
wants to merge
5
commits into
Azure:main
Choose a base branch
from
haiyuazhang:haiyzhan/csharp-model-suffix-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
8 changes: 8 additions & 0 deletions
8
.chronus/changes/add-csharp-model-suffix-rules-2026-7-10-9-20-0.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,8 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@azure-tools/typespec-client-generator-core" | ||
| - "@azure-tools/typespec-azure-rulesets" | ||
| --- | ||
|
|
||
| Add `csharp-no-options-suffix`, `csharp-no-request-suffix`, `csharp-no-response-suffix`, and `csharp-use-standard-acronyms` linter rules for C# SDK model naming. |
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
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
75 changes: 75 additions & 0 deletions
75
packages/typespec-client-generator-core/src/rules/csharp-model-suffix-utils.ts
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,75 @@ | ||
| import { Model, createRule, getNamespaceFullName, paramMessage } from "@typespec/compiler"; | ||
| import { createTCGCContext } from "../context.js"; | ||
| import { getLibraryName } from "../public-utils.js"; | ||
| import { createClientTspAugmentDecoratorCodeFix } from "./codefix-helpers.js"; | ||
|
|
||
| export interface CSharpModelSuffixRuleOptions { | ||
| name: string; | ||
| badSuffix: string; | ||
| replacementSuffix: string; | ||
| description: string; | ||
| url: string; | ||
| shouldSkip?: (model: Model, csharpName: string) => boolean; | ||
| } | ||
|
|
||
| function getSuggestedName(name: string, badSuffix: string, replacementSuffix: string) { | ||
| return name.slice(0, -badSuffix.length) + replacementSuffix; | ||
| } | ||
|
|
||
| export function createCSharpModelSuffixRule(options: CSharpModelSuffixRuleOptions) { | ||
| return createRule({ | ||
| name: options.name, | ||
| description: options.description, | ||
| severity: "warning", | ||
| url: options.url, | ||
| messages: { | ||
| default: paramMessage`Model '${"modelName"}' ends with '${"badSuffix"}'. Use '${"replacementSuffix"}' suffix instead (e.g. '${"suggestion"}'). Use @clientName("${"suggestion"}", "csharp") to rename it for C#.`, | ||
| }, | ||
| create(context) { | ||
| const tcgcContext = createTCGCContext( | ||
| context.program, | ||
| "@azure-tools/typespec-client-generator-core", | ||
| { mutateNamespace: false }, | ||
| ); | ||
|
|
||
| return { | ||
| model: (model: Model) => { | ||
| if (model.node === undefined) return; | ||
|
|
||
| const csharpName = getLibraryName(tcgcContext, model, "csharp"); | ||
| if (!csharpName.endsWith(options.badSuffix)) return; | ||
| if (options.shouldSkip?.(model, csharpName)) return; | ||
|
|
||
| const suggestion = getSuggestedName( | ||
| csharpName, | ||
| options.badSuffix, | ||
| options.replacementSuffix, | ||
| ); | ||
| context.reportDiagnostic({ | ||
| format: { | ||
| modelName: csharpName, | ||
| badSuffix: options.badSuffix, | ||
| replacementSuffix: options.replacementSuffix, | ||
| suggestion, | ||
| }, | ||
| target: model, | ||
| codefixes: [ | ||
| createClientTspAugmentDecoratorCodeFix(model, "clientName", context.program, [ | ||
| `"${suggestion}"`, | ||
| `"csharp"`, | ||
| ]), | ||
| ], | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export function isStandardAzureCoreErrorResponse(model: Model, csharpName: string) { | ||
| if (csharpName !== "ErrorResponse") return false; | ||
| const namespace = model.namespace ? getNamespaceFullName(model.namespace) : ""; | ||
| return ( | ||
| namespace === "Azure.Core.Foundations" || namespace === "Azure.ResourceManager.CommonTypes" | ||
| ); | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/typespec-client-generator-core/src/rules/csharp-no-options-suffix.ts
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it not make more sense to have a single one for all the suffix, this would be more inline with other linting rules we have |
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,11 @@ | ||
| import { createCSharpModelSuffixRule } from "./csharp-model-suffix-utils.js"; | ||
|
|
||
| export const csharpNoOptionsSuffixRule = createCSharpModelSuffixRule({ | ||
| name: "csharp-no-options-suffix", | ||
| badSuffix: "Options", | ||
| replacementSuffix: "Config", | ||
| description: | ||
| "Model names ending with 'Options' should use 'Config' suffix instead for C# SDKs, except client options.", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/rules/csharp-no-options-suffix", | ||
| shouldSkip: (_model, csharpName) => csharpName.endsWith("ClientOptions"), | ||
| }); |
9 changes: 9 additions & 0 deletions
9
packages/typespec-client-generator-core/src/rules/csharp-no-request-suffix.ts
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,9 @@ | ||
| import { createCSharpModelSuffixRule } from "./csharp-model-suffix-utils.js"; | ||
|
|
||
| export const csharpNoRequestSuffixRule = createCSharpModelSuffixRule({ | ||
| name: "csharp-no-request-suffix", | ||
| badSuffix: "Request", | ||
| replacementSuffix: "Content", | ||
| description: "Model names ending with 'Request' should use 'Content' suffix instead for C# SDKs.", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/rules/csharp-no-request-suffix", | ||
| }); |
13 changes: 13 additions & 0 deletions
13
packages/typespec-client-generator-core/src/rules/csharp-no-response-suffix.ts
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,13 @@ | ||
| import { | ||
| createCSharpModelSuffixRule, | ||
| isStandardAzureCoreErrorResponse, | ||
| } from "./csharp-model-suffix-utils.js"; | ||
|
|
||
| export const csharpNoResponseSuffixRule = createCSharpModelSuffixRule({ | ||
| name: "csharp-no-response-suffix", | ||
| badSuffix: "Response", | ||
| replacementSuffix: "Result", | ||
| description: "Model names ending with 'Response' should use 'Result' suffix instead for C# SDKs.", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/rules/csharp-no-response-suffix", | ||
| shouldSkip: isStandardAzureCoreErrorResponse, | ||
| }); |
107 changes: 107 additions & 0 deletions
107
packages/typespec-client-generator-core/src/rules/csharp-use-standard-acronyms.ts
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,107 @@ | ||
| import { | ||
| Enum, | ||
| Model, | ||
| ModelProperty, | ||
| Program, | ||
| createRule, | ||
| paramMessage, | ||
| type LinterRuleContext, | ||
| } from "@typespec/compiler"; | ||
| import { createTCGCContext } from "../context.js"; | ||
| import { getLibraryName } from "../public-utils.js"; | ||
| import { createClientTspAugmentDecoratorCodeFix } from "./codefix-helpers.js"; | ||
|
|
||
| const acronymMap = new Map([ | ||
| ["db", "DB"], | ||
| ["ip", "IP"], | ||
| ["os", "OS"], | ||
| ]); | ||
|
|
||
| // Match a known acronym (IP/DB/OS) only when it is a *complete word* within the | ||
| // identifier, never a substring that merely starts a longer word. This is what | ||
| // keeps "Oslo", "Ipsum" and "Osmosis" from being mangled into "OSlo" etc. | ||
| // | ||
| // /(?:^ip|^db|^os|Ip|Db|Os)(?![a-z])/g | ||
| // ^ip | ^db | ^os → lowercase acronym at the start of the name (e.g. "ipAddress", "osProfile") | ||
| // Ip | Db | Os → capitalized acronym at a camelCase boundary (e.g. "PublicIpAddress", "CosmosDb") | ||
| // (?![a-z]) → must NOT be followed by a lowercase letter, i.e. the acronym ends a | ||
| // word (next char is uppercase or end-of-string): keeps "IpAddress"/"MacOs", | ||
| // but rejects "Oslo" (Os+"lo") and "Ipsum" (Ip+"sum") | ||
| // /g → fix every acronym occurrence in the identifier | ||
| const acronymRegex = /(?:^ip|^db|^os|Ip|Db|Os)(?![a-z])/g; | ||
|
|
||
| function getCorrectedName(name: string): string | undefined { | ||
| const corrected = name.replace( | ||
| acronymRegex, | ||
| (match) => acronymMap.get(match.toLowerCase()) ?? match, | ||
| ); | ||
| return corrected === name ? undefined : corrected; | ||
| } | ||
|
|
||
| function reportIfNeeded( | ||
| context: LinterRuleContext<any>, | ||
| program: Program, | ||
| target: Enum | Model | ModelProperty, | ||
| csharpName: string, | ||
| ) { | ||
| const suggestion = getCorrectedName(csharpName); | ||
| if (suggestion === undefined) return; | ||
|
|
||
| context.reportDiagnostic({ | ||
| format: { name: csharpName, suggestion }, | ||
| target, | ||
| codefixes: [ | ||
| createClientTspAugmentDecoratorCodeFix(target, "clientName", program, [ | ||
| `"${suggestion}"`, | ||
| `"csharp"`, | ||
| ]), | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| export const csharpUseStandardAcronymsRule = createRule({ | ||
| name: "csharp-use-standard-acronyms", | ||
| description: "C# SDK names should use standard acronym casing.", | ||
| severity: "warning", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/rules/csharp-use-standard-acronyms", | ||
| messages: { | ||
| default: paramMessage`Name '${"name"}' should use standard C# acronym casing (e.g. '${"suggestion"}'). Use @clientName("${"suggestion"}", "csharp") to rename it for C#.`, | ||
| }, | ||
| create(context) { | ||
| const tcgcContext = createTCGCContext( | ||
| context.program, | ||
| "@azure-tools/typespec-client-generator-core", | ||
| { mutateNamespace: false }, | ||
| ); | ||
|
|
||
| return { | ||
| enum: (enumType: Enum) => { | ||
| if (enumType.node === undefined) return; | ||
| reportIfNeeded( | ||
| context, | ||
| context.program, | ||
| enumType, | ||
| getLibraryName(tcgcContext, enumType, "csharp"), | ||
| ); | ||
| }, | ||
| model: (model: Model) => { | ||
| if (model.node === undefined) return; | ||
| reportIfNeeded( | ||
| context, | ||
| context.program, | ||
| model, | ||
| getLibraryName(tcgcContext, model, "csharp"), | ||
| ); | ||
| }, | ||
| modelProperty: (property: ModelProperty) => { | ||
| if (property.node === undefined) return; | ||
| reportIfNeeded( | ||
| context, | ||
| context.program, | ||
| property, | ||
| getLibraryName(tcgcContext, property, "csharp"), | ||
| ); | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should that just be
Type, augment can target every single decorable type