Class: RuboCop::Cop::Lint::CopDirectiveSyntax

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/cop_directive_syntax.rb

Overview

Checks that ‘# rubocop:enable …` and `# rubocop:disable …` statements are strictly formatted.

A comment can be added to the directive by prefixing it with ‘–`.

Examples:

# 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.

Constant Summary collapse

COMMON_MSG =
'Malformed directive comment detected.'
MISSING_MODE_NAME_MSG =
'The mode name is missing.'
INVALID_MODE_NAME_MSG =
'The mode name must be one of `enable`, `disable`, or `todo`.'
MISSING_COP_NAME_MSG =
'The cop name is missing.'
MALFORMED_COP_NAMES_MSG =
'Cop names must be separated by commas. ' \
'Comment in the directive must start with `--`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_new_investigationObject



48
49
50
51
52
53
54
55
56
57
# 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
OSZAR »