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

Add support for providing features on ERB files #1055

Open
vinistock opened this issue Oct 2, 2023 · 6 comments · May be fixed by #1184
Open

Add support for providing features on ERB files #1055

vinistock opened this issue Oct 2, 2023 · 6 comments · May be fixed by #1184
Labels
enhancement New feature or request pinned This issue or pull request is pinned and won't be marked as stale

Comments

@vinistock
Copy link
Member

I think there's a path to add support for ERB without a ton of effort. We can try to require erb from the main bundle and provide features only if it is a dependency.

Basically, we can use the ERB gem to parse the .erb file and extract the Ruby code. Then we parse the Ruby code with Prism and provide features for it.

require "erb"
erb = "<% x = 32 %><p><%= x %></p>"
ruby_code = ERB.new(erb).src # => "#coding:UTF-8\n_erbout = +'';  x = 32 ; _erbout.<< \"<p>\".freeze; _erbout.<<(( x ).to_s); _erbout.<< \"</p>\".freeze; _erbout"

Prism.parse(ruby_code) # => AST

In terms of the codebase, I think the best way forward would be turning creating an abstract parent class Document and turning our existing document class into RubyDocument. Then we can create an ErbDocument class to handle the specific details of ERB.

Notes

Diagnostics, formatting and on type formatting need to happen differently on ERB files. For example, if some is using [].each do %> and breaks the line, we need to insert <% end %> and not just end. Similarly, we can see if erb lint is available to provide diagnostics and potentially format the code too.

It might be worth separating these requests into RubyFormatting and ErbFormatting.

@vinistock vinistock added enhancement New feature or request rubyconf-hackdays labels Oct 2, 2023
@Morriar
Copy link
Contributor

Morriar commented Oct 3, 2023

You may need a custom Engine to parse the ERB supported by Rails properly: https://github.com/Shopify/spoom/blob/at-deadcode/lib/spoom/deadcode/erb.rb#L8.

The complexity is correctly mapping lines and columns to the original ERB source.

@vinistock
Copy link
Member Author

Thanks for pointing that out. I suppose if Rails is available in the Gemfile, we'll probably be able to require their own custom engine and use it.

@vinistock
Copy link
Member Author

Attempt at erasing the HTML

# typed: strict
# frozen_string_literal: true

require "prism"
require "strscan"

original = File.read("index.html.erb")
scanner = StringScanner.new(original)
output = +""

until scanner.eos?
  non_ruby_code = scanner.scan_until(/<%(-|=)?/)
  break unless non_ruby_code

  output << non_ruby_code.gsub(/[^\n]/, " ")

  ruby_code = scanner.scan_until(/(-)?%>/)
  break unless ruby_code

  output << ruby_code[...-2]
  output << "  "
end

puts output

p Prism.parse(output)

@marcoroth marcoroth linked a pull request Nov 13, 2023 that will close this issue
@collimarco
Copy link

+1 for ERB support

ERB support is really important for Rails projects

@vinistock
Copy link
Member Author

We can use VS Code's embedded language features to forward HTML operations to the built-in language server and get features: https://code.visualstudio.com/api/language-extensions/embedded-languages.

Copy link
Contributor

This issue is being marked as stale because there was no activity in the last 2 months

@github-actions github-actions bot added the Stale label Apr 29, 2024
@vinistock vinistock added pinned This issue or pull request is pinned and won't be marked as stale and removed Stale labels Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pinned This issue or pull request is pinned and won't be marked as stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants