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

Normalize types, typespecs and module attributes #52

Merged
merged 6 commits into from
Dec 12, 2021

Conversation

jiegillet
Copy link
Contributor

Closes #24.

Sorry for another large PR, this problem was trickier than I thought.
Basically, we have to run the representation twice, one for the types and typespecs, one for the rest. This is so we can keep the integer types in @spec function(integer :: integer) :: integer but still replace the first one.
This is also why I had to modify the two_modules tests, since the traversal order changed.

I chose to represent all types without parentheses, as it's easy to read, the documentation uses that a lot and it made the implementation easier.

I made a bunch of other small changes and refactors, including making almost all functions private. I know it's not good practice for an established codebase, but this one is not fully mature yet.

@jiegillet jiegillet added x:module/representer Work on Representers x:type/coding Write code that is not student-facing content (e.g. test-runners, generators, but not exercises) x:size/large Large amount of work labels Nov 17, 2021
@angelikatyborska angelikatyborska self-requested a review November 20, 2021 17:49
Copy link
Member

@angelikatyborska angelikatyborska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't analyze every single added line, but the new tests are very extensive and should have detected potential issues, so LGTM 🙁

I made a bunch of other small changes and refactors, including making almost all functions private. I know it's not good practice for an established codebase, but this one is not fully mature yet.

Good choice - better sooner than later!

Comment on lines 36 to 43
# replacing function names and variables
|> Macro.prewalk(mapping, &use_existing_placeholders/2)
# dropping docs
|> Macro.prewalk(&drop_docstring/1)
# removing added metadata
|> Macro.prewalk(&drop_line_meta/1)
# adding parentheses in pipes
|> Macro.prewalk(&add_parentheses_in_pipes/1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure those comments are useful. They're almost the same as the function names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, they were very helpful to me as I was restructuring the code, but they don't need to stay.

Comment on lines 324 to 325
# hack: assuming that if a module has no complete placeholder name, that means it's not being defined in this file
# TODO: fix when dealing with aliases
nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does removing the TODO comment mean we're giving up on fixing this, or is it no longer considered a hack but the final solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I interpreted this line as "this doesn't work with aliases yet". I added alias support in my previous PR but forgot to remove the comment. So my intent was not to comment on the hack.

I don't really have an issue with the "hack" or any plan to remove it or finalize it... It works, so I didn't touch it :)

lib/representer.ex Outdated Show resolved Hide resolved
when placeholder_8: atom, placeholder_9: integer
@spec placeholder_10(placeholder_11) :: [placeholder_11] when placeholder_11: var
@callback placeholder_12(placeholder_19 :: placeholder_3(placeholder_1), placeholder_20 :: any) :: placeholder_10(any)
@macrocallback placeholder_13(placeholder_21 :: String.t()) :: Macro.t()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description says:

I chose to represent all types without parentheses, as it's easy to read, the documentation uses that a lot and it made the implementation easier.

yet here's String.t() and Macro.t(). IMO it doesn't matter, but I just wanted to point out in case it's a bug in your opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I missed that. Yeah, I would consider that a bug (or at the very least, it would bug me)

Copy link
Contributor Author

@jiegillet jiegillet Dec 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... Looks like I'm going to have to live with it. I do have code that sets no_parens: true in the metadata at the right place, but it looks like this is something the formatter feels strongly enough to overwrite (in Macro.to_string since 1.13), and I have not found a way to change it.

@jiegillet jiegillet merged commit 64b02a7 into main Dec 12, 2021
@jiegillet jiegillet deleted the jie-module-attributes branch December 12, 2021 13:29
@jiegillet
Copy link
Contributor Author

Thank you for the review!! I know it was a big one :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
x:module/representer Work on Representers x:size/large Large amount of work x:type/coding Write code that is not student-facing content (e.g. test-runners, generators, but not exercises)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Normalize custom module attribute names; keep custom type definitions
2 participants