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

WIP: Introduce formatter objects and add a JSON format. #116

Closed
wants to merge 2 commits into from

Conversation

RST-J
Copy link

@RST-J RST-J commented Oct 31, 2018

I took a shot at introducing formatters as suggested in #84 because I'd like this cool gem to become available in syntastic. The one-line-format for #84 could easily be added as another formatter.

Before I go on and add tests I would like to know what you think of this and whether there are suggestions to add/change things.

The JSON format is gratefully adopted from rubocop (without the :severity key and :linter instead of :cop_name in the offense hashes).

def report
io =
if @options[:out]
File.open(@options[:out], 'w')
Copy link
Author

Choose a reason for hiding this comment

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

There is no code to handle closing of the file because files are closed when the script ends and I had no idea how to use the block notation without repeating the report statement for both cases. Maybe someone has an idea for that?

@results.each do |relative_filename, runner|
runner.offenses.each do |offense|
io.puts <<~EOF
#{offense.message}#{Rainbow(' (not autocorrected)').red if options[:autocorrect]}
Copy link
Author

Choose a reason for hiding this comment

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

I think, this should be unless options[:autocorrect] from how I understand the original code but I let it as is for now. Can on of the maintainers or the original author shed light on this?

if stats.corrected > 0
corrected_found_diff = stats.found - stats.corrected
if corrected_found_diff > 0
io.puts Rainbow(
Copy link
Author

Choose a reason for hiding this comment

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

This previously was a call to warn. Is there a reason why this was written to STDERR? The same holds for the elsif stats.found > 0 case below. I did this because I wasn't sure how to handle this for file outpt.

Choose a reason for hiding this comment

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

I think reporting action results to stderr could cause troubles if you invoke the linter from another procces, exit code 1 should be used to report unexpected errors only.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed.

linter, message = linter_and_message(offense)
{
message: message,
linter: linter,
Copy link
Author

Choose a reason for hiding this comment

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

For now, if a rubocop cop found an offense, this is set to the particular cop name and not to 'rubocop' which is the actual erb-lint-linter. I first thought this is better but now think, maybe it is Ok/better to have 'rubocop' here and let the cop remain in the message. Thoughts on this?

Copy link

@vzamanillo vzamanillo Nov 18, 2018

Choose a reason for hiding this comment

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

I am agree with you, it would be nice to identify from where the cop comes and let the cop in the message.

end
end

def offense_corrected?(offense)
Copy link
Author

Choose a reason for hiding this comment

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

I'm totally not sure about this. The only thing I found in the code so far is the rubocop-linter setting this key on correction. However, I also observed that corrected offenses seem not to get reported - or I did something wrong for that and need to look that up somewhere else.

@vzamanillo
Copy link

vzamanillo commented Nov 18, 2018

I am working on an atom linter for erb-lint, the actual text output is very difficult to parse, this PR would be great, congrats.

@result = {
metadata: metadata_hash,
files: [],
summary: {offense_count: 0, inspected_file_count: 0}

Choose a reason for hiding this comment

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

It should be interesting to have an offending files counter.

Suggested change
summary: {offense_count: 0, inspected_file_count: 0}
summary: { offense_count: 0, inspected_file_count: 0, offending_file_count: 0 }

def file_completed(relative_filename, runner)
@result[:summary][:offense_count] += runner.offenses.count
@result[:summary][:inspected_file_count] += 1
@result[:files] = hash_for_file(relative_filename, runner)

Choose a reason for hiding this comment

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

We are reporting the results only for the last processed file, and I think it could be better add the files to the final report only if offenses are present.

Suggested change
@result[:files] = hash_for_file(relative_filename, runner)
if runner.offenses.count.positive?
@result[:summary][:offending_file_count] += 1
@result[:files] << hash_for_file(relative_filename, runner)
end

Copy link
Author

Choose a reason for hiding this comment

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

Oh, careless mistake ...

And I agree, we should only report files with issues. Iirc, the check is currently not necessary because files without offenses do not reach this code anyways. But to record the number of inspected files this would change.

@vzamanillo
Copy link

vzamanillo commented Nov 19, 2018

Could you please take a look at this commit in my fork? It is based on your work, the idea is:

  • The Offense base class implements a to_h method with the offense details
  • New RubocopOffense class for Rubocop who has the responsibility to get the message (native offense) details.
  • The Rubocop LInter add_offense overrides the parent's class method and uses the specific linter offense class.
  • The JSONFormatter is small and clear now, and linter independent.

What do you think? thoughts?

@RST-J
Copy link
Author

RST-J commented Nov 20, 2018

@vzamanillo I've had a look and wrote some comments.

@manuelpuyol manuelpuyol mentioned this pull request Jul 2, 2021
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.

4 participants