code-ruby 3.0.9 → 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 +4 -4
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/code/format.rb +53 -5
- data/spec/code/format_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fb5a51861ed784ebc8e0fbd2e6e6232ee05a3f3bafbc4f150e5cc92236590254
|
|
4
|
+
data.tar.gz: b2a82b40b6740387fde8490e5054cc1a0ab5d39c34e2ce7a694421cf5e0852ed
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca94cf8211ff117ca68db85a48d876a28f857398996b327a88a6488931d29bbdda34bc6d1b893af9196ecc25735a83875533f099cecbc15b5c9ffd248e34d0df
|
|
7
|
+
data.tar.gz: 9ecf7d03fffb87b1d7ade9242bc0cc4e75a8ff04b31c57b3c05cb2caba5811c1cb567de362e0c95c5852b2bebc3794df6291adb773b8b4a13da0c7320df03b3f
|
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
3.0.
|
|
1
|
+
3.0.11
|
data/lib/code/format.rb
CHANGED
|
@@ -434,7 +434,18 @@ class Code
|
|
|
434
434
|
"#{expression}#{operator}#{right}"
|
|
435
435
|
else
|
|
436
436
|
candidate = "#{expression} #{operator} #{right}"
|
|
437
|
-
if
|
|
437
|
+
if right.include?("\n")
|
|
438
|
+
first_line, *rest = right.lines(chomp: true)
|
|
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
|
|
448
|
+
elsif expression.include?("\n") || candidate.length > MAX_LINE_LENGTH
|
|
438
449
|
right_lines =
|
|
439
450
|
if right.include?("\n")
|
|
440
451
|
right.lines(chomp: true).map(&:lstrip)
|
|
@@ -442,7 +453,7 @@ class Code
|
|
|
442
453
|
right.split(" #{operator} ")
|
|
443
454
|
end
|
|
444
455
|
continuation_lines =
|
|
445
|
-
right_lines.
|
|
456
|
+
right_lines.map do |line|
|
|
446
457
|
content = line.lstrip
|
|
447
458
|
prefix =
|
|
448
459
|
(content.start_with?("#{operator} ") ? "" : "#{operator} ")
|
|
@@ -487,6 +498,17 @@ class Code
|
|
|
487
498
|
operator = operation[:operator].to_s
|
|
488
499
|
left = format_nested_statement(operation[:left], indent: indent)
|
|
489
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
|
+
|
|
490
512
|
"#{left} #{operator} #{right}"
|
|
491
513
|
end
|
|
492
514
|
|
|
@@ -594,6 +616,24 @@ class Code
|
|
|
594
616
|
values.join(", ").length > MAX_INLINE_COLLECTION_LENGTH
|
|
595
617
|
end
|
|
596
618
|
|
|
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
|
|
635
|
+
end
|
|
636
|
+
|
|
597
637
|
def multiline_call_arguments?(raw_arguments, arguments)
|
|
598
638
|
return true if arguments.any? { |argument| argument.include?("\n") }
|
|
599
639
|
return true if arguments.size > MAX_INLINE_COLLECTION_ITEMS
|
|
@@ -683,7 +723,7 @@ class Code
|
|
|
683
723
|
search_limit = [MAX_LINE_LENGTH, line.length - token.length].min
|
|
684
724
|
index = line.rindex(token, search_limit)
|
|
685
725
|
while index
|
|
686
|
-
break if index.positive? &&
|
|
726
|
+
break if index.positive? && outside_string_and_grouping?(line, index)
|
|
687
727
|
|
|
688
728
|
index = line.rindex(token, index - 1)
|
|
689
729
|
end
|
|
@@ -692,9 +732,10 @@ class Code
|
|
|
692
732
|
[index, token]
|
|
693
733
|
end
|
|
694
734
|
|
|
695
|
-
def
|
|
735
|
+
def outside_string_and_grouping?(line, index)
|
|
696
736
|
quote_count = 0
|
|
697
737
|
escaped = false
|
|
738
|
+
grouping_depth = 0
|
|
698
739
|
line[0...index].each_char do |char|
|
|
699
740
|
if escaped
|
|
700
741
|
escaped = false
|
|
@@ -705,10 +746,17 @@ class Code
|
|
|
705
746
|
escaped = true
|
|
706
747
|
elsif char == '"'
|
|
707
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
|
|
708
756
|
end
|
|
709
757
|
end
|
|
710
758
|
|
|
711
|
-
quote_count.even?
|
|
759
|
+
quote_count.even? && grouping_depth.zero?
|
|
712
760
|
end
|
|
713
761
|
end
|
|
714
762
|
end
|
data/spec/code/format_spec.rb
CHANGED
|
@@ -47,6 +47,22 @@ RSpec.describe Code::Format do
|
|
|
47
47
|
[
|
|
48
48
|
"x = { content: \"you select the funniest tweets for a french \" + \"audience from the provided candidates. pick up to \" + \"{max_selected}. prioritize genuinely funny content\" + \" (jokes, memes, punchlines, absurd). avoid \" + \"politics/news/serious content. output only json.\" }",
|
|
49
49
|
"x = {\n content: \"you select the funniest tweets for a french \"\n + \"audience from the provided candidates. pick up to \"\n + \"{max_selected}. prioritize genuinely funny content\"\n + \" (jokes, memes, punchlines, absurd). avoid \"\n + \"politics/news/serious content. output only json.\"\n}"
|
|
50
|
+
],
|
|
51
|
+
[
|
|
52
|
+
"blocks << { title: \"hello world\", description: \"lorem ipsum dolor es sit\", position: 1 }",
|
|
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}"
|
|
50
66
|
]
|
|
51
67
|
].each do |input, expected|
|
|
52
68
|
it "formats #{input.inspect}" do
|