Skip to content

Commit

Permalink
Fix #value for a dstr node.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis committed Jan 23, 2021
1 parent dc28255 commit 060e5b3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/change_fix_value_for_a_dstr_node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#167](https://github.com/rubocop-hq/rubocop-ast/pull/167): Fix `#value` for `dstr` nodes to return the actual string value. ([@dvandersluis][])
1 change: 1 addition & 0 deletions lib/rubocop/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
require_relative 'ast/node/self_class_node'
require_relative 'ast/node/send_node'
require_relative 'ast/node/str_node'
require_relative 'ast/node/dstr_node'
require_relative 'ast/node/super_node'
require_relative 'ast/node/symbol_node'
require_relative 'ast/node/until_node'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Builder < Parser::Builders::Default
def: DefNode,
defined?: DefinedNode,
defs: DefNode,
dstr: DstrNode,
ensure: EnsureNode,
for: ForNode,
forward_args: ForwardArgsNode,
Expand All @@ -68,7 +69,6 @@ class Builder < Parser::Builders::Default
csend: SendNode,
send: SendNode,
str: StrNode,
dstr: StrNode,
xstr: StrNode,
sclass: SelfClassNode,
super: SuperNode,
Expand Down
16 changes: 16 additions & 0 deletions lib/rubocop/ast/node/dstr_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module RuboCop
module AST
# A node extension for `dstr` nodes. This will be used
# in place of a plain node when the builder constructs the AST, making
# its methods available to all `dstr` nodes within RuboCop.
class DstrNode < StrNode
def value
child_nodes.map do |child|
child.respond_to?(:value) ? child.value : child.source
end.join
end
end
end
end
38 changes: 38 additions & 0 deletions spec/rubocop/ast/dstr_node_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::DstrNode do
subject(:dstr_node) { parse_source(source).ast }

describe '#value' do
subject { dstr_node.value }

context 'with a multiline string' do
let(:source) do
<<~RUBY
'this is a multiline ' \
'string'
RUBY
end

it { is_expected.to eq('this is a multiline string') }
end

context 'with interpolation' do
let(:source) do
'"foo #{bar} baz"'
end

it { is_expected.to eq('foo #{bar} baz') }
end

context 'with implicit concatenation' do
let(:source) do
<<~RUBY
'foo ' 'bar ' 'baz'
RUBY
end

it { is_expected.to eq('foo bar baz') }
end
end
end

0 comments on commit 060e5b3

Please sign in to comment.