Sha256: 22d5dc1a57cb5f46dbba80ff6da82fa32f491dc3f8a7b1ebab8d2bb74f997ad9

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Pelusa
  module Lint
    class ShortIdentifiers
      RESERVED_NAMES = ['p', 'pp', 'id']

      def initialize
        @violations = Set.new
      end

      def check(klass)
        iterate_lines!(klass)

        return SuccessfulAnalysis.new(name) if @violations.empty?

        FailedAnalysis.new(name, formatted_violations) do |violations|
          "Names are too short: #{violations.join(', ')}"
        end
      end

      private

      def name
        "Uses descriptive names"
      end

      def iterate_lines!(klass)
        ClassAnalyzer.walk(klass) do |node|
          if node.respond_to?(:name)
            name = node.name.respond_to?(:name) ? node.name.name.to_s : node.name.to_s
            if name =~ /[a-z]/ && name.length < 3 && !RESERVED_NAMES.include?(name)
              @violations << [name, node.line] unless name =~ /^[A-Z]/ # Ignore constants
            end
          end
        end
      end

      def formatted_violations
        grouped_violations = @violations.inject({}) do |hash, (name, line)|
          hash[name] ||= []
          hash[name] << line
          hash
        end

        violations = []

        grouped_violations.each_pair do |name, lines|
          violations << "#{name} (line #{lines.join(', ')})"
        end
        violations
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pelusa-0.2.4 lib/pelusa/lint/short_identifiers.rb
pelusa-0.2.3 lib/pelusa/lint/short_identifiers.rb