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.).
Direct Known Subclasses
Constant Summary collapse
- MAGIC_COMMENT_PATTERNS =
Ruby::Merge::MagicCommentSupport::MAGIC_COMMENT_PATTERNS
Instance Attribute Summary collapse
-
#magic_comment_type ⇒ Symbol?
readonly
Get the type of magic comment.
Class Method Summary collapse
-
.from_tree_haver(th_comment, magic_comment_types = {}) ⇒ Prism::Merge::Comment::Line
Build a Line from a
TreeHaver::Backends::Prism::Commentinstance. - .magic_comment_type_for(text) ⇒ Object
Instance Method Summary collapse
-
#initialize(text:, line_number:, magic_comment_type: nil) ⇒ 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_value ⇒ String?
Get the value of a magic comment.
-
#signature ⇒ Array
Generate signature for matching.
Constructor Details
#initialize(text:, line_number:, magic_comment_type: nil) ⇒ Line
Initialize a new Ruby comment Line.
Always uses hash_comment style for Ruby.
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_type ⇒ Symbol? (readonly)
Get the type of 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.
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
#inspect ⇒ String
Returns 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/...
62 63 64 |
# File 'lib/prism/merge/comment/line.rb', line 62 def magic_comment? !@magic_comment_type.nil? end |
#magic_comment_value ⇒ String?
Get the value of 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 |
#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).
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 |