Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #3779] Add Rails/FilePath cop #3822

Merged
merged 1 commit into from
Dec 26, 2016
Merged

[Fix #3779] Add Rails/FilePath cop #3822

merged 1 commit into from
Dec 26, 2016

Conversation

iguchi1124
Copy link
Contributor

@iguchi1124 iguchi1124 commented Dec 25, 2016

Add Rails/FilePath cop

Related issue: #3779


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

subject(:cop) { described_class.new }

context 'when Rails.root.join with some path strings' do
let(:source) { 'Rails.root.join(\'app\', \'models\', \'user.rb\')' }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use "" to eliminate the need to escape ' inside the string. We should actually have a cop for this! :-) /cc @Drenmi @pocke

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this exact thing in a code review last week, so I guess I agree that Style/StringLiterals should account for this. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll create this issue!


it 'registers an offense' do
inspect_source(cop, source)
expect(cop.offenses.size).to eq(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the usage of be_empty.

module RuboCop
module Cop
module Rails
# @example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also add some general explanations here.

context 'when Rails.root.join with some path strings' do
let(:source) { 'Rails.root.join(\'app\', \'models\', \'user.rb\')' }

it 'registers an offense' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not register :-)

describe RuboCop::Cop::Rails::FilePath do
subject(:cop) { described_class.new }

context 'when Rails.root.join with some path strings' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when using

end
end

context 'when File.join with Rails.root' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when using

offense(node)
end

def offense(node)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name this register_offense or something like this.


private

def file_join_with_rails_root(node)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name this check_for_file_join_with_rails_root or something else. Or maybe you could extract a separate predicate method from this one to simplify the code?

offense(node)
end

def rails_root_join_with_slash_separated_path(node)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 25, 2016

You should also write a matching rule in the Rails style guide.

@iguchi1124
Copy link
Contributor Author

@bbatsov

You should also write a matching rule in the Rails style guide.

OK, I will.
where kind of table content should I write in to? by the way.

https://github.com/bbatsov/rails-style-guide#table-of-contents

@iguchi1124
Copy link
Contributor Author

Thank you for great reviews and i fixed all of review comment point.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 25, 2016

where kind of table content should I write in to?

Guess such code most often comes up in configuration, so I guess it can be in this section...

@Bodacious
Copy link

Bodacious commented Oct 17, 2017

Can someone please explain to me why this:

Rails.root.join("one", "two", "three", "four", "five.rb")

Is better than this:

Rails.root.join("one/two/three/four/five.rb")

Surely the latter is shorter, easier to read, and better on memory (allocates fewer strings).

Thanks

@dominicsayers
Copy link
Contributor

dominicsayers commented Nov 3, 2017

@Bodacious You're hard coding the path separator. It's reasonable assumption that it will be a "/" but not 100% universal. One somebody might want to run your code on Windows or a future OS that doesn't use "/".

Regarding the ugliness of the first format, I sometimes use

Rails.root.join(*%w[one two three four five.rb])

which, while still a bit ugly, is at least shorter. File.join is more forgiving and you can do

File.join(%w[one two three four five.rb])

(i.e. it will handle an array while Rails.root.join won't)

@Drenmi
Copy link
Collaborator

Drenmi commented Nov 3, 2017

@dominicsayers: Care to open a new request to add a configuration option for the path separator? 🙂

@dominicsayers
Copy link
Contributor

dominicsayers commented Nov 3, 2017

You mean remove the hard-coded forward slash from the cop? That wouldn't need a configuration option, just replace the slash with File::SEPARATOR

EDITED TO ADD: ...although making a regex with File::SEPARATOR on a Windows box might be interesting because the backslash is also a regex escape character. I don't have Ruby installed on Windows anywhere to try it at the moment.

@Bodacious
Copy link

@dominicsayers thanks for taking the time to explain it. I never considered OS that don't use "/" 👍

@Drenmi
Copy link
Collaborator

Drenmi commented Nov 3, 2017

That wouldn't need a configuration option [...]

Ah. Yes. Of course. I think we have a Windows CI now, so I might give it a shot.

@iguchi1124 iguchi1124 deleted the feature-rails-root-join branch October 29, 2019 02:44
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.

5 participants