code-ruby 3.0.12 → 3.0.13

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: eef80c2ebc4ef6d318ee0af151704caca0f0a370c698dc727814836d6e5146db
4
- data.tar.gz: 6ac8ae69e473aa937eb4794466e710ec40444c53f5d1a12149368e03de0bc4c0
3
+ metadata.gz: 6baa3c7c3ef6b602529e2af4a36200029291ed89c115f1c1f3d750a8e815cc03
4
+ data.tar.gz: aa9c876fb0aff915f895ec3d209d25ac06270e3809eadba8f5e71b37b9cb29db
5
5
  SHA512:
6
- metadata.gz: 6b99011ea5c71b86b7709949e2d4096f4708b2a4c985e1f30e704bac060c58c710922797c4c4ade47f91b31ad37a47153f66fd8e204783786d55da38234e0d79
7
- data.tar.gz: 367d671e1fbb4aaacd278bd3e186a03a7304f7041f5381e502a54e44afe754048ed603eb83e6e21f2c8415916078b633fc352984ae6c5b4ab27f96c330394b19
6
+ metadata.gz: 444adc5d9b9a5223b82a921a888558a2f23dc40344a12b383137581b910ba4e33ea13873a2e5ce6698dc130ae77775cf9aa5b3e0e23f70947418301831df96b9
7
+ data.tar.gz: bcf7ecebc28788a2cf7464e3a7b67f32b2bfb86ec3d6365ca58674914d852181d27f3a051b596665807acd67291c2026d227b98d33603260e3122417e04d6a76
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (3.0.12)
4
+ code-ruby (3.0.13)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.12
1
+ 3.0.13
data/lib/code/format.rb CHANGED
@@ -434,7 +434,7 @@ class Code
434
434
 
435
435
  Array(operation[:others]).each do |other|
436
436
  right = format_nested_statement(other[:statement], indent: indent)
437
- operator = other[:operator]
437
+ operator = format_operator(other[:operator])
438
438
 
439
439
  expression =
440
440
  if compact_operator?(operator)
@@ -532,7 +532,7 @@ class Code
532
532
  end
533
533
 
534
534
  def format_right_operation(operation, indent:)
535
- operator = operation[:operator].to_s
535
+ operator = format_operator(operation[:operator])
536
536
  left =
537
537
  if %w[if unless].include?(operator)
538
538
  format_modifier_left(operation[:left], indent: indent)
@@ -595,6 +595,17 @@ class Code
595
595
  %w[. :: &. .. ...].include?(operator)
596
596
  end
597
597
 
598
+ def format_operator(operator)
599
+ case operator.to_s
600
+ when "||"
601
+ "or"
602
+ when "&&"
603
+ "and"
604
+ else
605
+ operator.to_s
606
+ end
607
+ end
608
+
598
609
  def format_ternary(ternary, indent:)
599
610
  left = format_nested_statement(ternary[:left], indent: indent)
600
611
  middle = format_nested_statement(ternary[:middle], indent: indent)
@@ -157,6 +157,10 @@ class Code
157
157
 
158
158
  def code_strip
159
159
  String.new(raw.strip)
160
+ rescue ArgumentError, Encoding::CompatibilityError => e
161
+ raise unless e.message.include?("invalid byte sequence")
162
+
163
+ String.new(sanitized_utf8_raw.strip)
160
164
  end
161
165
 
162
166
  def code_split(value)
@@ -172,6 +176,15 @@ class Code
172
176
  def present?
173
177
  raw.present?
174
178
  end
179
+
180
+ private
181
+
182
+ def sanitized_utf8_raw
183
+ raw
184
+ .dup
185
+ .force_encoding(::Encoding::UTF_8)
186
+ .encode(::Encoding::UTF_8, invalid: :replace, undef: :replace)
187
+ end
175
188
  end
176
189
  end
177
190
  end
@@ -13,6 +13,8 @@ RSpec.describe Code::Format do
13
13
  %w[100000 100_000],
14
14
  %w[1000000 1_000_000],
15
15
  %w[1.0000000001 1.000_000_000_1],
16
+ ["true || false", "true or false"],
17
+ ["true && false", "true and false"],
16
18
  ["{a:1}", "{ a: 1 }"],
17
19
  ["[1,2,3]", "[1, 2, 3]"],
18
20
  ["[1, 2, 3].select { |n| n.even? }", "[1, 2, 3].select { |n| n.even? }"],
@@ -54,7 +56,7 @@ RSpec.describe Code::Format do
54
56
  ],
55
57
  [
56
58
  "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)"
59
+ "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 or inline_url) { :source }\n } if (link or 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
60
  ],
59
61
  [
60
62
  "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\"))",
@@ -15,4 +15,12 @@ RSpec.describe Code::Object::String do
15
15
  expect(Code.evaluate(input).to_s).to eq(expected)
16
16
  end
17
17
  end
18
+
19
+ describe "#code_strip" do
20
+ it "replaces invalid utf-8 bytes instead of raising" do
21
+ string = described_class.new("\xC3 ".b.force_encoding(Encoding::UTF_8))
22
+
23
+ expect(string.code_strip.to_s).to eq("\uFFFD")
24
+ end
25
+ end
18
26
  end
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.12
4
+ version: 3.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié