Skip to content

Commit f10de32

Browse files
committed
Format non-Ruby STDIN/script content too
1 parent ea300f2 commit f10de32

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

lib/syntax_tree/cli.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ def writable?
6363
class ScriptItem
6464
attr_reader :source
6565

66-
def initialize(source)
66+
def initialize(source, handler_extension)
6767
@source = source
68+
@handler_extension = handler_extension
6869
end
6970

7071
def handler
71-
HANDLERS[".rb"]
72+
HANDLERS[@handler_extension]
7273
end
7374

7475
def filepath
@@ -82,8 +83,12 @@ def writable?
8283

8384
# An item of work that correspond to the content passed in via stdin.
8485
class STDINItem
86+
def initialize(handler_extension)
87+
@handler_extension = handler_extension
88+
end
89+
8590
def handler
86-
HANDLERS[".rb"]
91+
HANDLERS[@handler_extension]
8792
end
8893

8994
def filepath
@@ -457,7 +462,10 @@ def run(item)
457462
The maximum line width to use when formatting.
458463
459464
-e SCRIPT
460-
Parse an inline Ruby string.
465+
Parse an inline string.
466+
467+
--handler=EXTENSION
468+
A file extension matching the content passed in via STDIN or -e. Defaults to 'rb'
461469
HELP
462470

463471
# This represents all of the options that can be passed to the CLI. It is
@@ -468,13 +476,15 @@ class Options
468476
:plugins,
469477
:print_width,
470478
:scripts,
479+
:handler_extension,
471480
:target_ruby_version
472481

473482
def initialize
474483
@ignore_files = []
475484
@plugins = []
476485
@print_width = DEFAULT_PRINT_WIDTH
477486
@scripts = []
487+
@handler_extension = ".rb"
478488
@target_ruby_version = DEFAULT_RUBY_VERSION
479489
end
480490

@@ -523,6 +533,13 @@ def parser
523533
# it and add it to the list of scripts to run.
524534
opts.on("-e SCRIPT") { |script| @scripts << script }
525535

536+
# If there is a handler specified, then parse it and use it for
537+
# STDIN and scripts.
538+
opts.on("--handler=EXTENSION") do |extension|
539+
# Both ".rb" and "rb" are going to work
540+
@handler_extension = ".#{extension.delete_prefix(".")}"
541+
end
542+
526543
# If there is a target ruby version specified on the command line,
527544
# parse that out and use it when formatting.
528545
opts.on("--target-ruby-version=VERSION") do |version|
@@ -630,9 +647,11 @@ def run(argv)
630647
end
631648
end
632649

633-
options.scripts.each { |script| queue << ScriptItem.new(script) }
650+
options.scripts.each do |script|
651+
queue << ScriptItem.new(script, options.handler_extension)
652+
end
634653
else
635-
queue << STDINItem.new
654+
queue << STDINItem.new(options.handler_extension)
636655
end
637656

638657
# At the end, we're going to return whether or not this worker ever

test/cli_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def parse(source)
1010
source * 2
1111
end
1212

13+
def format(source, _print_width, **)
14+
"Formatted #{source}"
15+
end
16+
1317
def read(filepath)
1418
File.read(filepath)
1519
end
@@ -202,6 +206,26 @@ def test_multiple_inline_scripts
202206
assert_equal(["1 + 1", "2 + 2"], stdio.split("\n").sort)
203207
end
204208

209+
def test_format_script_with_custom_handler
210+
SyntaxTree.register_handler(".test", TestHandler.new)
211+
stdio, =
212+
capture_io { SyntaxTree::CLI.run(%w[format --handler=test -e <test>]) }
213+
assert_equal("Formatted <test>\n", stdio)
214+
ensure
215+
SyntaxTree::HANDLERS.delete(".test")
216+
end
217+
218+
def test_format_stdin_with_custom_handler
219+
SyntaxTree.register_handler(".test", TestHandler.new)
220+
stdin = $stdin
221+
$stdin = StringIO.new("<test>")
222+
stdio, = capture_io { SyntaxTree::CLI.run(%w[format --handler=test]) }
223+
assert_equal("Formatted <test>\n", stdio)
224+
ensure
225+
$stdin = stdin
226+
SyntaxTree::HANDLERS.delete(".test")
227+
end
228+
205229
def test_generic_error
206230
SyntaxTree.stub(:format, ->(*) { raise }) do
207231
result = run_cli("format")

0 commit comments

Comments
 (0)