Class: RuboCop::Cop::Sorbet::ForbidRBIOutsideOfAllowedPaths

Inherits:
Base
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_allowed_paths.rb

Overview

Makes sure that RBI files are always located under the defined allowed paths.

Options:

  • ‘AllowedPaths`: A list of the paths where RBI files are allowed (default: [“rbi/**”, “sorbet/rbi/**”])

Examples:

# bad
# lib/some_file.rbi
# other_file.rbi

# good
# rbi/external_interface.rbi
# sorbet/rbi/some_file.rbi
# sorbet/rbi/any/path/for/file.rbi

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_allowed_paths.rb', line 26

def on_new_investigation
  paths = allowed_paths

  if paths.nil?
    add_global_offense("AllowedPaths expects an array")
    return
  elsif paths.empty?
    add_global_offense("AllowedPaths cannot be empty")
    return
  end

  # When executed the path to the source file is absolute.
  # We need to remove the exec path directory prefix before matching with the filename regular expressions.
  rel_path = processed_source.file_path.sub("#{Dir.pwd}/", "")

  add_global_offense(
    "RBI file path should match one of: #{paths.join(", ")}",
  ) if paths.none? { |pattern| File.fnmatch(pattern, rel_path) }
end