Sha256: c62bce3b7a3db4ced965a4653a13d62be38fe29b3ef92a88cf544f5ed904ead9

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require_relative '../ruler'

class Tailor
  module Rulers
    class SpacesAfterConditionalRuler < Tailor::Ruler

      def initialize(config, options)
        super(config, options)
        add_lexer_observers :nl
      end

      def nl_update(current_lexed_line, lineno, _)
        measure(current_lexed_line, lineno)
      end

      # Checks to see if spacing is present after conditionals
      #
      # @param [Array] lexed_line The lexed line with a conditional
      # @param [Fixnum] lineno Line the problem was found on.
      def measure(lexed_line, lineno)

        idx = lexed_line.index do |_, token, name|
          token == :on_kw and %w{if unless case}.include?(name)
        end

        expected_spaces = @config
        spaces = expected_spaces

        if idx
          column = lexed_line[idx].first.last
          pos, token, _ = lexed_line[idx + 1]
          spaces = case token
          when :on_lparen then 0
          when :on_sp
            next_token = lexed_line[idx + 2]
            next_token.first.last - pos.last
          end
        end

        if expected_spaces != spaces
          @problems << Problem.new(problem_type, lineno, column,
            "#{spaces} spaces after conditional at column #{column}, " +
              "expected #{expected_spaces}.", @options[:level])
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tailor-1.4.1 lib/tailor/rulers/spaces_after_conditional_ruler.rb
tailor-1.4.0 lib/tailor/rulers/spaces_after_conditional_ruler.rb