diff options
author | Ufuk Kayserilioglu <[email protected]> | 2023-10-27 18:14:51 +0300 |
---|---|---|
committer | git <[email protected]> | 2023-10-30 14:15:47 +0000 |
commit | 1d51e4cadfbc1f445d00887189e06047759c9aec (patch) | |
tree | bfccd69e3e1eac646a6f00bb768b8d6dc514f0ed | |
parent | ab4781b64d945e962575f2eac20b72185235d23b (diff) |
[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
-rw-r--r-- | prism/templates/lib/prism/node.rb.erb | 5 |
1 files changed, 4 insertions, 1 deletions
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 %> |