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

Ensure custom linters are loaded at the same time as linters #165

Closed
wants to merge 2 commits into from

Conversation

jen-taylor-shopify
Copy link

@jen-taylor-shopify jen-taylor-shopify commented May 20, 2020

Closes #164

This PR updates the CLI to also load the custom linters when the options are parsed. This prevents not a valid linter name errors when attempting to run erb-lint with a specific custom linter:

$ bundle exec erblint --enable-linters custom_linter --lint-all
custom_linter: not a valid linter name (rubocop, rubocop_text, parser_errors, space_around_erb_tag, space_in_html_tag, extra_newline, right_trim, erb_safety, trailing_whitespace, closing_erb_tag_indent, space_indentation, hard_coded_string, no_javascript_tag_helper, allowed_script_type, self_closing_tag, final_newline, deprecated_classes)

A lot of the "why" is detailed in #164, but this seemed like the shortest lift.

@jen-taylor-shopify jen-taylor-shopify changed the title Ensure custom linters are loaded at the same time as linters [WIP] Ensure custom linters are loaded at the same time as linters May 20, 2020
@@ -51,7 +51,11 @@ def run(_processed_source)
expect { subject }.to(output(/erblint \[options\] \[file1, file2, ...\]/).to_stderr)
end

it 'shows all known linters in stderr' do
it 'runs load_custom_linters when showing all known linters in stderr' do
expect(ERBLint::LinterRegistry).to(receive(:load_custom_linters))
Copy link

@brendo brendo May 25, 2020

Choose a reason for hiding this comment

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

This is understandably cheeky, but loading of linters is completely stubbed in this test 🙈

This feels like a quick & dirty way to assert that the loading of custom linters happens even though .linters will always return the stubbed linters.

@brendo brendo changed the title [WIP] Ensure custom linters are loaded at the same time as linters Ensure custom linters are loaded at the same time as linters May 25, 2020
@@ -169,6 +169,13 @@ def load_options(args)
option_parser.parse!(args)
end

def load_linters
@load_linters ||= begin
ERBLint::LinterRegistry.load_custom_linters
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason why this is not being done at ERBLint::LinterRegistry.linters?

Copy link

Choose a reason for hiding this comment

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

linters is an attr_reader for an array with no logic at the moment. It's populated when files include the LinterRegistry class.

https://github.com/Shopify/erb-lint/blob/b1e657aae1951400059e344346937d3f062a0fc4/lib/erb_lint/linter_registry.rb#L7-L14

For linters that come with the project, this kinda all happens magically with autoload, but for linters defined outside of ERB Lint they need to be explicitly loaded by calling load_custom_linters:

https://github.com/Shopify/erb-lint/blob/b1e657aae1951400059e344346937d3f062a0fc4/lib/erb_lint/linter_registry.rb#L20-L23

I don't have any historical context as to why this is the case.

Copy link
Member

Choose a reason for hiding this comment

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

It feels more logical have it there. Maybe the attr_reader should be called loaded_linters, and linters should call load_custom_linters before returning the value of the instance variable? That way we wouldn't have to change ERBLint::CLI at all.

Copy link

Choose a reason for hiding this comment

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

I like that approach! @jen-taylor-shopify and I will pair tomorrow and give it a whirl, thanks!

@jen-taylor-shopify
Copy link
Author

Closing this branch and implementing Etienne's suggestion in this new branch: #178

@rafaelfranca rafaelfranca deleted the fix-custom-linters branch February 18, 2021 08:20
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.

Loading Custom Linters is broken
4 participants