-
Notifications
You must be signed in to change notification settings - Fork 72
Add arm-no-replace-inherited-props linting rule #4384
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
Copilot
wants to merge
14
commits into
main
Choose a base branch
from
copilot/add-arm-no-replace-inherited-props-linting-rule
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
14 commits
Select commit
Hold shift + click to select a range
c723c31
Initial plan
Copilot 4bfcb9d
Add arm-no-replace-inherited-props linting rule
Copilot 49acb48
Merge remote-tracking branch 'origin/main' into copilot/add-arm-no-re…
Copilot 291b897
Merge branch 'main' into copilot/add-arm-no-replace-inherited-props-l…
markcowl 8b8208e
Merge remote-tracking branch 'origin/main' into copilot/add-arm-no-re…
Copilot 6d60f84
Include typespec-azure-rulesets in changeset
Copilot 24bb84f
Merge branch 'main' into copilot/add-arm-no-replace-inherited-props-l…
markcowl f7c009e
Merge remote-tracking branch 'origin/main' into copilot/add-arm-no-re…
Copilot 5180f69
Allow scalar-compatible inherited-property overrides; simplify rule
Copilot 342537c
Merge remote-tracking branch 'origin/main' into copilot/add-arm-no-re…
Copilot 826590c
Use getUnionAsEnum helper; add tests for scalar/union/enum variant ov…
Copilot bf16a95
Merge branch 'main' into copilot/add-arm-no-replace-inherited-props-l…
markcowl 59bc662
Merge remote-tracking branch 'origin/main' into copilot/add-arm-no-re…
Copilot 2215445
Allow same-type inherited property overrides (alias, is TrackedResource)
Copilot 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-arm-no-replace-inherited-props-rule-2026-5-6-2-25-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-azure-resource-manager" | ||
| - "@azure-tools/typespec-azure-rulesets" | ||
| --- | ||
|
|
||
| Add new linting rule `arm-no-replace-inherited-props` that warns when a model redefines a property that is already defined in one of its base models. The 'name' property of an ARM resource and properties redefined as part of a model marked with `@discriminator` are not flagged by this rule. |
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
135 changes: 135 additions & 0 deletions
135
packages/typespec-azure-resource-manager/src/rules/arm-no-replace-inherited-props.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,135 @@ | ||
| import { getUnionAsEnum } from "@azure-tools/typespec-azure-core"; | ||
| import { | ||
| createRule, | ||
| ignoreDiagnostics, | ||
| Model, | ||
| paramMessage, | ||
| Scalar, | ||
| Type, | ||
| } from "@typespec/compiler"; | ||
|
|
||
| type ScalarFamily = "string" | "numeric" | "boolean"; | ||
|
|
||
| /** | ||
| * Determine if `type` is "logically" a value of one of the well-known scalar | ||
| * families (string, numeric, boolean). Walks unions, enums, scalar-extends | ||
| * chains, union variants, and literals. | ||
| * | ||
| * Returns the standard scalar family name if `type` reduces to a single | ||
| * family, or `undefined` otherwise (e.g. model types, mixed unions, custom | ||
| * non-numeric/non-string scalars such as utcDateTime, etc.). | ||
| */ | ||
| function getScalarFamily(type: Type): ScalarFamily | undefined { | ||
| switch (type.kind) { | ||
| case "Scalar": { | ||
| // Walk up baseScalar chain to the root scalar. | ||
| let root: Scalar = type; | ||
| while (root.baseScalar !== undefined) { | ||
| root = root.baseScalar; | ||
| } | ||
| switch (root.name) { | ||
| case "string": | ||
| return "string"; | ||
| case "boolean": | ||
| return "boolean"; | ||
| case "numeric": | ||
| return "numeric"; | ||
| default: | ||
| return undefined; | ||
| } | ||
| } | ||
| case "String": | ||
| case "StringTemplate": | ||
| return "string"; | ||
| case "Number": | ||
| return "numeric"; | ||
| case "Boolean": | ||
| return "boolean"; | ||
| case "Enum": { | ||
| const families = new Set<ScalarFamily>(); | ||
| for (const member of type.members.values()) { | ||
| if (typeof member.value === "number") { | ||
| families.add("numeric"); | ||
| } else { | ||
| // Members with string values or no explicit value are string-typed. | ||
| families.add("string"); | ||
| } | ||
| } | ||
| return families.size === 1 ? [...families][0] : undefined; | ||
| } | ||
| case "EnumMember": | ||
| return typeof type.value === "number" ? "numeric" : "string"; | ||
| case "UnionVariant": | ||
| return getScalarFamily(type.type); | ||
| case "Union": { | ||
| // Use the azure-core helper to classify the union as a string- or | ||
| // number-typed open/closed enum. | ||
| const unionEnum = ignoreDiagnostics(getUnionAsEnum(type)); | ||
| if (unionEnum === undefined) return undefined; | ||
| return unionEnum.kind === "string" ? "string" : "numeric"; | ||
| } | ||
| default: | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| export const armNoReplaceInheritedPropsRule = createRule({ | ||
| name: "arm-no-replace-inherited-props", | ||
| severity: "warning", | ||
| description: "Disallow redefining properties already defined in a base type.", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/arm-no-replace-inherited-props", | ||
| messages: { | ||
| default: paramMessage`The property '${"propertyName"}' is also defined in the base model. Redefining inherited properties can cause problems with OpenAPI tooling and some language representations of the models.`, | ||
| }, | ||
| create(context) { | ||
| return { | ||
|
markcowl marked this conversation as resolved.
|
||
| model: (model: Model) => { | ||
| if (model.baseModel === undefined) return; | ||
|
|
||
| for (const property of model.properties.values()) { | ||
| // Walk the chain of base models looking for a property with the | ||
| // same name. | ||
| let inherited: typeof property | undefined; | ||
| let current: Model | undefined = model.baseModel; | ||
| while (current !== undefined) { | ||
| const found = current.properties.get(property.name); | ||
| if (found !== undefined) { | ||
| inherited = found; | ||
| break; | ||
| } | ||
| current = current.baseModel; | ||
| } | ||
|
|
||
| if (inherited === undefined) continue; | ||
|
|
||
| // Allow overriding with the exact same type (identity). Aliases in | ||
| // TypeSpec resolve to the same Type instance, and template | ||
| // instantiations are cached, so this naturally handles cases such | ||
| // as redefining `systemData: SystemData` (alias) where the parent | ||
| // uses `systemData: Foundations.SystemData`, or `tags: Record<string>` | ||
| // cloned via `model X is TrackedResource<...>`. | ||
| if (property.type === inherited.type) continue; | ||
|
|
||
| // Allow overriding when both the inherited property and the override | ||
| // resolve to the same scalar family (e.g. both are "string" | ||
| // -- including string scalars, string-derived scalars, string | ||
| // literals, string enums, and open or closed string unions). | ||
| const overrideFamily = getScalarFamily(property.type); | ||
| const inheritedFamily = getScalarFamily(inherited.type); | ||
| if ( | ||
| overrideFamily !== undefined && | ||
| inheritedFamily !== undefined && | ||
| overrideFamily === inheritedFamily | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| context.reportDiagnostic({ | ||
| format: { propertyName: property.name }, | ||
| target: property, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
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.