Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force close final statement in templates #578

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ienders
Copy link

@ienders ienders commented Oct 30, 2024

This enables Hash Shorthand / punning on your final template statement (which I think is a good thing).

This was a somewhat tricky error to track down -- but this cropped up for us as I was adding Sorbet strict typing to our view layer. An example repro on Ruby 3.1+ would be:

example/test.json.jbuilder:

foobar = "hello"
json.partial! "example/partial", foobar:

And:

example/_partial.json.jbuilder:

json.baz foobar

This would result in {"baz": "{}"} rather than {"baz": "hello" }.

Ultimately a Jbuilder template compiles to something like:

 def __source_json_jbuilder___2759381185109067391_7700(local_assigns, output_buffer)
            @virtual_path = "source";;__already_defined = defined?(json); json||=JbuilderTemplate.new(self);       content = "hello"
      json.partial! "partial", content:

      json.target! unless (__already_defined && __already_defined != "method")
          end

In cases where the final statement could be construed as still open (spanning newlines), this string munging can take the result of the json.target! and pull it into local_assigns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant