lib/swiftformat/plugin.rb in danger-swiftformat-0.8.1 vs lib/swiftformat/plugin.rb in danger-swiftformat-0.9.0

- old
+ new

@@ -32,10 +32,15 @@ # The project Swift version # # @return [String] attr_accessor :swiftversion + # Show issues inline in the diff instead of in a comment. + # + # @return [Boolean] + attr_accessor :inline_mode + # Runs swiftformat # # @param [Boolean] fail_on_error # # @return [void] @@ -54,24 +59,28 @@ results = swiftformat.check_format(swift_files, additional_args, swiftversion) # 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 + 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 + unless additional_message.nil? + message << "\n" << additional_message + end + + markdown message end - markdown message - if fail_on_error fail "SwiftFormat found issues" end end @@ -91,9 +100,27 @@ files .select { |file| file.end_with?(".swift") } .reject { |file| @exclude.any? { |glob| File.fnmatch(glob, file) } } .uniq .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(", ").to_s.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]