-
Notifications
You must be signed in to change notification settings - Fork 308
Introduce @experimental decorator to tag client-side features #8086
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
joheredi
wants to merge
13
commits into
microsoft:main
Choose a base branch
from
joheredi:joheredi/feature-lifecycle
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
13 commits
Select commit
Hold shift + click to select a range
db375e1
add decorator and TK
joheredi a5c68f1
Introduce featureLifecycle decorator and tests
joheredi 5f058d3
remove commented code
joheredi 87ec832
Add change info
joheredi aadeb52
use unknown for Type
joheredi ae3a286
Update dependencies
joheredi 9ca3130
Use @experimental decorator
joheredi 6c40601
update codeowners
joheredi 3e28410
Address review comments
joheredi 5061a17
Use Tester instead of testHost and runner
joheredi 9bd1ea0
Merge remote-tracking branch 'upstream/main' into joheredi/feature-li…
joheredi dc1434b
Increase timeout
joheredi 773d79f
refert ef changes
joheredi 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/joheredi-feature-lifecycle-2025-7-1-14-16-12.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,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@typespec/http-client" | ||
--- | ||
|
||
Introduces @experimental decorator for annotating generated code as experimental. |
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
15 changes: 15 additions & 0 deletions
15
packages/http-client/generated-defs/TypeSpec.HttpClient.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,15 @@ | ||
import type { DecoratorContext, Type } from "@typespec/compiler"; | ||
|
||
export interface FeatureLifecycleOptions { | ||
readonly emitterScope?: string; | ||
} | ||
|
||
export type ExperimentalDecorator = ( | ||
context: DecoratorContext, | ||
target: Type, | ||
options?: FeatureLifecycleOptions, | ||
) => void; | ||
|
||
export type TypeSpecHttpClientDecorators = { | ||
experimental: ExperimentalDecorator; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/http-client/generated-defs/TypeSpec.HttpClient.ts-test.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,10 @@ | ||
// An error in the imports would mean that the decorator is not exported or | ||
// doesn't have the right name. | ||
|
||
import { $decorators } from "@typespec/http-client"; | ||
import type { TypeSpecHttpClientDecorators } from "./TypeSpec.HttpClient.js"; | ||
|
||
/** | ||
* An error here would mean that the exported decorator is not using the same signature. Make sure to have export const $decName: DecNameDecorator = (...) => ... | ||
*/ | ||
const _: TypeSpecHttpClientDecorators = $decorators["TypeSpec.HttpClient"]; |
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 @@ | ||
namespace TypeSpec.HttpClient; | ||
|
||
/** | ||
* Specifies the target emitters that the decorator should apply. If not set, the decorator will be applied to all emitters by default. | ||
* You can use ”!” to exclude specific emitters, for example: `!@typespec/http-client-js, !@typespec/http-client-csharp` | ||
*/ | ||
model ClientDecoratorOptions { | ||
emitterScope?: string; | ||
} |
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,10 @@ | ||
import "./common.tsp"; | ||
import "../dist/src/tsp-index.js"; | ||
|
||
namespace TypeSpec.HttpClient; | ||
|
||
model FeatureLifecycleOptions { | ||
...ClientDecoratorOptions; | ||
} | ||
|
||
extern dec experimental(target: unknown, options?: valueof FeatureLifecycleOptions); |
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,4 @@ | ||
import "./decorators.tsp"; | ||
import "../dist/src/tsp-index.js"; | ||
|
||
namespace TypeSpec.HttpClient; |
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,84 @@ | ||
import { createDiagnosticCollector, DiagnosticResult, Program, Type } from "@typespec/compiler"; | ||
import { useStateMap } from "@typespec/compiler/utils"; | ||
import { ExperimentalDecorator } from "../../generated-defs/TypeSpec.HttpClient.js"; | ||
import { createStateSymbol } from "../lib.js"; | ||
import { parseScopeFilter, ScopedValue } from "./scope-cache.js"; | ||
|
||
const featureLifecycleStateSymbol = createStateSymbol("featureLifecycleState"); | ||
|
||
export type FeatureLifecycleStage = "Experimental"; | ||
|
||
const [getFeatureLifecycleState, setFeatureLifecycleState] = useStateMap< | ||
Type, | ||
ScopedValue<FeatureLifecycleStage> | ||
>(featureLifecycleStateSymbol); | ||
|
||
export const $experimental: ExperimentalDecorator = (context, target, options) => { | ||
const scopeFilter = parseScopeFilter(options?.emitterScope); | ||
joheredi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (scopeFilter.excludedEmitters.length > 0 && scopeFilter.includedEmitters.length > 0) { | ||
context.program.reportDiagnostic({ | ||
code: "include-and-exclude-scopes", | ||
message: "The @experimental should only either include or exclude scopes, not both.", | ||
severity: "error", | ||
target, | ||
}); | ||
} | ||
setFeatureLifecycleState(context.program, target, { | ||
emitterFilter: scopeFilter, | ||
value: "Experimental", | ||
joheredi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}; | ||
|
||
export interface GetFeatureLifecycleOptions { | ||
emitterName?: string; | ||
} | ||
export function getClientFeatureLifecycle( | ||
program: Program, | ||
target: Type, | ||
options: GetFeatureLifecycleOptions = {}, | ||
): DiagnosticResult<FeatureLifecycleStage | undefined> { | ||
const diagnostics = createDiagnosticCollector(); | ||
|
||
const lifecycle = getFeatureLifecycleState(program, target); | ||
|
||
if (!lifecycle) { | ||
return diagnostics.wrap(undefined); | ||
} | ||
|
||
const emitterScope = options.emitterName; | ||
const lifecycleValue = lifecycle.value; | ||
|
||
if (!lifecycle.emitterFilter.isScoped) { | ||
return diagnostics.wrap(lifecycleValue); | ||
} | ||
|
||
// Lifecycle is scoped but no emitter scope is provided to the query function so we return undefined | ||
// this is because we can't determine which emitter the lifecycle is associated with. | ||
if (!emitterScope) { | ||
diagnostics.add({ | ||
code: "use-client-context-without-provider", | ||
message: "No emitter scope provided to getClientFeatureLifecycle.", | ||
target, | ||
severity: "warning", | ||
}); | ||
return diagnostics.wrap(undefined); | ||
} | ||
|
||
if (lifecycle.emitterFilter.includedEmitters.length) { | ||
const value = lifecycle.emitterFilter.includedEmitters.includes(emitterScope) | ||
? lifecycleValue | ||
: undefined; | ||
|
||
return diagnostics.wrap(value); | ||
} | ||
|
||
if (lifecycle.emitterFilter.excludedEmitters.length) { | ||
const value = lifecycle.emitterFilter.excludedEmitters.includes(emitterScope) | ||
? undefined | ||
: lifecycleValue; | ||
|
||
return diagnostics.wrap(value); | ||
} | ||
|
||
return diagnostics.wrap(undefined); | ||
} |
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 @@ | ||
export * from "./feature-lifecycle.js"; |
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,39 @@ | ||
export interface EmitterFilter { | ||
includedEmitters: string[]; | ||
excludedEmitters: string[]; | ||
isScoped: boolean; | ||
} | ||
|
||
export interface ScopedValue<T> { | ||
emitterFilter: EmitterFilter; | ||
value: T; | ||
} | ||
|
||
export function parseScopeFilter(string: string | undefined): EmitterFilter { | ||
if (!string) { | ||
return { | ||
excludedEmitters: [], | ||
includedEmitters: [], | ||
isScoped: false, | ||
}; | ||
} | ||
|
||
const parts = string.split(","); | ||
const includedEmitters: string[] = []; | ||
const excludedEmitters: string[] = []; | ||
|
||
for (const part of parts) { | ||
const trimmed = part.trim(); | ||
if (trimmed.startsWith("!")) { | ||
excludedEmitters.push(trimmed.substring(1)); | ||
} else { | ||
includedEmitters.push(trimmed); | ||
} | ||
} | ||
|
||
return { | ||
excludedEmitters, | ||
includedEmitters, | ||
isScoped: true, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export const namespace = "TypeSpec.HttpClient"; | ||
export * from "./context/index.js"; | ||
export type * from "./interfaces.js"; | ||
export { $lib } from "./lib.js"; | ||
export { $decorators } from "./tsp-index.js"; | ||
export * from "./utils/index.js"; |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { resolvePath } from "@typespec/compiler"; | ||
import { createTestLibrary, TypeSpecTestLibrary } from "@typespec/compiler/testing"; | ||
import { fileURLToPath } from "url"; | ||
import { | ||
TypeSpecTestLibrary, | ||
createTestLibrary, | ||
findTestPackageRoot, | ||
} from "@typespec/compiler/testing"; | ||
|
||
export const TypespecHttpClientLibraryTestLibrary: TypeSpecTestLibrary = createTestLibrary({ | ||
name: "@typespec/http-client-library", | ||
packageRoot: resolvePath(fileURLToPath(import.meta.url), "../../../../"), | ||
export const HttpClientTestLibrary: TypeSpecTestLibrary = createTestLibrary({ | ||
name: "@typespec/http-client", | ||
packageRoot: await findTestPackageRoot(import.meta.url), | ||
}); |
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 { TypeSpecHttpClientDecorators } from "../generated-defs/TypeSpec.HttpClient.js"; | ||
|
||
import { $experimental } from "./decorators/index.js"; | ||
|
||
export const $decorators = { | ||
"TypeSpec.HttpClient": { | ||
experimental: $experimental, | ||
} satisfies TypeSpecHttpClientDecorators, | ||
}; |
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
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.