-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: type getVariableValue helper #796
base: main
Are you sure you want to change the base?
Conversation
9710972
to
e9f8593
Compare
@@ -128,12 +120,12 @@ export function getVariableValue( | |||
); | |||
} | |||
if (variableInit.type === "Literal") { | |||
return variableInit.value; | |||
return variableInit.value as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this cast since it might not actually be a string.
Instead of casting it could we call toString
on it or something if we want the returned value to always be a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, we probably don't even need it to always be a string
} | ||
if (variableInit.type === "MemberExpression") { | ||
return getMemberExpression(variableInit); | ||
return variableInit as MemberExpression; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And rather than this cast can we fix the typing of variableInit to resolve as any
? That should make this implicitly type correctly.
} | ||
if (variableInit.type === "ObjectExpression") { | ||
return variableInit.properties; | ||
return variableInit.properties as Property[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this cast either, looking through the type defs I think it could also be spread props no?
// E.g. const key = "key"; someEnum[key] | ||
if (isIdentifier && computed) { | ||
const scope = context.getSourceCode().getScope(enumNode); | ||
const propertyName = (enumNode.property as Identifier).name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the Identifier
casting here and on line 22 really valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it is valid because of the isIdentifier
, but I'll rewrite it so the casting is not necessary
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The property accessing here looks pretty similar to the variableInit
in the last file, may be worth having a helper for dealing with resolving the value of a variable declaration, or fixing the typing of that helper?
Also similar note to my other one about the string assertion.
} | ||
// E.g. someEnum["key"] | ||
if (enumNode.property.type === "Literal") { | ||
return enumNode.property.value as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same string type assertion concern here.
const propertyName = (key as Identifier).name; | ||
const propertyVariable = getVariableDeclaration(propertyName, scope); | ||
return propertyVariable?.defs[0].node.init.value === name; | ||
} | ||
// E.g. {key: value}; someObject.key | ||
if (isIdentifier && !computed) { | ||
return (key as Identifier).name === name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same type casting concerns here.
const scope = context.getSourceCode().getScope(key); | ||
const propertyName = (key as Identifier).name; | ||
const propertyVariable = getVariableDeclaration(propertyName, scope); | ||
return propertyVariable?.defs[0].node.init.value === name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And same thought about it maybe being worth us making a type safe helper for traversing propertyVariable
@@ -54,19 +53,23 @@ module.exports = { | |||
const selectableActionsValue = getAttributeValue( | |||
context, | |||
selectableActionsProp.value | |||
); | |||
) as ObjectExpression["properties"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're having to cast the return values of getAttributeValue
in mods now I feel like there's something not right going on.
I don't want to spam this comment out so echo this same thing for the below files where we're casting the return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the casting is done because the actual selectableActions
prop on CardHeader
accepts only an object (CardHeaderSelectableActionsObject), so consumers shouldn't be able to pass anything else. I added a comment to make it clear.
The getAttributeValue
should ultimately get to the ObjectExpression, or return undefined (e.g. if it was an Identifier imported from another file) in which case we don't do the fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But yeah on many other places, the casting was done due to the lack of typing of the getAttributeValue
helper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on this so far! But I feel like for an issue focused on improving our typing we probably don't want to be adding a bunch of type casting if it can at all be avoided.
ccfc9bd
to
f553135
Compare
f553135
to
72f488f
Compare
Closes #790, #630
I had to add some new helpers to simplify proper type checking.