Sha256: d9ae775e60fdd0f6b69b7e1e6186d946d2174583ce6deefbf9ba8927c5b6bb90

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Skeptic
  module Rules
    class LinesPerMethod
      include SexpVisitor

      def initialize(limit = nil)
        env[:line_numbers] = []
        @line_counts = {}
        @limit = limit
      end

      def apply_to(tokens, sexp)
        visit sexp
        self
      end

      def size_of(qualified_method_name)
        @line_counts[qualified_method_name]
      end

      def violations
        @line_counts.select { |name, lines| lines > @limit }.map do |name, lines|
          "#{name} is #{lines} lines long"
        end
      end

      def name
        "Number of lines per method (#@limit)"
      end

      private

      on :class do |name, parent, body|
        class_name = [env[:class], extract_name(name)].compact.join('::')

        env.scoped :class => class_name do
          visit body
        end
      end

      on :def do |name, params, body|
        method_name = extract_name(name)

        env.scoped method: method_name, line_numbers: [] do
          visit body

          lines = env[:line_numbers].uniq.compact.length

          full_name = "#{env[:class]}##{env[:method]}"
          @line_counts[full_name] = lines + @line_counts.fetch(full_name, 0)
        end
      end

      on :@ident, :@const, :@gvar, :@ivar, :@cvar, :@int, :@float, :@tstring_content, :@kw do |text, location|
        env[:line_numbers] << location.first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skeptic-0.0.0 lib/skeptic/rules/lines_per_method.rb