Skip to content

Instantly share code, notes, and snippets.

@ixti
Last active September 27, 2018 19:03
Show Gist options
  • Save ixti/6515075a4a6f047c6f8b85e8fd3ad7a3 to your computer and use it in GitHub Desktop.
Save ixti/6515075a4a6f047c6f8b85e8fd3ad7a3 to your computer and use it in GitHub Desktop.
module AutocorrectThruDisableCuzWeSuckAtWritingGoodCode
def autocorrect(node)
->(corrector) do
until autocorrect_on.include? node&.type
node = node&.parent
return unless node
end
lines = node.source.lines
match = lines[0].match(/^(.*?)\s*((?<!\\)#(.*))?$/)
lines[0] = "#{match[1]} #{build_disable match[3].to_s}\n"
corrected = lines.join
corrector.replace(node.loc.expression, corrected)
end
end
private
def build_disable(original)
original = original.strip
disables = [name]
if original.gsub!(/^\s*rubocop\s*:\s*disable/, "")
disables.concat original.split(",").map(&:strip).reject(&:empty?)
disables.sort!
disables.uniq!
end
"# rubocop:disable #{disables.join ', '}"
end
##############################################################################
{
::RuboCop::Cop::Metrics::AbcSize => %i[def defs class module],
::RuboCop::Cop::Metrics::ClassLength => %i[class],
::RuboCop::Cop::Metrics::CyclomaticComplexity => %i[def defs class module],
::RuboCop::Cop::Metrics::MethodLength => %i[def defs],
::RuboCop::Cop::Metrics::ModuleLength => %i[module],
::RuboCop::Cop::Metrics::ParameterLists => %i[def defs],
::RuboCop::Cop::Metrics::PerceivedComplexity => %i[def defs class module]
}.each do |cop, autocorrect_on|
cop.send(:include, self)
cop.send(:define_method, :autocorrect_on, -> { autocorrect_on })
end
end
@ixti
Copy link
Author

ixti commented Sep 25, 2018

USAGE

  1. save the file to your repo
  2. rubocop --require ./auto_correct_thru_inline_disable.rb --auto-correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment