Class: Ast::Merge::Comment::Line

Inherits:
AstNode
  • Object
show all
Defined in:
lib/ast/merge/comment/line.rb

Overview

Represents a single comment line in source code.

A comment line is a line that starts with a comment delimiter (e.g., ‘#` in Ruby, `//` in JavaScript, `<!–` in HTML). The style determines how the comment is parsed and normalized.

Examples:

Ruby-style hash comment

line = Line.new(text: "# frozen_string_literal: true", line_number: 1)
line.slice #=> "# frozen_string_literal: true"
line.content #=> "frozen_string_literal: true"
line.signature #=> [:comment_line, "frozen_string_literal: true"]

JavaScript-style line comment

style = Style.for(:c_style_line)
line = Line.new(text: "// TODO: fix this", line_number: 5, style: style)
line.content #=> "TODO: fix this"

HTML-style comment

style = Style.for(:html_comment)
line = Line.new(text: "<!-- Important note -->", line_number: 1, style: style)
line.content #=> "Important note"

Instance Attribute Summary collapse

Attributes inherited from AstNode

#location, #slice, #source

Instance Method Summary collapse

Methods inherited from AstNode

#<=>, #child, #child_count, #children, #each, #end_byte, #end_point, #has_error?, #inner_node, #missing?, #named?, #start_byte, #start_point, #structural?, #to_s, #unwrap

Constructor Details

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

Initialize a new Line.

Parameters:

  • text (String)

    The full comment text including delimiter

  • line_number (Integer)

    The 1-based line number

  • style (Style, Symbol, nil) (defaults to: nil)

    The comment style (default: :hash_comment)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ast/merge/comment/line.rb', line 52

def initialize(text:, line_number:, style: nil)
  @text = text.to_s
  @line_number = line_number
  @style = resolve_style(style)

  location = AstNode::Location.new(
    start_line: line_number,
    end_line: line_number,
    start_column: 0,
    end_column: @text.length,
  )

  super(slice: @text, location: location)
end

Instance Attribute Details

#line_numberInteger (readonly)

Returns The line number in source.

Returns:

  • (Integer)

    The line number in source



36
37
38
# File 'lib/ast/merge/comment/line.rb', line 36

def line_number
  @line_number
end

#styleStyle (readonly)

Returns The comment style configuration.

Returns:

  • (Style)

    The comment style configuration



39
40
41
# File 'lib/ast/merge/comment/line.rb', line 39

def style
  @style
end

#textString (readonly)

Returns The raw text of the comment line.

Returns:

  • (String)

    The raw text of the comment line



33
34
35
# File 'lib/ast/merge/comment/line.rb', line 33

def text
  @text
end

Instance Method Details

#contains_token?(token, action: nil) ⇒ Boolean

Check if this comment contains a specific token pattern.

Useful for detecting freeze markers or other special directives.

Parameters:

  • token (String)

    The token to look for

  • action (String, nil) (defaults to: nil)

    Optional action suffix (e.g., “freeze”, “unfreeze”)

Returns:

  • (Boolean)

    true if the token is found



96
97
98
99
100
101
102
103
104
105
# File 'lib/ast/merge/comment/line.rb', line 96

def contains_token?(token, action: nil)
  return false unless token

  pattern = if action
    /#{Regexp.escape(token)}:#{action}/i
  else
    /#{Regexp.escape(token)}/i
  end
  text.match?(pattern)
end

#contentString

Extract the comment content without the delimiter.

Uses the style configuration to properly strip delimiters.

Returns:

  • (String)

    The comment text without the leading delimiter and whitespace



72
73
74
# File 'lib/ast/merge/comment/line.rb', line 72

def content
  @content ||= style.extract_line_content(text)
end

#freeze_marker?(freeze_token) ⇒ Boolean

Check if this comment contains a freeze marker.

Parameters:

  • freeze_token (String)

    The freeze token to look for

Returns:

  • (Boolean)

    true if this comment contains a freeze marker



111
112
113
114
115
116
# File 'lib/ast/merge/comment/line.rb', line 111

def freeze_marker?(freeze_token)
  return false unless freeze_token

  pattern = /#{Regexp.escape(freeze_token)}:(freeze|unfreeze)/i
  text.match?(pattern)
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



119
120
121
# File 'lib/ast/merge/comment/line.rb', line 119

def inspect
  "#<Comment::Line line=#{line_number} style=#{style.name} #{text.inspect}>"
end

#normalized_contentString

Returns Normalized content for comparison.

Returns:

  • (String)

    Normalized content for comparison



85
86
87
# File 'lib/ast/merge/comment/line.rb', line 85

def normalized_content
  content.strip
end

#signatureArray

Generate signature for matching. Uses normalized content (without delimiter) for better matching across files.

Returns:

  • (Array)

    Signature for matching



80
81
82
# File 'lib/ast/merge/comment/line.rb', line 80

def signature
  [:comment_line, normalized_content.downcase]
end

#typeString

TreeHaver::Node protocol: type

Returns:

  • (String)

    “comment_line”



43
44
45
# File 'lib/ast/merge/comment/line.rb', line 43

def type
  "comment_line"
end