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
28
# 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 = /(\{[^\}]+\}|\[[^\]]+\])/ # text in curly or square brackets
  type_and_param = /(#{type_notation}\s+#{parameter}|#{parameter}\s+#{type_notation})/ # type and param OR param and type
  tag?(:param, /#{type_and_param}\s+\S+/)
end

#describes_return?Boolean

Returns:

  • (Boolean)


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

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

#mentions_parameter?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mentions_return?Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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



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

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)


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

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