Class: Inch::Language::Nodejs::Provider::JSDoc::Docstring

Inherits:
Ruby::Provider::YARD::Docstring show all
Defined in:
lib/inch/language/nodejs/provider/jsdoc/docstring.rb

Constant Summary collapse

VISIBILITIES =
%w(public protected private)

Instance Method Summary collapse

Methods inherited from Ruby::Provider::YARD::Docstring

#code_examples, #contains_code_example?, #describes_private_object?, #empty?, #to_s

Constructor Details

#initialize(text) ⇒ Docstring



10
11
12
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 10

def initialize(text)
  @text = without_comment_markers(text)
end

Instance Method Details

#describes_internal_api?Boolean



14
15
16
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 14

def describes_internal_api?
  tag?(:api, :private) || super
end

#describes_parameter?(name) ⇒ Boolean



18
19
20
21
22
23
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 18

def describes_parameter?(name)
  return false if name.nil?
  parameter = parameter_notations(name)
  type_notation = /(\{[^\}]+\}|\[[^\]]+\])/
  tag?(:param, /#{type_notation}\s+#{parameter}\s+\S+/)
end

#describes_return?Boolean



35
36
37
38
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 35

def describes_return?
  type_notation = /(\{[^\}]+\}|\[[^\]]+\])/
  tag?(:return, /#{type_notation}*(\s\w+)/) || super
end

#mentions_parameter?(name) ⇒ Boolean



25
26
27
28
29
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 25

def mentions_parameter?(name)
  return false if name.nil?
  parameter = parameter_notations(name)
  tag?(:param, /#{parameter}/) || super
end

#mentions_return?Boolean



31
32
33
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 31

def mentions_return?
  tag?(:return) || super
end

#tag?(tagname, regex = nil) ⇒ Boolean



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 48

def tag?(tagname, regex = nil)
  tag_regex = /^\s*\@#{tagname}([^\n]*)$/m
  matches = @text.scan(tag_regex).flatten
  if !matches.empty?
    if regex.nil?
      true
    else
      matches.any? do |matched|
        matched =~ /#{regex}/
      end
    end
  end
end

#visibility(access_value = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 41

def visibility(access_value = nil)
  tagged_value = VISIBILITIES.detect do |v|
    tag?(v)
  end
  (tagged_value || access_value || 'public').to_sym
end

#without_comment_markers(text) ⇒ String

Removes the comment markers // /* */ from the given text.

Docstring.new("// test").without_comment_markers
# => "test"


68
69
70
71
72
# File 'lib/inch/language/nodejs/provider/jsdoc/docstring.rb', line 68

def without_comment_markers(text)
  text.to_s.lines.map do |line|
    line.strip.gsub(/^(\s*(\/\*+|\/\/|\*+\/|\*)+\s?)/m, '')
  end.join("\n").strip
end