Class: Prism::Merge::Comment::Line
- Inherits:
-
Ast::Merge::Comment::Line
- Object
- Ast::Merge::Comment::Line
- Prism::Merge::Comment::Line
- 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.).
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
-
#initialize(text:, line_number:) ⇒ Line
constructor
Initialize a new Ruby comment Line.
-
#inspect ⇒ String
Human-readable representation.
-
#magic_comment? ⇒ Boolean
Check if this is a Ruby magic comment.
-
#magic_comment_type ⇒ Symbol?
Get the type of magic comment.
-
#magic_comment_value ⇒ String?
Get the value of a magic comment.
-
#signature ⇒ Array
Generate signature for matching.
Constructor Details
#initialize(text:, line_number:) ⇒ Line
Initialize a new Ruby comment Line.
Always uses hash_comment style for Ruby.
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
#inspect ⇒ String
Returns 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/...
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_type ⇒ Symbol?
Get the type of 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_value ⇒ String?
Get the value of 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 |
#signature ⇒ Array
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).
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 |