Skip to content

Commit

Permalink
Avoid an error when parsing files including SyntaxError
Browse files Browse the repository at this point in the history
There are cases where ERB templates are saved as `*.rb`.
For example, `devise`.
refs: https://github.com/heartcombo/devise/blob/v4.9.3/lib/generators/active_record/templates/migration.rb

Running `rbs prototype rb` in a directory containing these files
will crash due to a SyntaxError.
This commit fixes that to avoid the error and continue processing.
  • Loading branch information
sinsoku committed Oct 18, 2023
1 parent 88523a3 commit fcdf631
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,12 @@ def run_prototype_file(format, args)
output_path = (output_dir + relative_path).sub_ext(".rbs")

parser = new_parser[]
parser.parse file_path.read()
begin
parser.parse file_path.read()
rescue SyntaxError
stdout.puts " ⚠️ Unable to parse due to SyntaxError: `#{file_path}`"
next
end

if output_path.file?
if force
Expand Down
26 changes: 26 additions & 0 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,32 @@ module A
end
end

def test_prototype_batch_syntax_error
Dir.mktmpdir do |dir|
dir = Pathname(dir)

(dir + "lib").mkdir
(dir + "lib/a.rb").write(<<-RUBY)
module A < <%= @superclass %>
end
RUBY

Dir.chdir(dir) do
with_cli do |cli|
cli.run(%w(prototype rb --out_dir=sig lib))

assert_equal <<-EOM, cli.stdout.string
Processing `lib`...
Generating RBS for `lib/a.rb`...
⚠️ Unable to parse due to SyntaxError: `lib/a.rb`
EOM
end
end

refute_predicate dir + "sig", :directory?
end
end

def test_prototype__runtime__todo
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
Expand Down

0 comments on commit fcdf631

Please sign in to comment.