From a60da632971d05dadf12a14da5adfef50aed1f3e Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 11:59:40 +0900 Subject: [PATCH 01/19] add PrismFormatter --- lib/rufo.rb | 9 +- lib/rufo/prism_formatter.rb | 36 ++++++++ spec/lib/rufo/prism_formatter_spec.rb | 123 ++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 lib/rufo/prism_formatter.rb create mode 100644 spec/lib/rufo/prism_formatter_spec.rb diff --git a/lib/rufo.rb b/lib/rufo.rb index 24d94b06..b7044f49 100644 --- a/lib/rufo.rb +++ b/lib/rufo.rb @@ -14,7 +14,13 @@ def initialize(message, lineno) end def self.format(code, **options) - Formatter.format(code, **options) + engine = options.delete(:engine) + case engine + when :prism + PrismFormatter.format(code, **options) + else + Formatter.format(code, **options) + end end end @@ -25,6 +31,7 @@ def self.format(code, **options) require_relative "rufo/parser" require_relative "rufo/formatter" require_relative "rufo/erb_formatter" +require_relative "rufo/prism_formatter" require_relative "rufo/version" require_relative "rufo/file_list" require_relative "rufo/file_finder" diff --git a/lib/rufo/prism_formatter.rb b/lib/rufo/prism_formatter.rb new file mode 100644 index 00000000..30f3612f --- /dev/null +++ b/lib/rufo/prism_formatter.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'prism' + +class Rufo::PrismFormatter + include Rufo::Settings + + def self.format(code, **options) + formatter = new(code, **options) + formatter.format + formatter.result + end + + def initialize(code, **options) + @code = code + @parse_result = Prism.parse(code) + unless @parse_result.errors.empty? + error = @parse_result.errors.first + raise Rufo::SyntaxError.new(error.message, error.location.start_line) + end + + init_settings(options) + end + + def format + @output = @code.dup + + @output.chomp! if @output.end_with?("\n\n") + @output.lstrip! + @output = "\n" if @output.empty? + end + + def result + @output + end +end diff --git a/spec/lib/rufo/prism_formatter_spec.rb b/spec/lib/rufo/prism_formatter_spec.rb new file mode 100644 index 00000000..58a7e829 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_spec.rb @@ -0,0 +1,123 @@ +require "fileutils" + +VERSION = Gem::Version.new(RUBY_VERSION) +FILE_PATH = Pathname.new(File.dirname(__FILE__)) + +def assert_source_specs(source_specs) + relative_path = Pathname.new(source_specs).relative_path_from(FILE_PATH).to_s + + describe relative_path do + tests = [] + current_test = nil + + File.foreach(source_specs).with_index do |line, index| + case + when line =~ /^#~# ORIGINAL ?([\w\s()]+)$/ + # save old test + tests.push current_test if current_test + + # start a new test + + name = $~[1].strip + name = "unnamed test" if name.empty? + + current_test = { name: name, line: index + 1, options: {}, original: "" } + when line =~ /^#~# EXPECTED$/ + current_test[:expected] = "" + when line =~ /^#~# PENDING$/ + # :nocov: + current_test[:pending] = true + # :nocov: + when line =~ /^#~# (.+)$/ + current_test[:options] = eval("{ #{$~[1]} }") + when current_test[:expected] + current_test[:expected] += line + when current_test[:original] + current_test[:original] += line + end + end + + tests.concat([current_test]).each do |test| + it "formats #{test[:name]} (line: #{test[:line]})" do + pending if test[:pending] + formatted = described_class.format(test[:original], **test[:options]) + expected = test[:expected].rstrip + "\n" + expect(formatted).to eq(expected) + idempotency_check = described_class.format(formatted, **test[:options]) + expect(idempotency_check).to eq(formatted) + end + end + end +end + +def assert_format(code, expected = code, **options) + expected = expected.rstrip + "\n" + + line = caller_locations[0].lineno + + opts = options.merge(engine: :prism) + ex = it "formats #{code.inspect} (line: #{line})" do + actual = Rufo.format(code, **opts) + if actual != expected + fail "Expected\n\n~~~\n#{code}\n~~~\nto format to:\n\n~~~\n#{expected}\n~~~\n\nbut got:\n\n~~~\n#{actual}\n~~~\n\n diff = #{expected.inspect}\n #{actual.inspect}" + end + + second = Rufo.format(actual, **opts) + if second != actual + fail "Idempotency check failed. Expected\n\n~~~\n#{actual}\n~~~\nto format to:\n\n~~~\n#{actual}\n~~~\n\nbut got:\n\n~~~\n#{second}\n~~~\n\n diff = #{second.inspect}\n #{actual.inspect}" + end + end + + # This is so we can do `rspec spec/rufo_spec.rb:26` and + # refer to line numbers for assert_format + ex.metadata[:line_number] = line +end + +RSpec.describe Rufo::PrismFormatter do + # Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/*")].each do |source_specs| + # assert_source_specs(source_specs) if File.file?(source_specs) + # end + + # if VERSION >= Gem::Version.new("3.0") + # Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/3.0/*")].each do |source_specs| + # assert_source_specs(source_specs) if File.file?(source_specs) + # end + # end + + # if VERSION >= Gem::Version.new("3.1") + # Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/3.1/*")].each do |source_specs| + # assert_source_specs(source_specs) if File.file?(source_specs) + # end + # end + + # if VERSION >= Gem::Version.new("3.2") + # Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/3.2/*")].each do |source_specs| + # assert_source_specs(source_specs) if File.file?(source_specs) + # end + # end + + # Empty + describe "empty" do + assert_format "", "" + assert_format " ", " " + assert_format "\n", "" + assert_format "\n\n", "" + assert_format "\n\n\n", "" + end + + describe "Syntax errors not handled by Prism", pending: 'no test-case for prism' do + it "raises an unknown syntax error" do + expect { + Rufo.format("def foo; FOO = 1; end", engine: :prism) + }.to raise_error(Rufo::UnknownSyntaxError) + end + end + + describe "Syntax errors handled by Prism" do + it "raises an syntax error" do + expect { + Rufo.format("def foo; FOO = 1; end", engine: :prism) + }.to raise_error(Rufo::SyntaxError) + end + end +end From dd6364320d23220d721767459e1e23e159459404 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:04:26 +0900 Subject: [PATCH 02/19] add prism as a development dependency --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 61159ab3..a0eb27dc 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ gemspec gem "bundler", ">= 1.15" gem "debug", ">= 1.0.0" gem "guard-rspec", "~> 4.0" +gem "prism", "~> 1.2" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rspec_junit_formatter", "~> 0.6.0" From 3ab962090531e744dcbd37d84a327940f495498c Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:06:58 +0900 Subject: [PATCH 03/19] add nil.rb.spec --- spec/lib/rufo/prism_formatter_source_specs/nil.rb.spec | 6 ++++++ spec/lib/rufo/prism_formatter_spec.rb | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/nil.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/nil.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/nil.rb.spec new file mode 100644 index 00000000..fcd14b71 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/nil.rb.spec @@ -0,0 +1,6 @@ +#~# ORIGINAL nil + +nil + +#~# EXPECTED +nil diff --git a/spec/lib/rufo/prism_formatter_spec.rb b/spec/lib/rufo/prism_formatter_spec.rb index 58a7e829..3ce082b9 100644 --- a/spec/lib/rufo/prism_formatter_spec.rb +++ b/spec/lib/rufo/prism_formatter_spec.rb @@ -74,9 +74,9 @@ def assert_format(code, expected = code, **options) end RSpec.describe Rufo::PrismFormatter do - # Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/*")].each do |source_specs| - # assert_source_specs(source_specs) if File.file?(source_specs) - # end + Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/*")].each do |source_specs| + assert_source_specs(source_specs) if File.file?(source_specs) + end # if VERSION >= Gem::Version.new("3.0") # Dir[File.join(FILE_PATH, "/prism_formatter_source_specs/3.0/*")].each do |source_specs| From c29bfc879b72430cc5458b71ed69cd2bc1007658 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:09:36 +0900 Subject: [PATCH 04/19] add chars.rb.spec --- spec/lib/rufo/prism_formatter_source_specs/chars.rb.spec | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/chars.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/chars.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/chars.rb.spec new file mode 100644 index 00000000..839f899d --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/chars.rb.spec @@ -0,0 +1,6 @@ +#~# ORIGINAL char + +?a + +#~# EXPECTED +?a From 619d92b00c15a0d1dd62d114f1b1585b130bedc7 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:10:48 +0900 Subject: [PATCH 05/19] add integers.rb.spec --- spec/lib/rufo/prism_formatter_source_specs/integers.rb.spec | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/integers.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/integers.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/integers.rb.spec new file mode 100644 index 00000000..9816a0bc --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/integers.rb.spec @@ -0,0 +1,6 @@ +#~# ORIGINAL 123 + +123 + +#~# EXPECTED +123 From 34cc82d9af316213dcddcd488cda87e2d3f3501c Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:11:22 +0900 Subject: [PATCH 06/19] add class_variables.rb.spec --- .../prism_formatter_source_specs/class_variables.rb.spec | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/class_variables.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/class_variables.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/class_variables.rb.spec new file mode 100644 index 00000000..90feed28 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/class_variables.rb.spec @@ -0,0 +1,6 @@ +#~# ORIGINAL + +@@foo + +#~# EXPECTED +@@foo From 3869d5138dd5b8695549633c114d77cb258d46ab Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:12:48 +0900 Subject: [PATCH 07/19] add leading_newlines.rb.spec --- .../leading_newlines.rb.spec | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/leading_newlines.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/leading_newlines.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/leading_newlines.rb.spec new file mode 100644 index 00000000..238e9846 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/leading_newlines.rb.spec @@ -0,0 +1,9 @@ +#~# ORIGINAL + + + + +a = 1 + +#~# EXPECTED +a = 1 From e65f2a9846f9926cf016cda08d921bf5779327cc Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:13:40 +0900 Subject: [PATCH 08/19] add rationals.rb.spec --- .../lib/rufo/prism_formatter_source_specs/rationals.rb.spec | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/rationals.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/rationals.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/rationals.rb.spec new file mode 100644 index 00000000..224c49d4 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/rationals.rb.spec @@ -0,0 +1,6 @@ +#~# ORIGINAL + +3.141592r + +#~# EXPECTED +3.141592r From 8e0a37bc04c7c2a2d920e118b9abb5dd3bacf28a Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:14:00 +0900 Subject: [PATCH 09/19] add imaginaries.rb.spec --- .../rufo/prism_formatter_source_specs/imaginaries.rb.spec | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/imaginaries.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/imaginaries.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/imaginaries.rb.spec new file mode 100644 index 00000000..b65b8262 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/imaginaries.rb.spec @@ -0,0 +1,6 @@ +#~# ORIGINAL + +3.141592i + +#~# EXPECTED +3.141592i From 990722eb6709fb79bf2c39f000eebd0d41b2c5a0 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:14:41 +0900 Subject: [PATCH 10/19] add spaces_inside_hash_brace.rb.spec --- .../spaces_inside_hash_brace.rb.spec | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/spaces_inside_hash_brace.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/spaces_inside_hash_brace.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/spaces_inside_hash_brace.rb.spec new file mode 100644 index 00000000..7e1c5df2 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/spaces_inside_hash_brace.rb.spec @@ -0,0 +1,7 @@ +#~# ORIGINAL + +{ 1 => 2 } + +#~# EXPECTED +{ 1 => 2 } + From 367288cf107c25232270f9fca23b43f8b6aaefdb Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:15:28 +0900 Subject: [PATCH 11/19] add floats.rb.spec --- .../prism_formatter_source_specs/floats.rb.spec | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/floats.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/floats.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/floats.rb.spec new file mode 100644 index 00000000..c837f0ea --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/floats.rb.spec @@ -0,0 +1,13 @@ +#~# ORIGINAL + +12.34 + +#~# EXPECTED +12.34 + +#~# ORIGINAL + +12.34e-10 + +#~# EXPECTED +12.34e-10 From 264c0faacd58b0cbb4efb88778e346b084943c35 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:15:54 +0900 Subject: [PATCH 12/19] add booleans.rb.spec --- .../prism_formatter_source_specs/booleans.rb.spec | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/booleans.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/booleans.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/booleans.rb.spec new file mode 100644 index 00000000..218d87dc --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/booleans.rb.spec @@ -0,0 +1,13 @@ +#~# ORIGINAL false + +false + +#~# EXPECTED +false + +#~# ORIGINAL true + +true + +#~# EXPECTED +true From 0627c5f670f4f9c7d683631d0fd9ba37d2989ff0 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:17:10 +0900 Subject: [PATCH 13/19] add special_global_variables.rb.spec --- .../special_global_variables.rb.spec | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/special_global_variables.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/special_global_variables.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/special_global_variables.rb.spec new file mode 100644 index 00000000..6d280ac3 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/special_global_variables.rb.spec @@ -0,0 +1,27 @@ +#~# ORIGINAL + +$~ + +#~# EXPECTED +$~ + +#~# ORIGINAL + +$1 + +#~# EXPECTED +$1 + +#~# ORIGINAL + +$! + +#~# EXPECTED +$! + +#~# ORIGINAL + +$@ + +#~# EXPECTED +$@ From 2273743bcffda0c324bde4537d7843784bcf5908 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:17:42 +0900 Subject: [PATCH 14/19] add symbol_literals.rb.spec --- .../symbol_literals.rb.spec | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/symbol_literals.rb.spec diff --git a/spec/lib/rufo/prism_formatter_source_specs/symbol_literals.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/symbol_literals.rb.spec new file mode 100644 index 00000000..91549979 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/symbol_literals.rb.spec @@ -0,0 +1,27 @@ +#~# ORIGINAL + +:foo + +#~# EXPECTED +:foo + +#~# ORIGINAL + +:"foo" + +#~# EXPECTED +:"foo" + +#~# ORIGINAL + +:"foo#{1}" + +#~# EXPECTED +:"foo#{1}" + +#~# ORIGINAL + +:* + +#~# EXPECTED +:* From 7d4839e08486b8bf0fa2aef12fc61fa9c40e1ddc Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 12:19:49 +0900 Subject: [PATCH 15/19] format --- lib/rufo/prism_formatter.rb | 2 +- spec/lib/rufo/prism_formatter_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rufo/prism_formatter.rb b/lib/rufo/prism_formatter.rb index 30f3612f..b5856679 100644 --- a/lib/rufo/prism_formatter.rb +++ b/lib/rufo/prism_formatter.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'prism' +require "prism" class Rufo::PrismFormatter include Rufo::Settings diff --git a/spec/lib/rufo/prism_formatter_spec.rb b/spec/lib/rufo/prism_formatter_spec.rb index 3ce082b9..53b93d9e 100644 --- a/spec/lib/rufo/prism_formatter_spec.rb +++ b/spec/lib/rufo/prism_formatter_spec.rb @@ -105,7 +105,7 @@ def assert_format(code, expected = code, **options) assert_format "\n\n\n", "" end - describe "Syntax errors not handled by Prism", pending: 'no test-case for prism' do + describe "Syntax errors not handled by Prism", pending: "no test-case for prism" do it "raises an unknown syntax error" do expect { Rufo.format("def foo; FOO = 1; end", engine: :prism) From fc62638bcf22251efdb589812e4cf87bfbe07f59 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Sat, 1 Mar 2025 13:53:52 +0900 Subject: [PATCH 16/19] traverse ast output by prism --- lib/rufo/prism_formatter.rb | 92 ++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/lib/rufo/prism_formatter.rb b/lib/rufo/prism_formatter.rb index b5856679..caa30940 100644 --- a/lib/rufo/prism_formatter.rb +++ b/lib/rufo/prism_formatter.rb @@ -23,14 +23,102 @@ def initialize(code, **options) end def format - @output = @code.dup + visitor = FormatVisitor.new(@code) + @parse_result.value.accept(visitor) + @output = visitor.output @output.chomp! if @output.end_with?("\n\n") @output.lstrip! - @output = "\n" if @output.empty? + @output << "\n" unless @output.end_with?("\n") end def result @output end + + class FormatVisitor < Prism::Visitor + attr_reader :output + + def initialize(code) + super() + @code = code + @output = +"" + end + + def visit_nil_node(_node) + write("nil") + end + + def visit_true_node(_node) + write("true") + end + + def visit_false_node(_node) + write("false") + end + + def visit_integer_node(node) + write_code_at(node.location) + end + + def visit_float_node(node) + write_code_at(node.location) + end + + def visit_rational_node(node) + write_code_at(node.location) + end + + def visit_imaginary_node(node) + write_code_at(node.location) + end + + def visit_symbol_node(node) + write_code_at(node.location) + end + + def visit_interpolated_symbol_node(node) + write_code_at(node.location) + end + + def visit_string_node(node) + write_code_at(node.location) + end + + def visit_class_variable_read_node(node) + write_code_at(node.location) + end + + def visit_global_variable_read_node(node) + write_code_at(node.location) + end + + def visit_numbered_reference_read_node(node) + write_code_at(node.location) + end + + def visit_local_variable_write_node(node) + write(node.name.to_s) + write(" = ") + node.value.accept(self) + end + + def visit_hash_node(node) + write_code_at(node.location) + end + + private + + def write(value) + @output << value + end + + def write_code_at(location) + write(code_at(location)) + end + + def code_at(location) + @code[location.start_offset...location.end_offset] + end + end end From 6f2a1a8da50a7b84e0b42329be3d80ad026b579f Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Fri, 20 Jun 2025 20:48:59 +0900 Subject: [PATCH 17/19] variables --- lib/rufo/prism_formatter.rb | 32 +++++++++++++++++++ .../variables.rb.spec | 15 +++++++++ 2 files changed, 47 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/variables.rb.spec diff --git a/lib/rufo/prism_formatter.rb b/lib/rufo/prism_formatter.rb index caa30940..3b2355d3 100644 --- a/lib/rufo/prism_formatter.rb +++ b/lib/rufo/prism_formatter.rb @@ -5,6 +5,8 @@ class Rufo::PrismFormatter include Rufo::Settings + DEBUG = true + def self.format(code, **options) formatter = new(code, **options) formatter.format @@ -23,6 +25,7 @@ def initialize(code, **options) end def format + debug_log @parse_result visitor = FormatVisitor.new(@code) @parse_result.value.accept(visitor) @output = visitor.output @@ -36,6 +39,12 @@ def result @output end + def debug_log(object) + if DEBUG + p [:debug, object] + end + end + class FormatVisitor < Prism::Visitor attr_reader :output @@ -97,6 +106,10 @@ def visit_numbered_reference_read_node(node) write_code_at(node.location) end + def visit_local_variable_read_node(node) + write_code_at(node.location) + end + def visit_local_variable_write_node(node) write(node.name.to_s) write(" = ") @@ -107,6 +120,19 @@ def visit_hash_node(node) write_code_at(node.location) end + def visit_instance_variable_read_node(node) + write(node.name.to_s) + end + + def visit_statements_node(node) + node.body.each do |child| + child.accept(self) + if child.newline? + write "\n" + end + end + end + private def write(value) @@ -120,5 +146,11 @@ def write_code_at(location) def code_at(location) @code[location.start_offset...location.end_offset] end + + def debug_log(object) + if Rufo::PrismFormatter::DEBUG + p [:debug, object] + end + end end end diff --git a/spec/lib/rufo/prism_formatter_source_specs/variables.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/variables.rb.spec new file mode 100644 index 00000000..b9609f1c --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/variables.rb.spec @@ -0,0 +1,15 @@ +#~# ORIGINAL + +a = 1 + a + +#~# EXPECTED +a = 1 +a + +#~# ORIGINAL + +@foo + +#~# EXPECTED +@foo From 1c6579366fcc6626e52eb215fff012d3ab59b0c2 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Fri, 20 Jun 2025 20:57:11 +0900 Subject: [PATCH 18/19] undef --- lib/rufo/prism_formatter.rb | 10 ++++++++++ .../rufo/prism_formatter_source_specs/undef.rb.spec | 13 +++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/undef.rb.spec diff --git a/lib/rufo/prism_formatter.rb b/lib/rufo/prism_formatter.rb index 3b2355d3..ffe7ad14 100644 --- a/lib/rufo/prism_formatter.rb +++ b/lib/rufo/prism_formatter.rb @@ -124,6 +124,16 @@ def visit_instance_variable_read_node(node) write(node.name.to_s) end + def visit_undef_node(node) + write("undef ") + node.names.each_with_index do |name, i| + if i > 0 + write ", " + end + name.accept(self) + end + end + def visit_statements_node(node) node.body.each do |child| child.accept(self) diff --git a/spec/lib/rufo/prism_formatter_source_specs/undef.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/undef.rb.spec new file mode 100644 index 00000000..70888a86 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/undef.rb.spec @@ -0,0 +1,13 @@ +#~# ORIGINAL + +undef foo + +#~# EXPECTED +undef foo + +#~# ORIGINAL + +undef foo , bar + +#~# EXPECTED +undef foo, bar From 1b2c0060e886e776d8adb71daa3db2241c88ee2c Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Fri, 20 Jun 2025 22:11:03 +0900 Subject: [PATCH 19/19] unary operator --- lib/rufo/prism_formatter.rb | 20 +++++++++-- .../unary_operators.rb.spec | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 spec/lib/rufo/prism_formatter_source_specs/unary_operators.rb.spec diff --git a/lib/rufo/prism_formatter.rb b/lib/rufo/prism_formatter.rb index ffe7ad14..4f917fa8 100644 --- a/lib/rufo/prism_formatter.rb +++ b/lib/rufo/prism_formatter.rb @@ -134,12 +134,28 @@ def visit_undef_node(node) end end + def visit_parentheses_node(node) + write_code_at(node.opening_loc) + node.body.accept(self) + write_code_at(node.closing_loc) + end + + def visit_call_node(node) + write(node.message) + if node.receiver + node.receiver.accept(self) + end + end + def visit_statements_node(node) + previous = nil node.body.each do |child| - child.accept(self) - if child.newline? + if previous&.newline? write "\n" end + + child.accept(self) + previous = child end end diff --git a/spec/lib/rufo/prism_formatter_source_specs/unary_operators.rb.spec b/spec/lib/rufo/prism_formatter_source_specs/unary_operators.rb.spec new file mode 100644 index 00000000..0bed1d69 --- /dev/null +++ b/spec/lib/rufo/prism_formatter_source_specs/unary_operators.rb.spec @@ -0,0 +1,34 @@ +#~# ORIGINAL + +- x + +#~# EXPECTED +-x + +#~# ORIGINAL + ++ x + +#~# EXPECTED ++x + +#~# ORIGINAL + ++x + +#~# EXPECTED ++x + +#~# ORIGINAL + ++(x) + +#~# EXPECTED ++(x) + +#~# ORIGINAL + ++ (x) + +#~# EXPECTED ++(x)