Skip to content

Commit

Permalink
Tweak offense highlight range for `Style/RedundantFileExtensionInRequ…
Browse files Browse the repository at this point in the history
…ire`

This PR tweaks offense highlight range for `Style/RedundantFileExtensionInRequire`.

## Before

```console
require 'foo.rb'
        ^^^^^^^^ Redundant `.rb` file extension detected.
```

## After

```console
require 'foo.rb'
            ^^^ Redundant `.rb` file extension detected.
```
  • Loading branch information
koic committed Sep 25, 2021
1 parent 05494a4 commit 730e4f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions lib/rubocop/cop/style/redundant_file_extension_in_require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Style
# require_relative '../foo.so'
#
class RedundantFileExtensionInRequire < Base
include RangeHelp
extend AutoCorrector

MSG = 'Redundant `.rb` file extension detected.'
Expand All @@ -39,13 +40,21 @@ def on_send(node)
require_call?(node) do |name_node|
return unless name_node.value.end_with?('.rb')

add_offense(name_node) do |corrector|
correction = name_node.value.delete_suffix('.rb')
extension_range = extension_range(name_node)

corrector.replace(name_node, "'#{correction}'")
add_offense(extension_range) do |corrector|
corrector.remove(extension_range)
end
end
end

private

def extension_range(name_node)
end_of_path_string = name_node.source_range.end_pos

range_between(end_of_path_string - 4, end_of_path_string - 1)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
it 'registers an offense and corrects when requiring filename ending with `.rb`' do
expect_offense(<<~RUBY)
require 'foo.rb'
^^^^^^^^ Redundant `.rb` file extension detected.
^^^ Redundant `.rb` file extension detected.
require_relative '../foo.rb'
^^^^^^^^^^^ Redundant `.rb` file extension detected.
^^^ Redundant `.rb` file extension detected.
RUBY

expect_correction(<<~RUBY)
Expand Down

0 comments on commit 730e4f8

Please sign in to comment.