Sha256: e4d1454b5e72d1623f06cd18ffc5485e2f8680353b96d78194c5ef78f9675927

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require_relative 'utils'

class Danger::DangerWCC < Danger::Plugin
  class Jshint
    include Utils

    DEFAULT_OPTIONS = {
      directory: 'app/assets/javascripts'
    }.freeze

    def initialize(plugin, options = {})
      @plugin = plugin
      @options = DEFAULT_OPTIONS.merge(options)
    end

    def perform
      return unless Dir.exist?(@options[:directory])

      run('PREFIX=/tmp npm install -g jshint')
      diff = run_jshint_diff

      # run jshint again to get line numbers
      jshint_lines = run '/tmp/lib/node_modules/jshint/bin/jshint '\
        "#{@options[:directory]}"
      jshint_lines = jshint_lines.lines

      each_addition_in_diff(diff) do |line|
        add_jshint_warning(jshint_lines[line.line_number.right - 1])
      end
    end

    private

    def run_jshint_diff
      diff =
        run_and_diff do
          write_jshintrc
          run('/tmp/lib/node_modules/jshint/bin/jshint '\
            "#{@options[:directory]} | sed 's/line [0-9]*, col [0-9]*,//g'")
        end
      GitDiff.from_string(diff)
    end

    def write_jshintrc
      return if File.exist?('.jshintrc')

      default = File.join(File.dirname(__FILE__), 'default.jshintrc')
      FileUtils.cp(default, './.jshintrc')
    end

    def add_jshint_warning(with_line_number)
      return unless warning = with_line_number.match(
        /^([^\:]+)\:\s+line\s+(\d+),\s+col\s+\d+,\s+(.+)$/i
      )

      plugin.warn(warning.captures[2],
                  file: warning.captures[0],
                  line: warning.captures[1].to_i)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
danger-wcc-0.1.4 lib/wcc/jshint.rb
danger-wcc-0.1.3 lib/wcc/jshint.rb
danger-wcc-0.1.2 lib/wcc/jshint.rb
danger-wcc-0.1.1 lib/wcc/jshint.rb
danger-wcc-0.1.0 lib/wcc/jshint.rb
danger-wcc-0.0.6 lib/wcc/jshint.rb