Skip to content

Commit 1ca8601

Browse files
committed
Add max-line-length CLI option
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent dbf800b commit 1ca8601

2 files changed

Lines changed: 123 additions & 2 deletions

File tree

lib/spoom/cli/srb/sigs.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@ class Sigs < Thor
1616
desc: "Use positional names when translating from RBI to RBS",
1717
default: true
1818
option :include_rbi_files, type: :boolean, desc: "Include RBI files", default: false
19+
option :max_line_length, type: :numeric, desc: "Max line length (pass 0 to disable)", default: 120
1920
def translate(*paths)
2021
from = options[:from]
2122
to = options[:to]
23+
max_line_length = options[:max_line_length]
2224

2325
if from == to
2426
say_error("Can't translate signatures from `#{from}` to `#{to}`")
2527
exit(1)
2628
end
2729

30+
if max_line_length.nil? || max_line_length.zero?
31+
max_line_length = nil
32+
elsif max_line_length.negative?
33+
say_error("--max-line-length can't be negative")
34+
exit(1)
35+
else
36+
max_line_length = max_line_length.to_i
37+
end
38+
2839
files = collect_files(paths, include_rbi_files: options[:include_rbi_files])
2940

3041
say("Translating signatures from `#{from}` to `#{to}` " \
@@ -33,11 +44,20 @@ def translate(*paths)
3344
case from
3445
when "rbi"
3546
transformed_files = transform_files(files) do |file, contents|
36-
Spoom::Sorbet::Translate.sorbet_sigs_to_rbs_comments(contents, file: file, positional_names: options[:positional_names])
47+
Spoom::Sorbet::Translate.sorbet_sigs_to_rbs_comments(
48+
contents,
49+
file: file,
50+
positional_names: options[:positional_names],
51+
max_line_length: max_line_length,
52+
)
3753
end
3854
when "rbs"
3955
transformed_files = transform_files(files) do |file, contents|
40-
Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs(contents, file: file)
56+
Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs(
57+
contents,
58+
file: file,
59+
max_line_length: max_line_length,
60+
)
4161
end
4262
end
4363

test/spoom/cli/srb/sigs_test.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,62 @@ def foo; end
206206
RB
207207
end
208208

209+
def test_translate_to_rbs_with_max_line_length_error
210+
result = @project.spoom("srb sigs translate --no-color --max-line-length -1")
211+
212+
assert_equal(<<~ERR, result.err)
213+
Error: --max-line-length can't be negative
214+
ERR
215+
refute(result.status)
216+
end
217+
218+
def test_translate_to_rbs_with_max_line_length_by_default
219+
@project.write!("file.rb", <<~RB)
220+
sig do
221+
params(
222+
param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
223+
param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
224+
).void
225+
end
226+
def foo(param1:, param2:); end
227+
RB
228+
229+
result = @project.spoom("srb sigs translate --no-color")
230+
231+
assert_empty(result.err)
232+
assert(result.status)
233+
234+
assert_equal(<<~RB, @project.read("file.rb"))
235+
#: (
236+
#| param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
237+
#| param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
238+
#| ) -> void
239+
def foo(param1:, param2:); end
240+
RB
241+
end
242+
243+
def test_translate_to_rbs_without_max_line_length
244+
@project.write!("file.rb", <<~RB)
245+
sig do
246+
params(
247+
param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
248+
param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
249+
).void
250+
end
251+
def foo(param1:, param2:); end
252+
RB
253+
254+
result = @project.spoom("srb sigs translate --no-color --max-line-length 0")
255+
256+
assert_empty(result.err)
257+
assert(result.status)
258+
259+
assert_equal(<<~RB, @project.read("file.rb"))
260+
#: (param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType, param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) -> void
261+
def foo(param1:, param2:); end
262+
RB
263+
end
264+
209265
# translate --from rbs --to rbi
210266

211267
def test_translate_from_rbs_to_rbi
@@ -230,6 +286,51 @@ def foo; end
230286
RB
231287
end
232288

289+
def test_translate_to_rbi_with_max_line_length_by_default
290+
@project.write!("file.rb", <<~RB)
291+
#: (
292+
#| param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
293+
#| param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
294+
#| ) -> void
295+
def foo(param1:, param2:); end
296+
RB
297+
298+
result = @project.spoom("srb sigs translate --no-color --from rbs --to rbi")
299+
300+
assert_empty(result.err)
301+
assert(result.status)
302+
303+
assert_equal(<<~RB, @project.read("file.rb"))
304+
sig do
305+
params(
306+
param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
307+
param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
308+
).void
309+
end
310+
def foo(param1:, param2:); end
311+
RB
312+
end
313+
314+
def test_translate_to_rbi_without_max_line_length
315+
@project.write!("file.rb", <<~RB)
316+
#: (
317+
#| param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType,
318+
#| param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType
319+
#| ) -> void
320+
def foo(param1:, param2:); end
321+
RB
322+
323+
result = @project.spoom("srb sigs translate --no-color --from rbs --to rbi --max-line-length 0")
324+
325+
assert_empty(result.err)
326+
assert(result.status)
327+
328+
assert_equal(<<~RB, @project.read("file.rb"))
329+
sig { params(param1: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType, param2: AVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType).void }
330+
def foo(param1:, param2:); end
331+
RB
332+
end
333+
233334
# export
234335

235336
def test_export_no_gemspec

0 commit comments

Comments
 (0)