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

Instance Method Summary collapse

Methods inherited from AstNode

#children, #respond_to_missing?, #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)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ast/merge/comment/line.rb', line 46

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



90
91
92
93
94
95
96
97
98
99
# File 'lib/ast/merge/comment/line.rb', line 90

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



66
67
68
# File 'lib/ast/merge/comment/line.rb', line 66

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



105
106
107
108
109
110
# File 'lib/ast/merge/comment/line.rb', line 105

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



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

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



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

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



74
75
76
# File 'lib/ast/merge/comment/line.rb', line 74

def signature
  [:comment_line, normalized_content.downcase]
end