Class: RuboCop::Cop::Cobra::LibFilePlacement

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
FilePlacementHelp
Defined in:
lib/rubocop/cop/cobra/lib_file_placement.rb

Overview

This cop disallows adding library files directly into the ‘lib/` directory.

The goal is to encourage developers to put new library files inside the correct namespace, where they can be more modularly isolated and ownership is clear.

Exceptions to this rule are ‘spec/lib/*` and `lib/tasks/*` file patterns.

Examples:

# bad
# path: components/my_component/lib/foo.rb
class Foo
  # ...
end

# good
# path: components/my_component/lib/my_component/foo.rb
module MyComponent
  class Foo
    # ...
  end
end

Constant Summary

Constants included from FilePlacementHelp

FilePlacementHelp::FILE_PLACEMENT_MSG

Instance Method Summary collapse

Methods included from FilePlacementHelp

#applicable_component_path?, #file_placement_msg, #namespaced_correctly?

Instance Method Details

#investigate(processed_source) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/cobra/lib_file_placement.rb', line 31

def investigate(processed_source)
  return if processed_source.blank?

  path = processed_source.file_path
  return unless applicable_component_path?(path, lib_path)
  return if acceptable_lib_path?(path) || namespaced_correctly?(path, lib_path)

  add_offense(processed_source.ast,
              message: file_placement_msg(path, lib_path))
end