Ameba::InlineComments - ameba 1.6.4

# module Ameba::InlineComments

##
Overview

A module that utilizes inline comments parsing and processing logic.

##
Direct including types

- Ameba::Source

##
Defined in:

ameba/inline_comments.cr

##
Constant Summary

COMMENT_DIRECTIVE_REGEX = `/# ameba:(?<action>\w+) (?<rules>\w+(?:\/\w+)?(?:,? \w+(?:\/\w+)?)*)/`

##
Instance Method Summary

- #comment?(line_number : Int32)

  Returns `true` if the line at the given `line_number` is a comment.

- #location_disabled?(location : Crystal::Location | Nil, rule)

  Returns `true` if current location is disabled for a particular rule, `false` otherwise.

- #parse_inline_directive(line)

  Parses inline comment directive.

##
Instance Method Detail

def comment?(line_number : Int32) #

Returns `true` if the line at the given `line_number` is a comment.

def location_disabled?(location : Crystal::Location | Nil, rule) #

Returns `true` if current location is disabled for a particular rule, `false` otherwise.

Location is disabled in two cases:

1. The line of the location ends with a comment directive.
2. The line above the location is a comment directive.

For example, here are two examples of disabled location:

    # ameba:disable Style/LargeNumbers
    Time.epoch(1483859302)

    Time.epoch(1483859302) # ameba:disable Style/LargeNumbers

But here are examples which are not considered as disabled location:

    # ameba:disable Style/LargeNumbers
    #
    Time.epoch(1483859302)

    if use_epoch? # ameba:disable Style/LargeNumbers
      Time.epoch(1483859302)
    end

def parse_inline_directive(line) #

Parses inline comment directive. Returns a tuple that consists of an action and parsed rules if directive found, nil otherwise.

    line = "# ameba:disable Rule1, Rule2"
    directive = parse_inline_directive(line)
    directive[:action] # => "disable"
    directive[:rules]  # => ["Rule1", "Rule2"]

It ignores the directive if it is commented out.

    line = "# # ameba:disable Rule1, Rule2"
    parse_inline_directive(line) # => nil