Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid an error when parsing files including SyntaxError #1574

Merged
merged 1 commit into from
Oct 30, 2023

Conversation

sinsoku
Copy link
Contributor

@sinsoku sinsoku commented Oct 18, 2023

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.

fixes #1414

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.
@ksss
Copy link
Collaborator

ksss commented Oct 18, 2023

LGTM.
How about to support file mode?

@sinsoku
Copy link
Contributor Author

sinsoku commented Oct 20, 2023

If a similar change is made to file mode, the output RBS will be mixed with warnings and the exit code will be 0.

$ cat a.rb
class A < <%= @superclass %>
end

$ cat b.rb
class A
end

$ git diff
diff --git lib/rbs/cli.rb lib/rbs/cli.rb
index 0a2aeaab..bb0e823f 100644
--- lib/rbs/cli.rb
+++ lib/rbs/cli.rb
@@ -996,6 +996,9 @@ EOU

         input_paths.each do |file|
           parser.parse file.read()
+        rescue SyntaxError
+          stdout.puts "⚠️  Unable to parse due to SyntaxError: `#{file}`"
+          next
         end

         writer = Writer.new(out: stdout)

$ ./exe/rbs prototype rb a.rb b.rb
⚠️  Unable to parse due to SyntaxError: `a.rb`
class A
end

I think the use case of specifying a SyntaxError file as an argument is rare, so I don't think it's necessary to support it now.
It is not too late to think about it after others raise their requests.

@soutaro soutaro added this to the RBS 3.3 milestone Oct 30, 2023
Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

@soutaro soutaro added this pull request to the merge queue Oct 30, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 30, 2023
@soutaro soutaro added this pull request to the merge queue Oct 30, 2023
Merged via the queue into ruby:master with commit fea32c3 Oct 30, 2023
23 checks passed
@sinsoku sinsoku deleted the avoid-syntax-error branch October 30, 2023 13:19
@soutaro soutaro added the Released PRs already included in the released version label Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

rbs prototype rb fails due to syntax error
3 participants