[typescript-operations] Fix enumValues not considering namingConvention in certain scenarios#10656
Conversation
…d use it for imports
🦋 Changeset detectedLatest commit: 40d48c3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| this.config.enumValues = parseEnumValues({ | ||
| schema: _schema, | ||
| mapOrStr: rawConfig.enumValues, | ||
| naming: { | ||
| convert: this.config.convert, | ||
| options: { | ||
| typesPrefix: this.config.typesPrefix, | ||
| typesSuffix: this.config.typesSuffix, | ||
| useTypesPrefix: this.config.enumPrefix, | ||
| useTypesSuffix: this.config.enumSuffix, | ||
| }, | ||
| }, | ||
| }); |
There was a problem hiding this comment.
enumValues now parses the type with naming convention, so we need to pass naming convention config in.
IMO the naming config and function are a bit confusing to use atm. However, we can fix it internally later to keep scope maintainable in this PR/major release
| options: { | ||
| typesPrefix: string; | ||
| typesSuffix: string; | ||
| useTypesPrefix?: boolean; | ||
| useTypesSuffix?: boolean; | ||
| }; |
There was a problem hiding this comment.
Grouping options here make it a bit easier to read
| const convertName = ({ | ||
| convert, | ||
| options, | ||
| }: { | ||
| options: { | ||
| typesPrefix: string; | ||
| useTypesPrefix?: boolean; | ||
| typesSuffix: string; | ||
| useTypesSuffix?: boolean; | ||
| }; | ||
| convert: () => string; | ||
| }): string => { | ||
| const useTypesPrefix = typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true; | ||
| const useTypesSuffix = typeof options.useTypesSuffix === 'boolean' ? options.useTypesSuffix : true; | ||
|
|
||
| let convertedName = ''; | ||
|
|
||
| if (useTypesPrefix) { | ||
| convertedName += options.typesPrefix; | ||
| } | ||
|
|
||
| convertedName += convert(); | ||
|
|
||
| if (useTypesSuffix) { | ||
| convertedName += options.typesSuffix; | ||
| } | ||
|
|
||
| return convertedName; | ||
| }; |
There was a problem hiding this comment.
This is moved to visitor-plugin-common here
| return allEnums | ||
| .filter(enumName => !enumName.startsWith('__')) | ||
| .reduce((prev, enumName) => { | ||
| .reduce<ParsedEnumValuesMap>((prev, enumName) => { |
There was a problem hiding this comment.
I prefer using .reduce's generic to avoid as SomeType usage at the bottom
| function convertNameParts(str: string, func: (str: string) => string, removeUnderscore = false): string { | ||
| if (removeUnderscore) { | ||
| return func(str); | ||
| } | ||
|
|
||
| return str | ||
| .split('_') | ||
| .map(s => func(s)) | ||
| .join('_'); | ||
| } |
There was a problem hiding this comment.
This convertNameParts function was previously in another file here, but is only ever used in this function. May as well move it here to keep things closer together.
| }); | ||
|
|
||
| expect(result).not.toEqual({ | ||
| expect(result).toEqual({ |
There was a problem hiding this comment.
Some of these tests have been turned to possible case, to be more explicit.
Description
Enum type naming convention was not being applied consistently across imports, Input, Variables and Result.
This causes some edge cases where the enum does not get referred to correctly, causing type issues.
Related #10496
Issues: #10471
Type of change
How Has This Been Tested?