Class: Parser::Source::Comment
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | lib/rubocop/ast/vendor/parser/source/comment.rb |
Overview
A comment in the source code.
Class Method Summary
-
.associate(ast, comments) ⇒ Hash<Parser::AST::Node, Array<Comment>>
deprecated
Deprecated.
Use .associate_locations.
-
.associate_by_identity(ast, comments) ⇒ Hash<Parser::Source::Node, Array<Comment>>
Associate
commentswithastnodes using identity. -
.associate_locations(ast, comments) ⇒ Hash<Parser::Source::Map, Array<Comment>>
Associate
commentswithastnodes by their location in the source. - .new(range) ⇒ Comment constructor
Instance Attribute Summary
- #document? ⇒ Boolean readonly
- #inline? ⇒ Boolean readonly
-
#loc
readonly
Alias for #location.
- #location ⇒ Parser::Source::Range (also: #loc) readonly
- #text ⇒ String readonly
Instance Method Summary
-
#==(other) ⇒ Boolean
Compares comments.
- #inspect ⇒ String
-
#type ⇒ Symbol
Type of this comment.
Constructor Details
.new(range) ⇒ Comment
Class Method Details
.associate(ast, comments) ⇒ Hash<Parser::AST::Node, Array<Comment>>
Deprecated.
Use .associate_locations.
Associate comments with ast nodes by their corresponding node.
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 32
def self.associate(ast, comments) associator = Associator.new(ast, comments) associator.associate end
.associate_by_identity(ast, comments) ⇒ Hash<Parser::Source::Node, Array<Comment>>
Associate comments with ast nodes using identity.
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 59
def self.associate_by_identity(ast, comments) associator = Associator.new(ast, comments) associator.associate_by_identity end
.associate_locations(ast, comments) ⇒ Hash<Parser::Source::Map, Array<Comment>>
Associate comments with ast nodes by their location in the
source.
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 46
def self.associate_locations(ast, comments) associator = Associator.new(ast, comments) associator.associate_locations end
Instance Attribute Details
#document? ⇒ Boolean (readonly)
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 109
def document? type == :document end
#inline? ⇒ Boolean (readonly)
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 101
def inline? type == :inline end
#loc (readonly)
Alias for #location.
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 21
alias_method :loc, :location
#location ⇒ Parser::Source::Range (readonly) Also known as: #loc
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 17
class Comment attr_reader :text attr_reader :location alias_method :loc, :location ## # Associate `comments` with `ast` nodes by their corresponding node. # # @param [Parser::AST::Node] ast # @param [Array<Comment>] comments # @return [Hash<Parser::AST::Node, Array<Comment>>] # @see Parser::Source::Comment::Associator#associate # @deprecated Use {associate_locations}. # def self.associate(ast, comments) associator = Associator.new(ast, comments) associator.associate end ## # Associate `comments` with `ast` nodes by their location in the # source. # # @param [Parser::AST::Node] ast # @param [Array<Comment>] comments # @return [Hash<Parser::Source::Map, Array<Comment>>] # @see Parser::Source::Comment::Associator#associate_locations # def self.associate_locations(ast, comments) associator = Associator.new(ast, comments) associator.associate_locations end ## # Associate `comments` with `ast` nodes using identity. # # @param [Parser::AST::Node] ast # @param [Array<Comment>] comments # @return [Hash<Parser::Source::Node, Array<Comment>>] # @see Parser::Source::Comment::Associator#associate_by_identity # def self.associate_by_identity(ast, comments) associator = Associator.new(ast, comments) associator.associate_by_identity end ## # @param [Parser::Source::Range] range # def initialize(range) @location = Parser::Source::Map.new(range) @text = range.source.freeze freeze end ## # Type of this comment. # # * Inline comments correspond to `:inline`: # # # whatever # # * Block comments correspond to `:document`: # # =begin # hi i am a document # =end # # @return [Symbol] # def type if text.start_with?("#".freeze) :inline elsif text.start_with?("=begin".freeze) :document end end ## # @see #type # @return [Boolean] true if this is an inline comment. # def inline? type == :inline end ## # @see #type # @return [Boolean] true if this is a block comment. # def document? type == :document end ## # Compares comments. Two comments are equal if they # correspond to the same source range. # # @param [Object] other # @return [Boolean] # def ==(other) other.is_a?(Source::Comment) && @location == other.location end ## # @return [String] a human-readable representation of this comment # def inspect "#<Parser::Source::Comment #{@location.expression.to_s} #{text.inspect}>" end end
#text ⇒ String (readonly)
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 17
class Comment attr_reader :text attr_reader :location alias_method :loc, :location ## # Associate `comments` with `ast` nodes by their corresponding node. # # @param [Parser::AST::Node] ast # @param [Array<Comment>] comments # @return [Hash<Parser::AST::Node, Array<Comment>>] # @see Parser::Source::Comment::Associator#associate # @deprecated Use {associate_locations}. # def self.associate(ast, comments) associator = Associator.new(ast, comments) associator.associate end ## # Associate `comments` with `ast` nodes by their location in the # source. # # @param [Parser::AST::Node] ast # @param [Array<Comment>] comments # @return [Hash<Parser::Source::Map, Array<Comment>>] # @see Parser::Source::Comment::Associator#associate_locations # def self.associate_locations(ast, comments) associator = Associator.new(ast, comments) associator.associate_locations end ## # Associate `comments` with `ast` nodes using identity. # # @param [Parser::AST::Node] ast # @param [Array<Comment>] comments # @return [Hash<Parser::Source::Node, Array<Comment>>] # @see Parser::Source::Comment::Associator#associate_by_identity # def self.associate_by_identity(ast, comments) associator = Associator.new(ast, comments) associator.associate_by_identity end ## # @param [Parser::Source::Range] range # def initialize(range) @location = Parser::Source::Map.new(range) @text = range.source.freeze freeze end ## # Type of this comment. # # * Inline comments correspond to `:inline`: # # # whatever # # * Block comments correspond to `:document`: # # =begin # hi i am a document # =end # # @return [Symbol] # def type if text.start_with?("#".freeze) :inline elsif text.start_with?("=begin".freeze) :document end end ## # @see #type # @return [Boolean] true if this is an inline comment. # def inline? type == :inline end ## # @see #type # @return [Boolean] true if this is a block comment. # def document? type == :document end ## # Compares comments. Two comments are equal if they # correspond to the same source range. # # @param [Object] other # @return [Boolean] # def ==(other) other.is_a?(Source::Comment) && @location == other.location end ## # @return [String] a human-readable representation of this comment # def inspect "#<Parser::Source::Comment #{@location.expression.to_s} #{text.inspect}>" end end
Instance Method Details
#==(other) ⇒ Boolean
Compares comments. Two comments are equal if they correspond to the same source range.
#inspect ⇒ String
# File 'lib/rubocop/ast/vendor/parser/source/comment.rb', line 128
def inspect "#<Parser::Source::Comment #{@location.expression.to_s} #{text.inspect}>" end
#type ⇒ Symbol
Type of this comment.
-
Inline comments correspond to
:inline:# whatever -
Block comments correspond to
:document:=begin hi i am a document =end