Module: Rubydex::Linter::Helpers::PathHelpers

Defined in:
lib/rubydex/linter/helpers/path_helpers.rb

Overview

@requires_ancestor: Rubydex::Linter::CustomRule

Constant Summary collapse

EXCLUDE_FNMATCH_FLAGS =

: Integer

File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
TEST_PATHS =
["test/**", "**/test/**", "**/*_test.rb"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.display_path(location, workspace:) ⇒ Object

Returns the path to show users for a location: relative to the workspace for files inside it, the absolute path for files outside it (dependencies, for example) and the URI opaque for non file URIs, so that untitled:Untitled-1 displays as Untitled-1.

: (Location, workspace: String) -> String



28
29
30
31
32
33
34
35
# File 'lib/rubydex/linter/helpers/path_helpers.rb', line 28

def display_path(location, workspace:)
  path = location.to_file_path
  return path unless path == workspace || path.start_with?("#{workspace}/")

  Pathname.new(path).relative_path_from(workspace).to_s
rescue Location::NotFileUriError
  URI(location.uri).opaque || location.uri
end

.path_matches_patterns?(path, patterns, workspace:, flags: 0) ⇒ Boolean

: (String, Array, workspace: String, ?flags: Integer) -> bool

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/rubydex/linter/helpers/path_helpers.rb', line 16

def path_matches_patterns?(path, patterns, workspace:, flags: 0)
  return false unless path == workspace || path.start_with?("#{workspace}/")

  relative_path = path.delete_prefix("#{workspace}/")
  patterns.any? { |pattern| File.fnmatch?(pattern, relative_path, flags) }
end

Instance Method Details

#reject_definitions_in_paths(definitions, excluded_patterns) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rubydex/linter/helpers/path_helpers.rb', line 39

def reject_definitions_in_paths(definitions, excluded_patterns)
  workspace = graph.workspace_path

  definitions.reject do |definition|
    path = path_for_definition(definition)
    path.nil? || (path != workspace && !path.start_with?("#{workspace}/")) ||
      path_matches_patterns?(path, excluded_patterns)
  end
end

#select_definitions_in_paths(definitions, patterns) ⇒ Object



50
51
52
53
54
55
# File 'lib/rubydex/linter/helpers/path_helpers.rb', line 50

def select_definitions_in_paths(definitions, patterns)
  definitions.select do |definition|
    path = path_for_definition(definition)
    path && path_matches_patterns?(path, patterns)
  end
end