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

Transform Phlex code output through RuboCop or SyntaxTree #59

Open
marcoroth opened this issue Jan 21, 2023 · 8 comments
Open

Transform Phlex code output through RuboCop or SyntaxTree #59

marcoroth opened this issue Jan 21, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@marcoroth
Copy link
Owner

marcoroth commented Jan 21, 2023

Let's say we have this:

<%= tag.div class: something? ? "class-1" : "class-2" do %>
  <%= content_tag :span, something.text %>
<% end %>

Which would generate this through phlexing today:

class SomethingComponent < Phlex::HTML
  include Phlex::Rails::Helpers::ContentTag
  include Phlex::Rails::Helpers::Tag

  attr_accessor :something

  def initialize(something:)
    @something = something
  end

  def template
    tag.div class: something? ? "class-1" : "class-2" do
      content_tag :span, something.text
    end
  end

  private

  def something?(*args, **kwargs)
    # TODO: Implement me
  end
end

But since this is a regular Ruby class now we could write any RuboCop rule (or maybe even a SyntaxTree mutation visitor) which statically analyses the code and auto-fixes it (we could even make the rules toggleable from the UI)

For example some rules could be:

  • rewrite tag.div as div { ... }
  • rewrite content_tag :span to span { ... }
  • rewrite all references of something inside def template to @something and remove the attr_accessor for it
  • rewrite class: something? ? "class-1" : "class-2" to **classes(something?: { then: "class-1", else: "class-2" })
  • ...

We could even release these rules standalone as rubocop-phlex which people could use in their project independent of phlexing

@marcoroth marcoroth added the enhancement New feature or request label Jan 21, 2023
@marcoroth marcoroth changed the title Transforming Phlex code output through RuboCop or SyntaxTree Transform Phlex code output through RuboCop or SyntaxTree Jan 21, 2023
@joeldrapper
Copy link
Collaborator

A SyntaxTree mutation visitor would probably be the easiest solution for this.

@jaredcwhite
Copy link

jaredcwhite commented Mar 9, 2023

@marcoroth @joeldrapper This dovetails into a separate project I've been slowly working on to transform HTML that has certain configurable directives…for example, in a Vue-style configuration, <div v-text="someValue"></div> would render out as <div v-text="someValue">Expanded with the value text</div>.

I had been using Nokogiri for this, but now I'm musing on the idea of using Phlexing to compile the HTML to Phlex and then using something like what you're describing to handle the directives so when the Phlex template runs it can expand out that content.

Any thoughts?

@jaredcwhite
Copy link

(Could be tricky with things like lists, where a single <template> tag with directives in its content would need to be duplicated and expanded out for n items in an array.)

@marcoroth
Copy link
Owner Author

@jaredcwhite maybe I'm not understanding 100% what you are trying to do, but it seems kinda overkill to me to use phlexing (or phlex for that matter) as an intermediate format for those kind of transformations. Unless you already have those transformations written and implemented based on a Ruby AST.

@joeldrapper
Copy link
Collaborator

joeldrapper commented Mar 10, 2023

I agree. I think you’ll find it's easier to use Nokogiri than SyntaxTree for this kind of thing @jaredcwhite. If PhlexML is the final target, you can always do the transformations in Nokogiri first, then run it through Phlexing.

@jaredcwhite
Copy link

If PhlexML is the final target, you can always do the transformations in Nokogiri first, then run it through Phlexing.

Interesting…I could essentially inject ERB into the markup via Nokogiri prior to the Phlexing stage. I'll give that a try!

@jaredcwhite
Copy link

@marcoroth @joeldrapper OK, this looks like it can work! I just need to use special delimiters (eg.,{{{{%= value %}}}}) and gsub back to angle brackets (so Nokogiri doesn't turn them into entities), class_eval a template method with the Phlexing conversion at "compile time", and then renders at run time simply execute the straightforward Phlex template.

This is rad!!

@marcoroth
Copy link
Owner Author

marcoroth commented Mar 11, 2023

@jaredcwhite depening what you are doing you might also find Phlexing::ERBTransformer or Phlexing::Parser helpful so you don't need to replace the output tags with weird markers

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

No branches or pull requests

3 participants