From 4977ea3d74f68981e1db7de372a9a96407c24975 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 2 May 2022 17:43:49 +0900 Subject: [PATCH] [Fix #700] Fix a false positive for `Rails/FilePath` --- ...ails_filepath_for_colon_separated_pathlist.md | 1 + lib/rubocop/cop/rails/file_path.rb | 7 +++++-- spec/rubocop/cop/rails/file_path_spec.rb | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_rails_filepath_for_colon_separated_pathlist.md diff --git a/changelog/fix_a_false_positive_for_rails_filepath_for_colon_separated_pathlist.md b/changelog/fix_a_false_positive_for_rails_filepath_for_colon_separated_pathlist.md new file mode 100644 index 0000000000..054afda625 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_filepath_for_colon_separated_pathlist.md @@ -0,0 +1 @@ +* [#700](https://github.com/rubocop/rubocop-rails/issues/700): Fix a false positive for `Rails/FilePath` when a list of paths separated by colon including Rails.root. ([@tk0miya][]) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index e8258537e4..560d4f528e 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -48,8 +48,11 @@ class FilePath < Base def on_dstr(node) return unless rails_root_nodes?(node) return unless node.children.last.str_type? - return unless node.children.last.source.start_with?('.') || - node.children.last.source.include?(File::SEPARATOR) + + last_child_source = node.children.last.source + return unless last_child_source.start_with?('.') || + last_child_source.include?(File::SEPARATOR) + return if last_child_source.start_with?(':') register_offense(node) end diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 6ae24d63bf..24a8c74383 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -122,6 +122,14 @@ RUBY end end + + context 'when concat Rails.root and colon using string interpoloation' do + it 'does not register an offense' do + expect_no_offenses(<<~'RUBY') + "#{Rails.root}:/foo/bar" + RUBY + end + end end context 'when EnforcedStyle is `arguments`' do @@ -245,5 +253,13 @@ RUBY end end + + context 'when concat Rails.root and colon using string interpoloation' do + it 'does not register an offense' do + expect_no_offenses(<<~'RUBY') + "#{Rails.root}:/foo/bar" + RUBY + end + end end end