From 7d124dc6ff9ffe7de8819a014766970f905abc09 Mon Sep 17 00:00:00 2001 From: Jackson Pires Date: Fri, 13 Nov 2026 09:06:32 -0300 Subject: [PATCH 1/6] Add HAML front-end: convert .haml templates into the shared AST Introduce HamlParser, which walks the haml gem's own parse tree (Haml::Parser, haml 5.2/6/7) and builds the same Nodes AST the ERB front-end produces, so the Transformer, RubyUI mapping, RailsHelpers, FormBuilder and LocalsDetector are reused unchanged. The haml gem is an optional dependency, required lazily with a friendly error. - RubyUIConverter.parse(source, format:) dispatches on :erb/:haml; convert_string accepts format:, Template derives it from the extension and FileWalker picks up **/*.{erb,haml}. - Tags, =/!=/&=, control flow with flattened else/elsif/when, blocks, interpolated text (unwrapping haml 5/6 escape wrappers), comments and doctype. Dynamic attribute hashes are splatted for now. - RubyLiteral splits Ruby string literals into text/code parts (Ripper). - Fix: an unparenthesized form_with (model: @x do |form|) now activates the form-builder mapping. Co-Authored-By: Claude Opus 5.5 --- lib/ruby_ui_converter.rb | 30 ++- lib/ruby_ui_converter/cli.rb | 11 +- lib/ruby_ui_converter/converter.rb | 2 +- lib/ruby_ui_converter/file_walker.rb | 17 +- lib/ruby_ui_converter/form_builder.rb | 4 + lib/ruby_ui_converter/haml_parser.rb | 257 ++++++++++++++++++++++++++ lib/ruby_ui_converter/ruby_literal.rb | 100 ++++++++++ lib/ruby_ui_converter/template.rb | 12 +- ruby_ui_converter.gemspec | 2 + test/converter_test.rb | 60 ++++++ test/form_builder_test.rb | 7 + test/haml_parser_test.rb | 171 +++++++++++++++++ test/ruby_literal_test.rb | 47 +++++ 13 files changed, 703 insertions(+), 17 deletions(-) create mode 100644 lib/ruby_ui_converter/haml_parser.rb create mode 100644 lib/ruby_ui_converter/ruby_literal.rb create mode 100644 test/haml_parser_test.rb create mode 100644 test/ruby_literal_test.rb diff --git a/lib/ruby_ui_converter.rb b/lib/ruby_ui_converter.rb index 506face..ba720ec 100644 --- a/lib/ruby_ui_converter.rb +++ b/lib/ruby_ui_converter.rb @@ -8,6 +8,8 @@ require_relative "ruby_ui_converter/lexer" require_relative "ruby_ui_converter/html_tokenizer" require_relative "ruby_ui_converter/parser" +require_relative "ruby_ui_converter/ruby_literal" +require_relative "ruby_ui_converter/haml_parser" require_relative "ruby_ui_converter/code_builder" require_relative "ruby_ui_converter/naming" require_relative "ruby_ui_converter/rails_helpers" @@ -24,13 +26,35 @@ module RubyUIConverter class Error < StandardError; end - # Convert a single .erb string into Ruby/Phlex source (no file IO). + # Template file extension -> source format (front-end parser). + FORMATS = { ".erb" => :erb, ".haml" => :haml }.freeze + + # The source format of a template path ("index.html.haml" -> :haml), or + # nil when it isn't a supported template. + def self.format_for(path) + FORMATS[File.extname(path.to_s)] + end + + # Parse template source into the shared AST (Nodes::Document). This is the + # single dispatch point between the front-ends: ERB (Lexer + HtmlTokenizer + # + Parser) and HAML (HamlParser, backed by the optional haml gem). + def self.parse(source, format: :erb) + case format.to_sym + when :erb then Parser.new(source).parse + when :haml then HamlParser.new(source).parse + else raise Error, "unsupported template format: #{format.inspect}" + end + end + + # Convert a single .erb (or, with format: :haml, .haml) string into + # Ruby/Phlex source (no file IO). # # RubyUIConverter.convert_string("

<%= @title %>

") + # RubyUIConverter.convert_string("%h1= @title", format: :haml) def self.convert_string(source, class_name: "Component", base_namespace: "", - base_class: "Phlex::HTML", **opts) + base_class: "Phlex::HTML", format: :erb, **opts) config = Configuration.new(base_namespace: base_namespace, base_class: base_class, **opts) - document = Parser.new(source).parse + document = parse(source, format: format) builder = CodeBuilder.new(indent: config.indent) builder.line("class #{class_name} < #{config.base_class}") builder.indent diff --git a/lib/ruby_ui_converter/cli.rb b/lib/ruby_ui_converter/cli.rb index 5cdb284..b6668fe 100644 --- a/lib/ruby_ui_converter/cli.rb +++ b/lib/ruby_ui_converter/cli.rb @@ -8,11 +8,12 @@ def self.exit_on_failure? true end - desc "convert PATH", "Convert .erb views/partials under PATH into RubyUI/Phlex .rb files" + desc "convert PATH", "Convert .erb/.haml views/partials under PATH into RubyUI/Phlex .rb files" long_desc <<~DESC - Recursively walks PATH looking for *.erb files and writes an equivalent - .rb component next to each one (e.g. index.html.erb -> index.rb). Rails - partials (_form.html.erb) become their own component classes (form.rb). + Recursively walks PATH looking for *.erb and *.haml files and writes an + equivalent .rb component next to each one (e.g. index.html.erb -> + index.rb). Rails partials (_form.html.haml) become their own component + classes (form.rb). Converting .haml requires the haml gem to be loadable. By default, basic HTML elements are mapped onto RubyUI kit components (a -> Link, button -> Button, input -> Input, table -> Table, ...). @@ -71,7 +72,7 @@ def version def report(results, config) if results.empty? - say "No .erb files found.", :yellow + say "No .erb or .haml files found.", :yellow return end diff --git a/lib/ruby_ui_converter/converter.rb b/lib/ruby_ui_converter/converter.rb index 2fda333..ce7977e 100644 --- a/lib/ruby_ui_converter/converter.rb +++ b/lib/ruby_ui_converter/converter.rb @@ -32,7 +32,7 @@ def root end def run - FileWalker.new(@path).erb_files.map { |file| convert_file(file) } + FileWalker.new(@path).template_files.map { |file| convert_file(file) } end private diff --git a/lib/ruby_ui_converter/file_walker.rb b/lib/ruby_ui_converter/file_walker.rb index d9bad94..68d339f 100644 --- a/lib/ruby_ui_converter/file_walker.rb +++ b/lib/ruby_ui_converter/file_walker.rb @@ -1,22 +1,29 @@ # frozen_string_literal: true module RubyUIConverter - # Finds .erb files under a path (recursively for directories). + # Finds template files (.erb, .haml) under a path (recursively for + # directories). class FileWalker - ERB_GLOB = "**/*.erb" + # Keep in sync with RubyUIConverter::FORMATS. + GLOB = "**/*.{erb,haml}" def initialize(path) @path = path end - def erb_files + def template_files if File.directory?(@path) - Dir.glob(File.join(@path, ERB_GLOB)).select { |f| File.file?(f) }.sort - elsif File.file?(@path) && @path.end_with?(".erb") + Dir.glob(File.join(@path, GLOB)).select { |f| File.file?(f) }.sort + elsif File.file?(@path) && RubyUIConverter.format_for(@path) [@path] else [] end end + + # Kept for backwards compatibility: only the .erb templates. + def erb_files + template_files.select { |f| RubyUIConverter.format_for(f) == :erb } + end end end diff --git a/lib/ruby_ui_converter/form_builder.rb b/lib/ruby_ui_converter/form_builder.rb index b9811dd..1bec324 100644 --- a/lib/ruby_ui_converter/form_builder.rb +++ b/lib/ruby_ui_converter/form_builder.rb @@ -60,6 +60,10 @@ def form_scope(header) end def model_expression(header) + # Drop the block opener first so an unparenthesized call + # (`form_with model: @product do |form|`) doesn't capture `do |form|`. + header = header.sub(/\s*\bdo\s*(\|[^|]*\|)?\s*\z/, "") + if (model = header[/\bmodel:\s*([^,)]+)/, 1]) return model.strip end diff --git a/lib/ruby_ui_converter/haml_parser.rb b/lib/ruby_ui_converter/haml_parser.rb new file mode 100644 index 0000000..0789d02 --- /dev/null +++ b/lib/ruby_ui_converter/haml_parser.rb @@ -0,0 +1,257 @@ +# frozen_string_literal: true + +module RubyUIConverter + # Builds the converter's AST (Nodes) from a HAML template. + # + # Parsing itself is delegated to the haml gem's own parser (Haml::Parser), an + # OPTIONAL dependency required lazily — only when a .haml file is converted — + # so ERB-only users keep `thor` as the sole runtime dependency. Apps with + # .haml views already have haml in their bundle. + # + # The result has the same shape the ERB Parser builds for the equivalent ERB + # (Text/Output/Statement/Control/Element nodes, attribute "parts"), so the + # whole downstream pipeline — Transformer, ComponentMap, RailsHelpers, + # FormBuilder, LocalsDetector — is shared between both formats. + # + # Verified against haml 5.2, 6.x and 7.x, which share the ParseNode tree + # shape and the `Haml::Parser.new(options).call(source)` API. + class HamlParser + # Mid-block keywords. Haml nests them (flattened) among the children of the + # `if`/`case`/`begin` node they belong to; each one starts a new Branch. + MID_KEYWORDS = %w[else elsif when in rescue ensure].freeze + + # With escape_html on, haml 5 and 6 wrap every text interpolation in an + # escape call (haml 5: `Haml::Helpers.html_escape((x))`, haml 6: + # `Haml::Util.escape_html_safe((x).to_s)`); haml 7 doesn't. Phlex escapes + # output itself, so the wrapper is stripped back to the original code. + ESCAPE_WRAPPER = /\A(?:::)?Haml::(?:Helpers\.html_escape|Util\.escape_html_safe|Util\.escape_html)\(\((.*)\)(?:\.to_s)?\)\z/m + + MISSING_HAML = "converting .haml templates requires the haml gem " \ + "(add `gem \"haml\"` to your Gemfile or run `gem install haml`)" + + def self.load_haml! + require_haml + true + rescue LoadError + raise Error, MISSING_HAML + end + + # Separate seam so tests can simulate a missing haml gem. + def self.require_haml + require "haml" + end + + # `Haml::Helpers.html_escape((name))` -> `name`; anything else unchanged. + def self.unwrap_escape(code) + code = code.to_s.strip + (inner = code[ESCAPE_WRAPPER, 1]) ? inner.strip : code + end + + def initialize(source) + @source = source.to_s + end + + def parse + self.class.load_haml! + # escape_html: true (the Rails default) is what lets the parser tell + # `=` (escaped) apart from `!=` (raw). + root = ::Haml::Parser.new(escape_html: true).call(@source) + + document = Nodes::Document.new + convert_children(root.children, document.children) + document + end + + private + + def convert_children(nodes, target) + nodes.each { |node| convert(node, target) } + end + + def convert(node, target) + case node.type + when :tag then target << element(node) + when :plain then target << Nodes::Text.new(content: node.value[:text].to_s) + when :script then script(node, target) + when :silent_script then silent_script(node, target) + when :haml_comment then target << ruby_comment(node.value[:text]) + when :comment then html_comment(node, target) + when :doctype then doctype(node, target) + when :filter then filter(node, target) + end + end + + # --- tags -------------------------------------------------------------- + + def element(node) + value = node.value + element = Nodes::Element.new( + name: value[:name], + attributes: attributes(value), + self_closing: value[:self_closing] ? true : false + ) + inline_content(value, element.children) + convert_children(node.children, element.children) + element + end + + # `%p Hello` / `%p= code` / `%p!= code` / `%p Hi #{name}` + def inline_content(value, target) + content = value[:value] + return if content.nil? || content.to_s.empty? + + if value[:parse] + expression(content, value[:escape_html], value[:escape_interpolation], target) + else + target << Nodes::Text.new(content: content.to_s) + end + end + + # Static attributes (`.a#b`, `(x="y")`, `(disabled)`) become text parts. + # Dynamic hashes (`{...}` / html-style with Ruby values) are passed through + # as a `**(hash)` splat for now. + def attributes(value) + attrs = [] + + value[:attributes].to_h.each do |name, val| + next if val.nil? || val == false + + attrs << [name.to_s, val == true ? nil : [[:text, val.to_s]]] + end + + dynamic = value[:dynamic_attributes] + [dynamic&.new, dynamic&.old].compact.each do |hash_code| + attrs << [:__splat__, [[:erb, output_token(splat_code(hash_code))]]] + end + + attrs + end + + # "{a: 1}" -> "{a: 1}"; "{html_attrs}" -> "html_attrs" (haml treats the + # braces' content as an argument list, so a bare expression is the hash). + def splat_code(hash_code) + inner = hash_code.to_s.strip.sub(/\A\{/, "").sub(/\}\z/, "").strip + args = RailsHelpers.split_args(inner) + pairs = args.any? { |arg| arg =~ /\A(\*\*|[\w?!]+:(?!:)|"[^"]*":|'[^']*':)/ || arg.include?("=>") } + pairs ? "{#{inner}}" : inner + end + + # --- Ruby -------------------------------------------------------------- + + # `= code` (with or without a block) and interpolated plain text, which + # haml hands over as a script node. + def script(node, target) + text = node.value[:text].to_s.strip + + if node.children.any? + target << control(node, text, output: true) + else + expression(text, node.value[:escape_html], node.value[:escape_interpolation], target) + end + end + + def silent_script(node, target) + text = node.value[:text].to_s.strip + + if mid_keyword?(node) && target.last.is_a?(Nodes::Control) + # Defensive: a mid-block keyword left as a sibling of its opener. + branch = Nodes::Branch.new(header: text) + target.last.branches << branch + convert_children(node.children, branch.children) + elsif node.children.any? + target << control(node, text, output: false) + else + target << Nodes::Statement.new(code: text) + end + end + + # `- if a` … `- elsif b` … `- else`, `- case`/`- when`, `- x.each do |i|`, + # `= form_with(...) do |f|`. Haml always closes blocks by indentation, so + # the matching `end` is implicit (the Transformer emits it). + def control(node, header, output:) + control = Nodes::Control.new(block: block_opener?(header), output: output) + branch = Nodes::Branch.new(header: header) + control.branches << branch + + node.children.each do |child| + if mid_keyword?(child) + branch = Nodes::Branch.new(header: child.value[:text].to_s.strip) + control.branches << branch + convert_children(child.children, branch.children) + else + convert(child, branch.children) + end + end + + control + end + + def mid_keyword?(node) + node.type == :silent_script && MID_KEYWORDS.include?(node.value[:keyword]) + end + + def block_opener?(code) + code =~ /\bdo\s*(\|[^|]*\|)?\s*\z/ ? true : false + end + + # A Ruby expression whose value is output: escaped (`=`, `&=`, `~`), raw + # (`!=`), or text with `#{}` interpolation — split into Text + Output + # nodes like the ERB `Hello <%= name %>` equivalent, so helpers and locals + # inside the interpolation are seen by the rest of the pipeline. + def expression(code, escape_html, escape_interpolation, target) + code = code.to_s.strip + parts = interpolated_text_parts(code, escape_html, escape_interpolation) + + if parts + parts.each do |kind, value| + target << (kind == :text ? Nodes::Text.new(content: value) : Nodes::Output.new(code: value)) + end + else + target << Nodes::Output.new(code: code, raw: escape_html == false) + end + end + + # Interpolated text is flagged with escape_interpolation (haml 6/7 plain + # lines, haml 7 inline text); haml 5/6 inline text is only recognizable by + # the escape wrappers around its interpolations. + def interpolated_text_parts(code, escape_html, escape_interpolation) + parts = RubyLiteral.string_parts(code) + return nil unless parts + + wrapped = parts.any? { |kind, value| kind == :code && value.match?(ESCAPE_WRAPPER) } + return nil unless escape_interpolation || (escape_html == false && wrapped) + + parts.map { |kind, value| kind == :code ? [kind, self.class.unwrap_escape(value)] : [kind, value] } + end + + # --- comments, doctype, filters ---------------------------------------- + + def ruby_comment(text) + Nodes::Comment.new(text: text.to_s.strip, html: false) + end + + def html_comment(node, target) + value = node.value + if value[:conditional] || node.children.any? + target << ruby_comment("TODO: HAML block/conditional comment #{value[:conditional]} not converted".squeeze(" ")) + else + target << Nodes::Comment.new(text: value[:text].to_s, html: true) + end + end + + def doctype(node, target) + return if node.value[:type].to_s.downcase == "xml" + + target << Nodes::Doctype.new(value: "") + end + + def filter(node, target) + text = node.value[:text].to_s.rstrip + target << ruby_comment("TODO: HAML :#{node.value[:name]} filter not converted\n#{text}") + end + + def output_token(code) + Lexer::Token.new(type: :output, value: code, raw: false) + end + end +end diff --git a/lib/ruby_ui_converter/ruby_literal.rb b/lib/ruby_ui_converter/ruby_literal.rb new file mode 100644 index 0000000..e335c68 --- /dev/null +++ b/lib/ruby_ui_converter/ruby_literal.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +require "ripper" + +module RubyUIConverter + # Splits a Ruby string literal into its literal text and interpolated code. + # Used by the HAML front-end, whose parser hands interpolated text and + # attribute values over as Ruby source (`"Hello #{name}!"`), so they can be + # turned into the same Text/Output nodes and attribute "parts" the ERB + # front-end produces. Lexing is done with Ripper (stdlib), so this has no + # dependency on the haml gem. + module RubyLiteral + module_function + + QUOTES = ['"', "'"].freeze + + # '"Hi #{name}!"' -> [[:text, "Hi "], [:code, "name"], [:text, "!"]] + # "'static'" -> [[:text, "static"]] + # + # Returns nil unless `code` is exactly one single- or double-quoted string + # literal (so `"a" + b`, `"a".upcase`, `:sym`, `%w[]` and heredocs are + # rejected), or when an escape sequence can't be decoded safely. + def string_parts(code) + code = code.to_s.strip + return nil unless single_string_literal?(code) + + tokens = Ripper.lex(code) + quote = tokens.first[2] + return nil unless QUOTES.include?(quote) && tokens.last[1] == :on_tstring_end + + parts = scan_parts(tokens[1...-1], quote) + parts && merge_text(parts) + rescue StandardError + nil + end + + def single_string_literal?(code) + sexp = Ripper.sexp(code) + statements = sexp && sexp[1] + statements.is_a?(Array) && statements.length == 1 && + statements[0].is_a?(Array) && statements[0][0] == :string_literal + end + + def scan_parts(tokens, quote) + parts = [] + depth = 0 + buffer = +"" + embedded_var = false + + tokens.each do |(_, type, tok, _)| + if depth.positive? + depth += 1 if type == :on_embexpr_beg + depth -= 1 if type == :on_embexpr_end + depth.zero? ? parts << [:code, buffer.strip] : buffer << tok + next + end + + case type + when :on_tstring_content + parts << [:text, unescape(tok, quote)] + when :on_embexpr_beg + depth = 1 + buffer = +"" + when :on_embvar # "#@ivar" / "#$global" shorthand + embedded_var = true + when :on_ivar, :on_gvar, :on_cvar + return nil unless embedded_var + + embedded_var = false + parts << [:code, tok] + else + return nil + end + end + + depth.zero? ? parts : nil + end + + # Decodes the escapes of a literal's raw content. String#undump understands + # every escape String#dump emits (which is how haml hands text over) but + # rejects raw non-ASCII characters, so those are \u-escaped first. + def unescape(content, quote) + return content.gsub(/\\([\\'])/, '\1') if quote == "'" + return content unless content.include?("\\") + + ascii = content.gsub(/[^\x00-\x7F]/) { |char| format("\\u{%x}", char.ord) } + "\"#{ascii}\"".undump.force_encoding(content.encoding) + end + + def merge_text(parts) + parts.each_with_object([]) do |part, merged| + if part[0] == :text && merged.last && merged.last[0] == :text + merged.last[1] = merged.last[1] + part[1] + elsif part[0] != :text || !part[1].empty? + merged << part.dup + end + end + end + end +end diff --git a/lib/ruby_ui_converter/template.rb b/lib/ruby_ui_converter/template.rb index f9c3696..da0130b 100644 --- a/lib/ruby_ui_converter/template.rb +++ b/lib/ruby_ui_converter/template.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true module RubyUIConverter - # Represents a single .erb file and knows how to render its .rb equivalent. + # Represents a single template file (.erb or .haml) and knows how to render + # its .rb equivalent. class Template attr_reader :path, :root, :config @@ -19,7 +20,12 @@ def filename File.basename(path) end - # "index.html.erb" -> "index"; "_form.html.erb" -> "_form" + # Source format, from the extension: :erb or :haml. + def format + RubyUIConverter.format_for(path) || :erb + end + + # "index.html.erb" -> "index"; "_form.html.haml" -> "_form" def basename filename.split(".").first end @@ -67,7 +73,7 @@ def output_path end def tree - @tree ||= Parser.new(source).parse + @tree ||= RubyUIConverter.parse(source, format: format) end def locals diff --git a/ruby_ui_converter.gemspec b/ruby_ui_converter.gemspec index 1d406a2..90ba374 100644 --- a/ruby_ui_converter.gemspec +++ b/ruby_ui_converter.gemspec @@ -40,4 +40,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "minitest", "~> 5.0" + # Optional at runtime: only needed to convert .haml templates (loaded lazily). + spec.add_development_dependency "haml", ">= 5.2" end diff --git a/test/converter_test.rb b/test/converter_test.rb index 296c8c2..f6e04a8 100644 --- a/test/converter_test.rb +++ b/test/converter_test.rb @@ -180,6 +180,66 @@ def test_produces_syntactically_valid_ruby end end + def test_converts_haml_views_and_partials + Dir.mktmpdir("ruc") do |dir| + write(dir, "app/views/users/index.html.haml", <<~HAML) + %h1= @title + - @users.each do |user| + = render "user", user: user + HAML + write(dir, "app/views/users/_user.html.haml", <<~HAML) + %li{id: dom_id(user)} + %strong= user.name + joined \#{time_ago_in_words(user.created_at)} ago by \#{inviter.name} + HAML + + results = RubyUIConverter::Converter.new(File.join(dir, "app/views/users")).run + assert_equal %i[written written], results.map(&:status) + + index_path = File.join(dir, "app/views/users/index.rb") + index = File.read(index_path) + assert_includes index, "# Source: users/index.html.haml" + assert_includes index, "class Index < Phlex::HTML" + assert_includes index, "def initialize(title: nil, users: nil)" + assert_includes index, "render Views::Users::User.new(user: user)" + + partial_path = File.join(dir, "app/views/users/user.rb") + partial = File.read(partial_path) + assert_includes partial, "class User < Phlex::HTML" + assert_includes partial, "def initialize(inviter: nil, user: nil)" + assert_includes partial, "attr_reader :inviter, :user" + + [index_path, partial_path].each do |out| + assert system("ruby", "-c", out, out: File::NULL, err: File::NULL), "invalid Ruby: #{out}" + end + end + end + + def test_converts_erb_and_haml_side_by_side_and_accepts_a_single_haml_file + Dir.mktmpdir("ruc") do |dir| + write(dir, "app/views/pages/about.html.erb", "

about

") + haml = write(dir, "app/views/pages/contact.html.haml", "%p contact") + + results = RubyUIConverter::Converter.new(File.join(dir, "app/views/pages")).run + assert_equal %w[about.rb contact.rb], results.map { |r| File.basename(r.output) } + + single = RubyUIConverter::Converter.new(haml, config: RubyUIConverter::Configuration.new(force: true)).run + assert_equal [:written], single.map(&:status) + end + end + + def test_reports_a_haml_syntax_error_as_a_per_file_error + Dir.mktmpdir("ruc") do |dir| + write(dir, "views/x/bad.html.haml", "%p Hi\n %span nested under inline content\n") + write(dir, "views/x/good.html.erb", "

ok

") + + results = RubyUIConverter::Converter.new(File.join(dir, "views")).run.sort_by(&:source) + + assert_equal :error, results[0].status + assert_equal :written, results[1].status + end + end + def test_maps_elements_to_rubyui_components_end_to_end Dir.mktmpdir("ruc") do |dir| path = write(dir, "views/w/list.html.erb", <<~ERB) diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 74953e0..48f8f98 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -16,6 +16,13 @@ def test_maps_text_fields_to_input_with_reconstructed_name_id_value %(Input(name: "product[name]", id: "product[name]", value: product.name.to_s)) end + def test_maps_fields_of_an_unparenthesized_form_with + out = convert(%(<%= form_with model: @product do |form| %><%= form.text_field :name %><% end %>)) + + assert_includes out, "form_with model: @product do\n" + assert_includes out, %(Input(name: "product[name]", id: "product[name]", value: @product.name.to_s)) + end + def test_adds_the_matching_type_for_typed_fields assert_includes form("<%= form.email_field :email %>"), %(Input(type: "email", name: "product[email]", id: "product[email]", value: product.email.to_s)) diff --git a/test/haml_parser_test.rb b/test/haml_parser_test.rb new file mode 100644 index 0000000..7d94d57 --- /dev/null +++ b/test/haml_parser_test.rb @@ -0,0 +1,171 @@ +# frozen_string_literal: true + +require "test_helper" +require "minitest/mock" + +class HamlParserTest < Minitest::Test + def tree(src) + RubyUIConverter.parse(src, format: :haml) + end + + def convert(src, **opts) + RubyUIConverter.convert_string(src, class_name: "T", base_namespace: "Views", format: :haml, **opts) + end + + # --- AST shape --------------------------------------------------------- + + def test_builds_elements_with_shorthand_class_and_id_as_static_attributes + element = tree("%p.lead.big#intro Hi").children.first + + assert_equal "p", element.name + assert_equal "intro", element.static_attr("id") + assert_equal %w[lead big], element.static_classes + assert_equal "Hi", element.children.first.content + end + + def test_turns_mid_block_keywords_into_branches_of_one_control + control = tree(<<~HAML).children.first + - if a + %p x + - elsif b + %p y + - else + %p z + HAML + + assert_kind_of RubyUIConverter::Nodes::Control, control + assert_equal ["if a", "elsif b", "else"], control.branches.map(&:header) + assert(control.branches.all? { |branch| branch.children.length == 1 }) + end + + def test_marks_output_blocks_and_plain_blocks + nodes = tree(<<~HAML).children + = form_with(model: user) do |form| + = form.submit + - items.each do |item| + %li= item + HAML + + assert nodes[0].output + assert nodes[0].block + refute nodes[1].output + assert nodes[1].block + end + + def test_splits_interpolated_text_into_text_and_output_nodes + nodes = tree("Hello \#{user.name}!").children + + assert_equal [RubyUIConverter::Nodes::Text, RubyUIConverter::Nodes::Output, RubyUIConverter::Nodes::Text], + nodes.map(&:class) + assert_equal "user.name", nodes[1].code + end + + def test_strips_the_escape_wrappers_haml_5_and_6_put_around_interpolations + assert_equal "name", RubyUIConverter::HamlParser.unwrap_escape("Haml::Helpers.html_escape((name))") + assert_equal "@user.name", + RubyUIConverter::HamlParser.unwrap_escape("Haml::Util.escape_html_safe((@user.name).to_s)") + assert_equal "a(b)", RubyUIConverter::HamlParser.unwrap_escape("a(b)") + end + + def test_reports_a_friendly_error_when_haml_is_not_available + RubyUIConverter::HamlParser.stub(:require_haml, -> { raise LoadError }) do + error = assert_raises(RubyUIConverter::Error) { tree("%p hi") } + assert_includes error.message, "requires the haml gem" + end + end + + # --- generated code ---------------------------------------------------- + + def test_converts_tags_with_inline_text_and_expressions + out = convert(<<~HAML) + %h1.title Hello + %p= user.email + HAML + + assert_includes out, %(h1(class: "title") { "Hello" }) + assert_includes out, "p { user.email }" + end + + def test_distinguishes_escaped_and_raw_output + out = convert(<<~HAML) + = escaped + != markup + &= forced + HAML + + assert_includes out, "plain(escaped)" + assert_includes out, "raw(safe(markup))" + assert_includes out, "plain(forced)" + end + + def test_converts_conditionals_and_case_into_ruby_control_flow + out = convert(<<~HAML) + - if a + %p x + - else + %p y + - case kind + - when :a + %span A + HAML + + assert_includes out, " if a\n p { \"x\" }\n else\n p { \"y\" }\n end\n" + assert_includes out, " case kind\n when :a\n span { \"A\" }\n end\n" + end + + def test_converts_each_blocks + out = convert("%ul\n - items.each do |i|\n %li= i\n") + assert_includes out, "items.each do |i|" + assert_includes out, "li { i }" + end + + def test_keeps_interpolated_helpers_visible_to_the_rails_helper_mapping + out = convert(%(%p Go to \#{link_to "Home", root_path})) + assert_includes out, %(Link(href: root_path) { "Home" }) + end + + def test_shares_the_rubyui_and_rails_helper_mapping_with_erb + out = convert(<<~HAML) + .card + %button Save + = link_to "Show", user + = render "shared/header" + HAML + + assert_includes out, %(Card(class: "card") do) + assert_includes out, %(Button() { "Save" }) + assert_includes out, %(Link(href: url_for(user)) { "Show" }) + assert_includes out, "render Views::Shared::Header.new" + end + + def test_maps_form_builder_fields_inside_a_haml_form + out = convert(<<~HAML) + = form_with(model: product) do |form| + = form.text_field :name + = form.submit + HAML + + assert_includes out, "form_with(model: product) do\n" + assert_includes out, %(Input(name: "product[name]", id: "product[name]", value: product.name.to_s)) + assert_includes out, %(Button(type: "submit") { "Save" }) + end + + def test_converts_comments_and_doctype + out = convert("!!!\n-# private note\n/ public note\n%p x\n") + + assert_includes out, "doctype" + assert_includes out, "# private note" + assert_includes out, %(comment { "public note" }) + end + + def test_passes_dynamic_attribute_hashes_through_as_a_splat + out = convert(%(%div{html_attrs}\n%span{**opts, id: "z"} x\n)) + + assert_includes out, "div(**(html_attrs))" + assert_includes out, %(span(**({**opts, id: "z"})) { "x" }) + end + + def test_keeps_boolean_html_style_attributes + assert_includes convert(%(%input(type="text" disabled)), ruby_ui: false), %(input(type: "text", disabled: true)) + end +end diff --git a/test/ruby_literal_test.rb b/test/ruby_literal_test.rb new file mode 100644 index 0000000..e42006e --- /dev/null +++ b/test/ruby_literal_test.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require "test_helper" + +class RubyLiteralTest < Minitest::Test + def parts(code) + RubyUIConverter::RubyLiteral.string_parts(code) + end + + def test_splits_a_double_quoted_literal_into_text_and_code + assert_equal [[:text, "Hello "], [:code, "@user.name"], [:text, "!"]], parts('"Hello #{@user.name}!"') + end + + def test_keeps_nested_strings_and_braces_inside_interpolations + assert_equal [[:code, 'a ? "x" : { b: 1 }[:b]'], [:text, " end"]], parts('"#{a ? "x" : { b: 1 }[:b]} end"') + end + + def test_supports_the_ivar_shorthand_interpolation + assert_equal [[:text, "x "], [:code, "@foo"], [:text, " y"]], parts('"x #@foo y"') + end + + def test_decodes_escapes_in_double_quoted_literals + assert_equal [[:text, %(say "hi"\nnow \#{not})]], parts('"say \"hi\"\nnow \#{not}"') + end + + def test_decodes_dumped_unicode_escapes_like_haml_produces + assert_equal [[:text, "Olá "], [:code, "x"]], parts('"Olá #{x}"') + end + + def test_keeps_raw_non_ascii_text_even_next_to_escapes + assert_equal [[:text, "Olá\t\"mundo\""]], parts('"Olá\t\"mundo\""') + end + + def test_decodes_single_quoted_literals + assert_equal [[:text, "it's static"]], parts(%q('it\'s static')) + end + + def test_returns_an_empty_list_for_an_empty_string + assert_equal [], parts('""') + end + + def test_rejects_anything_that_is_not_a_single_string_literal + ['"a" + b', '"a".upcase', ":delete", "user.name", "%w[a b]", '"a" "b"', "", "x(", '"#{x}" if y'].each do |code| + assert_nil parts(code), "expected nil for #{code.inspect}" + end + end +end From fa88af7ac88a9fe6fd8da1a264722deb3ecff1c3 Mon Sep 17 00:00:00 2001 From: Jackson Pires Date: Fri, 13 Nov 2026 09:06:32 -0300 Subject: [PATCH 2/6] Convert HAML attributes with HAML's merge rules HamlAttributes builds the attribute parts list from static attributes, html-style and hash attributes and object references: class values merge (strings into one string, expressions into a Phlex-flattened array), id values join with "_", false/nil drop, true is boolean, symbols become text, literal data:/aria: hashes expand to hyphenated attributes, [@obj] maps to dom_id/dom_class, and unsplittable hashes become a splat. String literals become static text so ComponentMap rules (Checkbox, Card) keep matching. Attribute expressions that are already self-delimited ([a, b], {...}, dom_id(user, :row)) are no longer wrapped in parentheses. Co-Authored-By: Claude Opus 5.5 --- lib/ruby_ui_converter.rb | 1 + lib/ruby_ui_converter/haml_attributes.rb | 186 +++++++++++++++++++++++ lib/ruby_ui_converter/haml_parser.rb | 35 +---- lib/ruby_ui_converter/transformer.rb | 37 ++++- test/haml_attributes_test.rb | 93 ++++++++++++ test/transformer_test.rb | 7 + 6 files changed, 324 insertions(+), 35 deletions(-) create mode 100644 lib/ruby_ui_converter/haml_attributes.rb create mode 100644 test/haml_attributes_test.rb diff --git a/lib/ruby_ui_converter.rb b/lib/ruby_ui_converter.rb index ba720ec..f5d57f3 100644 --- a/lib/ruby_ui_converter.rb +++ b/lib/ruby_ui_converter.rb @@ -9,6 +9,7 @@ require_relative "ruby_ui_converter/html_tokenizer" require_relative "ruby_ui_converter/parser" require_relative "ruby_ui_converter/ruby_literal" +require_relative "ruby_ui_converter/haml_attributes" require_relative "ruby_ui_converter/haml_parser" require_relative "ruby_ui_converter/code_builder" require_relative "ruby_ui_converter/naming" diff --git a/lib/ruby_ui_converter/haml_attributes.rb b/lib/ruby_ui_converter/haml_attributes.rb new file mode 100644 index 0000000..952a36c --- /dev/null +++ b/lib/ruby_ui_converter/haml_attributes.rb @@ -0,0 +1,186 @@ +# frozen_string_literal: true + +module RubyUIConverter + # Turns the attributes of a HAML tag (as Haml::Parser reports them) into the + # converter's attribute list — `[[name, parts], ...]`, the same shape the ERB + # Parser builds (see Nodes) — merging them the way HAML does: + # + # * sources, in order: static (`.a#b`, `(x="y")`, `(disabled)`), html-style + # dynamic `(x=expr)`, the `{...}` hash, then the object reference `[@obj]`; + # * `class` values are merged (space-joined), `id` values joined with `_`; + # any other repeated attribute is overridden by the later source; + # * `false`/`nil` literals drop the attribute, `true` makes it boolean; + # * literal `data:`/`aria:` hashes expand to hyphenated attributes + # (`data: {turbo_method: :delete}` -> `"data-turbo-method": "delete"`); + # * string literals become text/erb parts (so ComponentMap matchers like + # `static_classes` keep working); symbol literals become text; + # * a hash Haml can't split (`{html_attrs}`, `{**opts}`) becomes a + # `**(...)` splat. + class HamlAttributes + NESTED = %w[data aria].freeze + MERGED = { "class" => " ", "id" => "_" }.freeze + + Entry = Struct.new(:name, :values) + + def self.build(tag_value) + new(tag_value).build + end + + def initialize(tag_value) + @tag = tag_value + @entries = [] + end + + def build + static_attributes + dynamic = @tag[:dynamic_attributes] + dynamic_hash(dynamic&.new) + dynamic_hash(dynamic&.old) + object_reference + @entries.map { |entry| finalize(entry) } + end + + private + + # --- sources ----------------------------------------------------------- + + def static_attributes + @tag[:attributes].to_h.each do |name, value| + case value + when true then add(name.to_s, [:bool]) + when nil, false then add(name.to_s, nil) + else add(name.to_s, [:parts, [[:text, value.to_s]]]) + end + end + end + + def dynamic_hash(hash_code) + return if hash_code.nil? || hash_code.strip.empty? + + pairs = ::Haml::AttributeParser.parse(hash_code) + if pairs + pairs.each { |name, code| add_code(name.to_s, code) } + else + @entries << Entry.new(:__splat__, [[:parts, [erb(splat_code(hash_code))]]]) + end + end + + # `%div[@user]` / `%div[@user, :edit]` -> Rails' dom_id / dom_class. + def object_reference + ref = @tag[:object_ref] + return if ref.nil? || ref == :nil + + args = RailsHelpers.split_args(ref.to_s.strip.sub(/\A\[/, "").sub(/\]\z/, "")) + return if args.empty? + + call_args = args.first(2).join(", ") + add("id", [:code, "dom_id(#{call_args})"]) + add("class", [:code, "dom_class(#{call_args})"]) + end + + # --- values ------------------------------------------------------------ + + def add_code(name, code) + if NESTED.include?(name) && (nested = nested_hash(code)) + nested.each { |key, value| add_code("#{name}-#{key.to_s.tr("_", "-")}", value) } + else + add(name, value_for(code)) + end + end + + def nested_hash(code) + code = code.to_s.strip + return nil unless code.start_with?("{") + + ::Haml::AttributeParser.parse(code) + end + + # nil means "drop the attribute". + def value_for(code) + code = code.to_s.strip + return [:bool] if code == "true" + return nil if %w[false nil].include?(code) + return [:parts, [[:text, code[1..]]]] if code.match?(/\A:\w+[?!]?\z/) + + parts = RubyLiteral.string_parts(code) + return [:parts, parts.map { |kind, value| kind == :text ? [:text, value] : erb(value) }] if parts + + [:code, code] + end + + def add(name, value) + entry = @entries.find { |e| e.name == name } + + if MERGED.key?(name) + return unless value + + entry ? entry.values << value : @entries << Entry.new(name, [value]) + elsif value.nil? + @entries.delete(entry) + elsif entry + entry.values = [value] + else + @entries << Entry.new(name, [value]) + end + end + + # --- output ------------------------------------------------------------ + + def finalize(entry) + values = entry.values + return [entry.name, nil] if values == [[:bool]] + return [entry.name, to_parts(values.first)] if values.length == 1 + + separator = MERGED.fetch(entry.name, " ") + if entry.name == "class" && values.any? { |kind, _| kind == :code } + # Arbitrary expressions may be arrays/nil: let Phlex flatten them. + [entry.name, [erb("[#{values.map { |value| to_code(value) }.join(", ")}]")]] + else + parts = values.flat_map.with_index do |value, index| + (index.zero? ? [] : [[:text, separator]]) + to_parts(value) + end + [entry.name, parts] + end + end + + def to_parts(value) + kind, payload = value + case kind + when :parts then payload + when :code then [erb(payload)] + when :bool then [erb("true")] + end + end + + def to_code(value) + kind, payload = value + return payload if kind == :code + return "true" if kind == :bool + return payload[0][1].value if payload.length == 1 && payload[0][0] == :erb + + body = payload.map do |part_kind, part| + part_kind == :text ? escape(part) : "\#{#{part.value}}" + end.join + "\"#{body}\"" + end + + def escape(text) + text.gsub(/[\\"\n\t]|\#\{/) do |match| + { "\\" => "\\\\", "\"" => "\\\"", "\n" => "\\n", "\t" => "\\t", "\#{" => "\\\#{" }[match] + end + end + + # "{a: 1}" -> "{a: 1}"; "{html_attrs}" -> "html_attrs" (haml treats the + # braces' content as an argument list, so a bare expression is the hash). + def splat_code(hash_code) + inner = hash_code.to_s.strip.sub(/\A\{/, "").sub(/\}\z/, "").strip + args = RailsHelpers.split_args(inner) + pairs = args.any? { |arg| arg =~ /\A(\*\*|[\w?!]+:(?!:)|"[^"]*":|'[^']*':)/ || arg.include?("=>") } + pairs ? "{#{inner}}" : inner + end + + def erb(code) + [:erb, Lexer::Token.new(type: :output, value: code, raw: false)] + end + end +end diff --git a/lib/ruby_ui_converter/haml_parser.rb b/lib/ruby_ui_converter/haml_parser.rb index 0789d02..cc546db 100644 --- a/lib/ruby_ui_converter/haml_parser.rb +++ b/lib/ruby_ui_converter/haml_parser.rb @@ -87,7 +87,7 @@ def element(node) value = node.value element = Nodes::Element.new( name: value[:name], - attributes: attributes(value), + attributes: HamlAttributes.build(value), self_closing: value[:self_closing] ? true : false ) inline_content(value, element.children) @@ -107,35 +107,6 @@ def inline_content(value, target) end end - # Static attributes (`.a#b`, `(x="y")`, `(disabled)`) become text parts. - # Dynamic hashes (`{...}` / html-style with Ruby values) are passed through - # as a `**(hash)` splat for now. - def attributes(value) - attrs = [] - - value[:attributes].to_h.each do |name, val| - next if val.nil? || val == false - - attrs << [name.to_s, val == true ? nil : [[:text, val.to_s]]] - end - - dynamic = value[:dynamic_attributes] - [dynamic&.new, dynamic&.old].compact.each do |hash_code| - attrs << [:__splat__, [[:erb, output_token(splat_code(hash_code))]]] - end - - attrs - end - - # "{a: 1}" -> "{a: 1}"; "{html_attrs}" -> "html_attrs" (haml treats the - # braces' content as an argument list, so a bare expression is the hash). - def splat_code(hash_code) - inner = hash_code.to_s.strip.sub(/\A\{/, "").sub(/\}\z/, "").strip - args = RailsHelpers.split_args(inner) - pairs = args.any? { |arg| arg =~ /\A(\*\*|[\w?!]+:(?!:)|"[^"]*":|'[^']*':)/ || arg.include?("=>") } - pairs ? "{#{inner}}" : inner - end - # --- Ruby -------------------------------------------------------------- # `= code` (with or without a block) and interpolated plain text, which @@ -249,9 +220,5 @@ def filter(node, target) text = node.value[:text].to_s.rstrip target << ruby_comment("TODO: HAML :#{node.value[:name]} filter not converted\n#{text}") end - - def output_token(code) - Lexer::Token.new(type: :output, value: code, raw: false) - end end end diff --git a/lib/ruby_ui_converter/transformer.rb b/lib/ruby_ui_converter/transformer.rb index 13e7666..5906a4a 100644 --- a/lib/ruby_ui_converter/transformer.rb +++ b/lib/ruby_ui_converter/transformer.rb @@ -276,8 +276,43 @@ def attr_value(parts) # Expressions with whitespace (`dom_id user`, `a ? b : c`) would parse # incorrectly inside the attribute argument list, so wrap them in parens. + # Self-delimited expressions (`[a, b]`, `{a: 1}`, `dom_id(user, :x)`) are + # safe as-is. def bare_expression(code) - code =~ /\s/ ? "(#{code})" : code + return code unless code =~ /\s/ + + self_delimited?(code) ? code : "(#{code})" + end + + GROUP_TOKENS = { + on_lparen: 1, on_lbracket: 1, on_lbrace: 1, + on_rparen: -1, on_rbracket: -1, on_rbrace: -1 + }.freeze + CALL_PREFIX_TOKENS = %i[on_ident on_const on_ivar on_cvar on_gvar on_period].freeze + + # True when the code is a bracketed literal or a parenthesized call + # (optionally on a receiver chain written without spaces) whose opening + # bracket closes exactly at the end of the code. + def self_delimited?(code) + tokens = Ripper.lex(code) + index = 0 + while index < tokens.length && + (CALL_PREFIX_TOKENS.include?(tokens[index][1]) || + (tokens[index][1] == :on_op && ["::", "&."].include?(tokens[index][2]))) + index += 1 + end + return false if index.positive? && !%i[on_ident on_const].include?(tokens[index - 1][1]) + return false unless tokens[index] && GROUP_TOKENS[tokens[index][1]] == 1 + return false if index.positive? && tokens[index][1] != :on_lparen + + depth = 0 + tokens[index..].each_with_index do |token, offset| + depth += GROUP_TOKENS.fetch(token[1], 0) + return index + offset == tokens.length - 1 if depth.zero? + end + false + rescue StandardError + false end def interpolated(parts) diff --git a/test/haml_attributes_test.rb b/test/haml_attributes_test.rb new file mode 100644 index 0000000..af38870 --- /dev/null +++ b/test/haml_attributes_test.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +require "test_helper" + +class HamlAttributesTest < Minitest::Test + def element(src) + RubyUIConverter.parse(src, format: :haml).children.first + end + + def convert(src, **opts) + RubyUIConverter.convert_string(src, class_name: "T", format: :haml, **opts) + end + + def test_turns_literal_hash_strings_into_static_attributes_matchers_can_see + el = element(%(%span{class: "badge big", title: 'Hi'} x)) + + assert_equal %w[badge big], el.static_classes + assert_equal "Hi", el.static_attr("title") + end + + def test_merges_shorthand_classes_with_hash_classes + assert_equal %w[card shadow], element(%(.card{class: "shadow"})).static_classes + assert_includes convert(%(%p.title{class: "x \#{extra}"} y)), %(p(class: "title x \#{extra}") { "y" }) + end + + def test_merges_classes_with_arbitrary_expressions_into_an_array + assert_includes convert(%(%p.row{class: [a, ("on" if b)]} y)), %(p(class: ["row", [a, ("on" if b)]]) { "y" }) + assert_includes convert(%(%p{class: css} y)), "p(class: css)" + end + + def test_joins_ids_with_an_underscore_like_haml + assert_includes convert(%(%p#main{id: "x"} y)), %(p(id: "main_x")) + end + + def test_later_sources_override_other_attributes + assert_includes convert(%(%a(title="a"){title: "b"} y), ruby_ui: false), %(a(title: "b")) + end + + def test_supports_html_style_attributes_with_ruby_values + assert_includes convert(%(%a(href="/x" title=@title) Go), ruby_ui: false), %(a(href: "/x", title: @title) { "Go" }) + end + + def test_supports_hash_rocket_keys_and_symbol_values + out = convert(%(%a{:href => "#", "data-x" => 1, method: :delete} Del), ruby_ui: false) + assert_includes out, %(a(href: "#", "data-x": 1, method: "delete") { "Del" }) + end + + def test_expands_literal_data_and_aria_hashes_into_hyphenated_attributes + out = convert(%(%button{data: {turbo_method: :delete, id: item.id}, aria: {label: "Close"}} x), ruby_ui: false) + assert_includes out, %(button("data-turbo-method": "delete", "data-id": item.id, "aria-label": "Close")) + end + + def test_keeps_non_literal_data_hashes_as_an_expression + assert_includes convert(%(%p{data: stimulus_data} x)), "p(data: stimulus_data)" + end + + def test_drops_false_and_nil_attributes_and_keeps_true_as_boolean + out = convert(%(%input{type: "text", disabled: true, checked: false, value: nil}), ruby_ui: false) + assert_includes out, %(input(type: "text", disabled: true)) + end + + def test_dispatches_inputs_on_the_hash_type_to_rubyui_components + assert_includes convert(%(%input{type: "checkbox", name: "all"})), %(Checkbox(name: "all")) + end + + def test_maps_object_references_to_rails_dom_helpers + assert_includes convert(%(%li.item[@user] x)), %(li(class: ["item", dom_class(@user)], id: dom_id(@user))) + assert_includes convert(%(%li[@user, :edit] x)), %(li(id: dom_id(@user, :edit), class: dom_class(@user, :edit))) + end + + def test_splats_hashes_haml_cannot_split + assert_includes convert(%(%div{html_attrs} x)), "div(**(html_attrs))" + assert_includes convert(%(%div{**opts, id: "z"} x)), %(div(**({**opts, id: "z"}))) + end + + def test_keeps_non_ascii_attribute_text + assert_includes convert(%(%p{title: "Olá"} x)), %(p(title: "Olá")) + end + + def test_object_reference_ivars_are_initializer_args_of_a_top_level_view + Dir.mktmpdir("ruc") do |dir| + path = File.join(dir, "app/views/users/show.html.haml") + FileUtils.mkdir_p(File.dirname(path)) + File.write(path, "%div[@user]\n %h1{title: @title} x\n") + + RubyUIConverter::Converter.new(path).run + out = File.join(dir, "app/views/users/show.rb") + + assert_includes File.read(out), "def initialize(title: nil, user: nil)" + assert system("ruby", "-c", out, out: File::NULL, err: File::NULL) + end + end +end diff --git a/test/transformer_test.rb b/test/transformer_test.rb index c9093f8..3fa8474 100644 --- a/test/transformer_test.rb +++ b/test/transformer_test.rb @@ -39,6 +39,13 @@ def test_parenthesizes_attribute_expressions_that_take_unparenthesized_arguments assert_includes out, %(div(id: (dom_id user), class: "row")) end + def test_leaves_self_delimited_attribute_expressions_unwrapped + assert_includes convert(%(

x

)), "p(id: dom_id(user, :row))" + assert_includes convert(%(

x

)), "p(class: [a, b])" + assert_includes convert(%(

">x

)), %(p(class: ([a, b].join(" ")))) + assert_includes convert(%(

x

)), "p(class: (foo(a) + b))" + end + def test_converts_if_else_into_ruby_control_flow out = convert("<% if a %>

x

<% else %>

y

<% end %>") assert_includes out, "if a" From 99456d2b08718051fcc24b413c542f704d2c3d8a Mon Sep 17 00:00:00 2001 From: Jackson Pires Date: Fri, 13 Nov 2026 09:06:32 -0300 Subject: [PATCH 3/6] Convert HAML text, whitespace, comments and filters - HAML text is routed through the ERB parser (#{x} -> <%= x %>), so inline HTML becomes elements and interpolated helpers such as link_to map like their ERB equivalents. - Text next to rendered siblings gets a separating space (HAML's line break), except around tags that remove outer whitespace. - A / comment with nested content becomes comment do ... end (Comment#children; locals inside it are detected); IE conditional comments are left as a TODO. - Filters: :javascript/:css -> script/style, :plain/:preserve, :escaped, :erb (ERB parser), :ruby (statements); others become TODO comments. - Fix: a same-line space between inline siblings is kept as Phlex whitespace instead of being dropped (ERB too). Co-Authored-By: Claude Opus 5.5 --- lib/ruby_ui_converter/haml_parser.rb | 144 +++++++++++++++++++---- lib/ruby_ui_converter/locals_detector.rb | 2 + lib/ruby_ui_converter/nodes.rb | 6 +- lib/ruby_ui_converter/transformer.rb | 28 ++++- test/haml_text_test.rb | 141 ++++++++++++++++++++++ test/transformer_test.rb | 7 ++ 6 files changed, 301 insertions(+), 27 deletions(-) create mode 100644 test/haml_text_test.rb diff --git a/lib/ruby_ui_converter/haml_parser.rb b/lib/ruby_ui_converter/haml_parser.rb index cc546db..a36f87e 100644 --- a/lib/ruby_ui_converter/haml_parser.rb +++ b/lib/ruby_ui_converter/haml_parser.rb @@ -11,7 +11,9 @@ module RubyUIConverter # The result has the same shape the ERB Parser builds for the equivalent ERB # (Text/Output/Statement/Control/Element nodes, attribute "parts"), so the # whole downstream pipeline — Transformer, ComponentMap, RailsHelpers, - # FormBuilder, LocalsDetector — is shared between both formats. + # FormBuilder, LocalsDetector — is shared between both formats. HAML text + # (which may hold inline HTML) is itself routed through the ERB Parser, with + # each `#{}` interpolation turned into an `<%= %>` tag. # # Verified against haml 5.2, 6.x and 7.x, which share the ParseNode tree # shape and the `Haml::Parser.new(options).call(source)` API. @@ -26,6 +28,11 @@ class HamlParser # output itself, so the wrapper is stripped back to the original code. ESCAPE_WRAPPER = /\A(?:::)?Haml::(?:Helpers\.html_escape|Util\.escape_html_safe|Util\.escape_html)\(\((.*)\)(?:\.to_s)?\)\z/m + # Filters whose content is HAML/HTML text (interpolated, unescaped HTML). + TEXT_FILTERS = %w[plain preserve].freeze + # Filters rendered as an element wrapping their raw content. + RAW_ELEMENT_FILTERS = { "javascript" => "script", "css" => "style" }.freeze + MISSING_HAML = "converting .haml templates requires the haml gem " \ "(add `gem \"haml\"` to your Gemfile or run `gem install haml`)" @@ -64,14 +71,52 @@ def parse private + # Converts sibling HAML nodes, then pads text that sits next to another + # rendered sibling: HAML puts a newline between lines, so `Signed in as` + # followed by `%strong= name` renders with a space in between. def convert_children(nodes, target) - nodes.each { |node| convert(node, target) } + rendered = nodes.reject { |node| silent?(node) } + + nodes.each do |node| + start = target.length + convert(node, target) + next if silent?(node) + + position = rendered.index { |other| other.equal?(node) } + before = position.positive? && !nuke_outer?(rendered[position - 1]) + after = position < rendered.length - 1 && !nuke_outer?(rendered[position + 1]) + pad_text(target, start, before: before && !nuke_outer?(node), after: after && !nuke_outer?(node)) + end + end + + # Nodes that render nothing between their siblings. + def silent?(node) + node.type == :haml_comment || (node.type == :silent_script && node.children.empty?) + end + + # `%img>`: whitespace around the tag is removed. + def nuke_outer?(node) + node.type == :tag && node.value[:nuke_outer_whitespace] ? true : false + end + + def pad_text(target, start, before:, after:) + return if start >= target.length + + first = target[start] + if before && first.is_a?(Nodes::Text) && first.content !~ /\A\s/ + target[start] = Nodes::Text.new(content: " #{first.content}") + end + + last = target[-1] + return unless after && last.is_a?(Nodes::Text) && last.content !~ /\s\z/ + + target[-1] = Nodes::Text.new(content: "#{last.content} ") end def convert(node, target) case node.type when :tag then target << element(node) - when :plain then target << Nodes::Text.new(content: node.value[:text].to_s) + when :plain then target.concat(text_nodes([[:text, node.value[:text].to_s]])) when :script then script(node, target) when :silent_script then silent_script(node, target) when :haml_comment then target << ruby_comment(node.value[:text]) @@ -81,6 +126,16 @@ def convert(node, target) end end + # HAML text is unescaped HTML with escaped `#{}` interpolations — exactly + # what ERB text with `<%= %>` tags is — so it goes through the ERB Parser: + # inline markup (`Hi you`) becomes real elements, and + # interpolated helpers (`#{link_to ...}`) become Output nodes the + # RailsHelpers mapping understands. + def text_nodes(parts) + erb = parts.map { |kind, value| kind == :text ? value : "<%= #{value} %>" }.join + Parser.new(erb).parse.children + end + # --- tags -------------------------------------------------------------- def element(node) @@ -103,7 +158,7 @@ def inline_content(value, target) if value[:parse] expression(content, value[:escape_html], value[:escape_interpolation], target) else - target << Nodes::Text.new(content: content.to_s) + target.concat(text_nodes([[:text, content.to_s]])) end end @@ -141,19 +196,22 @@ def silent_script(node, target) # the matching `end` is implicit (the Transformer emits it). def control(node, header, output:) control = Nodes::Control.new(block: block_opener?(header), output: output) - branch = Nodes::Branch.new(header: header) - control.branches << branch + segments = [[header, []]] node.children.each do |child| if mid_keyword?(child) - branch = Nodes::Branch.new(header: child.value[:text].to_s.strip) - control.branches << branch - convert_children(child.children, branch.children) + segments << [child.value[:text].to_s.strip, child.children.dup] else - convert(child, branch.children) + segments.last[1] << child end end + segments.each do |branch_header, children| + branch = Nodes::Branch.new(header: branch_header) + convert_children(children, branch.children) + control.branches << branch + end + control end @@ -166,17 +224,15 @@ def block_opener?(code) end # A Ruby expression whose value is output: escaped (`=`, `&=`, `~`), raw - # (`!=`), or text with `#{}` interpolation — split into Text + Output - # nodes like the ERB `Hello <%= name %>` equivalent, so helpers and locals - # inside the interpolation are seen by the rest of the pipeline. + # (`!=`), or text with `#{}` interpolation — parsed like the ERB + # `Hello <%= name %>` equivalent, so helpers and locals inside the + # interpolation are seen by the rest of the pipeline. def expression(code, escape_html, escape_interpolation, target) code = code.to_s.strip parts = interpolated_text_parts(code, escape_html, escape_interpolation) if parts - parts.each do |kind, value| - target << (kind == :text ? Nodes::Text.new(content: value) : Nodes::Output.new(code: value)) - end + target.concat(text_nodes(parts)) else target << Nodes::Output.new(code: code, raw: escape_html == false) end @@ -195,18 +251,31 @@ def interpolated_text_parts(code, escape_html, escape_interpolation) parts.map { |kind, value| kind == :code ? [kind, self.class.unwrap_escape(value)] : [kind, value] } end + # Text with HAML `#{}` interpolation (filter content) as text/code parts. + def interpolation_parts(text) + return [[:text, text]] unless text =~ /#[{@$]/ + + RubyLiteral.string_parts(::Haml::Util.unescape_interpolation(text)) || [[:text, text]] + end + # --- comments, doctype, filters ---------------------------------------- def ruby_comment(text) Nodes::Comment.new(text: text.to_s.strip, html: false) end + # `/ note` -> comment { "note" }; a `/` with nested HAML renders that + # content inside the comment (`comment do ... end`). IE conditional + # comments (`/[if IE]`) are obsolete and left as a TODO. def html_comment(node, target) value = node.value - if value[:conditional] || node.children.any? - target << ruby_comment("TODO: HAML block/conditional comment #{value[:conditional]} not converted".squeeze(" ")) + + if value[:conditional] + target << ruby_comment("TODO: HAML conditional comment #{value[:conditional]} dropped (content not converted)") else - target << Nodes::Comment.new(text: value[:text].to_s, html: true) + comment = Nodes::Comment.new(text: value[:text].to_s, html: true) + convert_children(node.children, comment.children) + target << comment end end @@ -217,8 +286,41 @@ def doctype(node, target) end def filter(node, target) - text = node.value[:text].to_s.rstrip - target << ruby_comment("TODO: HAML :#{node.value[:name]} filter not converted\n#{text}") + name = node.value[:name].to_s + text = node.value[:text].to_s + + if RAW_ELEMENT_FILTERS.key?(name) + target << raw_element(RAW_ELEMENT_FILTERS[name], text) + elsif TEXT_FILTERS.include?(name) + target.concat(text_nodes(interpolation_parts(text.rstrip))) + elsif name == "escaped" + interpolation_parts(text.rstrip).each do |kind, value| + target << (kind == :text ? Nodes::Text.new(content: value) : Nodes::Output.new(code: value)) + end + elsif name == "erb" + target.concat(Parser.new(text).parse.children) + elsif name == "ruby" + text.each_line { |line| target << Nodes::Statement.new(code: line.rstrip) unless line.strip.empty? } + else + target << ruby_comment("TODO: HAML :#{name} filter not converted\n#{text.rstrip}") + end + end + + # :javascript / :css -> script / style. Content without interpolation is + # kept verbatim (RawText, like an ERB