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 configuration option CustomTransformPatterns to RSpec/SpecFilePathFormat #1716

Closed
wants to merge 1 commit into from

Conversation

ydah
Copy link
Member

@ydah ydah commented Sep 11, 2023

Motivation

For example, if you have the following files:

# rspec_foo_spec.rb
RSpec.describe RSpecFoo do
end

# rspec_bar_spec.rb
RSpec.describe RSpecBar do
end

# rspec_baz_spec.rb
RSpec.describe RSpecBaz do
end

Suppose you have the following .rubocop.yml:

RSpec/SpecFilePathFormat:
  CustomTransform:
    RSpec: rspec

But this would be offense:

❯ bundle exec rubocop rspec* --only RSpec/SpecFilePathFormat
Inspecting 3 files
CCC

Offenses:

rspec_bar_spec.rb:1:1: C: RSpec/SpecFilePathFormat: Spec path should end with r_spec_bar*_spec.rb.
RSpec.describe RSpecBar do

rspec_baz_spec.rb:1:1: C: RSpec/SpecFilePathFormat: Spec path should end with r_spec_baz*_spec.rb.
RSpec.describe RSpecBaz do

rspec_foo_spec.rb:1:1: C: RSpec/SpecFilePathFormat: Spec path should end with r_spec_foo*_spec.rb.
RSpec.describe RSpecFoo do


3 files inspected, 3 offenses detected

So how about something that can be specified in a pattern?


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 modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

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.

Wr had this option in FilePath? How comes we’ve missed copying it over to this cop whennsplitting ?
What about yhe other cop, does it need the option?
There is no way to declare this in the obsoletion conf, is there?

Thank you!

@ydah
Copy link
Member Author

ydah commented Sep 11, 2023

@pirj We didn't have this option in FilePath and had the same issue 👍🏻

❯ bundle exec rubocop rspec* --only RSpec/FilePath          
Inspecting 3 files
CCC

Offenses:

rspec_bar_spec.rb:1:1: C: RSpec/FilePath: Spec path should end with r_spec_bar*_spec.rb.
RSpec.describe RSpecBar do
^^^^^^^^^^^^^^^^^^^^^^^
rspec_baz_spec.rb:1:1: C: RSpec/FilePath: Spec path should end with r_spec_baz*_spec.rb.
RSpec.describe RSpecBaz do
^^^^^^^^^^^^^^^^^^^^^^^
rspec_foo_spec.rb:1:1: C: RSpec/FilePath: Spec path should end with r_spec_foo*_spec.rb.
RSpec.describe RSpecFoo do
^^^^^^^^^^^^^^^^^^^^^^^

3 files inspected, 3 offenses detected

@pirj
Copy link
Member

pirj commented Sep 11, 2023

We probably had the same issue because ‘rspec’ was not in the defaults?
I see this option in the docs https://docs.rubocop.org/rubocop-rspec/cops_rspec.html#rspecfilepath

@ydah
Copy link
Member Author

ydah commented Sep 11, 2023

This is occurring though RSpec: rspec is set by default.
https://docs.rubocop.org/rubocop-rspec/cops_rspec.html#configurable-attributes-16

It also occurs when explicitly specified as follows

RSpec/FilePath:.
  Enabled: true
  CustomTransform: true
    RSpec: rspec

@pirj
Copy link
Member

pirj commented Sep 11, 2023

That is weird. Wondering, why?
We even have a spec for that

let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } }

Is there a guarantee that specs from this PR won’t conceal the problem ?

@ydah
Copy link
Member Author

ydah commented Sep 11, 2023

This test does not guarantee this case.
The following cases can be guaranteed

# foofoo/some/class/bar_spec.rb
describe FooFoo::Some::Class, '#bar' do; end

Note, however, that the following cases of foofoo_bar and bar_foofoo are not guaranteed.

# foofoo_bar/some/class/bar_spec.rb
describe FooFooBar::Some::Class, '#bar' do; end

Because CustomTransform only works if the implementation is also an exact match.

custom_transform.fetch(name) { camel_to_snake_case(name) }

@pirj
Copy link
Member

pirj commented Sep 11, 2023

A-ha!
Please accept my sincere apologies, I misunderstood the whole thing 🙈

So this is an additional option.
Wondering if we could just treat existing transforms as patterns?

@ydah ydah force-pushed the custom_transform_patterns branch from 53cfeb7 to bba30a7 Compare October 2, 2023 15:01
@ydah
Copy link
Member Author

ydah commented Oct 2, 2023

So this is an additional option. Wondering if we could just treat existing transforms as patterns?

I'm wondering if adding this feature as a separate additional option might be better in order to ensure backward compatibility when there are already custom settings in CustomTransform. What do you think?

@ydah ydah requested a review from pirj October 2, 2023 15:04
@ydah ydah force-pushed the custom_transform_patterns branch from bba30a7 to a6faf3c Compare October 2, 2023 15:05

context 'when configured with `CustomTransformPatterns: ' \
'{ "RSpec" => "rspec" }`' do
let(:cop_config) { { 'CustomTransformPatterns' => { 'RSpec' => 'rspec' } } }
Copy link
Member

Choose a reason for hiding this comment

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

What i meant with reusing the option was to allow ‘“RSpec” => “rspec”’ config in CustomTransform.
That would still be backwards-compatible.
I’m not against the new option, but I experience some cognitive resistance due to the lack of real examples.
Would the following make good examples of partial trasgormations?
API -> api
DSL -> dsl

Copy link
Member

Choose a reason for hiding this comment

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

I’m still not convinced with RSpecClass.

@ydah ydah force-pushed the custom_transform_patterns branch 4 times, most recently from 6c70914 to 35c779b Compare December 6, 2023 11:28
@ydah
Copy link
Member Author

ydah commented Dec 6, 2023

Sorry for the delay. I have updated this PR. How do you like it?

@ydah ydah requested a review from pirj December 6, 2023 11:28
lib/rubocop/cop/rspec/spec_file_path_format.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rspec/spec_file_path_format.rb Outdated Show resolved Hide resolved

context 'when configured with `CustomTransformPatterns: ' \
'{ "RSpec" => "rspec" }`' do
let(:cop_config) { { 'CustomTransformPatterns' => { 'RSpec' => 'rspec' } } }
Copy link
Member

Choose a reason for hiding this comment

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

I’m still not convinced with RSpecClass.

@ydah ydah requested a review from a team as a code owner January 15, 2024 15:29
@ydah ydah force-pushed the custom_transform_patterns branch 3 times, most recently from d550f3a to 9937e8a Compare January 15, 2024 15:34
@ydah ydah requested a review from pirj January 15, 2024 15:37
@ydah
Copy link
Member Author

ydah commented Jan 31, 2024

I updated this PR.

it 'registers an offense when not an exact match to custom ' \
'module name transformation' do
expect_offense(<<-RUBY, 'rspec_foo_spec.rb')
RSpec.describe RSpecFoo do; end
Copy link
Member

Choose a reason for hiding this comment

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

The best way to demo the difference between those two option would be RSpec.describe CustomDSL and DSL => dsl mapping. One would insist on custom_d_s_l_*spec.rb, the other on custom_dsl_*spec.rb, right?

Namespaces and RSpecFoo don't help understand it.

I keep losing grip about this option.

Copy link
Member Author

Choose a reason for hiding this comment

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

This does not appear to be an offense when followed by only one uppercase letter, as in DSL, so this is not the case that can be resolved with this option. This option may not be a common use case.

@pirj
Copy link
Member

pirj commented Feb 6, 2024

Please pardon my continuous inability to understand what problem this PR solves.
Is it some capitalized words with untypical conversion to snake case like DBase, RSpec, VSync, macOS that are used as part of multiple classes (and specs for them)? Like MacOSFileSystem MacOSKeyChain etc?
Is it widespread? How does e.g Zeitwerk deal with classes themselves?

@ydah
Copy link
Member Author

ydah commented Feb 7, 2024

Is it some capitalized words with untypical conversion to snake case like DBase, RSpec, VSync, macOS that are used as part of multiple classes (and specs for them)? Like MacOSFileSystem MacOSKeyChain etc?

Yes
For example, if you are implementing a custom cop in a Rails application and the cop is a cop related to RSpec, you might create classes for cop called RSpecFoo and RSpecBar.
Of course, creating a department might solve the problem, but in many cases there are not that many custom cops created, so you might put files under lib/rubocop/cop/project_name.
At that time, setting CustomTransform for each file was troublesome.

For example, the following custom cup collection does not have separate divisions, so CustomTransform needs to set them up one by one.
https://github.com/ydah/workitcop/tree/main/lib/rubocop/cop/workit

I'm not sure this is common, though.

@ydah ydah requested a review from pirj February 7, 2024 14:32
@bquorning
Copy link
Collaborator

I’ll try to argue that CustomTransform solves this problem already. Yes, in a few cases it will require a long list, but at least that long list will be easy to understand. I find CustomTransformPatterns more difficult to understand.

@ydah
Copy link
Member Author

ydah commented Feb 7, 2024

I find CustomTransformPatterns more difficult to understand.

That may indeed be true. Thank you. I will close this PR.

@ydah ydah closed this Feb 7, 2024
@ydah ydah deleted the custom_transform_patterns branch February 7, 2024 22:20
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