From 1d5507cbe123e0dc4ff28a151468e5aa4f6b3685 Mon Sep 17 00:00:00 2001 From: adamviktora Date: Wed, 30 Oct 2024 11:37:01 +0100 Subject: [PATCH] feat(helpers): update isEnumValue helper to support list of strings --- .../src/rules/helpers/isEnumValue.ts | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/isEnumValue.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/isEnumValue.ts index c158335a..398b0792 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/isEnumValue.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/isEnumValue.ts @@ -3,21 +3,31 @@ import { propertyNameMatches } from "./propertyNameMatches"; import { Rule } from "eslint"; /** Checks whether an enum node (MemberExpression), e.g. ButtonVariant["primary"] - * has a given enumName ("ButtonVariant") and a given propertyName ("primary") */ + * has a given enumName ("ButtonVariant") and a given propertyName ("primary"), or one of given property names. */ export function isEnumValue( context: Rule.RuleContext, enumExpression: MemberExpression, enumName: string, - propertyName: string + propertyName: string | string[] ) { - return ( + if ( enumExpression.object?.type === "Identifier" && - enumExpression.object.name === enumName && - propertyNameMatches( - context, - enumExpression.property, - enumExpression.computed, - propertyName - ) - ); + enumExpression.object.name === enumName + ) { + const nameMatches = (name: string) => + propertyNameMatches( + context, + enumExpression.property, + enumExpression.computed, + name + ); + + if (Array.isArray(propertyName)) { + return propertyName.some((name) => nameMatches(name)); + } + + return nameMatches(propertyName); + } + + return false; }