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

#construct_keys, #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.



6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
# File 'lib/syntax_tree/node.rb', line 6709

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



6704
6705
6706
# File 'lib/syntax_tree/node.rb', line 6704

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6707
6708
6709
# File 'lib/syntax_tree/node.rb', line 6707

def comments
  @comments
end

#keyword_restObject (readonly)

nil | :nil | KwRestParam

the optional keyword rest parameter



6701
6702
6703
# File 'lib/syntax_tree/node.rb', line 6701

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Array[ [ Ident, nil | untyped

]] any keyword parameters and their

optional default values



6698
6699
6700
# File 'lib/syntax_tree/node.rb', line 6698

def keywords
  @keywords
end

#optionalsObject (readonly)

Array[ [ Ident, untyped

]] any optional parameters and their default

values



6686
6687
6688
# File 'lib/syntax_tree/node.rb', line 6686

def optionals
  @optionals
end

#postsObject (readonly)

Array[ Ident ]

any positional parameters that exist after a rest

parameter



6694
6695
6696
# File 'lib/syntax_tree/node.rb', line 6694

def posts
  @posts
end

#requiredsObject (readonly)

Array[ Ident ]

any required parameters



6682
6683
6684
# File 'lib/syntax_tree/node.rb', line 6682

def requireds
  @requireds
end

#restObject (readonly)

nil | ArgsForward | ExcessedComma | RestParam

the optional rest

parameter



6690
6691
6692
# File 'lib/syntax_tree/node.rb', line 6690

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



6740
6741
6742
# File 'lib/syntax_tree/node.rb', line 6740

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

#child_nodesObject Also known as: deconstruct



6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
# File 'lib/syntax_tree/node.rb', line 6744

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

#deconstruct_keys(_keys) ⇒ Object



6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
# File 'lib/syntax_tree/node.rb', line 6758

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)


6735
6736
6737
6738
# File 'lib/syntax_tree/node.rb', line 6735

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

#format(q) ⇒ Object



6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
# File 'lib/syntax_tree/node.rb', line 6772

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.is_a?(ExcessedComma)
  end

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