code-ruby 3.0.4 → 3.0.5

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: 5a1d60879ca94a53452e814d8c8de4975be88088beb0ce216f3584a48b0dfcae
4
- data.tar.gz: 0ad78db73acc96f99df57e3c945c5d97fb404ed692d5f9de7c7bc08c25562468
3
+ metadata.gz: 71c8d3a9deab8aee738543c6fb7a3b06732c5ecd0fbd042ec41c7e3d3a351bf1
4
+ data.tar.gz: e828824ea1a50b4fa0cdb071aeee9b0042933dfad6b80c90ee91df7010946fd1
5
5
  SHA512:
6
- metadata.gz: 2eec31b74d22e1d123ea138cf41063792cbff5e2192e26a692c7dfaa52834db9112bacb9929d3a88d59a6fd4296cc23ff0501a41761f95fa963b027878eda54c
7
- data.tar.gz: 3363b4f05ca9203842aa8c3a6eee0ccd2ac570609cd9bf3bfb77bda134b234c2ddd0e95a9265941ac8239659260823dfc8e36c8fb34643d835cc702dbc5e5ae4
6
+ metadata.gz: 75f6791a43a543361becdde69f55c47134dc2386ef6e018df1fed31b261f748afb1a16233cccc1d1512f95858866654954250b2d48570510e3910e71efdc38f7
7
+ data.tar.gz: '041049251701b05e2ea7f3c95ed48173b4cdc1fc76dfef311e5109f6c3c25de51fe9c0a604261915759617dc1684a59d01defffc5578e9eb57d65bb9c1658201'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (3.0.4)
4
+ code-ruby (3.0.5)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.4
1
+ 3.0.5
data/lib/code/format.rb CHANGED
@@ -413,10 +413,22 @@ class Code
413
413
  else
414
414
  candidate = "#{expression} #{operator} #{right}"
415
415
  if expression.include?("\n") || candidate.length > MAX_LINE_LENGTH
416
- right_parts = right.split(" #{operator} ")
416
+ right_lines =
417
+ if right.include?("\n")
418
+ right.lines(chomp: true).map(&:lstrip)
419
+ else
420
+ right.split(" #{operator} ")
421
+ end
417
422
  continuation_lines =
418
- right_parts.map do |part|
419
- "#{INDENT * (indent + 1)}#{operator} #{part}"
423
+ right_lines.each_with_index.map do |line, index|
424
+ content = line.lstrip
425
+ prefix =
426
+ if content.start_with?("#{operator} ")
427
+ ""
428
+ else
429
+ "#{operator} "
430
+ end
431
+ "#{INDENT * (indent + 1)}#{prefix}#{content}"
420
432
  end
421
433
  "#{expression}\n#{continuation_lines.join("\n")}"
422
434
  else
data/lib/code/parser.rb CHANGED
@@ -350,11 +350,10 @@ class Code
350
350
  skip_newlines
351
351
 
352
352
  if match?(:keyword, "if") || match?(:keyword, "unless")
353
- else_operator = advance.value
354
- skip_newlines
355
- else_statement = parse_expression
356
- else_body = parse_body(%w[elsif elsunless else end])
357
- elses << { operator: else_operator, statement: else_statement, body: else_body }
353
+ elses << {
354
+ operator: "else",
355
+ body: [parse_if_expression(advance.value)]
356
+ }
358
357
  else
359
358
  elses << { operator: "else", body: parse_body(%w[end]) }
360
359
  end
@@ -365,6 +364,7 @@ class Code
365
364
  break
366
365
  end
367
366
 
367
+ skip_newlines
368
368
  advance if match?(:keyword, "end")
369
369
 
370
370
  {
@@ -382,6 +382,7 @@ class Code
382
382
 
383
383
  statement = parse_expression unless operator == "loop"
384
384
  body = parse_body(%w[end])
385
+ skip_newlines
385
386
  advance if match?(:keyword, "end")
386
387
 
387
388
  {
@@ -414,7 +415,10 @@ class Code
414
415
  parse_delimited_code("{", "}")
415
416
  elsif match?(:keyword, "do") || match?(:keyword, "begin")
416
417
  advance
417
- parse_code(stop_keywords: ["end"]).tap { advance if match?(:keyword, "end") }
418
+ parse_code(stop_keywords: ["end"]).tap do
419
+ skip_newlines
420
+ advance if match?(:keyword, "end")
421
+ end
418
422
  else
419
423
  [parse_expression]
420
424
  end
@@ -548,6 +552,7 @@ class Code
548
552
  expect(:keyword, "do")
549
553
  parameters = parse_pipe_parameters
550
554
  body = parse_code(stop_keywords: ["end"])
555
+ skip_newlines
551
556
  expect(:keyword, "end")
552
557
  end
553
558
 
@@ -23,6 +23,14 @@ RSpec.describe Code::Format do
23
23
  "if true 1 elsif false 2 else 3 end",
24
24
  "if true\n 1\nelsif false\n 2\nelse\n 3\nend"
25
25
  ],
26
+ [
27
+ "if false 1 else if true 2 else 3 end",
28
+ "if false\n 1\nelse\n if true\n 2\n else\n 3\n end\nend"
29
+ ],
30
+ [
31
+ "event[:uid].present? and event[:summary].present? and event[:starts_at].present? and event[:ends_at].present?",
32
+ "event[:uid].present?\n and event[:summary].present?\n and event[:starts_at].present?\n and event[:ends_at].present?"
33
+ ],
26
34
  [
27
35
  "sum = (a, b: 2) => { a + b } sum(1)",
28
36
  "sum = (a, b: 2) => {\n a + b\n}\n\nsum(1)"
data/spec/code_spec.rb CHANGED
@@ -366,7 +366,10 @@ RSpec.describe Code do
366
366
  ["if false 1 else 2", "2"],
367
367
  ["if false 1 else if false 2", "nothing"],
368
368
  ["if false 1 else if true 2", "2"],
369
+ ["if false 1 else if false 2 else 3 end", "3"],
370
+ ["if false 1 else if true 2 else 3 end", "2"],
369
371
  ["if false 1 else unless false 2", "2"],
372
+ ["if false 1 else unless true 2 else 3 end", "3"],
370
373
  ["if false 1 else unless true 2", "nothing"],
371
374
  ["if false 1 elsif false 2", "nothing"],
372
375
  ["if false 1 elsif true 2", "2"],
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.4
4
+ version: 3.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié