class RuboCop::Cop::Sorbet::ForbidRBIOutsideOfAllowedPaths

# sorbet/rbi/any/path/for/file.rbi
# sorbet/rbi/some_file.rbi
# rbi/external_interface.rbi
# good
# other_file.rbi
# lib/some_file.rbi
# bad
@example
* ‘AllowedPaths`: A list of the paths where RBI files are allowed (default: [“rbi/**”, “sorbet/rbi/**”])
Options:
Makes sure that RBI files are always located under the defined allowed paths.

def allowed_paths

def allowed_paths
  paths = cop_config["AllowedPaths"]
  return unless paths.is_a?(Array)
  paths.compact
end

def on_new_investigation

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