Class: Parser::AST::Node

Inherits:
Object
  • Object
show all
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

  1. if value is a symbol, then compares ast node value as symbol, e.g. +message: :include+
  2. if value is a string, then compares ast node original source code, e.g. +name: 'Synvert::Application'+
  3. if value is a regexp, then compares ast node original source code, e.g. +message: /find_all_by_/+
  4. if value is an array, then compares each ast node, e.g. +arguments: ['FactoryGirl::Syntax::Methods']+
  5. if value is nil, then check if ast node is nil, e.g. +arguments: [nil]+
  6. if value is true or false, then check if ast node is :true or :false, e.g. +arguments: [false]+
  7. 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

Instance Method Details

#columnInteger

Get the column of node.

Returns:

  • (Integer)

    column.



42
43
44
# File 'lib/synvert/core/node_ext.rb', line 42

def column
  loc.expression.column
end

#filenameString

Get the file name of node.

Returns:

  • (String)

    file name.



35
36
37
# File 'lib/synvert/core/node_ext.rb', line 35

def filename
  loc.expression&.source_buffer.name
end

#lineInteger

Get the line of node.

Returns:

  • (Integer)

    line.



49
50
51
# File 'lib/synvert/core/node_ext.rb', line 49

def line
  loc.expression.line
end

#strip_curly_bracesString

Strip curly braces for hash.

Examples:

node # s(:hash, s(:pair, s(:sym, :foo), s(:str, "bar")))
node.strip_curly_braces # "foo: 'bar'"

Returns:

  • (String)


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_literalString

Convert lambda {} to -> {}

Examples:

node # s(:block, s(:send, nil, :lambda), s(:args), s(:send, nil, :foobar))
node.to_lambda_literal # "-> { foobar }"

Returns:

  • (String)


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.message == :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_quoteString

Get single quote string.

Examples:

node # s(:str, "foobar")
node.to_single_quote # "'foobar'"

Returns:

  • (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_stringString

Convert symbol to string.

Examples:

node # s(:sym, :foobar)
node.to_string # "foobar"

Returns:

  • (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_symbolString

Convert string to symbol.

Examples:

node # s(:str, "foobar")
node.to_symbol # ":foobar"

Returns:

  • (String)


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_bracesString

Wrap curly braces for hash.

Examples:

node # s(:hash, s(:pair, s(:sym, :foo), s(:str, "bar")))
node.wrap_curly_braces # "{ foo: 'bar' }"

Returns:

  • (String)


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