class RuboCop::Cop::Lint::CopDirectiveSyntax
Checks that ‘# rubocop:enable …` and `# rubocop:disable …` statements are strictly formatted.
A comment can be added to the directive by prefixing it with ‘–`.
@example
# bad # rubocop:disable Layout/LineLength Style/Encoding # ^ missing comma # bad # rubocop:disable # bad # rubocop:disable Layout/LineLength # rubocop:disable Style/Encoding # bad # rubocop:wrongmode Layout/LineLength # good # rubocop:disable Layout/LineLength # good # rubocop:disable Layout/LineLength, Style/Encoding # good # rubocop:disable all # good # rubocop:disable Layout/LineLength -- This is a good comment.
Constants
- COMMON_MSG
- INVALID_MODE_NAME_MSG
- MALFORMED_COP_NAMES_MSG
- MISSING_COP_NAME_MSG
- MISSING_MODE_NAME_MSG
Public Instance Methods
Source
# File lib/rubocop/cop/lint/cop_directive_syntax.rb, line 48 def on_new_investigation processed_source.comments.each do |comment| directive_comment = DirectiveComment.new(comment) next unless directive_comment.start_with_marker? next unless directive_comment.malformed? message = offense_message(directive_comment) add_offense(comment, message: message) end end
Private Instance Methods
Source
# File lib/rubocop/cop/lint/cop_directive_syntax.rb, line 62 def offense_message(directive_comment) comment = directive_comment.comment after_marker = comment.text.sub(DirectiveComment::DIRECTIVE_MARKER_REGEXP, '') mode = after_marker.split(' ', 2).first additional_msg = if mode.nil? MISSING_MODE_NAME_MSG elsif !DirectiveComment::AVAILABLE_MODES.include?(mode) INVALID_MODE_NAME_MSG elsif directive_comment.missing_cop_name? MISSING_COP_NAME_MSG else MALFORMED_COP_NAMES_MSG end "#{COMMON_MSG} #{additional_msg}" end
rubocop:disable Metrics/MethodLength