Sha256: abf1401c3b0f64798b3227761782d82929ba6593aa3bcbfee7f9ceb2ecb1cc53

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 KB

Contents

require 'gherkin_lint/linter'

module GherkinLint
  # service class to lint for unused variables
  class UnusedVariable < Linter
    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

    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

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

    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
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
gherkin_lint-1.2.2 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-1.2.1 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-1.1.0 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-1.0.0 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.6.3 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.6.2 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.6.1 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.6.0 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.5.0 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.4.4 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.4.3 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.4.2 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.4.1 lib/gherkin_lint/linter/unused_variable.rb
gherkin_lint-0.4.0 lib/gherkin_lint/linter/unused_variable.rb