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

Constant Summary collapse

MAGIC_COMMENT_PATTERNS =

Ruby magic comment patterns

{
  frozen_string_literal: /^frozen_string_literal:\s*(true|false)$/i,
  encoding: /^(encoding|coding):\s*\S+$/i,
  warn_indent: /^warn_indent:\s*(true|false)$/i,
  shareable_constant_value: /^shareable_constant_value:\s*\S+$/i,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text:, line_number:) ⇒ 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



34
35
36
# File 'lib/prism/merge/comment/line.rb', line 34

def initialize(text:, line_number:)
  super(text: text, line_number: line_number, style: :hash_comment)
end

Instance Method Details

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



95
96
97
98
# File 'lib/prism/merge/comment/line.rb', line 95

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



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

def magic_comment?
  MAGIC_COMMENT_PATTERNS.any? { |_, pattern| content.strip.match?(pattern) }
end

#magic_comment_typeSymbol?

Get the type of magic comment.

Returns:

  • (Symbol, nil)

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



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

def magic_comment_type
  MAGIC_COMMENT_PATTERNS.each do |type, pattern|
    return type if content.strip.match?(pattern)
  end
  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



65
66
67
68
69
70
71
72
73
74
# File 'lib/prism/merge/comment/line.rb', line 65

def magic_comment_value
  stripped = content.strip
  MAGIC_COMMENT_PATTERNS.each do |_, pattern|
    if stripped.match?(pattern)
      # Extract the value after the colon
      return stripped.split(":", 2).last&.strip
    end
  end
  nil
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



86
87
88
89
90
91
92
# File 'lib/prism/merge/comment/line.rb', line 86

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