Skip to content

Commit

Permalink
chore: add mixed option for quote_style
Browse files Browse the repository at this point in the history
Sometimes when we are working in a legacy code formatting a string
changing its quote style is not something we really want because it
makes a PR review hard as a lot of modifications may be made.

With this option a developer can choose if that modification is
needed or not

issue: #258
  • Loading branch information
lockland committed Dec 15, 2023
1 parent 972825d commit e3aaf60
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Use the specified quotation marks.

- `:double`: (default) use doublequotations unless one or more escaped double-quotations are included
- `:single`: use single quotations unless one or more interpolations `#{}` or escaped single quotations are included
- `:mixed`: use mixed quotations if you don't want to change quotes

This does not affect `%q()` (single), `%Q()` (double), or quotation marks within heredocs.

Expand Down Expand Up @@ -278,6 +279,8 @@ code = <<CODE
CODE
```

With `:mixed`, the formatter will keep lines as is

### Includes and excludes
Files can be excluded or included in formatting with rufo by specifying glob patterns for the `includes` or `excludes` configuration options. Multiple patterns are separated by a comma.

Expand Down
1 change: 1 addition & 0 deletions lib/rufo/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ def quote_char

# should we format this string according to :quote_style?
def should_format_string?(string)
return if quote_style == :mixed
# don't format %q or %Q
return unless current_token_value == "'" || current_token_value == '"'
# don't format strings containing slashes
Expand Down
2 changes: 1 addition & 1 deletion lib/rufo/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Rufo::Settings
align_case_when: [false, true],
align_chained_calls: [false, true],
trailing_commas: [true, false],
quote_style: [:double, :single],
quote_style: [:double, :single, :mixed],
includes: nil,
excludes: nil,
}
Expand Down
69 changes: 69 additions & 0 deletions spec/lib/rufo/formatter_source_specs/mixed_quotes.rb.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#~# ORIGINAL
#~# quote_style: :mixed

'a great string'
"a great string"

#~# EXPECTED
'a great string'
"a great string"

#~# ORIGINAL
#~# quote_style: :mixed

'πŸš€'
"πŸš€"

#~# EXPECTED
'πŸš€'
"πŸš€"

#~# ORIGINAL
#~# quote_style: :mixed

''
""

#~# EXPECTED
''
""

#~# ORIGINAL
#~# quote_style: :mixed

'import \"quotes\"'

#~# EXPECTED
'import \"quotes\"'

#~# ORIGINAL
#~# quote_style: :mixed

"#{interpolation}"

#~# EXPECTED
"#{interpolation}"

#~# ORIGINAL
#~# quote_style: :mixed

"\0 \x7e \e \n \r \t \u1f680 \'"

#~# EXPECTED
"\0 \x7e \e \n \r \t \u1f680 \'"

#~# ORIGINAL
#~# quote_style: :mixed

%q(single)

#~# EXPECTED
%q(single)

#~# ORIGINAL
#~# quote_style: :mixed

%Q(double)

#~# EXPECTED
%Q(double)

0 comments on commit e3aaf60

Please sign in to comment.