# 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