Class: Inch::Language::JavaScript::Provider::JSDoc::Docstring

Inherits:
Ruby::Provider::YARD::Docstring show all
Defined in:
lib/inch/language/javascript/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, #describes_private_object?, #empty?, #mentions_member?, #to_s

Constructor Details

#initialize(text) ⇒ Docstring

Returns a new instance of Docstring.



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

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

Instance Method Details

#contains_code_example?Boolean

Returns:

  • (Boolean)


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

def contains_code_example?
  tag?(:example) || super
end

#describes_internal_api?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/inch/language/javascript/provider/jsdoc/docstring.rb', line 18

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

#describes_parameter?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/inch/language/javascript/provider/jsdoc/docstring.rb', line 22

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

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/inch/language/javascript/provider/jsdoc/docstring.rb', line 39

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

#mentions_parameter?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mentions_return?Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/inch/language/javascript/provider/jsdoc/docstring.rb', line 52

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

Parameters:

  • access_value (nil, String) (defaults to: nil)

    visibility in JSDoc output



45
46
47
48
49
50
# File 'lib/inch/language/javascript/provider/jsdoc/docstring.rb', line 45

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"

Returns:

  • (String)


72
73
74
75
76
# File 'lib/inch/language/javascript/provider/jsdoc/docstring.rb', line 72

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