Class: RubbyCop::Cop::Team

Inherits:
Object
  • Object
show all
Defined in:
lib/rubbycop/cop/team.rb

Overview

FIXME

Defined Under Namespace

Classes: Investigation

Constant Summary collapse

INCOMPATIBLE_COPS =

If these cops try to autocorrect the same file at the same time, bad things are liable to happen

{
  Style::SymbolProc => [Layout::SpaceBeforeBlockBraces],
  Layout::SpaceBeforeBlockBraces => [Style::SymbolProc],
  Style::LineEndConcatenation => [Style::UnneededInterpolation],
  Style::UnneededInterpolation => [Style::LineEndConcatenation],
  Style::SelfAssignment => [Layout::SpaceAroundOperators],
  Layout::SpaceAroundOperators => [Style::SelfAssignment],
  Style::BracesAroundHashParameters => [Layout::MultilineHashBraceLayout]
}.freeze
DEFAULT_OPTIONS =
{
  auto_correct: false,
  debug: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cop_classes, config, options = nil) ⇒ Team

Returns a new instance of Team.



30
31
32
33
34
35
36
37
38
# File 'lib/rubbycop/cop/team.rb', line 30

def initialize(cop_classes, config, options = nil)
  @cop_classes = cop_classes
  @config = config
  @options = options || DEFAULT_OPTIONS
  @errors = []
  @warnings = []

  validate_config
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



26
27
28
# File 'lib/rubbycop/cop/team.rb', line 26

def errors
  @errors
end

#updated_source_fileObject (readonly) Also known as: updated_source_file?

Returns the value of attribute updated_source_file.



26
27
28
# File 'lib/rubbycop/cop/team.rb', line 26

def updated_source_file
  @updated_source_file
end

#warningsObject (readonly)

Returns the value of attribute warnings.



26
27
28
# File 'lib/rubbycop/cop/team.rb', line 26

def warnings
  @warnings
end

Instance Method Details

#autocorrect(buffer, cops) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubbycop/cop/team.rb', line 76

def autocorrect(buffer, cops)
  @updated_source_file = false
  return unless autocorrect?

  new_source = autocorrect_all_cops(buffer, cops)

  return if new_source == buffer.source

  if @options[:stdin]
    # holds source read in from stdin, when --stdin option is used
    @options[:stdin] = new_source
  else
    filename = buffer.name
    File.open(filename, 'w') { |f| f.write(new_source) }
  end
  @updated_source_file = true
end

#autocorrect?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubbycop/cop/team.rb', line 40

def autocorrect?
  @options[:auto_correct]
end

#copsObject



57
58
59
60
61
62
# File 'lib/rubbycop/cop/team.rb', line 57

def cops
  only_options = @options.fetch(:only, [])
  @cops ||= @cop_classes.enabled(@config, only_options).map do |cop_class|
    cop_class.new(@config, @options)
  end
end

#debug?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rubbycop/cop/team.rb', line 44

def debug?
  @options[:debug]
end

#forcesObject



64
65
66
# File 'lib/rubbycop/cop/team.rb', line 64

def forces
  @forces ||= forces_for(cops)
end

#forces_for(cops) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rubbycop/cop/team.rb', line 68

def forces_for(cops)
  Force.all.each_with_object([]) do |force_class, forces|
    joining_cops = cops.select { |cop| cop.join_force?(force_class) }
    next if joining_cops.empty?
    forces << force_class.new(joining_cops)
  end
end

#inspect_file(processed_source) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/rubbycop/cop/team.rb', line 48

def inspect_file(processed_source)
  # If we got any syntax errors, return only the syntax offenses.
  unless processed_source.valid_syntax?
    return Lint::Syntax.offenses_from_processed_source(processed_source)
  end

  offenses(processed_source)
end