Sha256: 103b320211b173fc8ce6587750abc91d4ee361893762e658b5fefca6c261fd97

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 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

4 entries across 4 versions & 1 rubygems

Version Path
danger-wcc-0.0.5 lib/wcc/jshint.rb
danger-wcc-0.0.4 lib/wcc/jshint.rb
danger-wcc-0.0.3 lib/wcc/jshint.rb
danger-wcc-0.0.2 lib/wcc/jshint.rb