Skip to content

Commit

Permalink
+ Source::TreeRewriter#inspect [#728]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Mar 7, 2021
1 parent 9d5e851 commit c4ef4c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/parser/source/tree_rewriter.rb
Expand Up @@ -330,6 +330,11 @@ def in_transaction?
@in_transaction
end

# :nodoc:
def inspect
"#<#{self.class} #{source_buffer.name}: #{action_summary}>"
end

##
# @api private
# @deprecated Use insert_after or wrap
Expand Down Expand Up @@ -361,6 +366,28 @@ def insert_after_multi(range, text)

private

def action_summary
replacements = as_replacements
case replacements.size
when 0 then return 'empty'
when 1..3 then #ok
else
replacements = replacements.first(3)
suffix = '…'
end
parts = replacements.map do |(range, str)|
if str.empty?
"-#{range.to_range}"
elsif range.size == 0
"+#{str.inspect}@#{range.begin_pos}"
else
"^#{str.inspect}@#{range.to_range}"
end
end
parts << suffix if suffix
parts.join(', ')
end

ACTIONS = %i[accept warn raise].freeze
def check_policy_validity
invalid = @policy.values - ACTIONS
Expand Down
11 changes: 11 additions & 0 deletions test/test_source_tree_rewriter.rb
Expand Up @@ -358,4 +358,15 @@ def test_empty_import
assert_equal @rewriter, @rewriter.import!(@rewriter2)
assert_equal @rewriter, @rewriter.import!(@rewriter, offset: 42)
end

def test_inspect
assert_equal '#<Parser::Source::TreeRewriter (rewriter): empty>', @rewriter.inspect
@rewriter.insert_before(@hello, '[')
@rewriter.replace(@ll, 'xxx')
@rewriter.remove(@comma_space)
assert_equal '#<Parser::Source::TreeRewriter (rewriter): +"["@5, ^"xxx"@8...10, -11...13>', @rewriter.inspect
@rewriter.insert_before(@hello, '{')
@rewriter.remove(@world)
assert_equal '#<Parser::Source::TreeRewriter (rewriter): +"{["@5, ^"xxx"@8...10, -11...13, …>', @rewriter.inspect
end
end

0 comments on commit c4ef4c7

Please sign in to comment.