Skip to content

Commit

Permalink
Fix error when parsing ERB files with yield on the top level
Browse files Browse the repository at this point in the history
Top level yields are invalid Ruby on files, but are valid on `eval`.

They exist on ERB files, and since those files are evaluated no written
to disk they are fine.

We can tell Prism to parse the code using the eval context by passing
`scopes: [[]]` to `parse`, but since are are using the
`Prism::Translation::Parser` class we have no way to do that.

A [PR][1] was proposed to Prism to always consider the code as being
evaluated in the `Parser` translation.

For now, we can safely ignore that class of error by overriding the
`valid_error?` method on our own parser.

Fixes #400.

[1]: ruby/prism#2741
  • Loading branch information
rafaelfranca committed May 2, 2024
1 parent 23f31a3 commit e24ea74
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PATH
constant_resolver (>= 0.2.0)
parallel
parser
prism (>= 0.24.0)
prism (>= 0.25.0)
sorbet-runtime (>= 0.5.9914)
zeitwerk (>= 2.6.1)

Expand Down Expand Up @@ -77,7 +77,7 @@ GEM
ast (~> 2.4.1)
racc
prettier_print (0.1.0)
prism (0.24.0)
prism (0.27.0)
racc (1.7.1)
rack (2.2.4)
rack-test (2.0.2)
Expand Down
6 changes: 6 additions & 0 deletions lib/packwerk/parsers/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def initialize(builder)
super(builder)
super.diagnostics.all_errors_are_fatal = true
end

private

def valid_error?(error)
error.type != :invalid_yield
end
end

class TolerateInvalidUtf8Builder < Parser::Builders::Default
Expand Down
2 changes: 1 addition & 1 deletion packwerk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Gem::Specification.new do |spec|
# For Ruby parsing
spec.add_dependency("ast")
spec.add_dependency("parser")
spec.add_dependency("prism", ">= 0.24.0") # 0.24.0 fixes a performance issue with the parser translator
spec.add_dependency("prism", ">= 0.25.0")

# For ERB parsing
spec.add_dependency("better_html")
Expand Down

0 comments on commit e24ea74

Please sign in to comment.