Class: SyntaxTree::Comment

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Comment represents a comment in the source.

# comment

Defined Under Namespace

Classes: List

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, inline:, location:) ⇒ Comment

Returns a new instance of Comment.



3938
3939
3940
3941
3942
3943
3944
3945
# File 'lib/syntax_tree.rb', line 3938

def initialize(value:, inline:, location:)
  @value = value
  @inline = inline
  @location = location

  @leading = false
  @trailing = false
end

Instance Attribute Details

#inlineObject (readonly) Also known as: inline?

boolean

whether or not there is code on the same line as this comment.

If there is, then inline will be true.



3932
3933
3934
# File 'lib/syntax_tree.rb', line 3932

def inline
  @inline
end

#locationObject (readonly)

Location

the location of this node



3936
3937
3938
# File 'lib/syntax_tree.rb', line 3936

def location
  @location
end

#valueObject (readonly)

String

the contents of the comment



3928
3929
3930
# File 'lib/syntax_tree.rb', line 3928

def value
  @value
end

Instance Method Details

#commentsObject



3967
3968
3969
# File 'lib/syntax_tree.rb', line 3967

def comments
  []
end

#format(q) ⇒ Object



3971
3972
3973
# File 'lib/syntax_tree.rb', line 3971

def format(q)
  q.text(value)
end

#ignore?Boolean

Returns:

  • (Boolean)


3963
3964
3965
# File 'lib/syntax_tree.rb', line 3963

def ignore?
  value[1..-1].strip == "stree-ignore"
end

#leading!Object



3947
3948
3949
# File 'lib/syntax_tree.rb', line 3947

def leading!
  @leading = true
end

#leading?Boolean

Returns:

  • (Boolean)


3951
3952
3953
# File 'lib/syntax_tree.rb', line 3951

def leading?
  @leading
end

#pretty_print(q) ⇒ Object



3975
3976
3977
3978
3979
3980
3981
3982
# File 'lib/syntax_tree.rb', line 3975

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("comment")

    q.breakable
    q.pp(value)
  end
end

#to_json(*opts) ⇒ Object



3984
3985
3986
3987
3988
3989
3990
3991
# File 'lib/syntax_tree.rb', line 3984

def to_json(*opts)
  {
    type: :comment,
    value: value.force_encoding("UTF-8"),
    inline: inline,
    loc: location
  }.to_json(*opts)
end

#trailing!Object



3955
3956
3957
# File 'lib/syntax_tree.rb', line 3955

def trailing!
  @trailing = true
end

#trailing?Boolean

Returns:

  • (Boolean)


3959
3960
3961
# File 'lib/syntax_tree.rb', line 3959

def trailing?
  @trailing
end