Sha256: bdb1899de02249238862ef1d88e79168f5902b9ecd2610c0382d1183e98d6cf7

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module Brakeman
  class FileTypeDetector < BaseProcessor
    def initialize
      super(nil)
      reset
    end

    def detect_type(file)
      reset
      process(file.ast)

      if @file_type.nil?
        @file_type = guess_from_path(file.path.relative)
      end

      @file_type || :lib
    end

    MODEL_CLASSES = [
      :'ActiveRecord::Base',
      :ApplicationRecord
    ]

    def process_class exp
      name = class_name(exp.class_name)
      parent = class_name(exp.parent_name)

      if name.match(/Controller$/)
        @file_type = :controller
        return exp
      elsif MODEL_CLASSES.include? parent
        @file_type = :model
        return exp
      end

      super
    end

    def guess_from_path path
      case
      when path.include?('app/models')
        :model
      when path.include?('app/controllers')
        :controller
      when path.include?('config/initializers')
        :initializer
      when path.include?('lib/')
        :lib
      when path.match?(%r{config/environments/(?!production\.rb)$})
        :skip
      when path.match?(%r{environments/production\.rb$})
        :skip
      when path.match?(%r{application\.rb$})
        :skip
      when path.match?(%r{config/routes\.rb$})
        :skip
      end
    end

    private

    def reset
      @file_type = nil
    end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
brakeman-7.0.0 lib/brakeman/processors/lib/file_type_detector.rb
brakeman-lib-7.0.0 lib/brakeman/processors/lib/file_type_detector.rb
brakeman-min-7.0.0 lib/brakeman/processors/lib/file_type_detector.rb