diff --git a/lib/rufo/new_formatter.rb b/lib/rufo/new_formatter.rb index db25746f..aaac0e25 100644 --- a/lib/rufo/new_formatter.rb +++ b/lib/rufo/new_formatter.rb @@ -18,7 +18,7 @@ def initialize(code, **options) unless @sexp raise ::Rufo::SyntaxError.new end - + @indent_size = 2 @line_length = options.fetch(:line_length, 80) @@ -53,7 +53,7 @@ def visit(node) # # [:program, exps] visit_exps node[1] #, with_indent: true - when :string_literal + when :string_literal, :xstring_literal visit_string_literal(node) when :string_content # [:string_content, exp] @@ -266,13 +266,28 @@ def visit_begin(node) def visit_string_literal(node) # [:string_literal, [:string_content, exps]] - consume_token :on_tstring_beg - - inner = node[1..-1] - + case current_token_kind + when :on_backtick + consume_token :on_backtick + else + consume_token :on_tstring_beg + end + + visit_string_literal_end(node) + end + + def visit_string_literal_end(node) + # [:string_literal, [:string_content, exps]] + inner = node[1] + inner = inner[1..-1] unless node[0] == :xstring_literal visit_exps(inner, with_lines: false) - consume_token :on_tstring_end + case current_token_kind + when :on_backtick + consume_token :on_backtick + else + consume_token :on_tstring_end + end end def visit_string_interpolation(node) diff --git a/spec/lib/rufo/formatter_source_specs/backtick_strings.rb.spec b/spec/lib/rufo/formatter_source_specs/backtick_strings.rb.spec index 244772d8..63d45144 100644 --- a/spec/lib/rufo/formatter_source_specs/backtick_strings.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/backtick_strings.rb.spec @@ -1,4 +1,4 @@ -#~# ORIGINAL +#~# ORIGINAL backtick_strings `cat meow` @@ -6,7 +6,7 @@ `cat meow` -#~# ORIGINAL +#~# ORIGINAL %x() %x( cat meow ) diff --git a/spec/lib/rufo/new_formatter_spec.rb b/spec/lib/rufo/new_formatter_spec.rb index d38a7bb0..03175a9f 100644 --- a/spec/lib/rufo/new_formatter_spec.rb +++ b/spec/lib/rufo/new_formatter_spec.rb @@ -93,7 +93,7 @@ def assert_format(code, expected) assert_source_specs(source_specs) if File.file?(source_specs) end - %w(array_literal hash_literal and_or_not assignment_operators assignments).each do |source_spec_name| + %w(array_literal hash_literal and_or_not assignment_operators assignments backtick_strings).each do |source_spec_name| file = File.join(NEW_FORMATTER_FILE_PATH, "/formatter_source_specs/#{source_spec_name}.rb.spec") fail "missing #{source_spec_name}" unless File.exist?(file) assert_source_specs(file) if File.file?(file)