Class: GherkinLint::UnusedVariable

Inherits:
Linter
  • Object
show all
Defined in:
lib/gherkin_lint/linter/unused_variable.rb

Overview

service class to lint for unused variables

Instance Attribute Summary

Attributes inherited from Linter

#issues

Instance Method Summary collapse

Methods inherited from Linter

#add_error, #add_warning, #backgrounds, #elements, #features, #files, #filled_scenarios, #filter_tag, #gather_tags, #initialize, #line, #lint_files, #name, #reference, #render_step, #scenarios, #steps, #suppress, #suppress_tags, #tag?

Constructor Details

This class inherits a constructor from GherkinLint::Linter

Instance Method Details

#lintObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gherkin_lint/linter/unused_variable.rb', line 6

def lint
  scenarios do |file, feature, scenario|
    next unless scenario.key? 'examples'
    scenario['examples'].each do |example|
      next unless example.key? 'rows'
      example['rows'].first['cells'].each do |variable|
        references = [reference(file, feature, scenario)]
        add_error(references, "'<#{variable}>' is unused") unless used?(variable, scenario)
      end
    end
  end
end

#used?(variable, scenario) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
# File 'lib/gherkin_lint/linter/unused_variable.rb', line 19

def used?(variable, scenario)
  variable = "<#{variable}>"
  return false unless scenario.key? 'steps'
  scenario['steps'].each do |step|
    return true if step['name'].include? variable
    return true if used_in_docstring?(variable, step)
    return true if used_in_table?(variable, step)
  end
  false
end

#used_in_docstring?(variable, step) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/gherkin_lint/linter/unused_variable.rb', line 30

def used_in_docstring?(variable, step)
  step.key?('doc_string') && step['doc_string']['value'].include?(variable)
end

#used_in_table?(variable, step) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/gherkin_lint/linter/unused_variable.rb', line 34

def used_in_table?(variable, step)
  return false unless step.key? 'rows'
  step['rows'].each do |row|
    row['cells'].each { |value| return true if value.include?(variable) }
  end
  false
end