Class: Prism::Merge::Comment::Block

Inherits:
Ast::Merge::Comment::Block
  • Object
show all
Defined in:
lib/prism/merge/comment/block.rb

Overview

Ruby-specific comment block with magic comment detection.

Extends the generic Ast::Merge::Comment::Block with Ruby-specific features like detection and enumeration of magic comments.

Examples:

block = Block.new(children: [
  Line.new(text: "# frozen_string_literal: true", line_number: 1),
  Line.new(text: "# Regular comment", line_number: 2),
])
block.contains_magic_comment? #=> true
block.magic_comments.first.magic_comment_type #=> :frozen_string_literal

Instance Method Summary collapse

Constructor Details

#initialize(children:) ⇒ Block

Initialize a new Ruby comment Block.

Parameters:

  • children (Array<Line, Ast::Merge::Comment::Empty>)

    Child nodes



23
24
25
# File 'lib/prism/merge/comment/block.rb', line 23

def initialize(children:)
  super(children: children, style: :hash_comment)
end

Instance Method Details

#contains_magic_comment?Boolean Also known as: magic_comment?

Check if this block contains a magic comment.

Returns:

  • (Boolean)

    true if any child is a magic comment



30
31
32
# File 'lib/prism/merge/comment/block.rb', line 30

def contains_magic_comment?
  children.any? { |c| c.is_a?(Line) && c.magic_comment? }
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



63
64
65
66
# File 'lib/prism/merge/comment/block.rb', line 63

def inspect
  magic = contains_magic_comment? ? ' has_magic_comments' : ''
  "#<Prism::Merge::Comment::Block lines=#{location.start_line}..#{location.end_line}#{magic} children=#{children.size}>"
end

#magic_commentsArray<Line>

Get all magic comments in this block.

Returns:

  • (Array<Line>)

    Magic comment lines



40
41
42
# File 'lib/prism/merge/comment/block.rb', line 40

def magic_comments
  children.select { |c| c.is_a?(Line) && c.magic_comment? }
end

#signatureArray

Generate signature for matching.

For blocks containing magic comments, uses the FIRST magic comment's signature (by type) so that blocks with the same type of magic comment will match regardless of value (e.g., true vs false).

For non-magic blocks, uses the parent implementation.

Returns:

  • (Array)

    Signature for matching



53
54
55
56
57
58
59
60
# File 'lib/prism/merge/comment/block.rb', line 53

def signature
  if contains_magic_comment?
    # Use the first magic comment's signature
    magic_comments.first.signature
  else
    super
  end
end