class Gitlab::Styles::Rubocop::Cop::RSpec::ExampleStartingCharacter
Checks for common mistakes in example descriptions.
This cop will correct docstrings that begin/end with space or words that start with a capital letter.
@see gitlab.com/gitlab-org/gitlab/-/merge_requests/46336#note_442669518
@example
# bad it 'Does something' do end # good it 'does nothing' do end
@example
# bad it ' does something' do end # good it 'does something' do end
@example
# bad it 'does something ' do end # good it 'does something' do end
@example
# bad it ' does something ' do end # good it 'does something' do end
Constants
- MSG
Public Instance Methods
on_block(node)
click to toggle source
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 64 def on_block(node) it_description(node) do |description_node, _message| add_wording_offense(description_node, MSG) if invalid_description?(text(description_node)) end end
Private Instance Methods
add_wording_offense(node, message)
click to toggle source
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 72 def add_wording_offense(node, message) docstring = docstring(node) add_offense(docstring, message: message) do |corrector| corrector.replace(docstring, replacement_text(node)) end end
docstring(node)
click to toggle source
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 79 def docstring(node) expr = node.loc.expression Parser::Source::Range.new( expr.source_buffer, expr.begin_pos + 1, expr.end_pos - 1 ) end
downcase_first_letter(str)
click to toggle source
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 116 def downcase_first_letter(str) str[0].downcase + str[1..] end
invalid_description?(message)
click to toggle source
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 89 def invalid_description?(message) message.match?(/(^([A-Z]{1}[a-z]+\s|\s)|\s$)/) end
replacement_text(node)
click to toggle source
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 93 def replacement_text(node) text = text(node) text.strip! text = downcase_first_letter(text) if invalid_description?(text) text end
text(node)
click to toggle source
Recursive processing is required to process nested dstr nodes that is the case for -separated multiline strings with interpolation.
# File lib/gitlab/styles/rubocop/cop/rspec/example_starting_character.rb, line 105 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