code-ruby 3.0.10 → 3.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e59505a1d473ed64e03853dd3465ce958bb81ee2dfcd2ec38b0280a5ee3dd98a
4
- data.tar.gz: e13a2d15ba45209e81213bd7b5a3f1ea58a26cdcb9446f29f9f6b5906aa7e9fe
3
+ metadata.gz: fb5a51861ed784ebc8e0fbd2e6e6232ee05a3f3bafbc4f150e5cc92236590254
4
+ data.tar.gz: b2a82b40b6740387fde8490e5054cc1a0ab5d39c34e2ce7a694421cf5e0852ed
5
5
  SHA512:
6
- metadata.gz: 82cf388eb36848ff8f01f122177e436205ccebfd6f4e19488e65e1adecdff267246292da159a99b42cca23f5adefb505483ebe1357b928922ee824ac4c832086
7
- data.tar.gz: 3069cb6a421189bad2b0aba4eed4a3d50d5e24128f1fcd6fa6ee12db5110b5a53babf2b310175ba4701b59a5bb3447bb4fd1505282c5f3de44c5e2c3a530fc6d
6
+ metadata.gz: ca94cf8211ff117ca68db85a48d876a28f857398996b327a88a6488931d29bbdda34bc6d1b893af9196ecc25735a83875533f099cecbc15b5c9ffd248e34d0df
7
+ data.tar.gz: 9ecf7d03fffb87b1d7ade9242bc0cc4e75a8ff04b31c57b3c05cb2caba5811c1cb567de362e0c95c5852b2bebc3794df6291adb773b8b4a13da0c7320df03b3f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (3.0.10)
4
+ code-ruby (3.0.11)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.10
1
+ 3.0.11
data/lib/code/format.rb CHANGED
@@ -434,10 +434,17 @@ class Code
434
434
  "#{expression}#{operator}#{right}"
435
435
  else
436
436
  candidate = "#{expression} #{operator} #{right}"
437
- if multiline_collection_statement?(other[:statement]) &&
438
- right.include?("\n")
437
+ if right.include?("\n")
439
438
  first_line, *rest = right.lines(chomp: true)
440
- [ "#{expression} #{operator} #{first_line}", *rest ].join("\n")
439
+ if multiline_operand_statement?(other[:statement]) ||
440
+ !%w[and or].include?(operator)
441
+ [ "#{expression} #{operator} #{first_line.lstrip}", *rest ].join("\n")
442
+ else
443
+ [
444
+ "#{expression}\n#{INDENT * (indent + 1)}#{operator} #{first_line.lstrip}",
445
+ *rest
446
+ ].join("\n")
447
+ end
441
448
  elsif expression.include?("\n") || candidate.length > MAX_LINE_LENGTH
442
449
  right_lines =
443
450
  if right.include?("\n")
@@ -491,6 +498,17 @@ class Code
491
498
  operator = operation[:operator].to_s
492
499
  left = format_nested_statement(operation[:left], indent: indent)
493
500
  right = format_nested_statement(operation[:right], indent: indent)
501
+ if right.include?("\n")
502
+ first_line, *rest = right.lines(chomp: true)
503
+ first_line = first_line.lstrip
504
+ return "#{left} #{operator} #{first_line}" if rest.empty?
505
+
506
+ return [
507
+ "#{left} #{operator} #{first_line}",
508
+ *rest
509
+ ].join("\n")
510
+ end
511
+
494
512
  "#{left} #{operator} #{right}"
495
513
  end
496
514
 
@@ -598,9 +616,22 @@ class Code
598
616
  values.join(", ").length > MAX_INLINE_COLLECTION_LENGTH
599
617
  end
600
618
 
601
- def multiline_collection_statement?(statement)
602
- statement.is_a?(Hash) &&
603
- (statement.key?(:dictionnary) || statement.key?(:list))
619
+ def multiline_operand_statement?(statement)
620
+ return false unless statement.is_a?(Hash)
621
+
622
+ return true if statement.key?(:dictionnary) || statement.key?(:list)
623
+ return true if statement.key?(:call)
624
+ if statement.key?(:left_operation)
625
+ operation = statement[:left_operation]
626
+ others = Array(operation[:others])
627
+
628
+ return false if others.empty?
629
+ return false unless others.all? { |other| compact_operator?(other[:operator]) }
630
+
631
+ return multiline_operand_statement?(operation[:first])
632
+ end
633
+
634
+ false
604
635
  end
605
636
 
606
637
  def multiline_call_arguments?(raw_arguments, arguments)
@@ -692,7 +723,7 @@ class Code
692
723
  search_limit = [MAX_LINE_LENGTH, line.length - token.length].min
693
724
  index = line.rindex(token, search_limit)
694
725
  while index
695
- break if index.positive? && outside_string?(line, index)
726
+ break if index.positive? && outside_string_and_grouping?(line, index)
696
727
 
697
728
  index = line.rindex(token, index - 1)
698
729
  end
@@ -701,9 +732,10 @@ class Code
701
732
  [index, token]
702
733
  end
703
734
 
704
- def outside_string?(line, index)
735
+ def outside_string_and_grouping?(line, index)
705
736
  quote_count = 0
706
737
  escaped = false
738
+ grouping_depth = 0
707
739
  line[0...index].each_char do |char|
708
740
  if escaped
709
741
  escaped = false
@@ -714,10 +746,17 @@ class Code
714
746
  escaped = true
715
747
  elsif char == '"'
716
748
  quote_count += 1
749
+ elsif quote_count.even?
750
+ case char
751
+ when "(", "[", "{"
752
+ grouping_depth += 1
753
+ when ")", "]", "}"
754
+ grouping_depth -= 1 if grouping_depth.positive?
755
+ end
717
756
  end
718
757
  end
719
758
 
720
- quote_count.even?
759
+ quote_count.even? && grouping_depth.zero?
721
760
  end
722
761
  end
723
762
  end
@@ -51,6 +51,18 @@ RSpec.describe Code::Format do
51
51
  [
52
52
  "blocks << { title: \"hello world\", description: \"lorem ipsum dolor es sit\", position: 1 }",
53
53
  "blocks << {\n title: \"hello world\",\n description: \"lorem ipsum dolor es sit\",\n position: 1\n}"
54
+ ],
55
+ [
56
+ "sections << Html.join([Html.p { Html.b { \"{index + 1}. {title}\" } }, Html.p { query } if query.presence, Html.p { Html.a(href: link || inline_url) { :source } } if (link || inline_url), Html.p { Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) } }, Html.p { Html.a(href: attachment_url) { \"télécharger\" } }].compact)",
57
+ "sections << Html.join(\n [\n Html.p { Html.b { \"{index + 1}. {title}\" } },\n Html.p { query } if query.presence,\n Html.p {\n Html.a(href: link || inline_url) { :source }\n } if (link || inline_url),\n Html.p {\n Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) }\n },\n Html.p {\n Html.a(href: attachment_url) { \"télécharger\" }\n }\n ].compact\n)"
58
+ ],
59
+ [
60
+ "safe = post.present? and !post[:over_18] and post[:post_hint] == :image and post[:url].to_string.strip.presence and (post[:url].to_string.strip.ends_with?(\".jpg\") or post[:url].to_string.strip.ends_with?(\".jpeg\") or post[:url].to_string.strip.ends_with?(\".png\") or post[:url].to_string.strip.include?(\"i.redd.it\"))",
61
+ "safe = post.present?\n and !post[:over_18]\n and post[:post_hint] == :image\n and post[:url].to_string.strip.presence\n and (post[:url].to_string.strip.ends_with?(\".jpg\")\n or post[:url].to_string.strip.ends_with?(\".jpeg\")\n or post[:url].to_string.strip.ends_with?(\".png\")\n or post[:url].to_string.strip.include?(\"i.redd.it\"))"
62
+ ],
63
+ [
64
+ "items.each { |item, index| proxied_image_url = if image_url proxy_url(image_url) else nothing end }",
65
+ "items.each { |item, index|\n proxied_image_url = if image_url\n proxy_url(image_url)\n else\n nothing\n end\n}"
54
66
  ]
55
67
  ].each do |input, expected|
56
68
  it "formats #{input.inspect}" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.10
4
+ version: 3.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié