@@ -63,12 +63,13 @@ def writable?
6363 class ScriptItem
6464 attr_reader :source
6565
66- def initialize ( source )
66+ def initialize ( source , extension )
6767 @source = source
68+ @extension = extension
6869 end
6970
7071 def handler
71- HANDLERS [ ".rb" ]
72+ HANDLERS [ @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 ( extension )
87+ @extension = extension
88+ end
89+
8590 def handler
86- HANDLERS [ ".rb" ]
91+ HANDLERS [ @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+ --extension=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+ :extension ,
471480 :target_ruby_version
472481
473482 def initialize
474483 @ignore_files = [ ]
475484 @plugins = [ ]
476485 @print_width = DEFAULT_PRINT_WIDTH
477486 @scripts = [ ]
487+ @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 extension specified, then parse it and use it for
537+ # STDIN and scripts.
538+ opts . on ( "--extension=EXTENSION" ) do |extension |
539+ # Both ".rb" and "rb" are going to work
540+ @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 |
@@ -633,9 +650,11 @@ def run(argv)
633650 end
634651 end
635652
636- options . scripts . each { |script | queue << ScriptItem . new ( script ) }
653+ options . scripts . each do |script |
654+ queue << ScriptItem . new ( script , options . extension )
655+ end
637656 else
638- queue << STDINItem . new
657+ queue << STDINItem . new ( options . extension )
639658 end
640659
641660 # At the end, we're going to return whether or not this worker ever
0 commit comments