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



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

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.



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

def cops
  @cops
end

#preambleObject (readonly)

Returns the value of attribute preamble.



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

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



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

def self.load_file(file_path = ".rubocop.yml")
  new(File.readlines(file_path, encoding: "UTF-8"))
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



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

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



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

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

#empty?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/rubocop/gusto/config_yml.rb', line 90

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

#linesObject



94
95
96
97
98
# File 'lib/rubocop/gusto/config_yml.rb', line 94

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

#sort!Object



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

def sort!
  # Sort the preamble chunks by our preferred order, falling back to key name
  # Comment-only chunks (nil key) sort to the end with "ZZZZZ"
  preamble.sort_by! do |chunk|
    key = chunk_name(chunk)
    PREAMBLE_KEYS.index(key)&.to_s || key || "ZZZZZ"
  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



100
101
102
# File 'lib/rubocop/gusto/config_yml.rb', line 100

def to_s
  lines.join
end

#update_section_data(section_name) ⇒ Object



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

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



104
105
106
# File 'lib/rubocop/gusto/config_yml.rb', line 104

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