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

Proposal: ERB Support #1409

Open
tk0miya opened this issue Dec 15, 2024 · 0 comments
Open

Proposal: ERB Support #1409

tk0miya opened this issue Dec 15, 2024 · 0 comments

Comments

@tk0miya
Copy link
Contributor

tk0miya commented Dec 15, 2024

ERB is a widely used template library in Ruby. It is adopted as the default view library in Ruby on Rails. It is one of the popular template libraries in Ruby. I believe it's valuable to support it in Steep.

Three enhancements are necessary to let Steep support ERB.

  • Allow to change the target file extension
  • Convert ERB code into Ruby before type-checking
  • Allow to change the "self" context via global-level annotation

Allow to change the target file extension

At present, Steep expects the target source code to be ".rb". It is hard-coded in some modules.

def source_pattern
Pattern.new(patterns: sources, ignores: ignored_sources, ext: ".rb")
end

target.source_pattern.prefixes.each do |pat|
path = project.base_dir + pat
patterns << (path + "**/*.rb").to_s unless path.file?
end

It would be better to specify file extension in Steepfile:

target :views do
  check "app/views"
  ext ".erb"
end

Convert ERB code into Ruby before type-checking

ERB to Ruby code conversion is needed to type-check ERB.

Input:

<html>
  <body>
    <ul>
    <% languages.each do |langage| %>
      <li><%= language %>
    <% end %>
    </ul>
  </body>
</html>

Output: (replace HTML tags with whitespace)

      
        
        
       languages.each do |langage|   
              language   
       end   
         
         
       

Allow to change the "self" context via global-level annotation

In the Rails application, some instance variables and helper methods are given from other components (controllers, helpers, ActionView, and so on).

Therefore, it is needed to switch the "self" context at the global level.

<%# @type self: MyERBTemplate %>
<%= @some_instance_variables %>
<% some_helper_methods %>

It must be helpful if we'll support other DSLs.

For example, we'll also be able to support Gemfile and Raketask:

# @type self: Gemfile

gem 'some_gem'
gem 'another_gem'
# @type self: Raketask

desc "my task"
task :my_task do
  ...
end

namespace :new_group do
  ...
end
tk0miya added a commit to tk0miya/steep that referenced this issue Jan 3, 2025
To support ERB, this allows to change the extension of the source files
via `ext` setting in Steepfile.

refs: soutaro#1409
tk0miya added a commit to tk0miya/steep that referenced this issue Jan 3, 2025
To support ERB, this allows to change the extension of the source files
via `ext` setting in Steepfile.

Example:

```
target :views do
  check "app/views"
  ext ".erb"
end
```

refs: soutaro#1409
tk0miya added a commit to tk0miya/steep that referenced this issue Jan 3, 2025
To support ERB, this allows to change the extension of the source files
via `ext` setting in Steepfile.

Example:

```
target :views do
  check "app/views"
  ext ".erb"
end
```

refs: soutaro#1409
tk0miya added a commit to tk0miya/steep that referenced this issue Jan 3, 2025
To support ERB, this allows to annotate "self" on the top level context.
It effects to the whole of the file.

It will also help to support other DSLs (ex. Gemfile, Rakefile, and so
on).

Example:

 ```
 # @type self: Gemfile

 group :develop do
   gem 'steep'
   gem 'rbs'
 end
 ```

 ```
 # @type self: Raketask

 namespace :my_group do
   desc "My Task"
   task :my_task do
     ...
   end
 end
 ```

refs: soutaro#1409
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

No branches or pull requests

1 participant