Skip to content
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

Improve how we handle Static Helpers #2716

Merged
merged 22 commits into from
Aug 9, 2024

Conversation

joheredi
Copy link
Member

@joheredi joheredi commented Jul 31, 2024

Objective:
Enhance the handling of static-helpers by transitioning from string injection into a ts-morph file to utilizing actual TypeScript files, which are copied to the output directory. This change aims to streamline the process and improve maintainability.

Key Changes:

  1. Static-Helpers as TS Files: Transition static-helpers from being strings to actual TypeScript files that get copied over to the output directory.
  2. Binder Integration:
    • Introduce awareness of static-helpers in the binder.
    • Allow references using resolveReference(PagingHelpers.buildPagedAsyncIterator), automatically adding the correct import.
    • Initial integration of the binder into the codebase to call the Polling and Pagination Helpers.

Future Work:

  • Follow-up changes will focus on automating export generation via the binder.
  • Subsequent PRs will aim to migrate all declarations and references to be managed through the binder.

I created 2 additional PRs that will merge into this one to make review a bit easier. This PR contains the changes to the code-gen.

PR with changes to generated CADL ranch tests: joheredi#8
PR with changes to generated smoke tests: joheredi#10

@@ -52,64 +53,4 @@ export interface CoreDependencies extends Record<string, ReferenceableSymbol> {
};
}

const _CoreDependencies: CoreDependencies = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving these to modular/external-dependencies.ts

@@ -39,15 +45,18 @@ export interface Binder {

class BinderImp implements Binder {
private declarations = new Map<unknown, DeclarationInfo>();
private references = new Map<unknown, Set<SourceFile>>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this map to track things that are used, this is helpful to cleanup unused static-helpers

@@ -146,7 +155,7 @@ class BinderImp implements Binder {
* @returns The serialized placeholder string.
*/
private serializePlaceholder(refkey: unknown): string {
return `"<PLACEHOLDER:${String(refkey)}>"`;
return `_PLACEHOLDER_${String(refkey)}_`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This format is safer with ts-morph. Sometimes the double quotes and < may cause unexpected ts-morph behavior. This makes the placeholder a valid identifier

@joheredi joheredi marked this pull request as ready for review July 31, 2024 21:11
@joheredi joheredi requested review from timovv and dgetu July 31, 2024 21:17
@joheredi joheredi changed the title Improve static file handling Improve how we handle Static Helpers Jul 31, 2024
Copy link
Member

@dgetu dgetu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -394,6 +397,12 @@ function getLroOnlyOperationFunction(operation: Operation, clientType: string) {
getOperationSignatureParameters(operation, clientType);
const returnType = buildLroReturnType(operation);
const { name, fixme = [] } = getOperationName(operation);
const pollerLikeReference = resolveReference(
Copy link
Member

@MaryGao MaryGao Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see this change! :)And we don't need to care the importing path stuff.

@joheredi joheredi merged commit 673ee1a into Azure:main Aug 9, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants