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.



6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
# File 'lib/syntax_tree/node.rb', line 6887

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



6882
6883
6884
# File 'lib/syntax_tree/node.rb', line 6882

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6885
6886
6887
# File 'lib/syntax_tree/node.rb', line 6885

def comments
  @comments
end

#keyword_restObject (readonly)

nil | :nil | KwRestParam

the optional keyword rest parameter



6879
6880
6881
# File 'lib/syntax_tree/node.rb', line 6879

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Array[ [ Ident, nil | untyped

]] any keyword parameters and their

optional default values



6876
6877
6878
# File 'lib/syntax_tree/node.rb', line 6876

def keywords
  @keywords
end

#optionalsObject (readonly)

Array[ [ Ident, untyped

]] any optional parameters and their default

values



6864
6865
6866
# File 'lib/syntax_tree/node.rb', line 6864

def optionals
  @optionals
end

#postsObject (readonly)

Array[ Ident ]

any positional parameters that exist after a rest

parameter



6872
6873
6874
# File 'lib/syntax_tree/node.rb', line 6872

def posts
  @posts
end

#requiredsObject (readonly)

Array[ Ident ]

any required parameters



6860
6861
6862
# File 'lib/syntax_tree/node.rb', line 6860

def requireds
  @requireds
end

#restObject (readonly)

nil | ArgsForward | ExcessedComma | RestParam

the optional rest

parameter



6868
6869
6870
# File 'lib/syntax_tree/node.rb', line 6868

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object



6918
6919
6920
# File 'lib/syntax_tree/node.rb', line 6918

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

#child_nodesObject Also known as: deconstruct



6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
# File 'lib/syntax_tree/node.rb', line 6922

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

#deconstruct_keys(_keys) ⇒ Object



6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
# File 'lib/syntax_tree/node.rb', line 6936

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)


6913
6914
6915
6916
# File 'lib/syntax_tree/node.rb', line 6913

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

#format(q) ⇒ Object



6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
# File 'lib/syntax_tree/node.rb', line 6950

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