Class: SyntaxTree::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Params.



8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
# File 'lib/syntax_tree.rb', line 8904

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



8896
8897
8898
# File 'lib/syntax_tree.rb', line 8896

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8902
8903
8904
# File 'lib/syntax_tree.rb', line 8902

def comments
  @comments
end

#keyword_restObject (readonly)

nil | :nil | KwRestParam

the optional keyword rest parameter



8893
8894
8895
# File 'lib/syntax_tree.rb', line 8893

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Array[ [ Ident, nil | untyped

]] any keyword parameters and their

optional default values



8890
8891
8892
# File 'lib/syntax_tree.rb', line 8890

def keywords
  @keywords
end

#locationObject (readonly)

Location

the location of this node



8899
8900
8901
# File 'lib/syntax_tree.rb', line 8899

def location
  @location
end

#optionalsObject (readonly)

Array[ [ Ident, untyped

]] any optional parameters and their default

values



8878
8879
8880
# File 'lib/syntax_tree.rb', line 8878

def optionals
  @optionals
end

#postsObject (readonly)

Array[ Ident ]

any positional parameters that exist after a rest

parameter



8886
8887
8888
# File 'lib/syntax_tree.rb', line 8886

def posts
  @posts
end

#requiredsObject (readonly)

Array[ Ident ]

any required parameters



8874
8875
8876
# File 'lib/syntax_tree.rb', line 8874

def requireds
  @requireds
end

#restObject (readonly)

nil | ArgsForward | ExcessedComma | RestParam

the optional rest

parameter



8882
8883
8884
# File 'lib/syntax_tree.rb', line 8882

def rest
  @rest
end

Instance Method Details

#child_nodesObject



8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
# File 'lib/syntax_tree.rb', line 8935

def child_nodes
  [
    *requireds,
    *optionals.flatten(1),
    rest,
    *posts,
    *keywords.flatten(1),
    (keyword_rest if keyword_rest != :nil),
    block
  ]
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)


8930
8931
8932
8933
# File 'lib/syntax_tree.rb', line 8930

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

#format(q) ⇒ Object



8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
# File 'lib/syntax_tree.rb', line 8947

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].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

#pretty_print(q) ⇒ Object



8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
# File 'lib/syntax_tree.rb', line 8981

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("params")

    if requireds.any?
      q.breakable
      q.group(2, "(", ")") { q.seplist(requireds) { |name| q.pp(name) } }
    end

    if optionals.any?
      q.breakable
      q.group(2, "(", ")") do
        q.seplist(optionals) do |(name, default)|
          q.pp(name)
          q.text("=")
          q.group(2) do
            q.breakable("")
            q.pp(default)
          end
        end
      end
    end

    if rest
      q.breakable
      q.pp(rest)
    end

    if posts.any?
      q.breakable
      q.group(2, "(", ")") { q.seplist(posts) { |value| q.pp(value) } }
    end

    if keywords.any?
      q.breakable
      q.group(2, "(", ")") do
        q.seplist(keywords) do |(name, default)|
          q.pp(name)

          if default
            q.text("=")
            q.group(2) do
              q.breakable("")
              q.pp(default)
            end
          end
        end
      end
    end

    if keyword_rest
      q.breakable
      q.pp(keyword_rest)
    end

    if block
      q.breakable
      q.pp(block)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
# File 'lib/syntax_tree.rb', line 9045

def to_json(*opts)
  {
    type: :params,
    reqs: requireds,
    opts: optionals,
    rest: rest,
    posts: posts,
    keywords: keywords,
    kwrest: keyword_rest,
    block: block,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end