Class: NodeMutation::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/node_mutation/adapter.rb

Direct Known Subclasses

ParserAdapter, PrismAdapter, SyntaxTreeAdapter

Constant Summary collapse

INDEX_REGEXP =
/\A-?\d+\z/

Instance Method Summary collapse

Instance Method Details

#child_node_range(node, child_name) ⇒ { start: Number, end: Number }

Get the start/end range of the child node

Parameters:

  • node (Node)

    ast node

  • child_name (String)

    child name selector

Returns:

  • ({ start: Number, end: Number })

    child node range

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/node_mutation/adapter.rb', line 34

def child_node_range(node, child_name)
  raise NotImplementedError, "child_node_range is not implemented"
end

#file_source(node) ⇒ Object

The file content of the ast node file

Parameters:

  • node (Node)

    ast node

Returns:

  • file content

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/node_mutation/adapter.rb', line 26

def file_source(node)
  raise NotImplementedError, "file_source is not implemented"
end

#get_end(node, child_name = nil) ⇒ Number

Get end position of ast node

Parameters:

  • node (Node)

    ast node

  • child_name (String) (defaults to: nil)

    child name selector

Returns:

  • (Number)

    end position of node or child node

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/node_mutation/adapter.rb', line 50

def get_end(node, child_name = nil)
  raise NotImplementedError, "get_end is not implemented"
end

#get_end_loc(node, child_name = nil) ⇒ { line: Number, column: Number }

Get end location of ast node

Parameters:

  • node (Node)

    ast node

  • child_name (String) (defaults to: nil)

    child name selector

Returns:

  • ({ line: Number, column: Number })

    end location of node or child node

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/node_mutation/adapter.rb', line 66

def get_end_loc(node, child_name = nil)
  raise NotImplementedError, "get_end_loc is not implemented"
end

#get_indent(node) ⇒ Number

Get indent of ast node

Parameters:

  • node (Node)

    ast node

Returns:

  • (Number)

    indent

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/node_mutation/adapter.rb', line 73

def get_indent(node)
  raise NotImplementedError, "get_indent is not implemented"
end

#get_source(node) ⇒ String

Get source code of the ast node

Parameters:

  • node (Node)

    ast node

Returns:

  • (String)

    source code

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/node_mutation/adapter.rb', line 9

def get_source(node)
  raise NotImplementedError, "get_source is not implemented"
end

#get_start(node, child_name = nil) ⇒ Number

Get start position of ast node

Parameters:

  • node (Node)

    ast node

  • child_name (String) (defaults to: nil)

    child name selector

Returns:

  • (Number)

    start position of node or child node

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/node_mutation/adapter.rb', line 42

def get_start(node, child_name = nil)
  raise NotImplementedError, "get_start is not implemented"
end

#get_start_loc(node, child_name = nil) ⇒ { line: Number, column: Number }

Get start location of ast node

Parameters:

  • node (Node)

    ast node

  • child_name (String) (defaults to: nil)

    child name selector

Returns:

  • ({ line: Number, column: Number })

    start location of node or child node

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/node_mutation/adapter.rb', line 58

def get_start_loc(node, child_name = nil)
  raise NotImplementedError, "get_start_loc is not implemented"
end

#rewritten_source(node, code) ⇒ String

Replace the child node selector with child node source code it will return ‘Boolean(foobar)`

Examples:

# source code of ast node is ‘!!foobar`, code is `Boolean(NodeMutation::Adapter.{expression{expression.operand{expression.operand.operand})`,

Parameters:

  • node (Node)

    ast node

  • code (String)

    code with child node selector, e.g. ‘Boolean(NodeMutation::Adapter.{expression{expression.operand{expression.operand.operand})`

Returns:

  • (String)

    code with source code of child node selector,

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/node_mutation/adapter.rb', line 19

def rewritten_source(node, code)
  raise NotImplementedError, "rewritten_source is not implemented"
end