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

Registers an offence even if the line includes a end-of-line # comment #4

Merged
merged 2 commits into from
Jan 29, 2024

Conversation

hoshinotsuyoshi
Copy link
Collaborator

@hoshinotsuyoshi hoshinotsuyoshi commented Jan 26, 2024

While using rubocop-grep, I realized that lines with end-of-line # comments cannot be detected by rubocop as violations, even if they do violate a rule.

I have expressed this behavior in a spec. The current codebase does not pass this test.

  context 'with a simple config' do
    let(:cop_config) { {
      'Rules' => [
        { 'Pattern' => 'foo', 'Message' => 'foo is bad' },
      ],
    } }

    ....
    it 'registers an offence even if the line includes a end-of-line # comment' do
      expect_offense(<<~RUBY)
        foo bar # comment
        ^^^ foo is bad
      RUBY
    end

This PR resolves this issue.

@hoshinotsuyoshi hoshinotsuyoshi marked this pull request as ready for review January 26, 2024 11:27
processed_source.comments.any? { |c| c.loc.line == line }
private def in_comment?(pos)
processed_source.comments.any? do |c|
c.loc.line == pos[:line] && c.loc.expression.begin_pos <= pos[:column]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the condition:

c.loc.expression.begin_pos <= pos[:column]

For example, if the source is foo bar # comment,

  • At what character the search target word starts = position of foo ( = 0)
    and
  • At what character the comment starts = position of # (= 8)

are comparing.

@hoshinotsuyoshi hoshinotsuyoshi merged commit b512b93 into dev Jan 29, 2024
3 checks passed
@hoshinotsuyoshi hoshinotsuyoshi deleted the fix-around-comment branch January 29, 2024 07:25
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.

1 participant