Class: RuboCop::DirectiveComment

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

Overview

This class wraps the ‘Parser::Source::Comment` object that represents a special `rubocop:disable` and `rubocop:enable` comment and exposes what cops it contains.

Constant Summary collapse

LINT_DEPARTMENT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'Lint'
LINT_REDUNDANT_DIRECTIVE_COP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"#{LINT_DEPARTMENT}/RedundantCopDisableDirective"
LINT_SYNTAX_COP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"#{LINT_DEPARTMENT}/Syntax"
COP_NAME_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'([A-Z]\w+/)*(?:[A-Z]\w+)'
COP_NAMES_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
COPS_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"(all|#{COP_NAMES_PATTERN})"
DIRECTIVE_COMMENT_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regexp.new(
  "# rubocop : ((?:disable|enable|todo))\\b #{COPS_PATTERN}"
    .gsub(' ', '\s*')
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment, cop_registry = Cop::Registry.global) ⇒ DirectiveComment

Returns a new instance of DirectiveComment.



32
33
34
35
36
# File 'lib/rubocop/directive_comment.rb', line 32

def initialize(comment, cop_registry = Cop::Registry.global)
  @comment = comment
  @cop_registry = cop_registry
  @mode, @cops = match_captures
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



30
31
32
# File 'lib/rubocop/directive_comment.rb', line 30

def comment
  @comment
end

#cop_registryObject (readonly)

Returns the value of attribute cop_registry.



30
31
32
# File 'lib/rubocop/directive_comment.rb', line 30

def cop_registry
  @cop_registry
end

#copsObject (readonly)

Returns the value of attribute cops.



30
31
32
# File 'lib/rubocop/directive_comment.rb', line 30

def cops
  @cops
end

#modeObject (readonly)

Returns the value of attribute mode.



30
31
32
# File 'lib/rubocop/directive_comment.rb', line 30

def mode
  @mode
end

Class Method Details

.before_comment(line) ⇒ Object



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

def self.before_comment(line)
  line.split(DIRECTIVE_COMMENT_REGEXP).first
end

Instance Method Details

#all_cops?Boolean

Checks if all cops specified in this directive

Returns:

  • (Boolean)


82
83
84
# File 'lib/rubocop/directive_comment.rb', line 82

def all_cops?
  cops == 'all'
end

#cop_namesObject

Returns array of specified in this directive cop names



87
88
89
# File 'lib/rubocop/directive_comment.rb', line 87

def cop_names
  @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
end

#department_namesObject

Returns array of specified in this directive department names when all department disabled



93
94
95
# File 'lib/rubocop/directive_comment.rb', line 93

def department_names
  splitted_cops_string.select { |cop| department?(cop) }
end

#directive_countObject



107
108
109
# File 'lib/rubocop/directive_comment.rb', line 107

def directive_count
  splitted_cops_string.count
end

#disabled?Boolean

Checks if this directive disables cops

Returns:

  • (Boolean)


62
63
64
# File 'lib/rubocop/directive_comment.rb', line 62

def disabled?
  %w[disable todo].include?(mode)
end

#disabled_all?Boolean

Checks if this directive disables all cops

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubocop/directive_comment.rb', line 77

def disabled_all?
  disabled? && all_cops?
end

#enabled?Boolean

Checks if this directive enables cops

Returns:

  • (Boolean)


67
68
69
# File 'lib/rubocop/directive_comment.rb', line 67

def enabled?
  mode == 'enable'
end

#enabled_all?Boolean

Checks if this directive enables all cops

Returns:

  • (Boolean)


72
73
74
# File 'lib/rubocop/directive_comment.rb', line 72

def enabled_all?
  !disabled? && all_cops?
end

#in_directive_department?(cop) ⇒ Boolean

Checks if directive departments include cop

Returns:

  • (Boolean)


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

def in_directive_department?(cop)
  department_names.any? { |department| cop.start_with?(department) }
end

#line_numberObject

Returns line number for directive



112
113
114
# File 'lib/rubocop/directive_comment.rb', line 112

def line_number
  comment.source_range.line
end

#match?(cop_names) ⇒ Boolean

Checks if this directive contains all the given cop names

Returns:

  • (Boolean)


44
45
46
# File 'lib/rubocop/directive_comment.rb', line 44

def match?(cop_names)
  parsed_cop_names.uniq.sort == cop_names.uniq.sort
end

#match_capturesObject

Returns match captures to directive comment pattern



57
58
59
# File 'lib/rubocop/directive_comment.rb', line 57

def match_captures
  @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
end

#overridden_by_department?(cop) ⇒ Boolean

Checks if cop department has already used in directive comment

Returns:

  • (Boolean)


103
104
105
# File 'lib/rubocop/directive_comment.rb', line 103

def overridden_by_department?(cop)
  in_directive_department?(cop) && splitted_cops_string.include?(cop)
end

#rangeObject



48
49
50
51
52
53
54
# File 'lib/rubocop/directive_comment.rb', line 48

def range
  match = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
  begin_pos = comment.source_range.begin_pos
  Parser::Source::Range.new(
    comment.source_range.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0)
  )
end

#single_line?Boolean

Checks if this directive relates to single line

Returns:

  • (Boolean)


39
40
41
# File 'lib/rubocop/directive_comment.rb', line 39

def single_line?
  !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP)
end