diff --git a/rubocop/cop/gemfile/missing_feature_category.rb b/rubocop/cop/gemfile/missing_feature_category.rb index 323900efd8453248642e947a8a410be3229d4f65..18e29fc30b2eae23758b30d9c806e78f0818f835 100644 --- a/rubocop/cop/gemfile/missing_feature_category.rb +++ b/rubocop/cop/gemfile/missing_feature_category.rb @@ -38,19 +38,11 @@ def on_send(node) value_node = feature_category_value(node) - if value_node - feature_categories.check( - value_node: value_node, - document_link: DOCUMENT_LINK - ) do |message| - add_offense(value_node, message: message) - end - else - message = format( - FeatureCategories::MSG, - msg_suggestion: nil, - document_link: DOCUMENT_LINK) - add_offense(node, message: message) + feature_categories.check( + value_node: value_node, + document_link: DOCUMENT_LINK + ) do |message| + add_offense(value_node || node, message: message) end end diff --git a/rubocop/feature_categories.rb b/rubocop/feature_categories.rb index 3aa8a50839ca27e357b0071f76e7634ba8eae798..5e02d974e7b10448a9b54e7059ebdf50db1d8015 100644 --- a/rubocop/feature_categories.rb +++ b/rubocop/feature_categories.rb @@ -47,21 +47,27 @@ def initialize(categories) end def check(value_node:, document_link:) - return yield(MSG_SYMBOL) unless value_node.sym_type? - return if categories.include?(value_node.value.to_s) + if value_node + if !value_node.sym_type? + yield MSG_SYMBOL + elsif !categories.include?(value_node.value.to_s) # rubocop:disable Rails/NegateInclude + yield format_message(value_node.value, document_link: document_link) + end + else + yield format_message(nil, document_link: document_link) + end + end - message = format( - MSG, - msg_suggestion: suggestion_message(value_node), - document_link: document_link) + private - yield(message) + def format_message(value, document_link:) + format(MSG, msg_suggestion: suggestion_message(value), document_link: document_link) end - def suggestion_message(value_node) + def suggestion_message(value) spell = DidYouMean::SpellChecker.new(dictionary: categories) - suggestions = spell.correct(value_node.value) + suggestions = spell.correct(value) return if suggestions.none? format(MSG_DID_YOU_MEAN, suggestion: suggestions.first) diff --git a/spec/rubocop/feature_categories_spec.rb b/spec/rubocop/feature_categories_spec.rb index 85d1f4f8aff29b661180fb3e1e9f47ab82ed81f8..ffe7ade82e2cb7607cfd8f395a756d9d3763dae3 100644 --- a/spec/rubocop/feature_categories_spec.rb +++ b/spec/rubocop/feature_categories_spec.rb @@ -45,6 +45,16 @@ def check end end + context 'when value_node is nil' do + let(:value_node) { nil } + + it 'yields a message asking for a feature category with document link only' do + check.to yield_with_args(<<~MARKDOWN.chomp) + Please use a valid feature category. See https://example.com + MARKDOWN + end + end + context 'when value_node is not a symbol node' do before do allow(value_node).to receive(:sym_type?).and_return(false) @@ -55,7 +65,7 @@ def check end end - context 'when categories contain the value the value_node has' do + context 'when category is found' do before do allow(value_node).to receive(:value).and_return(categories.first) end @@ -65,40 +75,27 @@ def check end end - context 'when categories do not contain the value the value_node has' do + context 'when a similar category is found' do before do allow(value_node).to receive(:value).and_return('invalid_category') end - it 'yields a message asking for a feature category with document link' do + it 'yields a message asking for a feature category with suggestion and document link' do check.to yield_with_args(<<~MARKDOWN.chomp) Please use a valid feature category. Did you mean `:valid_category`? See https://example.com MARKDOWN end end - end - describe '#suggestion_message' do - let(:value_node) { instance_double(RuboCop::AST::SymbolNode) } - - context 'when categories do not contain the value the value_node has' do + context 'when no similar category is found' do before do - allow(value_node).to receive(:value).and_return('invalid_category') - end - - it 'returns a message suggesting a similar category name' do - expect(feature_categories.suggestion_message(value_node)) - .to eq('Did you mean `:valid_category`? ') + allow(value_node).to receive(:value).and_return('something_completely_different') end - context 'when the value the value_node has is too different' do - before do - allow(value_node).to receive(:value).and_return('GitLab') - end - - it 'returns nil' do - expect(feature_categories.suggestion_message(value_node)).to be_nil - end + it 'yields a message asking for a feature category with document link only' do + check.to yield_with_args(<<~MARKDOWN.chomp) + Please use a valid feature category. See https://example.com + MARKDOWN end end end