From 1d51e4cadfbc1f445d00887189e06047759c9aec Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Fri, 27 Oct 2023 18:14:51 +0300 Subject: [ruby/prism] Improve comment generation in templates The existing comment generation was hard to read and was making a lot of string manipulation. However, ERB files are already designed to do string manipulation, so we can use that instead. So, instead of doing a split and a map, I opted to use the `#each_line` method to iterate over the lines of the file. Also, in order to add an optional space padding at the beginning of the line, I opted to pad it with a space and to then right trim it. This makes sure that no space is left behind if the line is empty, but a space is added if the line is not empty. https://github.com/ruby/prism/commit/5736711e70 --- prism/templates/lib/prism/node.rb.erb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/prism/templates/lib/prism/node.rb.erb b/prism/templates/lib/prism/node.rb.erb index 47ae6850a0..affd2875ea 100644 --- a/prism/templates/lib/prism/node.rb.erb +++ b/prism/templates/lib/prism/node.rb.erb @@ -32,7 +32,10 @@ module Prism end <%- nodes.each do |node| -%> - <%= "#{node.comment.split("\n").map { |line| line.empty? ? "#" : "# #{line}" }.join("\n ")}\n " if node.comment %>class <%= node.name -%> < Node + <%- node.comment.each_line do |line| -%> + #<%= line.prepend(" ").rstrip %> + <%- end -%> + class <%= node.name -%> < Node <%- node.fields.each do |field| -%> # attr_reader <%= field.name %>: <%= field.rbs_class %> <%= "private " if field.is_a?(Prism::FlagsField) %>attr_reader :<%= field.name %> -- cgit v1.2.3