class Chutney::Linter

base class for all linters

Constants

Lint

Attributes

configuration[R]
filename[R]
issues[RW]

Public Class Methods

descendants() click to toggle source
# File lib/chutney/linter.rb, line 13
def self.descendants
  ObjectSpace.each_object(::Class).select { |klass| klass < self }
end
linter_name() click to toggle source
# File lib/chutney/linter.rb, line 184
def self.linter_name
  name.split('::').last
end
new(filename, content, configuration) click to toggle source
# File lib/chutney/linter.rb, line 17
def initialize(filename, content, configuration)
  @content = content
  @filename = filename
  @issues = []
  @configuration = configuration
end

Public Instance Methods

add_issue(message, feature = nil, scenario = nil, item = nil) click to toggle source
# File lib/chutney/linter.rb, line 76
def add_issue(message, feature = nil, scenario = nil, item = nil)
  issues << Lint.new(
    message: message,
    gherkin_type: type(feature, scenario, item),
    location: location(feature, scenario, item),
    feature: feature&.name,
    scenario: scenario&.name,
    step: item&.parsing_data&.dig(:name)
  ).to_h
end
and_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 28
def and_word?(word)
  dialect_word(:and).include?(word)
end
background() { |feature, background| ... } click to toggle source
# File lib/chutney/linter.rb, line 138
def background
  return unless feature&.background
  return if off_switch?(feature)

  if block_given?
    yield(feature, feature.background)
  else
    feature.background
  end
end
background_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 32
def background_word?(word)
  dialect_word(:background).include?(word)
end
but_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 36
def but_word?(word)
  dialect_word(:but).include?(word)
end
dialect() click to toggle source
# File lib/chutney/linter.rb, line 68
def dialect
  @content.feature&.parsing_data&.dig(:language) || 'en'
end
dialect_word(word) click to toggle source
# File lib/chutney/linter.rb, line 64
def dialect_word(word)
  CukeModeler::Parsing.dialects[dialect][word.to_s].map(&:strip)
end
elements() { |feature, child| ... } click to toggle source
# File lib/chutney/linter.rb, line 113
def elements
  return [] unless feature

  if block_given?
    feature.children.each do |child|
      next if off_switch?(child)

      yield(feature, child)
    end
  else
    feature.children
  end
end
examples_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 40
def examples_word?(word)
  dialect_word(:examples).include?(word)
end
feature() { |feature| ... } click to toggle source
# File lib/chutney/linter.rb, line 105
def feature
  if block_given?
    yield(@content.feature) if @content.feature
  else
    @content.feature
  end
end
feature_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 44
def feature_word?(word)
  dialect_word(:feature).include?(word)
end
filled_scenarios() { |feature, scenario| ... } click to toggle source
# File lib/chutney/linter.rb, line 162
def filled_scenarios
  if block_given?
    scenarios do |feature, scenario|
      next if scenario.steps.empty?

      yield(feature, scenario)
    end
  else
    scenarios ? scenarios.filter { |s| !s.steps.empty? } : []
  end
end
given_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 48
def given_word?(word)
  dialect_word(:given).include?(word)
end
lint() click to toggle source
# File lib/chutney/linter.rb, line 24
def lint
  raise 'not implemented'
end
linter_name() click to toggle source
# File lib/chutney/linter.rb, line 188
def linter_name
  self.class.linter_name
end
location(feature, scenario, step) click to toggle source
# File lib/chutney/linter.rb, line 87
def location(feature, scenario, step)
  if step
    step.parsing_data[:location]
  elsif scenario
    scenario.parsing_data.dig(:scenario, :location) || scenario.parsing_data.dig(:background, :location)
  else
    feature ? feature.parsing_data[:location] : { line: 0, column: 0 }
  end
end
off_switch?(element = feature) click to toggle source
# File lib/chutney/linter.rb, line 127
def off_switch?(element = feature)
  off_switch = element.tags
                      .map(&:name)
                      .then { |tags| tags || [] }
                      .filter { |tag_name| tag_name == "@disable#{linter_name}" }
                      .count
                      .positive?
  off_switch ||= off_switch?(feature) unless element == feature
  off_switch
end
render_step(step) click to toggle source
# File lib/chutney/linter.rb, line 192
def render_step(step)
  value = "#{step.keyword} #{step.text}"
  value += render_step_argument(step.block) if step.block
  value
end
render_step_argument(argument) click to toggle source
# File lib/chutney/linter.rb, line 198
def render_step_argument(argument)
  return "\n#{argument.content}" if argument.is_a?(CukeModeler::DocString)

  result = argument.rows.map do |row|
    "|#{row.cells.map(&:value).join '|'}|"
  end.join "\n"
  "\n#{result}"
end
scenario_outline_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 52
def scenario_outline_word?(word)
  dialect_word(:scenarioOutline).include?(word)
end
scenarios() { |feature, test| ... } click to toggle source
# File lib/chutney/linter.rb, line 149
def scenarios
  if block_given?
    feature&.tests&.each do |test|
      next if off_switch?(test)

      yield(feature, test)
    end

  else
    feature&.tests
  end
end
steps() { |feature, test, step| ... } click to toggle source
# File lib/chutney/linter.rb, line 174
def steps
  feature&.tests&.each do |test|
    next if off_switch?(test)

    test.steps.each do |step|
      yield(feature, test, step)
    end
  end
end
tags_for(element) click to toggle source
# File lib/chutney/linter.rb, line 72
def tags_for(element)
  element.tags.map { |tag| tag.name[1..] }
end
then_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 56
def then_word?(word)
  dialect_word(:then).include?(word)
end
type(_feature, scenario, step) click to toggle source
# File lib/chutney/linter.rb, line 97
def type(_feature, scenario, step)
  if step
    :step
  else
    scenario ? :scenario : :feature
  end
end
when_word?(word) click to toggle source
# File lib/chutney/linter.rb, line 60
def when_word?(word)
  dialect_word(:when).include?(word)
end