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, descendants, #elements, #features, #files, #filled_scenarios, #filter_tag, #initialize, #line, #lint_files, #name, #reference, #render_step, #render_step_argument, #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? :tableHeader
      example[:tableHeader][:cells].map { |cell| cell[:value] }.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
29
# 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[:text].include? variable
    next unless step.include? :argument
    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)


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

def used_in_docstring?(variable, step)
  step[:argument][:type] == :DocString && step[:argument][:content].include?(variable)
end

#used_in_table?(variable, step) ⇒ Boolean

Returns:

  • (Boolean)


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

def used_in_table?(variable, step)
  return false unless step[:argument][:type] == :DataTable
  step[:argument][:rows].each do |row|
    row[:cells].each { |value| return true if value[:value].include?(variable) }
  end
  false
end