class CucumberLint::FeatureEmptyLinesLinter

A linter for a given feature’s empty lines

Public Class Methods

new(config:, feature:, linted_file: super config: config, linted_file: linted_file) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 5
def initialize config:, feature:, linted_file:
  super config: config, linted_file: linted_file

  @feature = feature
  @description = feature.description
end

Public Instance Methods

lint() click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 13
def lint
  lint_description
  lint_elements
end

Private Instance Methods

config_value(key) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 22
def config_value key
  @config.consistent_empty_lines.send(key)
end
element_end(element) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 27
def element_end element
  if element.type == 'scenario_outline'
    element.examples.last.rows.last.line
  else
    step_end element.steps.last
  end
end
element_start(element) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 36
def element_start element
  if element.tags
    element.tags[0].line
  else
    element.line
  end
end
expected_first_element_start() click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 45
def expected_first_element_start
  spacing = if @description == ''
              config_value(:between_feature_and_element)
            else
              @description.count("\n") +
              config_value(:between_description_and_element) +
              1
            end

  @feature.line + spacing + 1
end
extra_empty_line(line_number, count) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 58
def extra_empty_line line_number, count
  if @config.fix
    count.times do |i|
      add_fix line_number + i, -> (line) { line.sub("\n", '') }
    end
  else
    add_error "#{line_number}: Remove#{" #{count}" if count > 1} empty line#{'s' if count > 1}"
  end
end
lint_description() click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 69
def lint_description
  return if @description == ''

  shared_lines = @feature.line + 1
  expected_start = shared_lines + config_value(:between_feature_and_description)
  actual_start = shared_lines + @description.match(/\n*/)[0].length
  resolve_empty_line_difference actual_start, expected_start
end
lint_elements() click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 79
def lint_elements
  return unless @feature.elements

  expected_element_start = expected_first_element_start
  @feature.elements.each do |element|
    resolve_empty_line_difference element_start(element), expected_element_start
    lint_scenario_outline_and_examples element if element.type == 'scenario_outline'
    expected_element_start = element_end(element) + config_value(:between_elements) + 1
  end
end
lint_scenario_outline_and_examples(element) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 91
def lint_scenario_outline_and_examples element
  expected_start = step_end(element.steps.last) +
                   config_value(:between_scenario_outline_and_examples) +
                   1
  actual_start = element.examples.first.line
  resolve_empty_line_difference actual_start, expected_start
end
missing_empty_line(line_number, count) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 100
def missing_empty_line line_number, count
  if @config.fix
    add_fix line_number, -> (line) { "\n" * count + line }
  else
    add_error "#{line_number}: Add#{" #{count}" if count > 1} empty line#{'s' if count > 1}"
  end
end
resolve_empty_line_difference(actual, expected) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 109
def resolve_empty_line_difference actual, expected
  if actual < expected
    missing_empty_line actual, expected - actual
  elsif actual > expected
    extra_empty_line expected, actual - expected
  end
end
step_end(step) click to toggle source
# File lib/cucumber_lint/linter/feature_empty_lines_linter.rb, line 118
def step_end step
  rows = step.rows
  doc_string = step.doc_string

  if rows && rows.is_a?(Array)
    rows.last.line
  elsif doc_string
    doc_string.line + doc_string.value.count("\n") + 2
  else
    step.line
  end
end