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

Use relative path for offense message in Lint/DuplicateMethods #4361

Merged
merged 1 commit into from
May 10, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented May 9, 2017

Currently Lint/DuplicateMethods cop messages have absolute paths.

$ rubocop test.rb --only Lint/DuplicateMethods
Inspecting 1 file
W

Offenses:

test.rb:3:1: W: Method Object#foo is defined at both /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb:1 and /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb:3.
def foo
^^^

1 file inspected, 1 offense detected

I think Absolute path is unnecessarily long.
And we use relative path for a location of an offense(e.g. test.rb:3:1).
So, I think we should use relative path for this message also.

By this change, the absolute path is replaced with relative path.

For example:

$ rubocop test.rb --only Lint/DuplicateMethods
Inspecting 1 file
W

Offenses:

test.rb:3:1: W: Method Object#foo is defined at both test.rb:1 and test.rb:3.
def foo
^^^

1 file inspected, 1 offense detected

What do you think?


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).

@pocke pocke force-pushed the Lint/DuplicateMethods-relative-path branch from d393704 to a28adfa Compare May 9, 2017 10:03
@rrosenblum
Copy link
Contributor

rrosenblum commented May 9, 2017

Makes sense to me. Should there be a test that shows off this functionality?

@bbatsov
Copy link
Collaborator

bbatsov commented May 10, 2017

Ideally, yes. And we should check whether other cops aren't using absolute paths for consistency's sake.

Currently `Lint/DuplicateMethods` cop messages have absolute paths.

```bash
$ rubocop test.rb --only Lint/DuplicateMethods
Inspecting 1 file
W

Offenses:

test.rb:3:1: W: Method Object#foo is defined at both /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb:1 and /home/pocke/ghq/github.com/bbatsov/rubocop/test.rb:3.
def foo
^^^

1 file inspected, 1 offense detected
```

I think Absolute path is unnecessarily long.
And we use relative path for a location of an offense(e.g. `test.rb:3:1`).
So, I think we should use relative path for this message also.

By this change, the absolute path is replaced with relative path.

For example:

```bash
$ rubocop test.rb --only Lint/DuplicateMethods
Inspecting 1 file
W

Offenses:

test.rb:3:1: W: Method Object#foo is defined at both test.rb:1 and test.rb:3.
def foo
^^^

1 file inspected, 1 offense detected
```

What do you think?
@pocke pocke force-pushed the Lint/DuplicateMethods-relative-path branch from a28adfa to 26bad38 Compare May 10, 2017 09:57
@pocke
Copy link
Collaborator Author

pocke commented May 10, 2017

I've added tests, and changed to use smart_path method. The method changes path to relative only if file is in project root.

Should there be a test that shows off this functionality?

Thanks for your suggestion. I've added tests.

And we should check whether other cops aren't using absolute paths for consistency's sake.

$ git grep buffer.name
lib/rubocop/comment_config.rb:      Cop::Cop.qualified_cop_name(cop_name.strip, processed_source.buffer.name)
lib/rubocop/cop/commissioner.rb:        remove_irrelevant_cops(processed_source.buffer.name)
lib/rubocop/cop/layout/leading_comment_space.rb:                    config_ru?(processed_source.buffer.name)
lib/rubocop/cop/lint/duplicate_methods.rb:          path = smart_path(range.source_buffer.name)
lib/rubocop/cop/style/file_name.rb:          file_path = processed_source.buffer.name
lib/rubocop/cop/team.rb:          filename = buffer.name
lib/rubocop/processed_source.rb:      buffer_name = @path || STRING_SOURCE_NAME
lib/rubocop/processed_source.rb:      @buffer = Parser::Source::Buffer.new(buffer_name, 1)

Style/FileName cop uses buffer.name, but the cop uses relative paths for messages.
So, I guess cops that use absolute paths for messages doesn't exist.

@bbatsov bbatsov merged commit e9aab79 into rubocop:master May 10, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented May 10, 2017

👍

@pocke pocke deleted the Lint/DuplicateMethods-relative-path branch May 10, 2017 10:40
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.

3 participants