Skip to content

Feature: Store hank output in json file#184

Open
naman070 wants to merge 8 commits into
AdRoll:mainfrom
naman070:main
Open

Feature: Store hank output in json file#184
naman070 wants to merge 8 commits into
AdRoll:mainfrom
naman070:main

Conversation

@naman070

@naman070 naman070 commented Jun 3, 2022

Copy link
Copy Markdown

Provided an argument based option where if command is run like:
rebar3 hank -o "output.json"
then it stores the generated output of hank in the json file.
This json file can further be used for better analysis of dead code.

Sample Json:

[
  {
    "hank_rule_broken": "unnecessary_function_arguments",
    "message": "hello_world/2 doesn't need its #2 argument",
    "path": "apps/library/src/library_app.erl",
    "start_line": 19,
    "title": "Unused function arguments"
  },
  {
    "hank_rule_broken": "unused_hrls",
    "message": "This file is unused",
    "path": "apps/library/src/demo_var.hrl",
    "start_line": 0,
    "title": "Unused hrl files"
  }
]

@CLAassistant

CLAassistant commented Jun 3, 2022

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ Brujo Benavides
❌ NMN070


Brujo Benavides seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Comment thread rebar.config Outdated
Comment thread src/rebar3_hank_prv.erl Outdated
Comment thread src/rebar3_hank_prv.erl Outdated

@elbrujohalcon elbrujohalcon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a good idea, but (besides the changes that I suggest in the current code), what I would like to see is…

  1. That this would also be available as a rebar.config option with then hank section.
  2. Documentation about this added to the README.md.
  3. Some tests that ensure that we don't go down on code coverage.

Thanks :)

Comment thread src/rebar3_hank_prv.erl Outdated
Comment thread src/rebar3_hank_prv.erl Outdated
Comment thread src/rebar3_hank_prv.erl Outdated
Comment thread src/rebar3_hank_prv.erl Outdated

@elbrujohalcon elbrujohalcon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few grammatical/syntactical/consistency-related amendments.

Comment thread README.md
Usage: rebar3 hank [-u <unused_ignores>] [-o <output_json_file>]

-u, --unused_ignores Warn on unused ignores (default: true).
-o, --output_json_file Emit output (in JSON format) to a file (default: empty string - meaning: donot emit output

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
-o, --output_json_file Emit output (in JSON format) to a file (default: empty string - meaning: donot emit output
-o, --output_json_file Emit output (in JSON format) to a file (default: empty string - meaning: do not emit output).

Comment thread README.md Outdated

It's worth noting that, even when those warnings are printed, that doesn't affect the overall result of the command. That is, if Hank can't find any instances of oxbow code, it will return successfully (i.e. `exit code: 0`) even when it may print these warnings.

By default, if hank detect issues in your code, it will print those issue on the console. But you can save this result in JSON format,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
By default, if hank detect issues in your code, it will print those issue on the console. But you can save this result in JSON format,
By default, if Hank detects issues in your code, it will print those issues on the console. But you can save this result in a file (in JSON format),

Comment thread src/hank_rule.erl Outdated
unused_callbacks ->
<<"Unused callback functions">>;
unnecessary_function_arguments ->
<<"Unused function arguments found">>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<<"Unused function arguments found">>;
<<"Unused function arguments">>;

Comment thread src/hank_rule.erl Outdated
unnecessary_function_arguments ->
<<"Unused function arguments found">>;
single_use_hrls ->
<<"Hrl is only used once">>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<<"Hrl is only used once">>
<<"Hrl file only used once">>

Comment thread src/hank_rule.erl Outdated
unused_macros ->
<<"Unused Macros">>;
single_use_hrl_attrs ->
<<"Macro is only used once">>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<<"Macro is only used once">>;
<<"Macro only used once">>;

Comment thread src/hank_rule.erl Outdated
single_use_hrl_attrs ->
<<"Macro is only used once">>;
unused_record_fields ->
<<"Field in the record is unused">>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<<"Field in the record is unused">>;
<<"Unused field in record">>;

Comment thread src/rebar3_hank_prv.erl Outdated
$o,
"output_json_file",
string,
"Emit output (in JSON format) to a file (default: empty string - meaning: donot emit output"}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"Emit output (in JSON format) to a file (default: empty string - meaning: donot emit output"}
"Emit output (in JSON format) to a file (default: empty string - meaning: do not emit output)"}

@elbrujohalcon

Copy link
Copy Markdown
Collaborator

@naman070 You should run rebar3 test on your code :)

So, at this stage, what's still missing is:

  1. To make this option also available as a rebar.config option within the hank section.
  2. Some tests that ensure that we don't go down on code coverage.

Thanks again :)

@naman070

naman070 commented Jun 8, 2022

Copy link
Copy Markdown
Author

@elbrujohalcon
What do you mean by your 1st point => "To make this option also available as a rebar.config option within the hank section.". Can you please elaborate more.

@elbrujohalcon

Copy link
Copy Markdown
Collaborator

@elbrujohalcon What do you mean by your 1st point => "To make this option also available as a rebar.config option within the hank section.". Can you please elaborate more.

Yeah, I want users to be able to include this in their rebar.config

{hank, [{output_json_file, "/the/file/to/put/the/output.json"}]}.

And have Hank pick it up every time they run rebar3 hank on the project.
Just like they can do now with ignore or parsing_style.

@naman070

naman070 commented Jun 8, 2022

Copy link
Copy Markdown
Author

@elbrujohalcon

  1. Have added changes so that it can be added as an option in rebar.config.
  2. Testcases are still pending (Should I create a separate file for testcases or should I incorporate in some existing file)

@pbrudnick pbrudnick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hey nice work, thank you @naman070 !
I'd expect a json output example here in the PR description to review it. I mean, what's the desired output?

Also I agree with the tests request.

@naman070

naman070 commented Jun 8, 2022

Copy link
Copy Markdown
Author

@elbrujohalcon Does the output of rebar3 test supposed to look like this?

===> Plugin jsx does not export init/1. It will not be used.
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling rebar3_hank
===> Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes scissors.
src/rebar3_hank_prv.erl:43: The word "JSON" in string is unknown. Maybe you wanted to use "jason" or "sion" or "slon" or "soln" or "son" or "sond" or "sone" or "song" or "sonk" or "sons" or "soon" or "sorn" or "sown" or "jeon" or "joan" or "john" or "join" or "Tjon"?

@elbrujohalcon

Copy link
Copy Markdown
Collaborator

@elbrujohalcon Does the output of rebar3 test supposed to look like this?


===> Plugin jsx does not export init/1. It will not be used.

===> Verifying dependencies...

===> Analyzing applications...

===> Compiling rebar3_hank

===> Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes scissors.

src/rebar3_hank_prv.erl:43: The word "JSON" in string is unknown. Maybe you wanted to use "jason" or "sion" or "slon" or "soln" or "son" or "sond" or "sone" or "song" or "sonk" or "sons" or "soon" or "sorn" or "sown" or "jeon" or "joan" or "john" or "join" or "Tjon"?

It's telling you that Sheldon (the spell checker) doesn't know the word "JSON".
Add it to nextroll.dict and you will be able to move on... ;)

Comment thread src/rebar3_hank_prv.erl
Comment on lines +133 to +134
ConvertedResult = convert_data_to_binary(Result),
EncodedResult = jsx:encode(ConvertedResult),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's not terribly wrong, but I would prefer to only do this if we're going to use it (i.e. First try to come up with the name of the output file, if it's not empty,... Only then convert, encode, and emit the output).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

And please document the rebar.config option in the README.md

Comment thread src/rebar3_hank_prv.erl
end
end.

-spec convert_data_to_binary([hank_rules:result()]) -> list().

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This function doesn't return binaries. Therefore, its name is wrong. A better name would be results_to_json.

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.

6 participants