Skip to content

Match ::I18n.t call just like I18n.t does - #752

Open
sato11 wants to merge 1 commit into
glebm:mainfrom
sato11:patch-751
Open

Match ::I18n.t call just like I18n.t does#752
sato11 wants to merge 1 commit into
glebm:mainfrom
sato11:patch-751

Conversation

@sato11

@sato11 sato11 commented Aug 10, 2026

Copy link
Copy Markdown

Fixes #751.

Given this model definition;

class Post < ApplicationRecord
  def hello
    I18n.t("hello")
  end

  def world
    ::I18n.t("world")
  end
end

and this dictionary;

en:
  hello: "Hello"
  world: "World"

the unused task used to falsely flag the I18n.t instance with the scope resolution operator (::) prepended.

% i18n-tasks unused
#StandWithUkraine
Unused keys (1) | i18n-tasks v1.1.2
+--------+-------+-------+
| Locale | Key   | Value |
+--------+-------+-------+
|   en   | world | World |
+--------+-------+-------+

This change fixes it, and also adds a test suite that covers #scan_file which directly consumes the enhanced matcher.

Below is how one test case out of two fails without the fix:

Failures:

  1) I18n::Tasks::Scanners::RubyScanner #scan_file when source contains ::I18n.t calls is expected to be present
     Failure/Error: it { is_expected.to be_present }
       expected `[].present?` to be truthy, got false
     # ./spec/ruby_scanner_spec.rb:36:in 'block (4 levels) in <top (required)>'

Finished in 0.04819 seconds (files took 0.2276 seconds to load)
2 examples, 1 failure

Given this model definition;

```ruby
class Post < ApplicationRecord
  def hello
    I18n.t("hello")
  end

  def world
    ::I18n.t("world")
  end
end
```

and this dictionary;

```yml
en:
  hello: "Hello"
  world: "World"
```

the `unused` task used to falsely flag the `I18n.t` instance
with the scope resolution operator (`::`) prepended.

```
% i18n-tasks unused
#StandWithUkraine
Unused keys (1) | i18n-tasks v1.1.2
+--------+-------+-------+
| Locale | Key   | Value |
+--------+-------+-------+
|   en   | world | World |
+--------+-------+-------+
```

This change fixes it, and also adds a test suite that covers `#scan_file`
which directly consumes the enhanced matcher.

Below is how one test case out of two fails without the fix:

```
Failures:

  1) I18n::Tasks::Scanners::RubyScanner #scan_file when source contains ::I18n.t calls is expected to be present
     Failure/Error: it { is_expected.to be_present }
       expected `[].present?` to be truthy, got false
     # ./spec/ruby_scanner_spec.rb:36:in 'block (4 levels) in <top (required)>'

Finished in 0.04819 seconds (files took 0.2276 seconds to load)
2 examples, 1 failure
```
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

Successfully merging this pull request may close these issues.

The unused task makes false positives against ::I18n.t in contrast to I18n.t being fine

1 participant