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

Suggestion feature #48

Merged
merged 7 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/test_rdjson_formatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test rdjson_formatter
on:
push:
branches:
- master
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-ruby@v1
with:
ruby-version: '3.0.2'
- run: gem install rubocop
- name: Test rdjson_formatter
run: ./test/rdjson_formatter/test.sh
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AllCops:
Exclude:
- 'test/rdjson_formatter/**/*'
- 'vendor/**/*'
121 changes: 121 additions & 0 deletions rdjson_formatter/rdjson_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# frozen_string_literal: true

# https://docs.rubocop.org/rubocop/formatters.html
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
class RdjsonFormatter < RuboCop::Formatter::BaseFormatter
def started(_target_files)
@rdjson = {
source: {
name: 'rubocop',
url: 'https://rubocop.org/'
},
diagnostics: []
}
super
end

def file_finished(file, offenses)
offenses.each do |offense|
@rdjson[:diagnostics] << build_diagnostic(file, offense)
end
end

def finished(_inspected_files)
puts @rdjson.to_json
end

private

# @param [String] file
# @param [RuboCop::Cop::Offense] offense
# @return [Hash]
def build_diagnostic(file, offense)
code, message = offense.message.split(':', 2).map(&:strip)

diagnostic = {
message: message,
location: {
path: convert_path(file),
range: {
start: {
line: offense.location.begin.line,
column: offense.location.begin.column + 1
},
end: {
line: offense.location.end.line,
column: offense.location.end.column + 1
}
}
},
severity: convert_severity(offense.severity),
code: {
value: code
},
original_output: offense.to_s
}

diagnostic[:suggestions] = build_suggestions(offense) if offense.corrector

diagnostic
end

# @param [RuboCop::Cop::Offense] offense
# @return [Array{Hash}]
def build_suggestions(offense)
range, text = offense.corrector.as_replacements[0]

[
{
range: {
start: {
line: range.begin.line,
column: range.begin.column + 1 # rubocop is 0-origin, reviewdog is 1-origin
},
end: {
line: range.end.line,
column: range.end.column + 1
}
},
text: text
}
]
end

# https://github.com/reviewdog/reviewdog/blob/1d8f6d6897dcfa67c33a2ccdc2ea23a8cca96c8c/proto/rdf/reviewdog.proto
# https://docs.rubocop.org/rubocop/configuration.html#severity
#
# @param [Symbol] severity
# @return [String]
def convert_severity(severity)
case severity
when :info
'INFO'
when :warning
'WARNING'
when :error
'ERROR'
else
'UNKNOWN_SEVERITY'
end
end

# extract reasonable relative path from (ideally) the project root.
# if `path` is `"/path/to/project/lib/my_file.rb"`,
# it generates `"lib/my_file.rb"` as
#
# ref: rubocop approach
# https://github.com/rubocop/rubocop/blob/1e55b1aa5e4c5eaeccad5d61f08b7930ed6bc341/lib/rubocop/path_util.rb#L25
#
# @param [String] path
# @return [String]
def convert_path(path)
base_path = Dir.pwd

begin
Pathname.new(File.expand_path(path)).relative_path_from(base_path).to_s
rescue ArgumentError
path
end
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
4 changes: 2 additions & 2 deletions script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ fi

echo '::group:: Running rubocop with reviewdog 🐶 ...'
# shellcheck disable=SC2086
${BUNDLE_EXEC}rubocop ${INPUT_RUBOCOP_FLAGS} \
| reviewdog -f=rubocop \
${BUNDLE_EXEC}rubocop ${INPUT_RUBOCOP_FLAGS} --require ${GITHUB_ACTION_PATH}/rdjson_formatter/rdjson_formatter.rb --format RdjsonFormatter \
| reviewdog -f=rdjson \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
Expand Down
9 changes: 9 additions & 0 deletions test/rdjson_formatter/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# Expected to run from the root repository.
set -eux
CWD=$(pwd)
rubocop ./test/rdjson_formatter/testdata/*.rb --require ./rdjson_formatter/rdjson_formatter.rb --format RdjsonFormatter --cache false \
| jq . \
| sed -e "s!${CWD}/!!g" \
> ./test/rdjson_formatter/testdata/result.out
diff -u ./test/rdjson_formatter/testdata/result.*
1 change: 1 addition & 0 deletions test/rdjson_formatter/testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.out
4 changes: 4 additions & 0 deletions test/rdjson_formatter/testdata/correctable_offenses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def add(xyz,abc)
a = xyz+ abc;
return a
end
8 changes: 8 additions & 0 deletions test/rdjson_formatter/testdata/not_correctable_offenses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class MyClass < Object
def no_calc(a, b)
c = a + b
a
end
end
Loading