Class: Chutney::UnusedVariable

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

Overview

service class to lint for unused variables

Instance Attribute Summary

Attributes inherited from Linter

#configuration, #filename, #issues

Instance Method Summary collapse

Methods inherited from Linter

#add_issue, #and_word?, #background, #background_word?, #but_word?, descendants, #dialect, #dialect_word, #elements, #examples_word?, #feature, #feature_word?, #filled_scenarios, #given_word?, #initialize, linter_name, #linter_name, #location, #off_switch?, #render_step, #render_step_argument, #scenario_outline_word?, #scenarios, #steps, #tags_for, #then_word?, #type, #when_word?

Constructor Details

This class inherits a constructor from Chutney::Linter

Instance Method Details

#lintObject



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

def lint
  scenarios do |feature, scenario|
    next unless scenario.is_a? CukeModeler::Outline

    scenario.examples.each do |example|
      example.rows.first.cells.map(&:value).each do |variable|
        next if used?(variable, scenario)

        add_issue(I18n.t('linters.unused_variable', variable:), feature, scenario, example)
      end
    end
  end
end

#used?(variable, scenario) ⇒ Boolean

Returns:

  • (Boolean)


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

def used?(variable, scenario)
  variable = "<#{variable}>"

  scenario.steps.each do |step|
    return true if step.text.include? variable
    next unless step.block
    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)


32
33
34
# File 'lib/chutney/linter/unused_variable.rb', line 32

def used_in_docstring?(variable, step)
  step.block.is_a?(CukeModeler::DocString) && step.block.content.include?(variable)
end

#used_in_table?(variable, step) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/chutney/linter/unused_variable.rb', line 36

def used_in_table?(variable, step)
  return false unless step.block.is_a?(CukeModeler::Table)

  step.block.rows.each do |row|
    row.cells.each { |cell| return true if cell.value.include?(variable) }
  end
  false
end