Class: RuboCop::Gusto::ConfigYml

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/gusto/config_yml.rb

Overview

A class for reading and writing a .rubocop.yml file.

You may rightly ask why we don’t just use the standard library’s YAML.load_file and YAML.dump. Simple, we want to preserve the comments.

Constant Summary collapse

COMMENT_REGEX =
/\A\s*#.*\z/
COP_HEADER_REGEX =
/\A[A-Z0-9][A-Za-z0-9\/:]+:(\s*#.*)?\z/
KEY_REGEX =

case insensitive

/\A\w[\w\/]+:(\s*#.*)?\z/i
PREAMBLE_KEYS =
%w(inherit_mode inherit_gem inherit_from plugins require).freeze
INDENT_REGEX =
/\A(  |- )/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ ConfigYml

Returns a new instance of ConfigYml.

Parameters:

  • lines (Array<String>)

    the lines of the .rubocop.yml file



28
29
30
31
32
# File 'lib/rubocop/gusto/config_yml.rb', line 28

def initialize(lines)
  @preamble, @cops = chunk_blocks(lines).partition do |block|
    block.none? { |line| line.rstrip.match?(COP_HEADER_REGEX) }
  end
end

Instance Attribute Details

#copsObject (readonly)

Returns the value of attribute cops.



25
26
27
# File 'lib/rubocop/gusto/config_yml.rb', line 25

def cops
  @cops
end

#preambleObject (readonly)

Returns the value of attribute preamble.



25
26
27
# File 'lib/rubocop/gusto/config_yml.rb', line 25

def preamble
  @preamble
end

Class Method Details

.load_file(file_path = '.rubocop.yml') ⇒ Object

Parameters:

  • file_path (String) (defaults to: '.rubocop.yml')

    the path to the .rubocop.yml file



19
20
21
22
23
# File 'lib/rubocop/gusto/config_yml.rb', line 19

def self.load_file(file_path = '.rubocop.yml')
  new(File.readlines(file_path))
rescue Errno::ENOENT
  new([])
end

Instance Method Details

#add_inherit_gem(gem_name, *config_paths) ⇒ Object

Find if there’s already an inherit_gem section and add the gem to it if needed



35
36
37
38
39
40
41
# File 'lib/rubocop/gusto/config_yml.rb', line 35

def add_inherit_gem(gem_name, *config_paths)
  update_section_data('inherit_gem') do |data|
    data ||= {}
    data[gem_name.to_s] = config_paths.flatten
    data
  end
end

#add_plugin(plugins) ⇒ Object

Add a plugin to the plugins section or create it if it doesn’t exist



44
45
46
47
48
49
# File 'lib/rubocop/gusto/config_yml.rb', line 44

def add_plugin(plugins)
  update_section_data('plugins') do |data|
    data ||= []
    data.concat(plugins).uniq
  end
end

#empty?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/rubocop/gusto/config_yml.rb', line 88

def empty?
  cops.empty? && preamble.empty?
end

#linesObject



92
93
94
95
96
# File 'lib/rubocop/gusto/config_yml.rb', line 92

def lines
  combined = (preamble + cops).flatten
  combined.pop # there's always one empty newline because of how we parse
  combined
end

#sort!Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubocop/gusto/config_yml.rb', line 75

def sort!
  # Sort the preamble chunks by our preferred order, falling back to key name
  preamble.sort_by! do |chunk|
    key = chunk_name(chunk)
    PREAMBLE_KEYS.index(key)&.to_s || key
  end

  # Sort the cops by their key name, putting comments at the top
  cops.sort_by! { |cop| chunk_name(cop) || 'AAAAA/Comment?' }

  self
end

#to_sObject



98
99
100
# File 'lib/rubocop/gusto/config_yml.rb', line 98

def to_s
  lines.join
end

#update_section_data(section_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/gusto/config_yml.rb', line 51

def update_section_data(section_name, &)
  # Look for an existing section in the preamble
  section = preamble.find { |chunk| chunk_name(chunk) == section_name }

  if section
    comments = section.select { |line| line.match?(COMMENT_REGEX) }
    data = YAML.load(section.join)[section_name.to_s] # it can be present and nil
  else
    comments = []
    data = nil
  end

  data = yield data

  section_lines = YAML.dump({ section_name.to_s => data }).lines.drop(1) # drop the ---
  section_lines.map! { |line| line.sub(/\A(\s*)-/, '\1  -') } # prefer indented lists
  section_lines.insert(0, *comments) # add the comments back in at the top
  section_lines << "\n" # ensure there's a trailing newline

  section ? section.replace(section_lines) : preamble.unshift(section_lines)

  self
end

#write(file_path) ⇒ Object



102
103
104
# File 'lib/rubocop/gusto/config_yml.rb', line 102

def write(file_path)
  File.write(file_path, to_s)
end