Skip to content

Commit

Permalink
feat(helpers): update isEnumValue helper to support list of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Oct 30, 2024
1 parent c43d8a6 commit 1d5507c
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/eslint-plugin-pf-codemods/src/rules/helpers/isEnumValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 1d5507c

Please sign in to comment.