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 a string methods cop #2204

Merged
merged 4 commits into from
Sep 3, 2015
Merged

Conversation

imtayadeway
Copy link
Contributor

This change adds a cop that can be configured to add an offense when a
string method is used when another equivalent method is preferred. It
adds a default to prefer to_sym over intern. It is modeled after the
CollectionMethods cop which checks for preferred methods on Enumerable.

/cc @jrafanie @Fryguy

@@ -636,6 +636,16 @@ Style/StringLiteralsInInterpolation:
- single_quotes
- double_quotes

Style/StringMethods:
Copy link
Collaborator

Choose a reason for hiding this comment

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

You need to add the cop in enabled.yml too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. Just noticed that CollectionMethods is not in there - is that intentional?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I found it in disabled.yml and that's because we expect it to generate a fair amount of false positives. StringMethods might be OK to enable by default since the method intern is less likely to appear in other classes, as far as I can see.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 3, 2015

concat over <<

Something like this will likely yield many false positives.

inspect_source(cop, "'something'.#{method}")
expect(cop.offenses.size).to eq(1)
expect(cop.messages)
.to eq(["Prefer `#{preferred_method}` over `#{method}`."])
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be a check for cop.highlights?

@imtayadeway
Copy link
Contributor Author

@bbatsov I'll scrub this from my commit message. TBH I was struggling to find another example for String but felt obliged to create a StringMethods after CollectionMethods. What I'd really like is more room to expand upon preferred methods in general....but can start here if you think that's OK?

This change adds a cop that can be configured to add an offense when a
string method is used when another equivalent method is preferred. It
adds a default to prefer `to_sym` over `intern`. It is modeled after the
`CollectionMethods` cop which checks for preferred methods on
`Enumerable`.
@imtayadeway imtayadeway force-pushed the add-string-methods-cop branch from 662c246 to a61915f Compare September 3, 2015 16:12
@imtayadeway
Copy link
Contributor Author

@jonas054 @rrosenblum just updated to incorporate your comments/suggestions. Can you rereview?

@jonas054
Copy link
Collaborator

jonas054 commented Sep 3, 2015

👍 Looks good to me.

@bbatsov bbatsov merged this pull request into rubocop:master Sep 3, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 3, 2015

👍

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 3, 2015

Damn. I should have actually read the PR - 4 commits, instead of 1 and no changelog entry. Guess I'll do the unthinkable and rewrite the history.

@imtayadeway
Copy link
Contributor Author

@bbatsov apologies on missing the changelog. anything I can do to help? I'll make sure to squash next time prior to merging. Thanks for your assistance in getting this merged

❤️

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 3, 2015

anything I can do to help?

Guess you can add it in another PR. I've combined the 4 commits and force-pushed to master. Once again I realize it's a bad idea to review code when I'm that tired. :-)

@imtayadeway
Copy link
Contributor Author

@bbatsov thanks! I'll take care of that part

imtayadeway added a commit to imtayadeway/rubocop that referenced this pull request Sep 4, 2015
bbatsov added a commit that referenced this pull request Sep 5, 2015
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.

4 participants