Class: SyntaxTree::Params

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Params represents defining parameters on a method or lambda.

def method(param) end

Defined Under Namespace

Classes: KeywordFormatter, KeywordRestFormatter, OptionalFormatter

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(requireds: [], optionals: [], rest: nil, posts: [], keywords: [], keyword_rest: nil, block: nil, location:, comments: []) ⇒ Params

Returns a new instance of Params.



6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
# File 'lib/syntax_tree/node.rb', line 6065

def initialize(
  requireds: [],
  optionals: [],
  rest: nil,
  posts: [],
  keywords: [],
  keyword_rest: nil,
  block: nil,
  location:,
  comments: []
)
  @requireds = requireds
  @optionals = optionals
  @rest = rest
  @posts = posts
  @keywords = keywords
  @keyword_rest = keyword_rest
  @block = block
  @location = location
  @comments = comments
end

Instance Attribute Details

#blockObject (readonly)

nil | BlockArg

the optional block parameter



6060
6061
6062
# File 'lib/syntax_tree/node.rb', line 6060

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6063
6064
6065
# File 'lib/syntax_tree/node.rb', line 6063

def comments
  @comments
end

#keyword_restObject (readonly)

nil | :nil | KwRestParam

the optional keyword rest parameter



6057
6058
6059
# File 'lib/syntax_tree/node.rb', line 6057

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Array[ [ Ident, nil | untyped

]] any keyword parameters and their

optional default values



6054
6055
6056
# File 'lib/syntax_tree/node.rb', line 6054

def keywords
  @keywords
end

#optionalsObject (readonly)

Array[ [ Ident, untyped

]] any optional parameters and their default

values



6042
6043
6044
# File 'lib/syntax_tree/node.rb', line 6042

def optionals
  @optionals
end

#postsObject (readonly)

Array[ Ident ]

any positional parameters that exist after a rest

parameter



6050
6051
6052
# File 'lib/syntax_tree/node.rb', line 6050

def posts
  @posts
end

#requiredsObject (readonly)

Array[ Ident ]

any required parameters



6038
6039
6040
# File 'lib/syntax_tree/node.rb', line 6038

def requireds
  @requireds
end

#restObject (readonly)

nil | ArgsForward | ExcessedComma | RestParam

the optional rest

parameter



6046
6047
6048
# File 'lib/syntax_tree/node.rb', line 6046

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



6096
6097
6098
# File 'lib/syntax_tree/node.rb', line 6096

def accept(visitor)
  visitor.visit_params(self)
end

#child_nodesObject Also known as: deconstruct



6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
# File 'lib/syntax_tree/node.rb', line 6100

def child_nodes
  [
    *requireds,
    *optionals.flatten(1),
    rest,
    *posts,
    *keywords.flatten(1),
    (keyword_rest if keyword_rest != :nil),
    block
  ]
end

#deconstruct_keys(keys) ⇒ Object



6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
# File 'lib/syntax_tree/node.rb', line 6114

def deconstruct_keys(keys)
  {
    location: location,
    requireds: requireds,
    optionals: optionals,
    rest: rest,
    posts: posts,
    keywords: keywords,
    keyword_rest: keyword_rest,
    block: block,
    comments: comments
  }
end

#empty?Boolean

Params nodes are the most complicated in the tree. Occasionally you want to know if they are “empty”, which means not having any parameters declared. This logic accesses every kind of parameter and determines if it’s missing.

Returns:

  • (Boolean)


6091
6092
6093
6094
# File 'lib/syntax_tree/node.rb', line 6091

def empty?
  requireds.empty? && optionals.empty? && !rest && posts.empty? &&
    keywords.empty? && !keyword_rest && !block
end

#format(q) ⇒ Object



6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
# File 'lib/syntax_tree/node.rb', line 6128

def format(q)
  parts = [
    *requireds,
    *optionals.map { |(name, value)| OptionalFormatter.new(name, value) }
  ]

  parts << rest if rest && !rest.is_a?(ExcessedComma)
  parts +=
    [
      *posts,
      *keywords.map { |(name, value)| KeywordFormatter.new(name, value) }
    ]

  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
  parts << block if block

  contents = -> do
    q.seplist(parts) { |part| q.format(part) }
    q.format(rest) if rest && rest.is_a?(ExcessedComma)
  end

  if [Def, Defs, DefEndless].include?(q.parent.class)
    q.group(0, "(", ")") do
      q.indent do
        q.breakable("")
        contents.call
      end
      q.breakable("")
    end
  else
    q.nest(0, &contents)
  end
end