class Solargraph::Diagnostics::TypeCheck

TypeCheck reports methods with undefined return types, untagged parameters, and invalid param tags.

Public Instance Methods

diagnose(source, api_map) click to toggle source
# File lib/solargraph/diagnostics/type_check.rb, line 9
def diagnose source, api_map
  return [] unless args.include?('always') || api_map.workspaced?(source.filename)
  severity = Diagnostics::Severities::ERROR
  level = (args.reverse.find { |a| ['normal', 'typed', 'strict', 'strong'].include?(a) }) || :normal
  checker = Solargraph::TypeChecker.new(source.filename, api_map: api_map, level: level.to_sym)
  checker.problems
    .sort { |a, b| a.location.range.start.line <=> b.location.range.start.line }
    .map do |problem|
      {
        range: extract_first_line(problem.location, source),
        severity: severity,
        source: 'Typecheck',
        message: problem.message
      }
    end
end

Private Instance Methods

extract_first_line(location, source) click to toggle source

@param location [Location] @param source [Source] @return [Hash]

# File lib/solargraph/diagnostics/type_check.rb, line 31
def extract_first_line location, source
  return location.range.to_hash if location.range.start.line == location.range.ending.line
  {
    start: {
      line: location.range.start.line,
      character: location.range.start.character
    },
    end: {
      line: location.range.start.line,
      character: last_character(location.range.start, source)
    }
  }
end
last_character(position, source) click to toggle source

@param position [Solargraph::Position] @param source [Solargraph::Source] @return [Integer]

# File lib/solargraph/diagnostics/type_check.rb, line 48
def last_character position, source
  cursor = Position.to_offset(source.code, position)
  source.code.index(/[\r\n]/, cursor) || source.code.length
end