class RuboCop::Cop::RSpec::ExampleWording
end
it ‘does things’ do
# good
end
it ‘it does things’ do
# bad
@example
end
it ‘finds nothing’ do
# good
end
it ‘should find nothing’ do
# bad
@example
with CustomTransform (e.g. have => has) and IgnoredWords (e.g. only).
The autocorrect is experimental - use with care! It can be configured
@see betterspecs.org/#should<br><br>This cop will correct docstrings that begin with ‘should’ and ‘it’.
Checks for common mistakes in example descriptions.
def add_wording_offense(node, message)
def add_wording_offense(node, message) docstring = docstring(node) add_offense(docstring, message: message) do |corrector| next if node.heredoc? corrector.replace(docstring, replacement_text(node)) end end
def custom_transform
def custom_transform cop_config.fetch('CustomTransform', {}) end
def docstring(node)
def docstring(node) expr = node.loc.expression Parser::Source::Range.new( expr.source_buffer, expr.begin_pos + 1, expr.end_pos - 1 ) end
def ignored_words
def ignored_words cop_config.fetch('IgnoredWords', []) end
def on_block(node)
def on_block(node) it_description(node) do |description_node, message| if message.match?(SHOULD_PREFIX) add_wording_offense(description_node, MSG_SHOULD) elsif message.match?(IT_PREFIX) add_wording_offense(description_node, MSG_IT) end end end
def replacement_text(node)
def replacement_text(node) text = text(node) if text.match?(SHOULD_PREFIX) RuboCop::RSpec::Wording.new( text, ignore: ignored_words, replace: custom_transform ).rewrite else text.sub(IT_PREFIX, '') end end
def text(node)
Recursive processing is required to process nested dstr nodes
def text(node) case node.type when :dstr node.node_parts.map { |child_node| text(child_node) }.join when :str node.value when :begin node.source end end