Class: RubyLanguageServer::GoodCop

Inherits:
RuboCop::Runner
  • Object
show all
Defined in:
lib/ruby_language_server/good_cop.rb

Instance Method Summary collapse

Constructor Details

#initializeGoodCop

Returns a new instance of GoodCop.



7
8
9
10
11
12
13
14
15
# File 'lib/ruby_language_server/good_cop.rb', line 7

def initialize
  @initialization_error = nil
  config_store = RuboCop::ConfigStore.new
  config_store.options_config = config_path
  super({}, config_store)
rescue Exception => e
  RubyLanguageServer.logger.error(e)
  @initialization_error = "There was an issue loading the rubocop configuration file: #{e}.  Maybe you need to add some additional gems to the ide-ruby settings?"
end

Instance Method Details

#diagnostic_severity_for(severity) ⇒ Object

interface Diagnostic

/**
 * The range at which the message applies.
 */
range: Range;

/**
 * The diagnostic's severity. Can be omitted. If omitted it is up to the
 * client to interpret diagnostics as error, warning, info or hint.
 */
severity?: number;

/**
 * The diagnostic's code. Can be omitted.
 */
code?: number | string;

/**
 * A human-readable string describing the source of this
 * diagnostic, e.g. 'typescript' or 'super lint'.
 */
source?: string;

/**
 * The diagnostic's message.
 */
message: string;



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_language_server/good_cop.rb', line 65

def diagnostic_severity_for(severity)
  case severity.to_s
  when 'error', 'fatal'
    1
  when 'warning'
    2
  when 'refactor', 'convention'
    3
  else
    RubyLanguageServer.logger.error("Could not map severity for #{severity} - returning 2")
    2
  end
end

#diagnostics(text, filename = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_language_server/good_cop.rb', line 79

def diagnostics(text, filename = nil)
  return initialization_offenses unless @initialization_error.nil?

  maximum_severity = (ENV['LINT_LEVEL'] || 4).to_i
  enabled_offenses = offenses(text, filename).reject { |offense| offense.status == :disabled }
  enabled_offenses.map do |offense|
    {
      range: Location.position_hash(offense.location.line, offense.location.column, offense.location.last_line, offense.location.last_column),
      severity: diagnostic_severity_for(offense.severity),
      # code?: number | string;
      code: 'code',
      source: "RuboCop:#{offense.cop_name}",
      message: offense.message
    }
  end.select { |hash| hash[:severity] <= maximum_severity }
end