diff --git a/lib/rubocop/ast.rb b/lib/rubocop/ast.rb index 49c84a08e..79326b917 100644 --- a/lib/rubocop/ast.rb +++ b/lib/rubocop/ast.rb @@ -63,6 +63,7 @@ require_relative 'ast/node/yield_node' require_relative 'ast/builder' require_relative 'ast/processed_source' +require_relative 'ast/rubocop_compatibility' require_relative 'ast/token' require_relative 'ast/traversal' require_relative 'ast/version' diff --git a/lib/rubocop/ast/rubocop_compatibility.rb b/lib/rubocop/ast/rubocop_compatibility.rb new file mode 100644 index 000000000..2ece4518c --- /dev/null +++ b/lib/rubocop/ast/rubocop_compatibility.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module RuboCop + # ... + module AST + # Responsible for compatibility with main gem + # @api private + module RuboCopCompatibility + INCOMPATIBLE_COPS = { + '0.89.0' => 'Layout/LineLength' + }.freeze + def rubocop_loaded + loaded = Gem::Version.new(RuboCop::Version::STRING) + incompatible = INCOMPATIBLE_COPS.select do |k, _v| + loaded < Gem::Version.new(k) + end.values + return if incompatible.empty? + + warn <<~WARNING + *** WARNING – Incompatible versions of `rubocop` and `rubocop-ast` + You may encounter issues with the following \ + Cop#{'s' if incompatible.size > 1}: #{incompatible.join(', ')} + Please upgrade rubocop to at least v#{INCOMPATIBLE_COPS.keys.last} + WARNING + end + end + + extend RuboCopCompatibility + end +end diff --git a/spec/rubocop/ast/rubocop_compatibility_spec.rb b/spec/rubocop/ast/rubocop_compatibility_spec.rb new file mode 100644 index 000000000..7600f981d --- /dev/null +++ b/spec/rubocop/ast/rubocop_compatibility_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::AST::RuboCopCompatibility do + subject(:callback) { RuboCop::AST.rubocop_loaded } + before do + stub_const '::RuboCop::Version::STRING', rubocop_version + end + + context 'when ran from an incompatible version of Rubocop' do + let(:rubocop_version) { '0.42.0' } + + it 'issues a warning' do + expect { callback }.to output(/LineLength/).to_stderr + end + end + + context 'when ran from a compatible version of Rubocop' do + let(:rubocop_version) { '0.89.0' } + + it 'issues a warning' do + expect { callback }.not_to output.to_stderr + end + end +end