glebm / i18n-tasks

Manage translation and localization with static analysis, for Ruby i18n
http://glebm.github.io/i18n-tasks
MIT License
2.08k stars 264 forks source link

HAML AST Scanner #465

Open cantin opened 2 years ago

cantin commented 2 years ago

The HAML provides a method to return the precompiled content which is literally Ruby code. We can use it with the Ruby AST scanner to parse the HAML templates.

module I18n::Tasks::Scanners
  class HamlAstScanner < RubyAstScanner
    protected

    def make_buffer(path, contents = read_file(path))
      contents = Haml::Engine.new(contents).precompiled
      super(path, contents)
    end
  end
end

Does it make sense to you? Is it good to merge in as a default HAML scanner? Or put it on the Wiki?

glebm commented 2 years ago

Neat, but does this approach provide the correct line numbers and support i18n-tasks-use comments?

davidwessman commented 2 years ago

@cantin can you try to add tests similar to https://github.com/glebm/i18n-tasks/blob/main/spec/used_keys_erb_spec.rb which I added for the AST-scanner for ERB?

cantin commented 2 years ago

Neat, but does this approach provide the correct line numbers and support i18n-tasks-use comments?

Hey, @glebm, thanks for the reply. So far in my test the line numbers are correct because the compiled content contains exact same line breaks. Unfortunately the i18n-task-use comment is not. it got removed in the compiled content. I guess we need to extend the Haml::Compiler#compile_haml_comment to retain the comment.

cantin commented 2 years ago

Hey @davidwessman ,thanks for the reply. Nice spec, I'll look into it.