Skip to content
代码片段 群组 项目
未验证 提交 478bcaec 编辑于 作者: Peter Leitzen's avatar Peter Leitzen
浏览文件

Simplify implementation of Gemfile/MissingFeatureCategory

* Allow FeatureCategories#check to accept nil value nodes
* Make FeatureCategories#suggestion_message private again
上级 11b759d7
No related branches found
No related tags found
无相关合并请求
......@@ -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
......
......@@ -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)
......
......@@ -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
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册