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

refactor(ts): strict mode #696

Merged
merged 9 commits into from
Sep 14, 2023
Merged

refactor(ts): strict mode #696

merged 9 commits into from
Sep 14, 2023

Conversation

kanadgupta
Copy link
Member

@kanadgupta kanadgupta commented Aug 17, 2023

🧰 Changes

Flips TS strict mode on in all packages.

fixes RM-7991

@@ -89,7 +89,7 @@ cmd

try {
return Storage.isIdentifierValid(value, true);
} catch (err) {
} catch (err: any) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm gonna swap these out with the useUnknownInCatchVariables TSConfig option.

Co-authored-by: Kanad Gupta <[email protected]>
@erunion erunion changed the base branch from main to feat/core-package September 14, 2023 03:30
@erunion erunion added the enhancement New feature or request label Sep 14, 2023
Comment on lines +570 to +573
const parameters = {} as {
body: OptionalKind<ParameterDeclarationStructure>;
metadata: OptionalKind<ParameterDeclarationStructure>;
};
Copy link
Member

Choose a reason for hiding this comment

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

This slight reshuffling ended up fixing all of those ts-morph typing issues. When we use parameters.body and parameters.metadata they will be present so marking them as optional here is wrong. I believe I had originally done it this way to satisfy setting parameters to an empty object.

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotta love strict mode

@erunion erunion marked this pull request as ready for review September 14, 2023 03:42
@erunion
Copy link
Member

erunion commented Sep 14, 2023

@kanadgupta I've rebranched branched this off my core refactor and rewrote your commits on top of that. I've also fixed the remaining strict issues in the ts-morph code so this is all passing now.

I'm happy with it but please give it another look.

"outDir": "dist/"
"outDir": "dist/",
"strict": true,
"useUnknownInCatchVariables": false
Copy link
Member

Choose a reason for hiding this comment

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

Allows us to not have to do catch (err: any) nonsense that adds no value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh hell yeah I've been running into this a bunch in rdme, gonna steal this. Thanks!

const security = operation.prepareSecurity();
Object.keys(security).forEach((id: SecurityType) => {
security[id].forEach(scheme => {
Object.entries(operation.prepareSecurity()).forEach(([, schemes]) => {
Copy link
Member

Choose a reason for hiding this comment

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

Rewrote this whole loop because I don't know what I was smoking when I did that object.keys() mess earlier. 🪴

@@ -95,7 +91,7 @@ const client: Client<APIOptions> = {
convert: ({ cookiesObj, headersObj, postData, queryObj, url, ...source }, options) => {
const opts = {
...options,
};
} as APIOptions;
Copy link
Member

Choose a reason for hiding this comment

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

Eventhough all of our APIOptions are required for this snippet httpsnippet core marks all client options as optional so doing new Oas(opts.apiDefinition) thought we were trying to pass new Oas(undefined). Forcefully re-casing options as options here fixes that.

@@ -230,7 +231,7 @@ export default class TSGenerator extends CodeGeneratorLanguage {
sdkSource
.getImportDeclarations()
.find(id => id.getText().includes('HTTPMethodRange'))
.replaceWithText("import type { ConfigOptions, FetchResponse } from '@api/core'");
?.replaceWithText("import type { ConfigOptions, FetchResponse } from '@api/core'");
Copy link
Member

Choose a reason for hiding this comment

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

You originally had these as the following but I think optionally chaining these is a little nicer.

const declaration = sdkSource.getImportDeclarations(...).find(...);
if (declaration) declaration.replaceWithText(...);

Cool with the original solution if you want me to revert it though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I suppose this just returns undefined and doesn't actually touch the original sdkSource variable if it can't find anything right? Works for me!

@kanadgupta
Copy link
Member Author

kanadgupta commented Sep 14, 2023

@erunion LGTM, thanks for getting this across the finish line 🫶🏽 feel free to merge it in when you're ready!

Base automatically changed from feat/core-package to main September 14, 2023 22:19
@kanadgupta kanadgupta requested a review from erunion September 14, 2023 22:23
@erunion erunion merged commit 3529992 into main Sep 14, 2023
5 checks passed
@erunion erunion deleted the ts-strict-mode branch September 14, 2023 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants