Sha256: 707845e5d028d944f77637c5723e2ebb84861548839da286d6fae8f379467faf

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module RuboCop
  module Markdown # :nodoc:
    MARKDOWN_EXTENSIONS = %w[.md .markdown].freeze

    # Merge markdown config into default configuration
    # See https://github.com/backus/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
    def self.inject!
      path = CONFIG_DEFAULT.to_s
      hash = ConfigLoader.send(:load_yaml_configuration, path)
      config = Config.new(hash, path)
      puts "configuration from #{path}" if ConfigLoader.debug?
      config = ConfigLoader.merge_with_default(config, path)
      ConfigLoader.instance_variable_set(:@default_configuration, config)
    end

    def self.markdown_file?(file)
      MARKDOWN_EXTENSIONS.include?(File.extname(file))
    end
  end
end

RuboCop::Markdown.inject!

RuboCop::Runner.prepend(Module.new do
  # Do not cache markdown files, 'cause cache doesn't know about processing.
  # NOTE: we should involve preprocessing in RuboCop::CachedData#deserialize_offenses
  def file_offense_cache(file)
    return yield if RuboCop::Markdown.markdown_file?(file)
    super
  end

  # Run Preprocess.restore if file has been autocorrected
  def process_file(file)
    return super unless @options[:auto_correct] && RuboCop::Markdown.markdown_file?(file)

    offenses = super
    RuboCop::Markdown::Preprocess.restore!(file)

    offenses
  end
end)

# Allow Rubocop to analyze markdown files
RuboCop::TargetFinder.prepend(Module.new do
  def ruby_file?(file)
    super || RuboCop::Markdown.markdown_file?(file)
  end
end)

# Extend ProcessedSource#parse with pre-processing
RuboCop::ProcessedSource.prepend(Module.new do
  def parse(src, *args)
    # only process Markdown files
    src = RuboCop::Markdown::Preprocess.call(src) if
      path.nil? || RuboCop::Markdown.markdown_file?(path)
    super(src, *args)
  end
end)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-md-0.1.0.pre lib/rubocop/markdown/rubocop_ext.rb