rufo 0.15.1 → 0.16.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd22e372dba08b9779d73ceafaae522433e848e1283ef487baf5e366aaa58986
4
- data.tar.gz: 8729947df2863f8f36ad539879750dd53c9f33c0c2885c2985984e0d07862ddc
3
+ metadata.gz: a6b55f94b8276a2ca0dc8518aa98949e83f83a95adc25a039385cb6855bcbb1c
4
+ data.tar.gz: 03b7595cccdade48c215c3609726bd8a40a5ac72c66e6f64246e842a63f7729e
5
5
  SHA512:
6
- metadata.gz: 45f6c8259e2c20db8cd2e53778d0d8b7d3cfd57686eff05c650b61073338c9d5e89f3ed2e29a35f2e867a1b0a4ae50a7e205ce611b863b215751ffaf2522890a
7
- data.tar.gz: 7d1d00b02060d5b4dcf01e91c1ee41cb955917b1bac5fd149f3e1d217e95aeeeb3e25920c321bed22b5635c2168d1d962fa2b0149efa68138680d45229121632
6
+ metadata.gz: 6c15b016ff6914844a2da86cf3af9ae257abdd9c421dfc4186d2eea39cfba919a2ac3ec489a1f539c59abd49452a5af511e2bca538adb7c62b346859bab3912b
7
+ data.tar.gz: aadada98b7acf84c40c147210655c54681d49b2d2079c76f6d44c19775676556cbf6545bc0754b4c037ba94478b31259f1236a464f60b15704f1e6ecbb98def7
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ spec/examples.txt
17
17
  sample_code
18
18
 
19
19
  .byebug_history
20
+ .dir-locals.el
data/CHANGELOG.md CHANGED
@@ -12,6 +12,28 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
12
12
 
13
13
  ### Added
14
14
 
15
+ ## [0.16.1] - 2023-05-04
16
+
17
+ ### Fixed
18
+ - Fix Bug: raises error while formatting pattern matching syntax ([#304](https://github.com/ruby-formatter/rufo/issues/304))
19
+ - Fix Bug: avoid inserting a unnecessary comma to end of heredoc which in array with method call ([#303](https://github.com/ruby-formatter/rufo/pull/303))
20
+
21
+ ### Changed
22
+
23
+ ### Added
24
+
25
+ ## [0.16.0] - 2023-04-02
26
+
27
+ ### Fixed
28
+
29
+ - Formats ERB files with configuration ([#275](https://github.com/ruby-formatter/rufo/issues/275))
30
+
31
+ ### Changed
32
+
33
+ ### Added
34
+
35
+ - Support for pattern matching syntax
36
+
15
37
  ## [0.15.1] - 2023-02-08
16
38
 
17
39
  ### Fixed
@@ -46,8 +68,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
46
68
 
47
69
  ### Added
48
70
 
49
- - Support for partial argument forwarding. (issue [268](https://github.com/ruby-formatter/rufo/issues/268))
50
- - Support for endless method. (issue [271](https://github.com/ruby-formatter/rufo/issues/271))
71
+ - Support for partial argument forwarding. ([#268](https://github.com/ruby-formatter/rufo/issues/268))
72
+ - Support for endless method. ([#271](https://github.com/ruby-formatter/rufo/issues/271))
51
73
  - Add Ruby 3.2.0 to test runs on CI.
52
74
 
53
75
  ## [0.13.0] - 2021-05-07
@@ -72,7 +94,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
72
94
  - File.read default encode UTF-8
73
95
  - Handle case where the code is invalid but ripper does not raise an error.
74
96
  - Removed implicit dependency on `rake`.
75
- - Error when semicolon and then used in a case statement #230.
97
+ - Error when semicolon and then used in a case statement [#230](https://github.com/ruby-formatter/rufo/issues/230).
76
98
 
77
99
  ### Changed
78
100
 
@@ -163,22 +185,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
163
185
  ## [0.7.0] - 2019-04-28
164
186
 
165
187
  ### Fixed
166
- - Argument align with splat (issue [123](https://github.com/ruby-formatter/rufo/issues/123))
188
+ - Argument align with splat ([#123](https://github.com/ruby-formatter/rufo/issues/123))
167
189
 
168
190
  ### Changed
169
191
 
170
192
  ### Added
171
- - Support jbuilder files (issue [173](https://github.com/ruby-formatter/rufo/issues/173))
193
+ - Support jbuilder files ([#173](https://github.com/ruby-formatter/rufo/issues/173))
172
194
 
173
195
  ## [0.6.1] - 2019-04-27
174
196
 
175
197
  ### Fixed
176
- - Fix bug: symbol literal like `:"foo"` in ruby 2.6.3 (issue [168](https://github.com/ruby-formatter/rufo/issues/168))
198
+ - Fix bug: symbol literal like `:"foo"` in ruby 2.6.3 ([#168](https://github.com/ruby-formatter/rufo/issues/168))
177
199
 
178
200
  ## [0.6.0] - 2019-03-04
179
201
 
180
202
  ### Fixed
181
- - Fix odd indentation of nested one liner "if" statements (issue [128](https://github.com/ruby-formatter/rufo/issues/128))
203
+ - Fix odd indentation of nested one liner "if" statements ([#128](https://github.com/ruby-formatter/rufo/issues/128))
182
204
 
183
205
  ### Added
184
206
  - Ignore `vendor` directories from files to be checked. This allows rufo to be run without any arguments against a directory both locally and on common CI tools without needing any configuration.
@@ -192,7 +214,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
192
214
 
193
215
  ### Fixed
194
216
 
195
- - Fix bug: Rufo crashes on nested empty hash literal (issue [152](https://github.com/ruby-formatter/rufo/issues/152))
217
+ - Fix bug: Rufo crashes on nested empty hash literal ([#152](https://github.com/ruby-formatter/rufo/issues/152))
196
218
  - Handle case where rufo would change formatting if run multiple times for hashes in some cases.
197
219
 
198
220
  ## [0.5.0] - 2019-02-09
@@ -210,7 +232,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
210
232
  ## [0.4.2] - 2019-01-22
211
233
 
212
234
  ### Fixed
213
- - Fix bug: Rufo indents squiggly HEREDOC incorrectly causing bad formating (issue [136](https://github.com/ruby-formatter/rufo/issues/136))
235
+ - Fix bug: Rufo indents squiggly HEREDOC incorrectly causing bad formating ([#136](https://github.com/ruby-formatter/rufo/issues/136))
214
236
 
215
237
  ### Added
216
238
 
@@ -219,14 +241,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
219
241
  ## [0.4.1] - 2018-10-27
220
242
 
221
243
  ### Fixed
222
- - Fix bug for double colon operator in method definition (issue [#120](https://github.com/ruby-formatter/rufo/issues/120))
244
+ - Fix bug for double colon operator in method definition ([#120](https://github.com/ruby-formatter/rufo/issues/120))
223
245
 
224
246
  ## [0.4.0] - 2018-08-09
225
247
 
226
248
  ### Fixed
227
- - Fix Bug: Rufo breaks HEREDOC syntax. (issue [#88](https://github.com/ruby-formatter/rufo/issues/88)). This fixes heredoc within parens formatting causing bad formatting and subsequent syntax error.
228
- - Fix Bug: formatter.rb:3700:in `block in adjust_other_alignments': undefined method `[]' for nil:NilClass (NoMethodError) (issue [#87](https://github.com/ruby-formatter/rufo/issues/87)). Check alignment target exists.
229
- - Fix Bug: Rufo keeps switching formatting on same file every time it runs (issue [#86](https://github.com/ruby-formatter/rufo/issues/86)). Avoids re-indenting nested literal indents.
249
+ - Fix Bug: Rufo breaks HEREDOC syntax. ([#88](https://github.com/ruby-formatter/rufo/issues/88)). This fixes heredoc within parens formatting causing bad formatting and subsequent syntax error.
250
+ - Fix Bug: formatter.rb:3700:in `block in adjust_other_alignments': undefined method `[]' for nil:NilClass (NoMethodError) ([#87](https://github.com/ruby-formatter/rufo/issues/87)). Check alignment target exists.
251
+ - Fix Bug: Rufo keeps switching formatting on same file every time it runs ([#86](https://github.com/ruby-formatter/rufo/issues/86)). Avoids re-indenting nested literal indents.
230
252
 
231
253
  ### Added
232
254
  - Allow for simpler else_body node to suit new Ripper style in 2.6.0
@@ -238,7 +260,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
238
260
  ## [0.3.1] - 2018-04-12
239
261
 
240
262
  ### Fixed
241
- - Fix `quote_style` config not being respected (issue [#95](https://github.com/ruby-formatter/rufo/issues/95)).
263
+ - Fix `quote_style` config not being respected ([#95](https://github.com/ruby-formatter/rufo/issues/95)).
242
264
 
243
265
  ## [0.3.0] - 2018-03-24
244
266
 
@@ -247,9 +269,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
247
269
  - Normalize string quotes according to quote_style, which defaults to :double.
248
270
 
249
271
  ### Fixed
250
- - Fix printing of extraneous trailing semicolons from inline classes, modules, methods (issue [#59](https://github.com/ruby-formatter/rufo/issues/59))
251
- - Fix unhandled white space between array getter/setters `x[0] [0]` (issue [#62](https://github.com/ruby-formatter/rufo/issues/62))
252
- - Fix comma printing for heredocs when inside a hash (issue [#61](https://github.com/ruby-formatter/rufo/issues/61))
272
+ - Fix printing of extraneous trailing semicolons from inline classes, modules, methods ([#59](https://github.com/ruby-formatter/rufo/issues/59))
273
+ - Fix unhandled white space between array getter/setters `x[0] [0]` ([#62](https://github.com/ruby-formatter/rufo/issues/62))
274
+ - Fix comma printing for heredocs when inside a hash ([#61](https://github.com/ruby-formatter/rufo/issues/61))
253
275
  - Fix array literals against changes to Ruby 2.5.0
254
276
 
255
277
  ## [0.2.0] - 2017-11-13
@@ -290,8 +312,8 @@ The following configuration options have been **removed**, and replaced with non
290
312
 
291
313
  ### Fixed
292
314
  - Fix crash in Ruby <= 2.3.4, <= 2.4.1 in the presence of certain squiggly doc (`<<~`) with multiple embedded expressions. The real fix here is to upgrade Ruby to >= 2.3.5 / >= 2.4.2
293
- - Fix dedent bug and bad formatting caused by comments within chained calls (issue [#49](https://github.com/ruby-formatter/rufo/issues/49))
294
- - Fix formatting bug for `for i, in [[1, 2]] ; x ; end` (issue [#45](https://github.com/ruby-formatter/rufo/issues/45))
315
+ - Fix dedent bug and bad formatting caused by comments within chained calls ([#49](https://github.com/ruby-formatter/rufo/issues/49))
316
+ - Fix formatting bug for `for i, in [[1, 2]] ; x ; end` ([#45](https://github.com/ruby-formatter/rufo/issues/45))
295
317
 
296
318
  ## [0.1.0] - 2017-10-08
297
319
  Beginning of logging changes!
@@ -159,7 +159,7 @@ class Rufo::ErbFormatter
159
159
  end
160
160
 
161
161
  def format_code(str)
162
- Rufo::Formatter.format(str).chomp
162
+ Rufo::Formatter.format(str, **@options).chomp
163
163
  end
164
164
 
165
165
  def enable_code_mode
@@ -227,7 +227,7 @@ class Rufo::Formatter
227
227
  when :@CHAR
228
228
  # [:@CHAR, "?a", [1, 0]]
229
229
  consume_token :on_CHAR
230
- when :@gvar, :@backref, :@label, :@op
230
+ when :@backref, :@label, :@op
231
231
  # [:@gvar, "$abc", [1, 0]]
232
232
  # [:@backref, "$1", [1, 0]]
233
233
  # [:@label, "foo:", [1, 3]]
@@ -287,6 +287,7 @@ class Rufo::Formatter
287
287
  when :dyna_symbol
288
288
  visit_quoted_symbol_literal(node)
289
289
  when :@ident
290
+ consume_pin_op
290
291
  consume_token :on_ident
291
292
  when :var_ref, :var_field, :const_ref, :vcall, :fcall
292
293
  # [:var_ref, exp]
@@ -300,10 +301,16 @@ class Rufo::Formatter
300
301
  consume_token :on_kw
301
302
  when :@ivar
302
303
  # [:@ivar, "@foo", [1, 0]]
304
+ consume_pin_op
303
305
  consume_token :on_ivar
304
306
  when :@cvar
305
307
  # [:@cvar, "@@foo", [1, 0]]
308
+ consume_pin_op
306
309
  consume_token :on_cvar
310
+ when :@gvar
311
+ # [:@gvar, "$foo", [1, 0]]
312
+ consume_pin_op
313
+ consume_token :on_gvar
307
314
  when :@const
308
315
  # [:@const, "FOO", [1, 0]]
309
316
  consume_token :on_const
@@ -377,7 +384,7 @@ class Rufo::Formatter
377
384
  visit_until(node)
378
385
  when :case
379
386
  visit_case(node)
380
- when :when
387
+ when :when, :in
381
388
  visit_when(node)
382
389
  when :unary
383
390
  visit_unary(node)
@@ -470,6 +477,12 @@ class Rufo::Formatter
470
477
  visit_end_node(node)
471
478
  when :args_forward
472
479
  consume_op("...")
480
+ when :aryptn
481
+ visit_array_pattern(node)
482
+ when :fndptn
483
+ visit_find_pattern(node)
484
+ when :hshptn
485
+ visit_hash_pattern(node)
473
486
  else
474
487
  bug "Unhandled node: #{node.first}"
475
488
  end
@@ -587,8 +600,8 @@ class Rufo::Formatter
587
600
  needs_comma = !comma? && trailing_commas
588
601
 
589
602
  if inside_literal_elements_list && needs_comma
590
- write ","
591
603
  @last_was_heredoc = true
604
+ @end_of_heredoc_position = @output.length
592
605
  end
593
606
 
594
607
  @output << captured_output
@@ -1543,13 +1556,26 @@ class Rufo::Formatter
1543
1556
  end
1544
1557
 
1545
1558
  def visit_begin(node)
1546
- # begin
1547
- # body
1548
- # end
1549
- #
1550
- # [:begin, [:bodystmt, body, rescue_body, else_body, ensure_body]]
1551
- consume_keyword "begin"
1552
- visit node[1]
1559
+ if op?("^")
1560
+ # ^(expression)
1561
+ #
1562
+ # [:begin, expression_node]
1563
+ consume_op "^"
1564
+ skip_space
1565
+ consume_token :on_lparen
1566
+ skip_space
1567
+ visit node[1]
1568
+ skip_space
1569
+ consume_token :on_rparen
1570
+ else
1571
+ # begin
1572
+ # body
1573
+ # end
1574
+ #
1575
+ # [:begin, [:bodystmt, body, rescue_body, else_body, ensure_body]]
1576
+ consume_keyword "begin"
1577
+ visit node[1]
1578
+ end
1553
1579
  end
1554
1580
 
1555
1581
  def visit_bodystmt(node)
@@ -1800,7 +1826,7 @@ class Rufo::Formatter
1800
1826
  # Maybe a Ripper bug, but if there's something before a star
1801
1827
  # then a star shouldn't be here... but if it is... handle it
1802
1828
  # somehow...
1803
- if current_token_kind == :on_op && current_token_value == "*"
1829
+ if op?("*")
1804
1830
  star = before
1805
1831
  else
1806
1832
  visit_comma_separated_list to_ary(before)
@@ -2353,7 +2379,7 @@ class Rufo::Formatter
2353
2379
  token_column = current_token_column
2354
2380
 
2355
2381
  closing_brace_token, _ = find_closing_brace_token
2356
- need_space = need_space_for_hash?(node, closing_brace_token)
2382
+ need_space = need_space_for_hash?(node, node[1], closing_brace_token)
2357
2383
 
2358
2384
  check :on_lbrace
2359
2385
  write "{"
@@ -2733,7 +2759,7 @@ class Rufo::Formatter
2733
2759
  visit_comma_separated_list exps
2734
2760
  end
2735
2761
 
2736
- def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:)
2762
+ def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:, keep_final_newline: false, &block)
2737
2763
  base_column = @column
2738
2764
  base_line = @line
2739
2765
  needs_final_space = (inside_hash || inside_array) && space?
@@ -2770,6 +2796,7 @@ class Rufo::Formatter
2770
2796
  wrote_comma = false
2771
2797
  first_space = nil
2772
2798
 
2799
+ visitor = block_given? ? block : ->(elem) { visit elem }
2773
2800
  elements.each_with_index do |elem, i|
2774
2801
  @literal_elements_level = @node_level
2775
2802
 
@@ -2777,9 +2804,9 @@ class Rufo::Formatter
2777
2804
  wrote_comma = false
2778
2805
 
2779
2806
  if needs_trailing_comma
2780
- indent(needed_indent) { visit elem }
2807
+ indent(needed_indent) { visitor.call(elem) }
2781
2808
  else
2782
- visit elem
2809
+ visitor.call(elem)
2783
2810
  end
2784
2811
 
2785
2812
  # We have to be careful not to aumatically write a heredoc on next_token,
@@ -2820,7 +2847,13 @@ class Rufo::Formatter
2820
2847
  @literal_elements_level = nil
2821
2848
 
2822
2849
  if needs_trailing_comma
2823
- write "," unless wrote_comma || !trailing_commas || @last_was_heredoc
2850
+ if !wrote_comma && trailing_commas
2851
+ if @last_was_heredoc
2852
+ @output.insert(@end_of_heredoc_position, ",")
2853
+ else
2854
+ write ","
2855
+ end
2856
+ end
2824
2857
 
2825
2858
  consume_end_of_line(first_space: first_space)
2826
2859
  write_indent
@@ -2829,6 +2862,8 @@ class Rufo::Formatter
2829
2862
  else
2830
2863
  if needs_final_space
2831
2864
  consume_space
2865
+ elsif keep_final_newline
2866
+ skip_space
2832
2867
  else
2833
2868
  skip_space_or_newline
2834
2869
  end
@@ -2938,31 +2973,66 @@ class Rufo::Formatter
2938
2973
  # [:case, cond, case_when]
2939
2974
  _, cond, case_when = node
2940
2975
 
2941
- consume_keyword "case"
2976
+ # If node is inline pattern matching, case_expression will be false
2977
+ case_expression = keyword?("case")
2978
+ if case_expression
2979
+ consume_keyword "case"
2980
+ end
2942
2981
 
2943
2982
  if cond
2944
2983
  consume_space
2945
2984
  visit cond
2946
2985
  end
2947
2986
 
2948
- consume_end_of_line
2949
-
2950
- write_indent
2987
+ if case_expression
2988
+ consume_end_of_line
2989
+ write_indent
2990
+ else
2991
+ consume_space
2992
+ end
2951
2993
  visit case_when
2952
2994
 
2953
- write_indent
2954
- consume_keyword "end"
2995
+ if case_expression
2996
+ write_indent
2997
+ consume_keyword "end"
2998
+ end
2955
2999
  end
2956
3000
 
2957
3001
  def visit_when(node)
2958
3002
  # [:when, conds, body, next_exp]
2959
- _, conds, body, next_exp = node
3003
+ # [:in, pattern, body, next_exp]
3004
+ kw, conds_or_pattern, body, next_exp = node
2960
3005
 
2961
- consume_keyword "when"
3006
+ case kw
3007
+ when :when
3008
+ consume_keyword "when"
3009
+ when :in
3010
+ if current_token_kind == :on_op
3011
+ consume_op "=>"
3012
+ else
3013
+ consume_keyword "in"
3014
+ end
3015
+ end
2962
3016
  consume_space
2963
3017
 
2964
3018
  indent(@column) do
2965
- visit_comma_separated_list conds
3019
+ case kw
3020
+ when :when
3021
+ visit_comma_separated_list conds_or_pattern
3022
+ when :in
3023
+ parens = current_token_kind == :on_lparen
3024
+ if parens
3025
+ consume_token :on_lparen
3026
+ skip_space_or_newline
3027
+ end
3028
+
3029
+ visit conds_or_pattern
3030
+
3031
+ if parens
3032
+ skip_space_or_newline
3033
+ consume_token :on_rparen
3034
+ end
3035
+ end
2966
3036
  skip_space
2967
3037
  end
2968
3038
  written_space = false
@@ -3020,12 +3090,15 @@ class Rufo::Formatter
3020
3090
  end
3021
3091
  end
3022
3092
 
3023
- if inline
3024
- indent do
3025
- visit_exps body
3093
+ # If node is inline pattern matching, body will be nil
3094
+ if body
3095
+ if inline
3096
+ indent do
3097
+ visit_exps body
3098
+ end
3099
+ else
3100
+ indent_body body
3026
3101
  end
3027
- else
3028
- indent_body body
3029
3102
  end
3030
3103
 
3031
3104
  if next_exp
@@ -3056,6 +3129,232 @@ class Rufo::Formatter
3056
3129
  end
3057
3130
  end
3058
3131
 
3132
+ def visit_array_pattern(node)
3133
+ # [:aryptn, const_ref, pre_rest, rest, post_rest]
3134
+ _, const_ref, pre_rest, rest, post_rest = node
3135
+
3136
+ if const_ref
3137
+ return visit_constant_pattern(node)
3138
+ end
3139
+
3140
+ # pattern is [*]
3141
+ if !pre_rest && !post_rest && rest == [:var_field, nil]
3142
+ consume_token :on_lbracket
3143
+ skip_space_or_newline
3144
+ consume_op "*"
3145
+ skip_space_or_newline
3146
+ consume_token :on_rbracket
3147
+ return
3148
+ end
3149
+
3150
+ token_column = current_token_column
3151
+
3152
+ has_bracket = current_token_kind == :on_lbracket
3153
+ if has_bracket
3154
+ consume_token :on_lbracket
3155
+ skip_space
3156
+ end
3157
+
3158
+ write_comma = false
3159
+ if pre_rest
3160
+ visit_literal_elements pre_rest, inside_array: true, token_column: token_column, keep_final_newline: !has_bracket
3161
+ write_comma = true
3162
+ end
3163
+
3164
+ # pattern like `[a,]` will make `rest` as `[:var_field, nil]`
3165
+ if rest && ((var_name_node = rest[1]) || current_token_value == "*")
3166
+ if write_comma
3167
+ write ","
3168
+ consume_space
3169
+ else
3170
+ skip_space_or_newline
3171
+ end
3172
+
3173
+ consume_op "*"
3174
+ if var_name_node
3175
+ visit rest
3176
+ end
3177
+ end
3178
+
3179
+ if post_rest
3180
+ skip_space
3181
+ check :on_comma
3182
+ write ","
3183
+ consume_space
3184
+ next_token
3185
+
3186
+ visit_literal_elements post_rest, inside_array: true, token_column: token_column, keep_final_newline: !has_bracket
3187
+ end
3188
+
3189
+ skip_space
3190
+ if has_bracket
3191
+ consume_token :on_rbracket
3192
+ end
3193
+ end
3194
+
3195
+ def visit_constant_pattern(node)
3196
+ # [:aryptn, const_ref, args]
3197
+ _, const_ref, args = node
3198
+
3199
+ visit const_ref
3200
+
3201
+ parens = current_token_kind == :on_lparen
3202
+ if parens
3203
+ consume_token :on_lparen
3204
+ else
3205
+ consume_token :on_lbracket
3206
+ end
3207
+
3208
+ skip_space
3209
+ visit_comma_separated_list args
3210
+
3211
+ skip_space
3212
+ if parens
3213
+ consume_token :on_rparen
3214
+ else
3215
+ consume_token :on_rbracket
3216
+ end
3217
+ end
3218
+
3219
+ def visit_find_pattern(node)
3220
+ # [:fndptn, const_ref, pre, patterns, post]
3221
+ _, const_ref, pre, patterns, post = node
3222
+
3223
+ parens = if const_ref
3224
+ visit const_ref
3225
+ current_token_kind == :on_lparen
3226
+ else
3227
+ false
3228
+ end
3229
+
3230
+ if parens
3231
+ consume_token :on_lparen
3232
+ else
3233
+ consume_token :on_lbracket
3234
+ end
3235
+
3236
+ skip_space
3237
+ consume_op "*"
3238
+ if pre[1] # check pre has name or not
3239
+ visit pre
3240
+ end
3241
+
3242
+ patterns.each do |pattern|
3243
+ skip_space
3244
+ consume_token :on_comma
3245
+ consume_space
3246
+ visit pattern
3247
+ end
3248
+
3249
+ skip_space
3250
+ consume_token :on_comma
3251
+ consume_space
3252
+ consume_op "*"
3253
+ if post[1] # check post has name or not
3254
+ visit post
3255
+ end
3256
+
3257
+ skip_space
3258
+ if parens
3259
+ consume_token :on_rparen
3260
+ else
3261
+ consume_token :on_rbracket
3262
+ end
3263
+ end
3264
+
3265
+ def visit_hash_pattern(node)
3266
+ _, const_ref, elements, rest = node
3267
+
3268
+ if const_ref
3269
+ visit const_ref
3270
+ end
3271
+
3272
+ token_column = current_token_column
3273
+
3274
+ need_space = false
3275
+ expected_right_token = nil
3276
+ if const_ref
3277
+ if current_token_kind == :on_lparen
3278
+ consume_token :on_lparen
3279
+ expected_right_token = :on_rparen
3280
+ else
3281
+ consume_token :on_lbracket
3282
+ expected_right_token = :on_rbracket
3283
+ end
3284
+ elsif current_token_kind == :on_lbrace
3285
+ expected_right_token = :on_rbrace
3286
+ closing_brace_token, _ = find_closing_brace_token
3287
+ need_space = need_space_for_hash?(node, [*elements, rest].compact, closing_brace_token)
3288
+
3289
+ consume_token :on_lbrace
3290
+ brace_position = @output.length - 1
3291
+ consume_space if need_space
3292
+ end
3293
+
3294
+ # pattern is {}
3295
+ empty = !const_ref && !elements && !rest
3296
+ if empty
3297
+ consume_token :on_rbrace
3298
+ return
3299
+ end
3300
+
3301
+ # pattern is {**}
3302
+ empty = !const_ref && elements.empty? && !rest
3303
+ if empty
3304
+ consume_space
3305
+ consume_op "**"
3306
+ consume_space
3307
+ consume_token :on_rbrace
3308
+ return
3309
+ end
3310
+
3311
+ visit_literal_elements elements, inside_hash: true, token_column: token_column, keep_final_newline: expected_right_token.nil? do |element|
3312
+ key, value = element
3313
+ visit key
3314
+ if value
3315
+ consume_space
3316
+ visit value
3317
+ end
3318
+ end
3319
+
3320
+ if rest || op?("**") || comma?
3321
+ unless elements.empty?
3322
+ write ","
3323
+ end
3324
+
3325
+ skip_space_or_newline
3326
+ if rest || op?("**")
3327
+ consume_space
3328
+ consume_op "**"
3329
+ if rest
3330
+ visit rest
3331
+ end
3332
+ end
3333
+ end
3334
+
3335
+ unless expected_right_token.nil?
3336
+ skip_space
3337
+
3338
+ if expected_right_token == :on_rbrace
3339
+ # in some case, need_space_for_hash? might be unexpected behaviour for some patterns, example:
3340
+ # { a: 1,
3341
+ # ** }
3342
+ # so re-check need_space? at here, and insert a space in the missing position if needed.
3343
+ char_after_brace = @output[brace_position + 1]
3344
+ if !need_space && !["\n", " "].include?(char_after_brace)
3345
+ need_space = true
3346
+ @output.insert(brace_position + 1, " ")
3347
+ end
3348
+ end
3349
+
3350
+ check expected_right_token
3351
+ right = current_token_value
3352
+ write " " if need_space
3353
+ write right
3354
+ next_token
3355
+ end
3356
+ end
3357
+
3059
3358
  def consume_space(want_preserve_whitespace: false)
3060
3359
  first_space = skip_space
3061
3360
  if want_preserve_whitespace && !newline? && !comment? && first_space
@@ -3407,6 +3706,13 @@ class Rufo::Formatter
3407
3706
  end
3408
3707
  end
3409
3708
 
3709
+ def consume_pin_op
3710
+ return unless op?("^")
3711
+
3712
+ consume_op "^"
3713
+ skip_space
3714
+ end
3715
+
3410
3716
  def indent(value = nil)
3411
3717
  if value
3412
3718
  old_indent = @indent
@@ -3656,6 +3962,10 @@ class Rufo::Formatter
3656
3962
  current_token_kind == :on_sp
3657
3963
  end
3658
3964
 
3965
+ def op?(value)
3966
+ current_token_kind == :on_op && current_token_value == value
3967
+ end
3968
+
3659
3969
  def void_exps?(node)
3660
3970
  node.size == 1 && node[0].size == 1 && node[0][0] == :void_stmt
3661
3971
  end
@@ -3887,8 +4197,8 @@ class Rufo::Formatter
3887
4197
  end
3888
4198
 
3889
4199
  # Check to see if need to add space inside hash literal braces.
3890
- def need_space_for_hash?(node, closing_brace_token)
3891
- return false unless node[1]
4200
+ def need_space_for_hash?(node, elements, closing_brace_token)
4201
+ return false if elements.nil? || elements.empty?
3892
4202
 
3893
4203
  left_need_space = current_token_line == node_line(node, beginning: true)
3894
4204
  right_need_space = closing_brace_token[0][0] == node_line(node, beginning: false)
@@ -3901,8 +4211,16 @@ class Rufo::Formatter
3901
4211
  # get line of node, it is only used in visit_hash right now,
3902
4212
  # so handling the following node types is enough.
3903
4213
  case node.first
3904
- when :hash, :string_literal, :symbol_literal, :symbol, :vcall, :string_content, :assoc_splat, :var_ref, :dyna_symbol
4214
+ when :hash, :string_literal, :symbol_literal, :symbol, :vcall, :string_content, :assoc_splat, :var_ref, :dyna_symbol, :var_field
3905
4215
  node_line(node[1], beginning: beginning)
4216
+ when :hshptn
4217
+ _, _, elements, rest = node
4218
+ elem = if beginning
4219
+ (elements[0] && elements[0][0]) || rest
4220
+ else
4221
+ rest || elements.last[0]
4222
+ end
4223
+ node_line(elem, beginning: beginning)
3906
4224
  when :assoc_new
3907
4225
  # There's no line number info for empty strings or hashes.
3908
4226
  if node[1] != EMPTY_STRING && node[1] != EMPTY_HASH
data/lib/rufo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rufo
4
- VERSION = "0.15.1"
4
+ VERSION = "0.16.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.1
4
+ version: 0.16.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-08 00:00:00.000000000 Z
11
+ date: 2023-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -210,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
210
  - !ruby/object:Gem::Version
211
211
  version: '0'
212
212
  requirements: []
213
- rubygems_version: 3.3.7
213
+ rubygems_version: 3.4.10
214
214
  signing_key:
215
215
  specification_version: 4
216
216
  summary: Ruby code formatter