Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

adamviktora
Copy link
Contributor

@adamviktora adamviktora commented Oct 23, 2024

Closes #790, #630

I had to add some new helpers to simplify proper type checking.

@@ -128,12 +120,12 @@ export function getVariableValue(
);
}
if (variableInit.type === "Literal") {
return variableInit.value;
return variableInit.value as string;
Copy link
Collaborator

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?

Copy link
Contributor Author

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;
Copy link
Collaborator

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[];
Copy link
Collaborator

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;
Copy link
Collaborator

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?

Copy link
Contributor Author

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;
Copy link
Collaborator

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;
Copy link
Collaborator

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.

Comment on lines 18 to 24
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;
Copy link
Collaborator

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;
Copy link
Collaborator

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"];
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Collaborator

@wise-king-sullyman wise-king-sullyman left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants