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 Lint/TripleQuotes cop #9402

Merged
merged 1 commit into from
Jan 26, 2021

Conversation

dvandersluis
Copy link
Member

@dvandersluis dvandersluis commented Jan 21, 2021

Follows #9394 (comment).

Looks for instances of triple (or quintuple, or septuple, etc.) quotes and registers an offense for them as they do not have a special meaning in Ruby but could cause confusion that they do.

The case of useless implicit concatenation (ie. 'a' 'b' 'c', '' '', etc.) is not registered by this cop to keep things simple (but I'm going to look at adding a cop for that as well).


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@dvandersluis dvandersluis changed the title Added new Lint/TripleQuotes cop. Added new Lint/TripleQuotes cop Jan 21, 2021
@dvandersluis dvandersluis requested a review from pirj January 21, 2021 20:05
@dvandersluis
Copy link
Member Author

On further thought, maybe it might be worthwhile to handle triple quotes and other implicit string concatenation in the same Lint/ImplicitStringContatenation cop...

@dvandersluis dvandersluis changed the title Added new Lint/TripleQuotes cop Add new Lint/TripleQuotes cop Jan 21, 2021
Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

LGTM, just a small comment

lib/rubocop/cop/lint/triple_quotes.rb Show resolved Hide resolved
@marcandre
Copy link
Contributor

On further thought, maybe it might be worthwhile to handle triple quotes and other implicit string concatenation in the same Lint/ImplicitStringContatenation cop...

Either way works for me. Separate cop makes sense though as ImplicitStringContatenation could be disabled, for example, but triple quotes is kind of different.

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.

Incredible, thank you!

opening_quotes = node.source.scan(/(?<=\A)['"]*/)[0]
return if opening_quotes.size < 3

# If the entire `dstr` is empty `str` nodes, leave one so that there is
Copy link
Member

Choose a reason for hiding this comment

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

What do you think of "If dstr node is composed of empty strings only, keep one"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated, I worded it slightly differently but I think it's clearer now.

expect_no_offenses(<<~RUBY)
'' ''
'a''b''c'
'a''''b'
Copy link
Member

Choose a reason for hiding this comment

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

I believe this one line is offensive as well, but it's not a dstr, and not such a big deal anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it's not technically a "triple quote" so I didn't want to cover it if we're going to have a separate cop for these situations (as discussed above), but I wanted it to be in the tests for clarity there.

Copy link
Member

@koic koic left a comment

Choose a reason for hiding this comment

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

This looks good to me.

@bbatsov bbatsov merged commit 1ac510e into rubocop:master Jan 26, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 26, 2021

Nicely done!

@dvandersluis dvandersluis deleted the lint/triple-quotes branch January 27, 2021 16:03
jmkoni pushed a commit to standardrb/standard that referenced this pull request May 3, 2021
* Update rubocop from 1.12.1 to [1.13.0](https://github.com/rubocop-hq/rubocop/releases/tag/v1.13.0)
* Update rubocop-performance from 1.9.2 to [1.11.1](https://github.com/rubocop-hq/rubocop-performance/releases/tag/v1.11.1)
* Enabled the following rules:
  * [`Performance/RedundantSplitRegexpArgument`](rubocop/rubocop-performance#190)
  * [`Style/IfWithBooleanLiteralBranches`](rubocop/rubocop#9396)
  * [`Lint/TripleQuotes`](rubocop/rubocop#9402)
  * [`Lint/SymbolConversion`](rubocop/rubocop#9362)
  * [`Lint/OrAssignmentToConstant`](rubocop/rubocop#9363)
  * [`Lint/NumberedParameterAssignment`](rubocop/rubocop#9326)
  * [`Style/HashConversion`](rubocop/rubocop#9478)
  * [`Gemspec/DateAssignment`](rubocop/rubocop#9496)
  * [`Style/StringChars`](rubocop/rubocop#9615)
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.

5 participants