Sha256: cbf72cd2471a78f2141a487d7b7f9c25c1297e99cb76d8a68bd73a040d0450c1

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8

module RuboCop
  # Common methods and behaviors for dealing with paths.
  module PathUtil
    module_function

    def relative_path(path, base_dir = Dir.pwd)
      path_name = Pathname.new(File.expand_path(path))
      path_name.relative_path_from(Pathname.new(base_dir)).to_s
    end

    # TODO: The old way of matching patterns is flawed, so a new one has been
    # introduced. We keep supporting the old way for a while and issue
    # deprecation warnings when a pattern is used that produced a match with
    # the old way but doesn't match with the new.
    def match_path?(pattern, path, config_path)
      case pattern
      when String
        basename = File.basename(path)
        old_match = basename == pattern || File.fnmatch?(pattern, path)
        new_match = File.fnmatch?(pattern, path, File::FNM_PATHNAME)
        if old_match && !new_match
          issue_deprecation_warning(basename, pattern, config_path)
        end
        old_match || new_match
      when Regexp
        path =~ pattern
      end
    end

    def issue_deprecation_warning(basename, pattern, config_path)
      instruction = if basename == pattern
                      ". Change to '**/#{pattern}'."
                    elsif pattern.end_with?('**')
                      ". Change to '#{pattern}/*'."
                    end
      warn("Warning: Deprecated pattern style '#{pattern}' in " \
           "#{config_path}#{instruction}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.27.0 lib/rubocop/path_util.rb