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

Add new RSpec/UndescriptiveLiteralsDescription cop #1796

Merged
merged 1 commit into from
Apr 1, 2024
Merged

Conversation

ydah
Copy link
Member

@ydah ydah commented Feb 1, 2024

Fix: #1754


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

@ydah ydah requested a review from a team as a code owner February 1, 2024 15:25
@ydah ydah force-pushed the add-xstr-only branch 2 times, most recently from f0b91b2 to 0736d4a Compare February 1, 2024 15:42
@bquorning
Copy link
Collaborator

Is it ok to have a cop that singles out the xstr type. You can pass an Integer or a Regexp to it and it’ll still run – should we allow that? What if e.g. we only allowed str and dstr nodes instead?

@ydah ydah changed the title Add new RSpec/ExecuteStringOnlyDescription cop Add new RSpec/DescriptiveDescription cop Feb 7, 2024
@ydah
Copy link
Member Author

ydah commented Feb 7, 2024

we only allowed str and dstr nodes instead?

For example, description specifies the class to be tested, so I don't think it is just string and dstr. Regex and Integer have added support and renamed Cop.

config/default.yml Outdated Show resolved Hide resolved
@ydah ydah changed the title Add new RSpec/DescriptiveDescription cop Add new RSpec/UnclearDescription cop Feb 15, 2024
@ydah ydah requested a review from pirj February 15, 2024 09:38
.vscode/settings.json Outdated Show resolved Hide resolved
@ydah ydah changed the title Add new RSpec/UnclearDescription cop Add new RSpec/UndescriptiveLiteralsDescription cop Feb 16, 2024
@ydah
Copy link
Member Author

ydah commented Feb 16, 2024

I changed this cop name from UnclearDescription to UndescriptiveLiteralsDescription.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

The code looks good. Thank you!

i’d love to run this on real-world-rspec but lack capacity to do this myself

@ydah ydah force-pushed the add-xstr-only branch 2 times, most recently from d5836ea to feb60e6 Compare February 18, 2024 01:42
@ydah ydah requested a review from pirj February 18, 2024 01:45
private

def offense?(node)
if node.send_type? && node.method?(:+)
Copy link
Member

Choose a reason for hiding this comment

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

You’ve added a test for foo + time, but only when all? is false.

If your experience suggests that we need to dive as deep as handling +, I think that if any of parts is an xstr, the whole thing is non-deterministic.
Is sometimh like “‘time’ + ‘whoami’” real? Or just as mistyped quotes like “‘when it is’ + time”?

Again, no objections on merging.

Copy link
Member Author

@ydah ydah Feb 27, 2024

Choose a reason for hiding this comment

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

I re-think it might be better to always allow for concatenation by +. Because it seems difficult to understand the offense in case of offenses, and it seems to be outside the scope of this cop.

  it 'registers an offense when using `describe` with `time`+"foo"' do
    expect_offense(<<~RUBY)
      describe `time` + 'foo' do
               ^^^^^^^^^^^^^^ Description should be descriptive.
      end
    RUBY
  end

  it 'registers an offense when using `describe` with "foo"+`time`' do
    expect_offense(<<~RUBY)
      describe 'foo' + `time` do
               ^^^^^^^^^^^^^^ Description should be descriptive.
      end
    RUBY
  end

  it 'registers an offense when using `describe` with "foo+(`time`+"bar")' do
    expect_offense(<<~RUBY)
      describe 'foo' + (`time` + 'bar') do
               ^^^^^^^^^^^^^^^^^^^^^^^^ Description should be descriptive.
      end
    RUBY
  end

@ydah ydah force-pushed the add-xstr-only branch 2 times, most recently from 7bf03f9 to b60ff5f Compare February 27, 2024 14:49
@ydah ydah requested a review from pirj February 27, 2024 14:52
@ydah
Copy link
Member Author

ydah commented Feb 27, 2024

Looks fine:

Offenses:

/ydah/real-world-rspec/gitlabhq/spec/services/merge_requests/base_service_spec.rb:83:12: C: RSpec/UndescriptiveLiteralsDescription: Description should be descriptive.
  describe `#create_pipeline_for` do
           ^^^^^^^^^^^^^^^^^^^^^^

22772 files inspected, 1 offense detected

@ydah ydah requested a review from bquorning February 28, 2024 08:17
@ydah
Copy link
Member Author

ydah commented Mar 31, 2024

@pirj Do you have any concerns?

@pirj
Copy link
Member

pirj commented Apr 1, 2024

@ydah The code looks good.
As usual, I’d prefer to run new cops against real-world-rspec, as a preventive measure to user-reported errors. But if you lack capacity to do so, too, I’m fine with releasing this.
My main concern with user-reported errors is that the first thing they suggest is to mark cops as unsafe. And we end up with a bunch of effectively retired cops.

@ydah
Copy link
Member Author

ydah commented Apr 1, 2024

@pirj I'm sorry. It seems that I didn't explain it enough, but the following comment shows that after doing it with real-wolrld-rspec, I was able to see the offense I expected to detect without generating any false positives or errors 🙇🏻

@pirj
Copy link
Member

pirj commented Apr 1, 2024

@ydah my apologies, this slipped my attention.
Thanks a lot for the cop, it looks great!

@pirj pirj merged commit ef302ee into master Apr 1, 2024
24 checks passed
@pirj pirj deleted the add-xstr-only branch April 1, 2024 08:51
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.

Cop Idea: Only xstr in description
4 participants