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

fix(schema): use scule types for runtimeConfig type hints #23696

Merged
merged 33 commits into from Nov 15, 2023

Conversation

luc122c
Copy link
Contributor

@luc122c luc122c commented Oct 16, 2023

πŸ”— Linked issue

Resolves #23246

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

In the original code, if the first character was a digit, the code was adding an underscore before the character and then converting it to uppercase. This was causing the additional underscores when the input string was already in upper snake case.

TypeScript Playground link

This PR also adds a couple of tests to catch values already in upper snake case (as in the original issue).

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@stackblitz
Copy link

stackblitz bot commented Oct 16, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@danielroe danielroe changed the title fix: UpperSnakeCase type not correctly handling strings already in upper snake case fix(schema): correctly generate type hints for upper-snake-case config Oct 16, 2023
@luc122c luc122c marked this pull request as ready for review October 16, 2023 12:00
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

Thank you ❀️

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

Would you check the type fixture failures? πŸ™

@luc122c
Copy link
Contributor Author

luc122c commented Oct 16, 2023

The error that was being thrown was saying that the toEqualTypeOf function required one parameter with a type of 'never'. It seems a bit hacky, but by providing it with a value of [][0], the first value in an empty array, the error went away and the test runs as expected.

@danielroe
Copy link
Member

I believe that is how the type assertion indicates there is a mismatch. This is what the mismatch looks like:

CleanShot 2023-10-16 at 22 55 37@2x

@luc122c
Copy link
Contributor Author

luc122c commented Oct 16, 2023

Oh, interesting. I wasn't seeing any errors in the console output so I didn't think the tests were failing. I'll take another look.

@luc122c
Copy link
Contributor Author

luc122c commented Oct 16, 2023

@danielroe I've changed the input for the last test that was failing. It was originally baseAPIToken, now baseApiToken, and I understand why this was struggling. Although we know that 'API' is a common acronym, TypeScript doesn't. It would be possible to add a case to check for this, however I think it may be more beneficial to correctly handle camelCase than to correctly handle this particular case? Please let me know your thoughts.

@danielroe
Copy link
Member

danielroe commented Oct 16, 2023

The intent is to split the string with _ immediately before the final capital letter of a sequence of capital letters. So:

abcDEFghi => abc_DE_FGHI
abcDefGhi => ABC_DEF_GHI

This should be achievable in TS.

@luc122c
Copy link
Contributor Author

luc122c commented Oct 16, 2023

The intent is to split the string with _ immediately before the final capital letter of a sequence of capital letters.

Ok, i've implemented this change.

baseAPIToken now becomes BASE_API_TOKEN however APP_NAMES, which is already correct, now becomes A_PP_NAME_S which was a case highlighted in the original issue.

@luc122c luc122c marked this pull request as draft October 17, 2023 08:31
@danielroe danielroe added the 3.x label Oct 19, 2023
@luc122c luc122c marked this pull request as draft October 20, 2023 23:28
@luc122c
Copy link
Contributor Author

luc122c commented Oct 23, 2023

@danielroe Some of these test cases may not be possible with the current logic. For example, TESTAppName I can't figure out a way for this to work, since TypeScript doesn't know that 'Test', 'App' and 'Name' are words, so it doesn't know to split 'TESTA' before the A.

@danielroe
Copy link
Member

The logic isn't related to whether they are words. Note that scule splits these all correctly; it's based on splitting before the change of case.

I think we can use scule's native types for this - but we should ideally expose them upstream.

@danielroe danielroe mentioned this pull request Oct 23, 2023
8 tasks
@luc122c
Copy link
Contributor Author

luc122c commented Oct 23, 2023

The logic isn't related to whether they are words. Note that scule splits these all correctly; it's based on splitting before the change of case.

This is the closest I've managed to get separately, just following the logic.

I think we can use scule's native types for this - but we should ideally expose them upstream.

Does Scule expose itself as a type, like Defu does?

@danielroe
Copy link
Member

No - see unjs/scule#58. I pushed a change in c5f9a49 (#23696) that uses the inferred return type of snakeCase, however.

@luc122c
Copy link
Contributor Author

luc122c commented Oct 23, 2023

No - see unjs/scule#58. I pushed a change in c5f9a49 (#23696) that uses the inferred return type of snakeCase, however.

Brilliant, this will be a much cleaner solution πŸ‘

packages/schema/src/types/config.ts Outdated Show resolved Hide resolved
test/fixtures/basic-types/types.ts Outdated Show resolved Hide resolved
test/fixtures/basic-types/types.ts Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests βœ…

❗ No coverage uploaded for pull request base (main@c069239). Click here to learn what that means.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #23696   +/-   ##
=======================================
  Coverage        ?   58.76%           
=======================================
  Files           ?        5           
  Lines           ?      861           
  Branches        ?       46           
=======================================
  Hits            ?      506           
  Misses          ?      355           
  Partials        ?        0           

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

@luc122c luc122c marked this pull request as ready for review November 15, 2023 17:55
@danielroe danielroe changed the title fix(schema): correctly generate type hints for upper-snake-case config fix(schema): use scule types for runtimeConfig type hints Nov 15, 2023
@danielroe danielroe merged commit 6ec267b into nuxt:main Nov 15, 2023
33 checks passed
@github-actions github-actions bot mentioned this pull request Nov 15, 2023
@luc122c luc122c deleted the upper-snake-case-fix branch November 16, 2023 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RuntimeConfig does not display proper environment variable hint when already using UPPER_SNAKE_CASE
3 participants