Class: JsDuck::Lint
- Inherits:
-
Object
- Object
- JsDuck::Lint
- Defined in:
- lib/jsduck/lint.rb
Overview
Reports bugs and problems in documentation
Instance Attribute Summary collapse
-
#relations ⇒ Object
Returns the value of attribute relations.
Instance Method Summary collapse
-
#each_member(&block) ⇒ Object
Loops through all members of all classes.
-
#initialize(relations) ⇒ Lint
constructor
A new instance of Lint.
-
#run ⇒ Object
Runs the linter.
-
#warn(type, msg, member) ⇒ Object
Prints warning + filename and linenumber from doc-context.
-
#warn_duplicate_members ⇒ Object
print warnings for duplicate member names.
-
#warn_duplicate_params ⇒ Object
print warnings for duplicate parameter names.
-
#warn_empty_enums ⇒ Object
print warnings for enums with no values.
-
#warn_no_doc ⇒ Object
print warning for each class or public member with no name.
-
#warn_optional_params ⇒ Object
print warning for each non-optional parameter that follows an optional parameter.
-
#warn_singleton_statics ⇒ Object
Print warnings for static members in singleton classes.
-
#warn_unnamed ⇒ Object
print warning for each member or parameter with no name.
Constructor Details
#initialize(relations) ⇒ Lint
Returns a new instance of Lint.
10 11 12 |
# File 'lib/jsduck/lint.rb', line 10 def initialize(relations) @relations = relations end |
Instance Attribute Details
#relations ⇒ Object
Returns the value of attribute relations.
8 9 10 |
# File 'lib/jsduck/lint.rb', line 8 def relations @relations end |
Instance Method Details
#each_member(&block) ⇒ Object
Loops through all members of all classes
121 122 123 |
# File 'lib/jsduck/lint.rb', line 121 def each_member(&block) @relations.each {|cls| cls.all_local_members.each(&block) } end |
#run ⇒ Object
Runs the linter
15 16 17 18 19 20 21 22 23 |
# File 'lib/jsduck/lint.rb', line 15 def run warn_no_doc warn_unnamed warn_optional_params warn_duplicate_params warn_duplicate_members warn_singleton_statics warn_empty_enums end |
#warn(type, msg, member) ⇒ Object
Prints warning + filename and linenumber from doc-context
126 127 128 129 |
# File 'lib/jsduck/lint.rb', line 126 def warn(type, msg, member) context = member[:files][0] Logger.warn(type, msg, context[:filename], context[:linenr]) end |
#warn_duplicate_members ⇒ Object
print warnings for duplicate member names
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/jsduck/lint.rb', line 82 def warn_duplicate_members @relations.each do |cls| members = {:members => {}, :statics => {}} cls.all_local_members.each do |m| group = (m[:meta] && m[:meta][:static]) ? :statics : :members type = m[:tagname] name = m[:name] hash = members[group][type] || {} if hash[name] warn(:dup_member, "Duplicate #{type} name #{name}", hash[name]) warn(:dup_member, "Duplicate #{type} name #{name}", m) end hash[name] = m members[group][type] = hash end end end |
#warn_duplicate_params ⇒ Object
print warnings for duplicate parameter names
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jsduck/lint.rb', line 69 def warn_duplicate_params each_member do |member| params = {} (member[:params] || []).each do |p| if params[p[:name]] warn(:dup_param, "Duplicate parameter name #{p[:name]}", member) end params[p[:name]] = true end end end |
#warn_empty_enums ⇒ Object
print warnings for enums with no values
112 113 114 115 116 117 118 |
# File 'lib/jsduck/lint.rb', line 112 def warn_empty_enums @relations.each do |cls| if cls[:enum] && cls[:members].length == 0 warn(:enum, "Enum #{cls[:name]} defined without values in it", cls) end end end |
#warn_no_doc ⇒ Object
print warning for each class or public member with no name
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jsduck/lint.rb', line 40 def warn_no_doc @relations.each do |cls| if cls[:doc] == "" warn(:no_doc, "No documentation for #{cls[:name]}", cls) end end each_member do |member| if member[:doc] == "" && !member[:private] && !member[:meta][:hide] && !JsDuck::Class.constructor?(member) warn(:no_doc, "No documentation for #{member[:owner]}##{member[:name]}", member) end end end |
#warn_optional_params ⇒ Object
print warning for each non-optional parameter that follows an optional parameter
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jsduck/lint.rb', line 54 def warn_optional_params each_member do |member| if member[:tagname] == :method optional_found = false member[:params].each do |p| if optional_found && !p[:optional] warn(:req_after_opt, "Optional param followed by regular param #{p[:name]}", member) end optional_found = optional_found || p[:optional] end end end end |
#warn_singleton_statics ⇒ Object
Print warnings for static members in singleton classes
101 102 103 104 105 106 107 108 109 |
# File 'lib/jsduck/lint.rb', line 101 def warn_singleton_statics @relations.each do |cls| if cls[:singleton] cls.find_members({:local => true, :static => true}).each do |m| warn(:sing_static, "Static members don't make sense in singleton class #{cls[:name]}", m) end end end end |
#warn_unnamed ⇒ Object
print warning for each member or parameter with no name
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jsduck/lint.rb', line 26 def warn_unnamed each_member do |member| if !member[:name] || member[:name] == "" warn(:name_missing, "Unnamed #{member[:tagname]}", member) end (member[:params] || []).each do |p| if !p[:name] || p[:name] == "" warn(:name_missing, "Unnamed parameter", member) end end end end |