lib/codeowners/checker/group/pattern.rb in codeowners-checker-1.0.0 vs lib/codeowners/checker/group/pattern.rb in codeowners-checker-1.0.1
- old
+ new
@@ -1,19 +1,22 @@
# frozen_string_literal: true
require_relative 'line'
+require_relative '../owner'
module Codeowners
class Checker
class Group
# Defines and manages line type pattern.
+ # Parse the line into pattern, owners and whitespaces.
class Pattern < Line
- attr_accessor :pattern, :owners
+ attr_accessor :owners, :whitespace
+ attr_reader :pattern
def self.match?(line)
_pattern, *owners = line.split(/\s+/)
- owners.any? && owners.all? { |owner| owner.include?('@') }
+ Owner.valid?(*owners)
end
def initialize(line)
super
parse(line)
@@ -21,25 +24,41 @@
def owner
owners.first
end
+ # Parse the line counting whitespaces between pattern and owners.
def parse(line)
@pattern, *@owners = line.split(/\s+/)
+ @whitespace = line.split('@').first.count(' ') - 1
end
def match_file?(file)
- regex.match(file)
+ if !pattern.include?('/') || pattern.include?('**')
+ File.fnmatch(pattern.gsub(%r{^/}, ''), file, File::FNM_DOTMATCH)
+ else
+ File.fnmatch(pattern.gsub(%r{^/}, ''), file, File::FNM_PATHNAME | File::FNM_DOTMATCH)
+ end
end
- def to_s
- [@pattern, @owners].join(' ')
+ def pattern=(new_pattern)
+ @whitespace += @pattern.size - new_pattern.size
+ @whitespace = 1 if @whitespace < 1
+
+ @pattern = new_pattern
end
- private
+ # @return String with the pattern and owners
+ # Use @param preserve_whitespaces to keep the previous identation.
+ def to_file(preserve_whitespaces: true)
+ line = pattern
+ spaces = preserve_whitespaces ? whitespace : 0
+ line << ' ' * spaces
+ [line, *owners].join(' ')
+ end
- def regex
- Regexp.new(pattern.gsub(%r{/\*\*}, '(/[^/]+)+').gsub(/\*/, '[^/]+'))
+ def to_s
+ to_file(preserve_whitespaces: false)
end
end
end
end
end