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

Should Style/StringLiterals also apply to quoted symbols? #9793

Closed
sambostock opened this issue May 12, 2021 · 2 comments · Fixed by #9809
Closed

Should Style/StringLiterals also apply to quoted symbols? #9793

sambostock opened this issue May 12, 2021 · 2 comments · Fixed by #9809
Assignees

Comments

@sambostock
Copy link
Contributor

  • Style/StringLiterals provides the ability to enforce a consistent quoting style ("..." vs '...'), with rich support for edge cases.
  • Lint/SymbolConversion provides the ability to enforce consistent symbol literal syntax (again with rich support for edge cases), and respects the style specified by Style/StringLiterals when autocorrecting by inserting quotes.

However, when using quoted symbol literals, there does not appear to be a way to enforce a consistent quote style. It seems logical that Style/StringLiterals should extend its enforcement to Symbol literals as well.


Expected behavior

  • Style/StringLiterals enforced style should apply to Symbol literals, or
  • there should be another way to enforce quotation style for quoted symbols.

Actual behavior

  • Style/StringLiterals enforced style does not apply to Symbol literals, and
  • there is no other way (AFAIK) to enforce quotation style for quoted symbols.

Steps to reproduce the problem

Setup

# .rubocop.yml
AllCops:
  DisabledByDefault: true
Style/StringLiterals:
  Enabled: true
# example.rb
puts  "abc-def" # Offensive, as expected
puts :"abc-def" # Inoffensive, but should be

Output

$ rubocop example.rb
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
puts "abc-def"  # Offensive
     ^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable

RuboCop version

$ bundle exec rubocop -V
1.14.0 (using Parser 3.0.1.1, rubocop-ast 1.5.0, running on ruby 2.7.1 x86_64-darwin19)
@tejasbubane
Copy link
Contributor

I think we should add a new boolean config option EnforceForSymbolConversion which if true will apply EnforcedStyle for symbol conversions. This config should be set to false by default to avoid breaking changes.

@bbatsov
Copy link
Collaborator

bbatsov commented May 17, 2021

@tejasbubane Add this option where? I assume you're referrring to StringLiterals, but I don't like the idea of piling too much on individual cops. If there's a need for consistent quoting in symbol literals this should be a dedicated cop IMO.

@dvandersluis dvandersluis self-assigned this May 17, 2021
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue May 17, 2021
…n quoted symbols.

Like `Style/StringLiterals`, this cop checks for the quoting style for symbols to be consistent. Checking whether a symbol _should_ be quoted or not is still handled by `Lint/SymbolConversion`.

This cop shares as much as possible with `Style/StringLiterals` for ease of maintainabilty. As well, for simple configuration, the default "style" is `same_as_string_literals` which will automatically apply the same configuration set up for `Style/StringLiterals` to this cop as well (it defaults to `single_quotes` if `StringLiterals` is not enabled).
dvandersluis added a commit that referenced this issue May 19, 2021
[Fix #9793] Add Style/QuotedSymbols to enforce consistency in quoted symbols
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 a pull request may close this issue.

4 participants