rootstrap / i18n_linter

Rails i18n Linter Gem
https://rootstrap.com
MIT License
44 stars 2 forks source link

Refactor of class_name rule #19

Closed f19ps closed 5 years ago

f19ps commented 5 years ago

New version of class_name rule

This is a NegativeContextRule that cuts the file tree when a definition of a class name appears.

For example, if we have the following code:

class Employee < ApplicationRecord
  belongs_to :manager, class_name: "Employee"
end

Ripper will throw:

[:program,
 [[:class,
   [:const_ref, [:@const, "Employee", [1, 6]]],
   [:var_ref, [:@const, "ApplicationRecord", [1, 17]]],
   [:bodystmt,
    [[:command,
      [:@ident, "belongs_to", [2, 2]],
      [:args_add_block,
       [[:symbol_literal, [:symbol, [:@ident, "manager", [2, 14]]]], [:bare_assoc_hash, [[:assoc_new, [:@label, "class_name:", [2, 23]], [:string_literal, [:string_content, [:@tstring_content, "Employee", [2, 36]]]]]]]],
       false]]],
    nil,
    nil,
    nil]]]]

So, when the branch [[:assoc_new, [:@label, "class_name:", [2, 23]], [:string_literal, [:string_content, [:@tstring_content, "Employee", [2, 36]]]]]] is checked it will be cut to prevent that "Employee" will be detected.