Skip to content

Commit

Permalink
[ruby/prism] Provide heredoc? queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Dec 1, 2023
1 parent 2a8d9c5 commit ec83bd7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
57 changes: 41 additions & 16 deletions lib/prism/node_ext.rb
Expand Up @@ -14,8 +14,49 @@ def options
end
end

class InterpolatedMatchLastLineNode < Node
include RegularExpressionOptions
end

class InterpolatedRegularExpressionNode < Node
include RegularExpressionOptions
end

class MatchLastLineNode < Node
include RegularExpressionOptions
end

class RegularExpressionNode < Node
include RegularExpressionOptions
end

private_constant :RegularExpressionOptions

module HeredocQuery # :nodoc:
# Returns true if this node was represented as a heredoc in the source code.
def heredoc?
opening&.start_with?("<<")
end
end

class InterpolatedStringNode < Node
include HeredocQuery
end

class InterpolatedXStringNode < Node
include HeredocQuery
end

class StringNode < Node
include HeredocQuery
end

class XStringNode < Node
include HeredocQuery
end

private_constant :HeredocQuery

class FloatNode < Node
# Returns the value of the node as a Ruby Float.
def value
Expand All @@ -37,29 +78,13 @@ def value
end
end

class InterpolatedMatchLastLineNode < Node
include RegularExpressionOptions
end

class InterpolatedRegularExpressionNode < Node
include RegularExpressionOptions
end

class MatchLastLineNode < Node
include RegularExpressionOptions
end

class RationalNode < Node
# Returns the value of the node as a Ruby Rational.
def value
Rational(slice.chomp("r"))
end
end

class RegularExpressionNode < Node
include RegularExpressionOptions
end

class ConstantReadNode < Node
# Returns the list of parts for the full name of this constant.
# For example: [:Foo]
Expand Down
12 changes: 12 additions & 0 deletions test/prism/ruby_api_test.rb
Expand Up @@ -103,6 +103,18 @@ def test_location_character_offsets
assert_equal 7, location.end_character_column
end

def test_heredoc?
refute parse_expression("\"foo\"").heredoc?
refute parse_expression("\"foo \#{1}\"").heredoc?
refute parse_expression("`foo`").heredoc?
refute parse_expression("`foo \#{1}`").heredoc?

assert parse_expression("<<~HERE\nfoo\nHERE\n").heredoc?
assert parse_expression("<<~HERE\nfoo \#{1}\nHERE\n").heredoc?
assert parse_expression("<<~`HERE`\nfoo\nHERE\n").heredoc?
assert parse_expression("<<~`HERE`\nfoo \#{1}\nHERE\n").heredoc?
end

private

def parse_expression(source)
Expand Down

0 comments on commit ec83bd7

Please sign in to comment.