Module: Linecounter::StructureAnalyzer

Defined in:
lib/linecounter/structure_analyzer.rb

Defined Under Namespace

Classes: StructureVisitor

Constant Summary collapse

STRUCTURE_ORDER =
[
  :module_inclusion,
  :constants,
  :association,
  :public_attribute_macros,
  :public_delegate,
  :macros,
  :public_class_methods,
  :initializer,
  :public_methods,
  :protected_attribute_macros,
  :protected_methods,
  :private_attribute_macros,
  :private_delegate,
  :private_methods
].freeze
STRUCTURE_ITEMS =

Item metadata: key -> (type, human label). Counts are keyed by item; each item rolls up into exactly one type.

[
  { key: :include, type: :module_inclusion, label: "include" },
  { key: :extend, type: :module_inclusion, label: "extend" },
  { key: :prepend, type: :module_inclusion, label: "prepend" },
  { key: :constant_assignment, type: :constants, label: "CONSTANT =" },

  { key: :has_many, type: :association, label: "has_many" },
  { key: :has_one, type: :association, label: "has_one" },
  { key: :belongs_to, type: :association, label: "belongs_to" },
  { key: :habtm, type: :association, label: "has_and_belongs_to_many" },
  { key: :has_one_attached, type: :association, label: "has_one_attached" },
  { key: :has_many_attached, type: :association, label: "has_many_attached" },

  { key: :scope, type: :macros, label: "scope" },
  { key: :validates, type: :macros, label: "validates" },
  { key: :validate, type: :macros, label: "validate" },
  { key: :before_callback, type: :macros, label: "before_*" },
  { key: :after_callback, type: :macros, label: "after_*" },
  { key: :around_callback, type: :macros, label: "around_*" },
  { key: :enum, type: :macros, label: "enum" },
  { key: :serialize, type: :macros, label: "serialize" },
  { key: :store, type: :macros, label: "store" },
  { key: :store_accessor, type: :macros, label: "store_accessor" },

  { key: :public_attr_reader, type: :public_attribute_macros, label: "public attr_reader" },
  { key: :public_attr_writer, type: :public_attribute_macros, label: "public attr_writer" },
  { key: :public_attr_accessor, type: :public_attribute_macros, label: "public attr_accessor" },
  { key: :protected_attr_reader, type: :protected_attribute_macros, label: "protected attr_reader" },
  { key: :protected_attr_writer, type: :protected_attribute_macros, label: "protected attr_writer" },
  { key: :protected_attr_accessor, type: :protected_attribute_macros, label: "protected attr_accessor" },
  { key: :private_attr_reader, type: :private_attribute_macros, label: "private attr_reader" },
  { key: :private_attr_writer, type: :private_attribute_macros, label: "private attr_writer" },
  { key: :private_attr_accessor, type: :private_attribute_macros, label: "private attr_accessor" },

  { key: :public_delegate, type: :public_delegate, label: "public delegate" },
  { key: :private_delegate, type: :private_delegate, label: "private delegate" },

  { key: :public_class_method_def, type: :public_class_methods, label: "public class def" },
  { key: :public_instance_method_def, type: :public_methods, label: "public def" },
  { key: :protected_instance_method_def, type: :protected_methods, label: "protected def" },
  { key: :private_instance_method_def, type: :private_methods, label: "private def" },
  { key: :initializer_def, type: :initializer, label: "initialize" }
].freeze
KEY_TO_TYPE =
STRUCTURE_ITEMS.each_with_object({}) { |item, h| h[item[:key]] = item[:type] }.freeze
ASSOCIATION_KEYS =
{
  has_many: :has_many, has_one: :has_one, belongs_to: :belongs_to,
  has_and_belongs_to_many: :habtm, has_one_attached: :has_one_attached,
  has_many_attached: :has_many_attached
}.freeze
PLAIN_MACROS =
%i[scope validates validate enum serialize store store_accessor].freeze
MODULE_INCLUSION =
%i[include extend prepend].freeze
ATTRIBUTE_MACROS =
%i[attr_reader attr_writer attr_accessor].freeze
VISIBILITY_NAMES =
%i[public protected private].freeze

Class Method Summary collapse

Class Method Details

.counts(content) ⇒ Object



217
218
219
220
221
# File 'lib/linecounter/structure_analyzer.rb', line 217

def counts(content)
  visitor = StructureVisitor.new
  Prism.parse(content).value.accept(visitor)
  [visitor.type_counts, visitor.item_counts, visitor.item_loc_sums]
end