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

Adds experimental ErbAstScanner #416

Merged
merged 1 commit into from
Feb 14, 2022
Merged

Conversation

davidwessman
Copy link
Collaborator

@davidwessman davidwessman commented Feb 2, 2022

  • Create a basic AST for all dynamic and code calls in ERB based on the
    Temple::ERB::Parser and convert results to Parser::AST::Nodes to be
    able to reuse a lot of RubyAstScanner.

  • It does not handle magic comments (maybe it would make sense to do
    them with regex instead?

  • It will not get the correct position or line position, only row
    number.

  • Adds an AST-based parser using https://github.com/Shopify/better-html

@glebm Do you think something like this would be useful? I have seen it mentioned a few times in issues and such.

@davidwessman
Copy link
Collaborator Author

Could probably make another go at this by using https://github.com/Shopify/better-html or parts of https://github.com/Shopify/erb-lint.

@glebm
Copy link
Owner

glebm commented Feb 5, 2022

Do you think something like this would be useful?

Absolutely, anything that replaces the current regexp parser is an improvement!

@davidwessman
Copy link
Collaborator Author

Do you think something like this would be useful?

Absolutely, anything that replaces the current regexp parser is an improvement!

Cool 😊 it passes the test suite locally as is. But maybe it needs to be added as an opt-in.

I was also thinking if we could restructure the AST-matching to allow adding custom matchers like support for Rails model_name.human and human_attribute_name

@glebm
Copy link
Owner

glebm commented Feb 5, 2022

But maybe it needs to be added as an opt-in.

It can be the default (assuming comments and file positions are implemented), and we'll bump the minor version number.

I was also thinking if we could restructure the AST-matching to allow adding custom matchers like support for Rails model_name.human and human_attribute_name

Yeah, I'm sure that'd be helpful for lots of folks.

@davidwessman
Copy link
Collaborator Author

It can be the default (assuming comments and file positions are implemented), and we'll bump the minor version number.

I will try to think if I can figure out the file positions, must look through the Shopify-gems if they have any ideas.
temple removes too much information in its parsing :/

Would it be okay to only support comments that are in ERB-tags?

<!-- i18n-tasks-use t('activerecord.models.company') --> <!-- this will be ignored -->
<% # i18n-tasks-use t('activerecord.models.company') %> <!-- this will be a magic comment -->
<h1><%= Company.model_name.human %></h1>

@glebm
Copy link
Owner

glebm commented Feb 6, 2022

Would it be okay to only support comments that are in ERB-tags?

Yes, this is how it is currently as well

'erb' => /^\s*<%\s*#(?!\si18n-tasks-use)/,

@davidwessman davidwessman force-pushed the parse-erb-ast branch 4 times, most recently from 24f2059 to 50cc09b Compare February 13, 2022 21:32
Comment on lines +212 to +219
@parser.reset
associated_node = comment_to_node[comment]
@call_finder.collect_calls(
@parser.parse(make_buffer(path, comment.text.sub(MAGIC_COMMENT_PREFIX, '').split(/\s+(?=t)/).join('; ')))
) do |send_node, _method_name|
# method_name is not available at this stage
send_node_to_key_occurrence(send_node, nil, location: associated_node || comment.location)
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we could replace the parsing here with the LocalRubyParser I needed in the erb parser.

Copy link
Owner

Choose a reason for hiding this comment

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

Yep sounds good 👍

@davidwessman
Copy link
Collaborator Author

@glebm I think it works now 👀

@davidwessman davidwessman marked this pull request as ready for review February 13, 2022 21:40
@glebm
Copy link
Owner

glebm commented Feb 13, 2022

Clean and easy-to-read code with tests, amazing contribution 👍

@davidwessman
Copy link
Collaborator Author

Clean and easy-to-read code with tests, amazing contribution 👍

Glad to hear 🙂
Please ping me if you need anything else from me on this PR.

I will start to tinker with configurable AST-call-finders to add support for human_attribute_name and model_name.human for Rails 🙂

@glebm glebm merged commit d7ae6e6 into glebm:main Feb 14, 2022
@davidwessman davidwessman deleted the parse-erb-ast branch March 24, 2022 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants