-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(misc): get rid of type casting / explain type casting
- Loading branch information
1 parent
e9f8593
commit 72f488f
Showing
7 changed files
with
66 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 13 additions & 15 deletions
28
packages/eslint-plugin-pf-codemods/src/rules/helpers/getEnumPropertyName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import { Rule } from "eslint"; | ||
import { Identifier, MemberExpression } from "estree-jsx"; | ||
import { getVariableDeclaration } from "."; | ||
import { MemberExpression } from "estree-jsx"; | ||
import { getVariableValue } from "."; | ||
|
||
/** Used to get a property name on an enum (MemberExpression). */ | ||
export function getEnumPropertyName( | ||
context: Rule.RuleContext, | ||
enumNode: MemberExpression | ||
) { | ||
const isIdentifier = enumNode.property.type === "Identifier"; | ||
const computed = enumNode.computed; | ||
if (enumNode.property.type === "Identifier") { | ||
// E.g. const key = "key"; someEnum[key] | ||
if (enumNode.computed) { | ||
const scope = context.getSourceCode().getScope(enumNode); | ||
const propertyName = enumNode.property.name; | ||
|
||
// E.g. const key = "key"; someEnum[key] | ||
if (isIdentifier && computed) { | ||
const scope = context.getSourceCode().getScope(enumNode); | ||
const propertyName = (enumNode.property as Identifier).name; | ||
const propertyVariable = getVariableDeclaration(propertyName, scope); | ||
return propertyVariable?.defs[0].node.init.value as string; | ||
} | ||
// E.g. someEnum.key | ||
if (isIdentifier && !computed) { | ||
return (enumNode.property as Identifier).name; | ||
return getVariableValue(propertyName, scope, context)?.toString(); | ||
} | ||
// E.g. someEnum.key | ||
return enumNode.property.name; | ||
} | ||
|
||
// E.g. someEnum["key"] | ||
if (enumNode.property.type === "Literal") { | ||
return enumNode.property.value as string; | ||
return enumNode.property.value?.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters