lib/yard/handlers/c/handler_methods.rb in yard-0.9.5 vs lib/yard/handlers/c/handler_methods.rb in yard-0.9.6

- old
+ new

@@ -1,5 +1,6 @@ +# frozen_string_literal: true module YARD module Handlers module C module HandlerMethods include Parser::C @@ -7,12 +8,12 @@ def handle_class(var_name, class_name, parent, in_module = nil) parent = nil if parent == "0" namespace = in_module ? ensure_variable_defined!(in_module) : Registry.root if namespace.nil? - raise Parser::UndocumentableError, "class #{class_name}. " + - "Cannot find definition for parent namespace." + raise Parser::UndocumentableError, + "class #{class_name}. Cannot find definition for parent namespace." end register ClassObject.new(namespace, class_name) do |obj| if parent parent_class = namespace_for_variable(parent) @@ -29,21 +30,21 @@ end def handle_module(var_name, module_name, in_module = nil) namespace = in_module ? ensure_variable_defined!(in_module) : Registry.root if namespace.nil? - raise Parser::UndocumentableError, "module #{module_name}. " + - "Cannot find definition for parent namespace." + raise Parser::UndocumentableError, + "module #{module_name}. Cannot find definition for parent namespace." end register ModuleObject.new(namespace, module_name) do |obj| namespaces[var_name] = obj register_file_info(obj, statement.file, statement.line) end end - def handle_method(scope, var_name, name, func_name, source_file = nil) + def handle_method(scope, var_name, name, func_name, _source_file = nil) visibility = :public case scope when "singleton_method"; scope = :class when "module_function"; scope = :module when "private_method"; scope = :instance; visibility = :private @@ -82,11 +83,12 @@ end def handle_alias(var_name, new_name, old_name) namespace = namespace_for_variable(var_name) return if namespace.nil? - new_meth, old_meth = new_name.to_sym, old_name.to_sym + new_meth = new_name.to_sym + old_meth = old_name.to_sym old_obj = namespace.child(:name => old_meth, :scope => :instance) new_obj = register MethodObject.new(namespace, new_meth, :instance) do |o| register_visibility(o, visibility) register_file_info(o, statement.file, statement.line) end @@ -103,15 +105,12 @@ namespace.aliases[new_obj] = old_meth end def handle_constants(type, var_name, const_name, value) return unless type =~ /^const$|^global_const$/ - if type == 'global_const' - namespace = :root - else - namespace = namespace_for_variable(var_name) - end + namespace = type == 'global_const' ? + :root : namespace_for_variable(var_name) register ConstantObject.new(namespace, const_name) do |obj| obj.source_type = :c obj.value = value register_file_info(obj, statement.file, statement.line) find_constant_docstring(obj) @@ -127,11 +126,10 @@ override_comments.each do |name, override_comment| next unless override_comment.file == statement.file just_const_name = name.gsub(/\A.+::/, '') if object.path == name || object.name.to_s == just_const_name comment = override_comment.source - stmt = override_comment break end end # use any comments on this statement as a last resort @@ -151,17 +149,20 @@ register_docstring(object, comment, stmt) end end def find_method_body(object, symbol) - file, in_file = statement.file, false + file = statement.file + in_file = false if statement.comments && statement.comments.source =~ /\A\s*in (\S+)\Z/ - file, in_file = $1, true + file = $1 + in_file = true process_file(file, object) end - if src_stmt = symbols[symbol] + src_stmt = symbols[symbol] + if src_stmt register_file_info(object, src_stmt.file, src_stmt.line, true) register_source(object, src_stmt) record_parameters(object, symbol, src_stmt) unless src_stmt.comments.nil? || src_stmt.comments.source.empty? register_docstring(object, src_stmt.comments.source, src_stmt) @@ -169,23 +170,21 @@ end end # found source (possibly) but no docstring # so look in overrides - override_comments.each do |name, override_comment| + return if override_comments.any? do |name, override_comment| next unless override_comment.file == file name = name.gsub(/::([^:\.#]+?)\Z/, '.\1') - path = if name =~ /\.|#/ # explicit namespace in override comment - object.path - else - object.name.to_s - end - + # explicit namespace in override comment + path = (name =~ /\.|#/ ? object.path : object.name.to_s) if path == name || path == name.sub(/new$/, 'initialize') || path == name.sub('.', '#') register_docstring(object, override_comment.source, override_comment) - return + true + else + false end end # use any comments on this statement as a last resort if !in_file && statement.comments && statement.comments.source =~ /\S/ @@ -194,16 +193,16 @@ end def record_parameters(object, symbol, src) # use regex to extract comma-delimited list of parameters from cfunc definition if src.source =~ /VALUE\s+#{symbol}\(([^)]*)\)\s*\{/m - params = $~[1].split(/\s*,\s*/) + params = $~[1].split(/\s*,\s*/) # rubocop:disable Style/SpecialGlobalVars # cfunc for a "varargs" method has params "int argc, VALUE *argv" if params[0] =~ /int\s+argc/ && params[1] =~ /VALUE\s*\*\s*argv/ object.parameters = [['*args', nil]] else # the first cfunc argument is the 'self' argument, we don't need that - object.parameters = params.drop(1).map { |s| [s[/VALUE\s+(\S+)/, 1], nil] } + object.parameters = params.drop(1).map {|s| [s[/VALUE\s+(\S+)/, 1], nil] } end end end end end