lib/yard/handlers/ruby/legacy/base.rb in yard-0.9.5 vs lib/yard/handlers/ruby/legacy/base.rb in yard-0.9.6

- old
+ new

@@ -1,5 +1,6 @@ +# frozen_string_literal: true module YARD module Handlers module Ruby::Legacy # This is the base handler for the legacy parser. To implement a legacy # handler, subclass this class. @@ -40,26 +41,22 @@ end end def call_params if statement.tokens.first.is_a?(TkDEF) - extract_method_details.last.map {|param| param.first } + extract_method_details.last.map(&:first) else tokens = statement.tokens[1..-1] - tokval_list(tokens, :attr, :identifier, TkId).map do |value| - value.to_s - end + tokval_list(tokens, :attr, :identifier, TkId).map(&:to_s) end end def caller_method if statement.tokens.first.is_a?(TkIDENTIFIER) statement.tokens.first.text elsif statement.tokens.first.is_a?(TkDEF) extract_method_details.first - else - nil end end private @@ -68,18 +65,17 @@ # @todo This is a duplicate implementation of {MethodHandler}. Refactor. # @return [Array<String,Array<Array<String>>>] the method name followed by method # arguments (name and optional value) def extract_method_details if statement.tokens.to_s =~ /^def\s+(#{METHODMATCH})(?:(?:\s+|\s*\()(.*)(?:\)\s*$)?)?/m - meth, args = $1, $2 - meth.gsub!(/\s+/,'') + meth = $1 + args = $2 + meth.gsub!(/\s+/, '') args = tokval_list(Parser::Ruby::Legacy::TokenList.new(args), :all) args.map! {|a| k, v = *a.split('=', 2); [k.strip, (v ? v.strip : nil)] } if args - if meth =~ /(?:#{NSEPQ}|#{CSEPQ})([^#{NSEP}#{CSEPQ}]+)$/ - meth = $` - end - return meth, args + meth = $` if meth =~ /(?:#{NSEPQ}|#{CSEPQ})([^#{NSEP}#{CSEPQ}]+)$/ + [meth, args] end end # The string value of a token. For example, the return value for the symbol :sym # would be :sym. The return value for a string +"foo #{ bar}"+ would be the literal @@ -143,11 +139,11 @@ when TkFLOAT token.text.to_f when TkINTEGER token.text.to_i when TkREGEXP - token.text =~ /\A\/(.+)\/([^\/])\Z/ + token.text =~ %r{\A/(.+)/([^/])\Z} Regexp.new($1, $2) when TkTRUE true when TkFALSE false @@ -180,25 +176,26 @@ # @return [Array<EMPTY>] if there are no symbols or Strings in the list # @see #tokval def tokval_list(tokenlist, *accepted_types) return [] unless tokenlist out = [[]] - parencount, beforeparen = 0, 0 + parencount = 0 + beforeparen = 0 needcomma = false seen_comma = true tokenlist.each do |token| tokval = accepted_types == [:all] ? token.text : tokval(token, *accepted_types) - parencond = !out.last.empty? && tokval != nil - #puts "#{seen_comma.inspect} #{parencount} #{token.class.class_name} #{out.inspect}" + parencond = !out.last.empty? && !tokval.nil? + # puts "#{seen_comma.inspect} #{parencount} #{token.class.class_name} #{out.inspect}" case token when TkCOMMA if parencount == 0 out << [] unless out.last.empty? needcomma = false seen_comma = true - else - out.last << token.text if parencond + elsif parencond + out.last << token.text end when TkLPAREN if seen_comma beforeparen += 1 else @@ -207,27 +204,27 @@ end when TkRPAREN if beforeparen > 0 beforeparen -= 1 else - out.last << token.text if parencount > 0 && tokval != nil + out.last << token.text if parencount > 0 && !tokval.nil? parencount -= 1 end when TkLBRACE, TkLBRACK, TkDO parencount += 1 - out.last << token.text if tokval != nil + out.last << token.text unless tokval.nil? when TkRBRACE, TkRBRACK, TkEND - out.last << token.text if tokval != nil + out.last << token.text unless tokval.nil? parencount -= 1 else break if TkKW === token && ![TkTRUE, TkFALSE, TkSUPER, TkSELF, TkNIL].include?(token.class) seen_comma = false unless TkWhitespace === token if parencount == 0 next if needcomma next if TkWhitespace === token - if tokval != nil + if !tokval.nil? out.last << tokval else out.last.clear needcomma = true end @@ -235,16 +232,14 @@ needcomma = true out.last << token.text end end - if beforeparen == 0 && parencount < 0 - break - end + break if beforeparen == 0 && parencount < 0 end # Flatten any single element lists out.map {|e| e.empty? ? nil : (e.size == 1 ? e.pop : e.flatten.join) }.compact end end end end -end \ No newline at end of file +end