Skip to content

Commit

Permalink
Report formatting issues with inline comments (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
pahnev authored Nov 10, 2023
1 parent 7498482 commit 6ed4665
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ certain directories or files such as `Pods`, you can use the `exclude` parameter
swiftformat.exclude = %w(Pods/** Carthage/** Sources/Nope.swift **/*_autogenerated.swift)
```

If you want the format results shows in the diff instead of a comment, you can use inline_mode option. Violations that are out of the diff, will be shown in danger's fail or warn section.
```ruby
swiftformat.inline_mode = true
```

The `exclude` option takes an array of glob patterns; you can find additional documentation on the patterns
[here](https://ruby-doc.org/core-2.6.3/File.html#method-c-fnmatch).

Expand Down
48 changes: 35 additions & 13 deletions lib/swiftformat/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DangerSwiftformat < Plugin
#
# @return [void]
#
def check_format(fail_on_error: false)
def check_format(fail_on_error: false, inline_mode: false)
# Check if SwiftFormat is installed
raise "Could not find SwiftFormat executable" unless swiftformat.installed?

Expand All @@ -56,20 +56,24 @@ def check_format(fail_on_error: false)
# Stop processing if the errors array is empty
return if results[:errors].empty?

# Process the errors
message = "### SwiftFormat found issues:\n\n"
message << "| File | Rules |\n"
message << "| ---- | ----- |\n"
results[:errors].each do |error|
message << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n"
end

unless additional_message.nil?
message << "\n" << additional_message
if inline_mode
send_inline_comment(results, fail_on_error ? :fail : :warn)
else
# Process the errors
message = "### SwiftFormat found issues:\n\n"
message << "| File | Rules |\n"
message << "| ---- | ----- |\n"
results[:errors].each do |error|
message << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n"
end

unless additional_message.nil?
message << "\n" << additional_message
end

markdown message
end

markdown message

if fail_on_error
fail "SwiftFormat found issues"
end
Expand All @@ -95,6 +99,24 @@ def find_swift_files
.sort
end

# Send inline comment with danger's warn or fail method
#
# @return [void]
def send_inline_comment(results, method)
results[:errors].each do |error|
file = error[:file]
file_components = file.split(':')
line = file_components[1]
filename = file_components.first.split('/').last
file_path = file_components.first

message = "#{error[:rules].join(', ')}".dup
message << " `#{filename}:#{line}`" # file:line for pasting into Xcode Quick Open

send(method, message, file: file_path, line: line)
end
end

# Constructs the SwiftFormat class
#
# @return [SwiftFormat]
Expand Down

0 comments on commit 6ed4665

Please sign in to comment.