Skip to content

Commit

Permalink
Silence parser warning and patch version mismatch when loading parser…
Browse files Browse the repository at this point in the history
…/current (#613)

* Implement RubyParserFactory to avoid warning when requiring parser/current
  • Loading branch information
navidemad authored Feb 17, 2025
1 parent c560714 commit f81bfba
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Silence parser warning and patch version mismatch for Ruby 3.4.x > 3.4.0 [#612](https://github.com/glebm/i18n-tasks/issues/612)
* Append JSON instructions to OpenAI system prompt to be able to use response_format json_object [#615](https://github.com/glebm/i18n-tasks/pull/615)
* Set `log_errors: true` on OpenAI::Client options in order to display HTTP client errors. [#614](https://github.com/glebm/i18n-tasks/pull/614)
* Uses AST-parser for all ERB-files, not just `.html.erb`
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/tasks/scanners/local_ruby_parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'parser/current'
require 'i18n/tasks/scanners/ruby_parser_factory'

module I18n::Tasks::Scanners
class LocalRubyParser
Expand All @@ -9,7 +9,7 @@ class LocalRubyParser
BLOCK_EXPR = /\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze

def initialize(ignore_blocks: false)
@parser = ::Parser::CurrentRuby.new
@parser = RubyParserFactory.create_parser
@ignore_blocks = ignore_blocks
end

Expand Down
6 changes: 3 additions & 3 deletions lib/i18n/tasks/scanners/ruby_ast_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
require 'i18n/tasks/scanners/file_scanner'
require 'i18n/tasks/scanners/relative_keys'
require 'i18n/tasks/scanners/ruby_ast_call_finder'
require 'i18n/tasks/scanners/ruby_parser_factory'
require 'i18n/tasks/scanners/ast_matchers/default_i18n_subject_matcher'
require 'i18n/tasks/scanners/ast_matchers/message_receivers_matcher'
require 'i18n/tasks/scanners/ast_matchers/rails_model_matcher'
require 'parser/current'

module I18n::Tasks::Scanners
# Scan for I18n.translate calls using whitequark/parser
Expand All @@ -18,8 +18,8 @@ class RubyAstScanner < FileScanner

def initialize(**args)
super(**args)
@parser = ::Parser::CurrentRuby.new
@magic_comment_parser = ::Parser::CurrentRuby.new
@parser = RubyParserFactory.create_parser
@magic_comment_parser = RubyParserFactory.create_parser
@matchers = setup_matchers
end

Expand Down
27 changes: 27 additions & 0 deletions lib/i18n/tasks/scanners/ruby_parser_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# This module provides a factory class for creating a Ruby parser instance.
# It temporarily disables verbose mode to suppress compatibility warnings
# when loading the "parser/current" library.
#
# Example warning for the release of Ruby 3.4.1:
# warning: parser/current is loading parser/ruby34, which recognizes
# 3.4.0-compliant syntax, but you are running 3.4.1.
# Please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
#
# By disabling verbose mode, these warnings are suppressed to provide a cleaner
# output and avoid confusion. The verbose mode is restored after the parser
# instance is created to maintain the original behavior.

module I18n::Tasks::Scanners
class RubyParserFactory
def self.create_parser
prev = $VERBOSE
$VERBOSE = nil
require 'parser/current'
::Parser::CurrentRuby.new
ensure
$VERBOSE = prev
end
end
end

0 comments on commit f81bfba

Please sign in to comment.