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

# good
# rubocop:disable Layout/LineLength, Style/Encoding

# bad
# rubocop:disable

# good
# rubocop:disable all

# bad
# rubocop:disable Layout/LineLength # rubocop:disable Style/Encoding

# good
# rubocop:disable Layout/LineLength
# rubocop:disable Style/Encoding

# bad
# rubocop:wrongmode Layout/LineLength

# good
# rubocop:disable Layout/LineLength

# bad
# rubocop:disable Layout/LineLength comment

# good
# rubocop:disable Layout/LineLength -- comment

# bad
# rucocop:disable Layout/LineLength

# good
# rubocop:disable Layout/LineLength

# bad
# rubocop:disable Layout/LineLenght

# good
# rubocop:disable Layout/LineLength

Constant Summary collapse

COMMON_MSG =
'Malformed directive comment detected.'
MISSING_MODE_NAME_MSG =
'The mode name is missing.'
INVALID_MODE_NAME_MSG =

rubocop:disable Layout/LineLength

'The mode name must be one of `enable`, `disable`, `disable-next`, `todo`, `todo-next`, `push`, or `pop`.'
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 `--`.'
NEXT_DIRECTIVE_AT_EOL_MSG =
'A `-next` directive must be on its own line, above the ' \
'statement it applies to.'
INVALID_KEYWORD_MSG =
'The directive keyword must be `rubocop`, not `%<keyword>s`.'
UNKNOWN_COP_MSG =
'Unknown cop name `%<name>s`%<suggestion>s.'
NEAR_MISS_KEYWORD_REGEXP =

A comment that looks like an attempted directive: any keyword followed by a colon and a valid mode name.

/
  \A\#+\s*(?<keyword>[A-Za-z][\w-]*)\s*:\s*
  (?:#{DirectiveComment::MODES_PATTERN})\b
/x.freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

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

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

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

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



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/lint/cop_directive_syntax.rb', line 77

def on_new_investigation
  processed_source.comments.each do |comment|
    directive_comment = DirectiveComment.new(comment)
    if directive_comment.start_with_marker?
      check_directive(comment, directive_comment)
    elsif (keyword = near_miss_keyword(comment))
      message = format("#{COMMON_MSG} #{INVALID_KEYWORD_MSG}", keyword: keyword)
      add_offense(comment, message: message)
    end
  end
end