Class: RuboCop::Cop::RubyLsp::UseLanguageServerAliases

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/ruby_lsp/use_language_server_aliases.rb

Overview

Prefer using ‘Interface`, `Transport` and `Constant` aliases within the `RubyLsp` module, without having to prefix with `LanguageServer::Protocol`

Examples:

# bad
module RubyLsp
  class FoldingRanges
    sig { override.returns(T.all(T::Array[LanguageServer::Protocol::Interface::FoldingRange], Object)) }
    def run; end
  end

# good
module RubyLsp
  class FoldingRanges
    sig { override.returns(T.all(T::Array[Interface::FoldingRange], Object)) }
    def run; end
  end
end

Constant Summary collapse

ALIASED_CONSTANTS =
T.let([:Interface, :Transport, :Constant].freeze, T::Array[Symbol])
MSG =
"Use constant alias `%{constant}`."

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/ruby_lsp/use_language_server_aliases.rb', line 44

def on_new_investigation
  return if processed_source.blank?

  ruby_lsp_modules(processed_source.ast).each do |ruby_lsp_mod|
    lsp_constant_usages(ruby_lsp_mod).each do |node|
      lsp_const = node.children.last

      next unless ALIASED_CONSTANTS.include?(lsp_const)

      add_offense(node, message: format(MSG, constant: lsp_const)) do |corrector|
        corrector.replace(node, lsp_const)
      end
    end
  end
end