Class: Parser::AST::Node
- Inherits:
-
Object
- Object
- Parser::AST::Node
- Defined in:
- lib/synvert/core/node_ext.rb
Overview
Extend Parser::AST::Node. https://github.com/whitequark/parser/blob/master/lib/parser/ast/node.rb
Rules
Synvert compares ast nodes with key / value pairs, each ast node has multiple attributes, e.g. +receiver+, +message+ and +arguments+, it matches only when all of key / value pairs match.
+type: 'send', message: :include, arguments: ['FactoryGirl::Syntax::Methods']+
Synvert does comparison based on the value type
- if value is a symbol, then compares ast node value as symbol, e.g. +message: :include+
- if value is a string, then compares ast node original source code, e.g. +name: 'Synvert::Application'+
- if value is a regexp, then compares ast node original source code, e.g. +message: /find_all_by_/+
- if value is an array, then compares each ast node, e.g. +arguments: ['FactoryGirl::Syntax::Methods']+
- if value is nil, then check if ast node is nil, e.g. +arguments: [nil]+
- if value is true or false, then check if ast node is :true or :false, e.g. +arguments: [false]+
- if value is ast, then compare ast node directly, e.g. +to_ast: Parser::CurrentRuby.parse("self.class.serialized_attributes")+
It can also compare nested key / value pairs, like
+type: 'send', receiver: { type: 'send', receiver: { type: 'send', message: 'config' }, message: 'active_record' }, message: 'identity_map='+
Source Code to Ast Node https://playground.synvert.net/ruby
Instance Method Summary collapse
-
#column ⇒ Integer
Get the column of node.
-
#filename ⇒ String
Get the file name of node.
-
#line ⇒ Integer
Get the line of node.
-
#strip_curly_braces ⇒ String
Strip curly braces for hash.
-
#to_lambda_literal ⇒ String
Convert lambda {} to -> {}.
-
#to_single_quote ⇒ String
Get single quote string.
-
#to_string ⇒ String
Convert symbol to string.
-
#to_symbol ⇒ String
Convert string to symbol.
-
#wrap_curly_braces ⇒ String
Wrap curly braces for hash.
Instance Method Details
#column ⇒ Integer
Get the column of node.
42 43 44 |
# File 'lib/synvert/core/node_ext.rb', line 42 def column loc.expression.column end |
#filename ⇒ String
Get the file name of node.
35 36 37 |
# File 'lib/synvert/core/node_ext.rb', line 35 def filename loc.expression&.source_buffer.name end |
#line ⇒ Integer
Get the line of node.
49 50 51 |
# File 'lib/synvert/core/node_ext.rb', line 49 def line loc.expression.line end |
#strip_curly_braces ⇒ String
Strip curly braces for hash.
58 59 60 61 62 |
# File 'lib/synvert/core/node_ext.rb', line 58 def strip_curly_braces return to_source unless type == :hash to_source.sub(/^{(.*)}$/) { Regexp.last_match(1).strip } end |
#to_lambda_literal ⇒ String
Convert lambda {} to -> {}
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/synvert/core/node_ext.rb', line 113 def to_lambda_literal if type == :block && caller.type == :send && caller.receiver.nil? && caller. == :lambda new_source = to_source if arguments.size > 1 new_source = new_source[0...arguments.first.loc.expression.begin_pos - 2] + new_source[arguments.last.loc.expression.end_pos + 1..-1] new_source = new_source.sub('lambda', "->(#{arguments.map(&:to_source).join(', ')})") else new_source = new_source.sub('lambda', '->') end new_source else to_source end end |
#to_single_quote ⇒ String
Get single quote string.
80 81 82 83 84 |
# File 'lib/synvert/core/node_ext.rb', line 80 def to_single_quote return to_source unless type == :str "'#{to_value}'" end |
#to_string ⇒ String
Convert symbol to string.
102 103 104 105 106 |
# File 'lib/synvert/core/node_ext.rb', line 102 def to_string return to_source unless type == :sym to_value.to_s end |
#to_symbol ⇒ String
Convert string to symbol.
91 92 93 94 95 |
# File 'lib/synvert/core/node_ext.rb', line 91 def to_symbol return to_source unless type == :str ":#{to_value}" end |
#wrap_curly_braces ⇒ String
Wrap curly braces for hash.
69 70 71 72 73 |
# File 'lib/synvert/core/node_ext.rb', line 69 def wrap_curly_braces return to_source unless type == :hash "{ #{to_source} }" end |