Class: Prism::Merge::Comment::Line

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

Overview

Ruby-specific comment line with magic comment detection.

Extends the generic Ast::Merge::Comment::Line with Ruby-specific features like detection of magic comments (frozen_string_literal, encoding, etc.).

Examples:

line = Line.new(text: "# frozen_string_literal: true", line_number: 1)
line.magic_comment? #=> true
line.magic_comment_type #=> :frozen_string_literal

Direct Known Subclasses

RuntimeLine

Constant Summary collapse

MAGIC_COMMENT_PATTERNS =
Ruby::Merge::MagicCommentSupport::MAGIC_COMMENT_PATTERNS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, line_number:, magic_comment_type: nil) ⇒ Line

Initialize a new Ruby comment Line.

Always uses hash_comment style for Ruby.

Parameters:

  • text (String)

    The full comment text including #

  • line_number (Integer)

    The 1-based line number



47
48
49
50
# File 'lib/prism/merge/comment/line.rb', line 47

def initialize(text:, line_number:, magic_comment_type: nil)
  @magic_comment_type = magic_comment_type
  super(text: text, line_number: line_number, style: :hash_comment)
end

Instance Attribute Details

#magic_comment_typeSymbol? (readonly)

Get the type of magic comment.

Returns:

  • (Symbol, nil)

    The magic comment type, or nil if not a magic comment



69
70
71
# File 'lib/prism/merge/comment/line.rb', line 69

def magic_comment_type
  @magic_comment_type
end

Class Method Details

.from_tree_haver(th_comment, magic_comment_types = {}) ⇒ Prism::Merge::Comment::Line

Build a Line from a TreeHaver::Backends::Prism::Comment instance.

Parameters:

  • th_comment (TreeHaver::Backends::Prism::Comment)
  • magic_comment_types (Hash{Integer => Symbol}) (defaults to: {})

    optional pre-computed map of line_number => magic_comment_type (avoids duplicate lookups)

Returns:



31
32
33
34
35
36
37
38
# File 'lib/prism/merge/comment/line.rb', line 31

def from_tree_haver(th_comment, magic_comment_types = {})
  line_number = th_comment.location.start_line
  new(
    text: th_comment.text,
    line_number: line_number,
    magic_comment_type: magic_comment_types[line_number]
  )
end

.magic_comment_type_for(text) ⇒ Object



21
22
23
# File 'lib/prism/merge/comment/line.rb', line 21

def magic_comment_type_for(text)
  Prism::Merge::MagicCommentSupport.magic_comment_type_for_text(text)
end

Instance Method Details

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



100
101
102
103
# File 'lib/prism/merge/comment/line.rb', line 100

def inspect
  magic = magic_comment? ? " magic=#{magic_comment_type}" : ''
  "#<Prism::Merge::Comment::Line line=#{line_number}#{magic} #{text.inspect}>"
end

#magic_comment?Boolean

Check if this is a Ruby magic comment.

Magic comments are special comments that affect Ruby's behavior:

  • # frozen_string_literal: true/false
  • # encoding: UTF-8
  • # coding: UTF-8
  • # warn_indent: true/false
  • # shareable_constant_value: literal/...

Returns:

  • (Boolean)

    true if this is a magic comment



62
63
64
# File 'lib/prism/merge/comment/line.rb', line 62

def magic_comment?
  !@magic_comment_type.nil?
end

#magic_comment_valueString?

Get the value of a magic comment.

Returns:

  • (String, nil)

    The magic comment value, or nil if not a magic comment



74
75
76
77
78
79
# File 'lib/prism/merge/comment/line.rb', line 74

def magic_comment_value
  return unless magic_comment?

  stripped = content.strip
  stripped.split(':', 2).last&.strip
end

#signatureArray

Generate signature for matching.

For magic comments, uses the magic comment TYPE as the signature so that # frozen_string_literal: true matches # frozen_string_literal: false. This allows preference to be applied when both template and dest have the same type of magic comment with different values.

For non-magic comments, uses the parent implementation (normalized content).

Returns:

  • (Array)

    Signature for matching



91
92
93
94
95
96
97
# File 'lib/prism/merge/comment/line.rb', line 91

def signature
  if magic_comment?
    [:magic_comment, magic_comment_type]
  else
    super
  end
end