Class: RuboCop::Cop::Layout::EmptyLineAfterMagicComment
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
Overview
Checks for a newline after the final magic comment.
NumberOfEmptyLines configures the minimum number of empty lines required. Set it
to 2 when using YARD, which otherwise treats the magic comments as documentation
for the first module or class in the file.
NOTE: Layout/EmptyLines has to be disabled for values greater than 1, as it
removes the extra empty lines this cop adds, and autocorrecting with both enabled
loops between them.
Constant Summary collapse
- MSG =
'Expected at least %<expected>d empty %<lines>s after magic comments; found ' \ '%<actual>d.'
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source, #project_index
Instance Method Summary collapse
Methods included from AutoCorrector
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?, #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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#on_new_investigation ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rubocop/cop/layout/empty_line_after_magic_comment.rb', line 68 def on_new_investigation return unless (last_magic_comment = last_magic_comment(processed_source)) actual = empty_lines_after(last_magic_comment) # Trailing empty lines are `Layout/TrailingEmptyLines`' responsibility. return unless processed_source[last_magic_comment.loc.line + actual] return if actual >= number_of_empty_lines offending_range = offending_range(last_magic_comment) add_offense(offending_range, message: (actual)) do |corrector| corrector.insert_before(offending_range, "\n" * (number_of_empty_lines - actual)) end end |
#validate_config ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/layout/empty_line_after_magic_comment.rb', line 59 def validate_config return if number_of_empty_lines.is_a?(Integer) && number_of_empty_lines.positive? raise ValidationError, 'The `Layout/EmptyLineAfterMagicComment` cop only accepts a positive ' \ 'integer for its `NumberOfEmptyLines` configuration parameter, but ' \ "`#{number_of_empty_lines.inspect}` was given." end |