-
Notifications
You must be signed in to change notification settings - Fork 229
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
feat(versioning): add code fixes for incompatible version errors #5459
Open
archerzz
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
archerzz:versioning/code-fix-incompatible-versions
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
7 changes: 7 additions & 0 deletions
7
.chronus/changes/versioning-code-fix-incompatible-versions-2024-11-27-17-5-24.md
This file contains 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/versioning" | ||
--- | ||
|
||
add code fixes for incompatible version errors |
This file contains 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 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,93 @@ | ||
import { | ||
getSourceLocation, | ||
getTypeName, | ||
type CodeFix, | ||
type Program, | ||
type SourceLocation, | ||
type Type, | ||
type TypeNameOptions, | ||
} from "@typespec/compiler"; | ||
import type { Version } from "./types.js"; | ||
import { getAllVersions } from "./versioning.js"; | ||
|
||
export function getVersionAdditionCodefixes( | ||
version: string | Version, | ||
type: Type, | ||
program: Program, | ||
typeOptions?: TypeNameOptions, | ||
): CodeFix[] | undefined { | ||
if (typeof version === "string") { | ||
return getVersionAdditionCodeFixFromString(version, type, program, typeOptions); | ||
} | ||
|
||
return getVersionAdditionCodeFixFromVersion(version, type, typeOptions); | ||
} | ||
|
||
function getVersionAdditionCodeFixFromVersion( | ||
version: Version, | ||
type: Type, | ||
typeOptions?: TypeNameOptions, | ||
): CodeFix[] | undefined { | ||
if (type.node === undefined) return undefined; | ||
|
||
const enumMember = version.enumMember; | ||
const decoratorDeclaration = `@added(${enumMember.enum.name}.${enumMember.name})`; | ||
return [ | ||
getDecorationAdditionCodeFix( | ||
"add-version-to-type", | ||
decoratorDeclaration, | ||
getTypeName(type, typeOptions), | ||
getSourceLocation(type.node), | ||
), | ||
]; | ||
} | ||
|
||
function getVersionAdditionCodeFixFromString( | ||
version: string, | ||
type: Type, | ||
program: Program, | ||
typeOptions?: TypeNameOptions, | ||
): CodeFix[] | undefined { | ||
const targetVersion = getAllVersions(program, type)?.find((v) => v.value === version); | ||
if (targetVersion === undefined) return undefined; | ||
|
||
return getVersionAdditionCodeFixFromVersion(targetVersion, type, typeOptions); | ||
} | ||
|
||
export function getVersionRemovalCodeFixes( | ||
version: string, | ||
type: Type, | ||
program: Program, | ||
typeOptions?: TypeNameOptions, | ||
): CodeFix[] | undefined { | ||
if (type.node === undefined) return undefined; | ||
|
||
const targetVersion = getAllVersions(program, type)?.find((v) => v.value === version); | ||
if (targetVersion === undefined) return; | ||
|
||
const enumMember = targetVersion.enumMember; | ||
const decoratorDeclaration = `@removed(${enumMember.enum.name}.${enumMember.name})`; | ||
return [ | ||
getDecorationAdditionCodeFix( | ||
"remove-version-from-type", | ||
decoratorDeclaration, | ||
getTypeName(type, typeOptions), | ||
getSourceLocation(type.node), | ||
), | ||
]; | ||
} | ||
|
||
function getDecorationAdditionCodeFix( | ||
id: string, | ||
decoratorDeclaration: string, | ||
typeName: string, | ||
location: SourceLocation, | ||
): CodeFix { | ||
return { | ||
id: id, | ||
label: `Add '${decoratorDeclaration}' to '${typeName}'`, | ||
fix: (context) => { | ||
return context.prependText(location, `${decoratorDeclaration}\n`); | ||
}, | ||
}; | ||
} |
This file contains 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 |
---|---|---|
|
@@ -22,12 +22,13 @@ import { | |
getReturnTypeChangedFrom, | ||
getTypeChangedFrom, | ||
getUseDependencies, | ||
getVersion, | ||
} from "./decorators.js"; | ||
import { reportDiagnostic } from "./lib.js"; | ||
import type { Version } from "./types.js"; | ||
import { getVersionAdditionCodefixes, getVersionRemovalCodeFixes } from "./validate.codefix.js"; | ||
import { | ||
Availability, | ||
getAllVersions, | ||
getAvailabilityMap, | ||
getVersionDependencies, | ||
getVersions, | ||
|
@@ -208,13 +209,6 @@ export function $onValidate(program: Program) { | |
validateVersionedNamespaceUsage(program, namespaceDependencies); | ||
} | ||
|
||
function getAllVersions(p: Program, t: Type): Version[] | undefined { | ||
const [namespace, _] = getVersions(p, t); | ||
if (namespace === undefined) return undefined; | ||
|
||
return getVersion(p, namespace)?.getVersions(); | ||
} | ||
|
||
/** | ||
* Ensures that properties whose type has changed with versioning are valid. | ||
*/ | ||
|
@@ -254,6 +248,7 @@ function validateTypeAvailability( | |
version: prettyVersion(version), | ||
}, | ||
target: source, | ||
codefixes: getVersionAdditionCodefixes(version, type, program, options), | ||
}); | ||
} | ||
|
||
|
@@ -665,9 +660,10 @@ function validateTargetVersionCompatible( | |
const [targetNamespace] = getVersions(program, targetAvailability.type); | ||
if (!targetAvailability.map || !targetNamespace) return; | ||
|
||
let versionMap: Map<Version, Version> | Version | undefined; | ||
if (sourceNamespace !== targetNamespace) { | ||
const dependencies = sourceNamespace && getVersionDependencies(program, sourceNamespace); | ||
const versionMap = dependencies?.get(targetNamespace); | ||
versionMap = dependencies?.get(targetNamespace); | ||
if (versionMap === undefined) return; | ||
|
||
targetAvailability.map = translateAvailability( | ||
|
@@ -697,6 +693,7 @@ function validateTargetVersionCompatible( | |
targetAvailability.map, | ||
sourceAvailability.type, | ||
targetAvailability.type, | ||
versionMap instanceof Map ? versionMap : undefined, | ||
); | ||
} | ||
} | ||
|
@@ -728,6 +725,7 @@ function translateAvailability( | |
targetAddedOn: addedAfter, | ||
}, | ||
target: source, | ||
codefixes: getVersionAdditionCodefixes(version, target, program), | ||
}); | ||
} | ||
if (removedBefore) { | ||
|
@@ -741,6 +739,7 @@ function translateAvailability( | |
targetAddedOn: removedBefore, | ||
}, | ||
target: source, | ||
codefixes: getVersionAdditionCodefixes(version, target, program), | ||
}); | ||
} | ||
} | ||
|
@@ -799,12 +798,16 @@ function validateAvailabilityForRef( | |
targetAvail: Map<string, Availability>, | ||
source: Type, | ||
target: Type, | ||
sourceOptions?: TypeNameOptions, | ||
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. those two parameters are never really passed in, so I delete them for simplicity |
||
targetOptions?: TypeNameOptions, | ||
versionMap?: Map<Version, Version>, | ||
) { | ||
// if source is unversioned and target is versioned | ||
if (sourceAvail === undefined) { | ||
if (!isAvailableInAllVersion(targetAvail)) { | ||
const firstAvailableVersion = Array.from(targetAvail.entries()) | ||
.filter(([_, val]) => val === Availability.Available || val === Availability.Added) | ||
.map(([key, _]) => key) | ||
.sort() | ||
.shift(); | ||
reportDiagnostic(program, { | ||
code: "incompatible-versioned-reference", | ||
messageId: "default", | ||
|
@@ -813,6 +816,9 @@ function validateAvailabilityForRef( | |
targetName: getTypeName(target), | ||
}, | ||
target: source, | ||
codefixes: firstAvailableVersion | ||
? getVersionAdditionCodefixes(firstAvailableVersion, source, program) | ||
: undefined, | ||
}); | ||
} | ||
return; | ||
|
@@ -840,6 +846,12 @@ function validateAvailabilityForRef( | |
[Availability.Removed, Availability.Unavailable].includes(targetVal) | ||
) { | ||
const targetAddedOn = findAvailabilityAfterVersion(key, Availability.Added, targetAvail); | ||
let targetVersion: Version | string = key; | ||
if (versionMap) { | ||
// the `key` here could have already been converted to source version string, thus we need to find the | ||
// original target version so that we can provide the correct codefix | ||
targetVersion = findMatchingTargetVersion(key, versionMap) ?? key; | ||
} | ||
|
||
reportDiagnostic(program, { | ||
code: "incompatible-versioned-reference", | ||
|
@@ -851,6 +863,7 @@ function validateAvailabilityForRef( | |
targetAddedOn: targetAddedOn!, | ||
}, | ||
target: source, | ||
codefixes: getVersionAdditionCodefixes(targetVersion, target, program), | ||
}); | ||
} | ||
if ( | ||
|
@@ -862,16 +875,23 @@ function validateAvailabilityForRef( | |
Availability.Removed, | ||
targetAvail, | ||
); | ||
|
||
let targetVersion: Version | string = key; | ||
if (versionMap) { | ||
targetVersion = findMatchingTargetVersion(key, versionMap) ?? key; | ||
} | ||
|
||
reportDiagnostic(program, { | ||
code: "incompatible-versioned-reference", | ||
messageId: "removedBefore", | ||
format: { | ||
sourceName: getTypeName(source, sourceOptions), | ||
targetName: getTypeName(target, targetOptions), | ||
sourceName: getTypeName(source), | ||
targetName: getTypeName(target), | ||
sourceRemovedOn: key, | ||
targetRemovedOn: targetRemovedOn!, | ||
}, | ||
target: source, | ||
codefixes: getVersionAdditionCodefixes(targetVersion, target, program), | ||
}); | ||
} | ||
} | ||
|
@@ -934,6 +954,7 @@ function validateAvailabilityForContains( | |
targetAddedOn: key, | ||
}, | ||
target: target, | ||
codefixes: getVersionAdditionCodefixes(key, source, program, targetOptions), | ||
}); | ||
} | ||
if ( | ||
|
@@ -952,6 +973,7 @@ function validateAvailabilityForContains( | |
targetRemovedOn: targetRemovedOn!, | ||
}, | ||
target: target, | ||
codefixes: getVersionRemovalCodeFixes(key, target, program, targetOptions), | ||
}); | ||
} | ||
} | ||
|
@@ -967,3 +989,15 @@ function isAvailableInAllVersion(avail: Map<string, Availability>): boolean { | |
function prettyVersion(version: Version | undefined): string { | ||
return version?.value ?? "<n/a>"; | ||
} | ||
|
||
function findMatchingTargetVersion( | ||
sourceVersion: string, | ||
versionMap: Map<Version, Version>, | ||
): Version | undefined { | ||
for (const [source, target] of versionMap.entries()) { | ||
if (source.value === sourceVersion) { | ||
return target; | ||
} | ||
} | ||
return undefined; | ||
} |
This file contains 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.
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.
it's already defined in
versioning.ts
:typespec/packages/versioning/src/versioning.ts
Line 184 in 2fd4d33