Module: RuboCop::Version

Defined in:
lib/rubocop/version.rb

Overview

This module holds the RuboCop version information.

Constant Summary collapse

STRING =
'1.93.0'
MSG =
'%<version>s (using %<parser_version>s, ' \
'rubocop-ast %<rubocop_ast_version>s, ' \
'analyzing as Ruby %<target_ruby_version>s, ' \
'running on %<ruby_engine>s %<ruby_version>s)' \
'%<rubydex_indicator>s%<server_mode>s [%<ruby_platform>s]'
MINIMUM_PARSABLE_PRISM_VERSION =
3.3
CANONICAL_FEATURE_NAMES =
{
  'Rspec' => 'RSpec', 'Graphql' => 'GraphQL', 'Md' => 'Markdown', 'Factory_bot' => 'FactoryBot',
  'Thread_safety' => 'ThreadSafety', 'Rspec_rails' => 'RSpecRails'
}.freeze
EXTENSION_PATH_NAMES =
{
  'rubocop-md' => 'markdown', 'rubocop-factory_bot' => 'factory_bot'
}.freeze

Class Method Summary collapse

Class Method Details

.config_for_pwd(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



131
132
133
134
135
136
137
# File 'lib/rubocop/version.rb', line 131

def self.config_for_pwd(env)
  Util.silence_warnings do
    # Suppress any config issues when loading the config (ie. deprecations,
    # pending cops, etc.).
    env.config_store.unvalidated.for_pwd
  end
end

.document_version ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



161
162
163
# File 'lib/rubocop/version.rb', line 161

def self.document_version
  STRING.match('\d+\.\d+').to_s
end

.extension_versions(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubocop/version.rb', line 85

def self.extension_versions(env)
  plugins = config_for_pwd(env).loaded_plugins
  plugin_versions = plugins.filter_map do |plugin|
    next if Plugin::BUILTIN_INTERNAL_PLUGINS.key?(plugin.about.name)
    next unless (plugin_name = plugin.about.name)

    "  - #{plugin_name} #{plugin.about.version}"
  end

  # TODO: It needs to be maintained for a while to ensure compatibility with extensions that
  # don't support plugins. It should be removed in future once the old style becomes obsolete.
  features = config_for_pwd(env).loaded_features.sort
  features -= plugins.map { |plugin| plugin.about.name }
  feature_versions = features.filter_map do |loaded_feature|
    next unless (match = loaded_feature.match(/rubocop-(?<feature>.*)/))

    # Get the expected name of the folder containing the extension code.
    # Usually it would be the same as the extension name. but sometimes authors
    # can choose slightly different name for their gems, e.g. rubocop-md instead of
    # rubocop-markdown.
    feature = EXTENSION_PATH_NAMES.fetch(loaded_feature, match[:feature])

    begin
      require "rubocop/#{feature}/version"
    rescue LoadError
      # Not worth mentioning libs that are not installed
    end

    next unless (feature_version = feature_version(feature))

    "  - #{loaded_feature} #{feature_version}"
  end

  plugin_versions + feature_versions
end

.feature_version(feature) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns feature version in one of two ways:

  • Find by RuboCop core version style (e.g. rubocop-performance, rubocop-rspec)
  • Find by bundle gem version style (e.g. rubocop-rake)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rubocop/version.rb', line 145

def self.feature_version(feature)
  capitalized_feature = feature.capitalize
  extension_name = CANONICAL_FEATURE_NAMES.fetch(capitalized_feature, capitalized_feature)

  # Find by RuboCop core version style (e.g. rubocop-performance, rubocop-rspec)
  RuboCop.const_get(extension_name)::Version::STRING
rescue NameError
  begin
    # Find by `bundle gem` version style (e.g. rubocop-rake, rubocop-packaging)
    RuboCop.const_get(extension_name)::VERSION
  rescue NameError
    # noop
  end
end

.parser_engine(configured, target_ruby_version) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default parser engine depends on the target Ruby version and on the version of Prism, so the one that is used is asked from RuboCop AST.



77
78
79
80
81
# File 'lib/rubocop/version.rb', line 77

def self.parser_engine(configured, target_ruby_version)
  return configured unless configured.nil? || configured.to_s == 'default'

  ProcessedSource.default_parser_engine(target_ruby_version)
end

.parser_version(target_ruby_version) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/version.rb', line 58

def self.parser_version(target_ruby_version)
  config_path = ConfigFinder.find_config_path(PathUtil.pwd)
  yaml = Util.silence_warnings do
    ConfigLoader.load_yaml_configuration(config_path)
  end
  # Without the `parser` gem its version is that of the copy of its source
  # layer in RuboCop AST, which is not what parses the code.
  parser = "Parser #{Parser::VERSION}" if ProcessedSource.parser_gem_available?
  prism = "Prism #{Prism::VERSION}"
  return parser || prism if target_ruby_version < MINIMUM_PARSABLE_PRISM_VERSION

  parser_engine = parser_engine(yaml.dig('AllCops', 'ParserEngine'), target_ruby_version)

  [parser, prism, parser_engine].compact.join(', ')
end

.rubydex_enabled?(env) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
# File 'lib/rubocop/version.rb', line 171

def self.rubydex_enabled?(env)
  config_for_pwd(env).for_all_cops['UseProjectIndex']
rescue StandardError
  # Keep `rubocop -V` usable when the config cannot be loaded (broken YAML,
  # missing `inherit_from` target, etc). It is the first command users run to
  # diagnose a broken setup, so the indicator just hides rather than letting
  # the version banner crash.
  false
end

.rubydex_indicator(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



166
167
168
# File 'lib/rubocop/version.rb', line 166

def self.rubydex_indicator(env)
  env && rubydex_enabled?(env) && ProjectIndexLoader.available? ? ' +Rubydex' : ''
end

.server_mode ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



182
183
184
# File 'lib/rubocop/version.rb', line 182

def self.server_mode
  RuboCop.const_defined?(:Server) && Server.running? ? ' +server' : ''
end

.target_ruby_version(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
125
126
127
128
# File 'lib/rubocop/version.rb', line 122

def self.target_ruby_version(env)
  if env
    config_for_pwd(env).target_ruby_version
  else
    TargetRuby.new(Config.new).version
  end
end

.verbose(env: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
# File 'lib/rubocop/version.rb', line 53

def self.verbose(env: nil)
  version(debug: true, env: env)
end

.version(debug: false, env: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

NOTE: Marked as private but used by gems like standard. rubocop:disable-next Metrics/MethodLength



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/version.rb', line 27

def self.version(debug: false, env: nil)
  if debug
    target_ruby_version = target_ruby_version(env)
    verbose_version = format(MSG, version: STRING,
                                  parser_version: parser_version(target_ruby_version),
                                  rubocop_ast_version: RuboCop::AST::Version::STRING,
                                  target_ruby_version: target_ruby_version,
                                  ruby_engine: RUBY_ENGINE, ruby_version: RUBY_VERSION,
                                  rubydex_indicator: rubydex_indicator(env),
                                  server_mode: server_mode,
                                  ruby_platform: RUBY_PLATFORM)
    return verbose_version unless env

    extension_versions = extension_versions(env)
    return verbose_version if extension_versions.empty?

    <<~VERSIONS
      #{verbose_version}
      #{extension_versions.join("\n")}
    VERSIONS
  else
    STRING
  end
end