Repository navigation
erb: back ERB with the go-ruby-erb library - #41
Merged
Merged
Conversation
Rewire rbgo's ERB so the template scan/trim/compile is done by the pure-Go github.com/go-ruby-erb/erb library (which mirrors MRI's ERB::Compiler byte for byte) instead of a hand-written compiler in the prelude — the same way rbgo already binds go-ruby-regexp for regexps. - Add internal/vm/erb.go: a native ERB class skeleton with a private __compile(template, trim_mode, eoutvar) hook over erb.Compile, plus an ERB::Util module whose html_escape/url_encode (and h/u aliases) bind to erb.HTMLEscape/erb.URLEncode. The Compile-error branch is reached through the erbCompile seam (the library never fails on a well-formed template). - Prelude class ERB is now a thin shim: initialize calls __compile to get @src; result/result_with_hash/run keep the interpreter-bound binding+eval logic. The inner Compiler (scan/trim/expand_percent_lines/quote) and the Ruby Util/HTML_ESCAPE table are deleted (net 224 fewer Ruby lines). - Re-froze the embedded prelude. Behavior is byte-for-byte unchanged vs MRI 4.0.5: all existing ERB tests, the conformance sweep, and Puppet's trim_mode '-' templatewrapper path pass without changes. Cross-package coverage stays at 100.0%. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Rewire rbgo's
ERBso its template scan/trim/compile is performed by the standalone pure-Gogithub.com/go-ruby-erb/erblibrary — which mirrors MRI's realERB::Compilerbyte for byte — instead of the hand-written compiler that lived in the prelude. This is the same pattern rbgo already uses to back regexps withgo-ruby-regexp.Changes
internal/vm/erb.go(new native binding):ERBclass skeleton with a private__compile(template, trim_mode, eoutvar)hook overerb.Compile, returning the compiled Rubysrc(which already carries the#coding:UTF-8\nmagic prefix). ACompileerror is surfaced as a RubyArgumentError.ERB::Utilmodule whosehtml_escape/url_encode(and theh/ualiases) bind directly toerb.HTMLEscape/erb.URLEncode, so the pure escaping functions come from the library too. Non-string args are coerced with#to_s(MRI semantics).internal/vm/prelude.rb:class ERBis now a thin shim —initializecalls__compileto fill@src;result/result_with_hash/runkeep the interpreter-bound binding+evallogic that must stay in rbgo. The innerclass Compiler(scan / trim /expand_percent_lines/quote) and the RubyUtil/HTML_ESCAPEtable are deleted — net 224 fewer lines of prelude Ruby (236 removed / 12 added).cmd/freeze-prelude); drift test passes.github.com/go-ruby-erb/erb v0.0.0-20260629074717-0999ae4dd529.Behavior
Byte-for-byte unchanged vs MRI 4.0.5. All existing ERB tests and the conformance sweep pass without any test edits — the library reproduces MRI's
<%%/%%>-in-body handling identically, so no rbgo test asserted incorrect behavior. Confirmed againstruby -rerb: the three tag kinds, every trim mode (-/>/<>/%), the<%%/%%>literals, unterminated tags,ERB::Utilescaping, and Puppet'sERB.new(content, trim_mode: '-')templatewrapper path all match.Quality gates
Compile-error branch is covered via theerbCompilefault-injection seam in an internal test).gofmtclean,go vet ./internal/...clean,go build ./cmd/rbgosucceeds, full./internal/...suite green.🤖 Generated with Claude Code