Skip to content

Commit

Permalink
refactor: Remove unused feature flag
Browse files Browse the repository at this point in the history
Remove non typescript check option
Name variable underscore to imply non-use
Refactor tests for flag test specs
  • Loading branch information
denis-kralj-novu committed May 6, 2024
1 parent 1dbf6aa commit b0c9be8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
27 changes: 15 additions & 12 deletions libs/shared/src/types/feature-flags/flags.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,36 @@ enum ValidFlagsEnum {
}
testFlagEnumValidity(ValidFlagsEnum);

enum InvalidKeyFlagsEnum {
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED',
enum InvalidFlagsEnum {
INVALID_ENABLED = 'INVALID_ENABLED',
}
// @ts-expect-error - Invalid key - INVALID_ENABLED
testFlagEnumValidity(InvalidKeyFlagsEnum);
enum InvalidValueFlagsEnum {
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED',
INVALID_ENABLED = 'INVALID_ENABLED',
// @ts-expect-error - not matching pattern
testFlagEnumValidity(InvalidFlagsEnum);

enum NonMatchingKeyValueEnum {
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ELSE_ENABLED',
}
// @ts-expect-error - Invalid value on IS_INVALID_ENABLED: 'INVALID_ENABLED'
testFlagEnumValidity(InvalidValueFlagsEnum);

// Ensure that the keys and values of FeatureFlagsKeysEnum match
type ValidateNonMatchingKeyValueEnum = {
[K in keyof typeof NonMatchingKeyValueEnum]: K extends IFlagKey ? K : `Value doesn't match key`;
};
// @ts-expect-error - non matching key-value pair in enum
const validateNonMatchingKeyValueEnum: ValidateNonMatchingKeyValueEnum = NonMatchingKeyValueEnum;

/**
* Verifying declared FlagEnums
*/
testFlagEnumValidity(FeatureFlagsKeysEnum);
testFlagEnumValidity(SystemCriticalFlagsEnum);

// Ensure that the keys and values of FeatureFlagsKeysEnum match
type ValidateFeatureFlagsKeysEnum = {
[K in keyof typeof FeatureFlagsKeysEnum]: K extends IFlagKey ? K : `Value doesn't match key`;
};
const validateFeatureFlagsKeysEnum: ValidateFeatureFlagsKeysEnum = FeatureFlagsKeysEnum;
testFlagEnumValidity(FeatureFlagsKeysEnum);

// Ensure that the keys and values of SystemCriticalFlagsEnum match
type ValidateSystemCriticalFlagsEnum = {
[K in keyof typeof SystemCriticalFlagsEnum]: K extends IFlagKey ? K : `Value doesn't match key`;
};
const validateSystemCriticalFlagsEnum: ValidateSystemCriticalFlagsEnum = SystemCriticalFlagsEnum;
testFlagEnumValidity(SystemCriticalFlagsEnum);
16 changes: 2 additions & 14 deletions libs/shared/src/types/feature-flags/flags.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,5 @@ export type IFlagKey = `IS_${Uppercase<string>}_ENABLED`;
* @param testEnum - the Enum to type check
*/
export function testFlagEnumValidity<TEnum extends IFlags, IFlags = Record<IFlagKey, IFlagKey>>(
testEnum: TEnum & Record<Exclude<keyof TEnum, keyof IFlags>, ['Key must follow `IFlagKey` format']>
) {
for (const key in testEnum) {
if (testEnum.hasOwnProperty(key) && isIFlagKey(key) && isIFlagKey(testEnum[key])) {
if (key !== testEnum[key]) {
throw Error('Enum name must match the value');
}
}
}
}

function isIFlagKey(value: unknown): value is IFlagKey {
return typeof value === 'string';
}
_: TEnum & Record<Exclude<keyof TEnum, keyof IFlags>, ['Key must follow `IFlagKey` format']>
) {}

0 comments on commit b0c9be8

Please sign in to comment.