Class: I18n::Tasks::Scanners::LocalRubyParser
- Inherits:
-
Object
- Object
- I18n::Tasks::Scanners::LocalRubyParser
- Defined in:
- lib/i18n/tasks/scanners/local_ruby_parser.rb
Constant Summary collapse
- BLOCK_EXPR =
ignore_blocks feature inspired by shopify/better-html github.com/Shopify/better-html/blob/087943ffd2a5877fa977d71532010b0c91239519/lib/better_html/test_helper/ruby_node.rb#L24
/\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze
Instance Method Summary collapse
-
#initialize(ignore_blocks: false) ⇒ LocalRubyParser
constructor
A new instance of LocalRubyParser.
-
#normalize_comment_location(comment, location) ⇒ Parser::Source::Comment
Normalize location for comment.
- #normalize_location(node, location) ⇒ Parser::AST::Node
-
#parse(source, location: nil) ⇒ Object
Parse string and normalize location.
-
#updated_location(global_location, local_location) ⇒ Parser::Source::Map
Calculate location relative to a global location.
Constructor Details
#initialize(ignore_blocks: false) ⇒ LocalRubyParser
Returns a new instance of LocalRubyParser.
11 12 13 14 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 11 def initialize(ignore_blocks: false) @parser = ::Parser::CurrentRuby.new @ignore_blocks = ignore_blocks end |
Instance Method Details
#normalize_comment_location(comment, location) ⇒ Parser::Source::Comment
Normalize location for comment
76 77 78 79 80 81 82 83 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 76 def normalize_comment_location(comment, location) range = ::Parser::Source::Range.new( location.expression.source_buffer, location.expression.to_range.begin + comment.location.expression.to_range.begin, location.expression.to_range.begin + comment.location.expression.to_range.end ) ::Parser::Source::Comment.new(range) end |
#normalize_location(node, location) ⇒ Parser::AST::Node
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 37 def normalize_location(node, location) return node.map { |child| normalize_location(child, location) } if node.is_a?(Array) return node unless node.is_a?(::Parser::AST::Node) node.updated( nil, node.children.map { |child| normalize_location(child, location) }, { location: updated_location(location, node.location) } ) end |
#parse(source, location: nil) ⇒ Object
Parse string and normalize location
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 17 def parse(source, location: nil) buffer = ::Parser::Source::Buffer.new('(string)') buffer.source = if @ignore_blocks source.sub(BLOCK_EXPR, '') else source end @parser.reset ast, comments = @parser.parse_with_comments(buffer) ast = normalize_location(ast, location) comments = comments.map { |comment| normalize_comment_location(comment, location) } [ast, comments] end |
#updated_location(global_location, local_location) ⇒ Parser::Source::Map
Calculate location relative to a global location
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 54 def updated_location(global_location, local_location) return global_location if local_location.expression.nil? range = ::Parser::Source::Range.new( global_location.expression.source_buffer, global_location.expression.to_range.begin + local_location.expression.to_range.begin, global_location.expression.to_range.begin + local_location.expression.to_range.end ) ::Parser::Source::Map::Definition.new( range.begin, range.begin, range.begin, range.end ) end |